二叉排序树完整代码

tree.h(存放函数声明):

#pragma once
#include
#include
using namespace std;
typedef struct BiTree {																											//所用的二叉树数据结构
	int key;																															//键值
	BiTree* left;																														//左子树
	BiTree* right;																													//右子树
	BiTree* parent;																													//父结点
}BiTree;

//创造一个二叉树结点
BiTree* createBiNode();

//新插入一个结点至二叉树,且每次返回根结点(也用于创建二叉树)
BiTree* &Insert(BiTree* &root, BiTree* node);

//递归遍历
void midTrav(BiTree* root);																										//递归中序遍历
void preTrav(BiTree* root);																										//递归先序遍历
void lastTrav(BiTree* root);																										//递归后序遍历
//非递归
void midTrav_notRe(BiTree* root);																								//非递归中序遍历
void preTrav_notRe(BiTree* root);																								//非递归先序遍历
void lastTrav_notRe(BiTree* root);																								//非递归后序遍历

//查找最大值和最小值
BiTree* maxValue(BiTree* root);																								//查找最大值
BiTree* minValue(BiTree* root);																								//查找最小值

//寻找前驱和后继
BiTree* findNode(BiTree* root, int value);																					//工具函数 查找到某个结点并返回其地址
BiTree* predecessorNode(BiTree* root, int value);																			//查找前驱
BiTree* successorNode(BiTree* root, int value);																				//查找后继

//二叉树的删除
BiTree* deleteNode(BiTree* root, int value);																					//删除结点
//测试工具,Perm:生成全排列,createBiNodeTest:有参数形式创建结点函数
void swap(int& a, int& b);
void Perm(vector<int> vec, int b, int e);
BiTree* createBiNodeTest(int value);
int factorial(int n);
void testDelete();	

tree.cpp(存放函数实现):

#include"tree.h"
#include
#include																													//注意这里用到了stl库中的stack
#include
#include
using namespace std;

//创造一个二叉树结点
BiTree* createBiNode() {
	BiTree* node = new BiTree;
	node->left = node->right = node->parent = NULL;
	cout << "输入新结点key值:";
	cin >> node->key;
	return node;
}

//插入结点,返回根结点(也用于创建二叉树)
BiTree* &Insert(BiTree* &root, BiTree* node) {
	BiTree* parent = NULL;
	BiTree* child = root;
	while (child) {
		parent = child;																												//child是实际遍历的结点,parent用来保存应该插入位置的父结点
		child = node->key < child->key ? child->left : child->right;
	}
	node->parent = parent;	//当root是空的时候,令node->parent = parent即赋NULL。当root不是空的时候,令node->parent = parent即链接到父结点。
	if (!parent)																														//如果parent是空,说明该树是空树
		root = node;
	else if (node->key < parent->key)
		parent->left = node;
	else
		parent->right = node;
	return root;
}

//下面是遍历
//递归中序遍历
void midTrav(BiTree* root) {
	if (root) {
		midTrav(root->left);
		cout << root->key << " ";
		midTrav(root->right);
	}
}
//递归先序遍历
void preTrav(BiTree* root) {
	if (root) {
		cout << root->key << " ";
		preTrav(root->left);
		preTrav(root->right);
	}
}
//递归后序遍历
void lastTrav(BiTree* root) {
	if (root) {
		lastTrav(root->left);
		lastTrav(root->right);
		cout << root->key << " ";
	}
}
//非递归中序遍历
void midTrav_notRe(BiTree* root) {
	stack<BiTree*> stack;
	while (!stack.empty() || root) {																								//注意这里是或的关系
		if (root) {
			stack.push(root);
			root = root->left;
		}
		else {
			root = stack.top();
			stack.pop();
			cout << root->key << " ";
			root = root->right;
		}
	}
}
//非递归先序遍历
void preTrav_notRe(BiTree* root) {
	stack<BiTree*> stack;
	while (root || !stack.empty()) {
		if (root) {
			cout << root->key << " ";																								//因为是先序 所以就要先遍历
			stack.push(root);
			root = root->left;
		}
		else {
			root = stack.top();
			stack.pop();
			root = root->right;
		}
	}
}
//非递归后序遍历
void lastTrav_notRe(BiTree* root) {
	stack<BiTree*> stack;
	BiTree* flag = NULL;
	while (root || !stack.empty()) {
		if (root) {
			stack.push(root);
			root = root->left;
		}
		else {
			root = stack.top();
			if (root->right && root->right != flag) {
				root = root->right;
				stack.push(root);
				root = root->left;
			}
			else {
				cout << stack.top()->key << " ";
				flag = stack.top();
				stack.pop();
				root = NULL;
			}
		}
	}
}

//下面是查找最大、最小值
//查找最大值
BiTree* maxValue(BiTree* root) {
	while (root->right)
		root = root->right;
	return root;
}
//查找最小值
BiTree* minValue(BiTree* root) {
	while (root->left)
		root = root->left;
	return root;
}

//寻找前驱和后继
//工具函数 查找结点位置
BiTree* findNode(BiTree* root, int value) {
	while (root) {
		if (value > root->key)
			root = root->right;
		else if (value < root->key)
			root = root->left;
		else
			return root;
	}
	return root;
}
//前驱
BiTree* predecessorNode(BiTree* root, int value) {
	BiTree* current = findNode(root, value);																					//先查找到当前结点
	if (current->left)																												//有左子树 要向下查询,即直接返回左子树最大结点即为前驱
		return maxValue(current->left);
	BiTree* parent = current->parent;																						//没有左子树 要向上查询
	BiTree* child = current;
	while (parent && parent->left == child) {
		child = parent;
		parent = parent->parent;
	}
	return parent;
}
//后继
BiTree* successorNode(BiTree* root, int value) {
	BiTree* current = findNode(root, value);																					//先查找到当前结点
	if (current->right)																												//有左子树 要向下查询,即直接返回左子树最大结点即为前驱
		return minValue(current->right);
	BiTree* parent = current->parent;																						//没有左子树 要向上查询
	BiTree* child = current;
	while (parent && parent->right == child) {
		child = parent;
		parent = parent->parent;
	}
	return parent;
}

//删除结点
BiTree* deleteNode(BiTree* root, int value) {
	BiTree* node = findNode(root, value);																						//首先查找到要删除的结点
	//第一种情况:如果要删除的结点是叶子结点
	if (node->left == NULL && node->right == NULL) {
		if (!node->parent) {																										//要删除的是根
			delete node;
			return NULL;
		}
		if (node == node->parent->left)																						//不是根
			node->parent->left = NULL;
		else
			node->parent->right = NULL;
		delete node;
		return root;
	}
	//第二种情况:如果要删除的结点左右子树有一个为空
	if (node->left == NULL || node->right == NULL) {
		if (!node->parent) {																										//如果是根
			BiTree* temp = NULL;
			if (node->left) 
				temp = node->left;
			else 
				temp = node->right;
			temp->parent = NULL;
			delete node;
			return temp;
		}																																//如果不是根
		if (node->left == NULL) {																								//1. 如果要删除结点左子树为空
			node->right->parent = node->parent;
			if (node->parent->left == node) 																					//	a. 要删除结点是父结点的左子树
				node->parent->left = node->right;
			else 																														//	b. 要删除结点是父结点的右子树
				node->parent->right = node->right;
			delete node;
		}
		else {																															//2. 要删除结点右子树为空
			node->left->parent = node->parent;
			if (node->parent->left == node) 																					//	a. 要删除结点是父结点的左子树
				node->parent->left = node->left;
			else 																														//	b. 要删除结点是父结点的右子树
				node->parent->right = node->left;
			delete node;
		}
		return root;
	}
	//第三种情况:左右子树都不为空
	/*
	方法:
	1. 找出要删除结点的后继结点
	2. 处理后继结点的问题
	3. 用后继结点替换本身
	*/
	BiTree* successor = successorNode(root, value);																			//1. 查找后继结点
	/*																																	//2. 处理后继结点
	说明:
	1. 其后继结点一定只有右子树或者没有子树。
	2. node的后继结点只有两种情况:
		a. node后继结点为他的右子树根结点(右子树根结点没有向左分叉)
		b. 其余情况
			对于其他情况来说,node的后继结点一定没有左子树且node一定是它的parent的左子树
	*/
	if (successor->right) {																											//后继结点有右子树
		successor->right->parent = successor->parent;
		if (successor == successor->parent->right)																			//后继结点情况a
			successor->parent->right = successor->right;
		else																															//后继结点情况b
			successor->parent->left = successor->right;
	}
	else{																																//后继结点无右子树
		if (successor == successor->parent->right)																			//后继结点情况a
			successor->parent->right = NULL;
		else																															//后继结点情况b
			successor->parent->left = NULL;
	}
	successor->parent = node->parent;																						//3. 下面进行替换操作
	successor->left = node->left;
	successor->right = node->right;
	node->left->parent = successor;
	if (node->right)																													//如果是后继结点情况a,那么在上面处理后继节点的时候就已经使node->right = NULL 所以这里需要判断一下
		node->right->parent = successor;
	if (node->parent) {																											//如果node不是根
		if (node->parent->left == node)
			 node->parent->left = successor;
		else
			node->parent->right = successor;
	}
	else
		root = successor;																											//如果node是根
	return root;
}
//测试工具函数
void swap(int& c1, int& c2)
{
	char temp;
	temp = c1;
	c1 = c2;
	c2 = temp;
}
BiTree* createBiNodeTest(int value) {
	BiTree* node = new BiTree;
	node->left = node->right = node->parent = NULL;
	node->key = value;
	return node;
}
void Perm(vector<int> p, int b, int e)
{
	static int num = 1;
	if (b == e) {
		BiTree* testRoot = NULL;
		for (int i = 0; i <= e; i++) {
			BiTree* temp = createBiNodeTest(p[i]);
			Insert(testRoot, temp);
		}
		cout << "--- 测试组" << num++ << "---" << endl;
		cout << "插入顺序:";
		for (int i = 0; i <= e; i++)
			cout << p[i] << " ";
		cout << endl << endl;
		for (int i = 0; i <= e; i++) {
			testRoot = deleteNode(testRoot, p[i]);
			cout << "删除" << p[i] << "后的数据";
			midTrav(testRoot);
			cout << endl;
			
			testRoot = NULL;
			for (int j = 0; j <= e; j++) {
				BiTree* temp = createBiNodeTest(p[j]);
				Insert(testRoot, temp);
			}
		}
		cout << "--- 测试组 ---" << endl << endl;
		//Sleep(100);
	}
	else
	{
		for (int i = b; i <= e; i++)
		{
			swap(p[b], p[i]);
			Perm(p, b + 1, e);
			swap(p[b], p[i]);
		}
	}
}
int factorial(int n) {
	if (n == 1)
		return 1;
	return n * factorial(n - 1);
}
void testDelete() {
	//测试删除:
	int n;
	cout << "接下来生成由[0, n - 1)中整数构成的测试用例,将这n个数的全排列中每一种排列数都作为生成二叉排序树的新方式,并测试删除这一种排列方式中的每一个数据。总测试数应为n * n!。" << endl << endl;
	cout << "输入n:";
	cin >> n;
	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t" << "下面测试删除结点正确性" << endl;
	vector<int> p;
	for (int i = 0; i < n; i++)
		p.push_back(i);
	Perm(p, 0, n - 1);
	cout << "共有 " << n <<"! = " << factorial(n) << " 组测试数据进行测试,总测试通过组数为 " << n << " * " << n << "! = " <<  factorial(n) * n << "组。" << endl;
	cout << "--- 测试通过!---" << endl;
	cout << "--------------------------------------------------------------" << endl;
}

main.cpp(主程序):

#include
#include
#include"tree.h"
using namespace std;
int main(void) {
	int n;
	cout << "要创建多少结点:";
	cin >> n;

	//声明根结点
	BiTree* root = NULL;
	//创建二叉树
	for (int i = 0; i < n; i++) {
		BiTree* temp = createBiNode();
		Insert(root, temp);
	}
	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t" << "递归遍历" << endl;
	//中序遍历
	cout << "中序遍历:" << endl;
	midTrav(root);
	cout << endl;
	//先序遍历
	cout << "先序遍历:" << endl;
	preTrav(root);
	cout << endl;
	//后序遍历
	cout << "后序遍历:" << endl;
	lastTrav(root);
	cout << endl;
	cout << "--------------------------------------------------------------" << endl;

	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t" << "非递归遍历" << endl;
	//非递归中序遍历
	cout << "非递归中序遍历:" << endl;
	midTrav_notRe(root);
	cout << endl;
	//先序遍历
	cout << "非递归先序遍历:" << endl;
	preTrav_notRe(root);
	cout << endl;
	//后序遍历
	cout << "非递归后序遍历:" << endl;
	lastTrav_notRe(root);
	cout << endl;
	cout << "--------------------------------------------------------------" << endl;

	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t" << "输出最大、最小值" << endl;
	cout << "该二叉排序树中的最大值为:";
	cout << maxValue(root)->key << endl;
	cout << "该二叉排序树中的最小值为:";
	cout << minValue(root)->key << endl;
	cout << "--------------------------------------------------------------" << endl;

	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t寻找前驱、后继。" << endl;
	cout << "输入要查询的结点key值:";
	int key;
	cin >> key;
	if (predecessorNode(root, key))
		cout << "前驱为:" << predecessorNode(root, key)->key << endl;
	else
		cout << "该结点没有前驱!" << endl;
	if (successorNode(root, key))
		cout << "后继为:" << successorNode(root, key)->key << endl;
	else
		cout << "该结点没有后继!" << endl;
	cout << "--------------------------------------------------------------" << endl;

	测试删除
	//testDelete();

	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t删除结点" << endl;
	cout << "请输入要删除结点的key值:";
	cin >> key;
	root = deleteNode(root, key);
	cout << "删除过后为:" << endl;
	midTrav(root);
	cout << endl;
	cout << "--------------------------------------------------------------" << endl;

	_getch();
	return 0;
}

你可能感兴趣的:(二叉树,二叉树)