算法导论二叉搜索树

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

vector<int> G;


struct BTNode
{
	int m_value;
	BTNode *m_left;
	BTNode *m_right;
	BTNode *parent;
};

//先序创建二叉树
void CreatBTree(BTNode *&root) {
	int nValue;
	cin >> nValue;
	if (0 == nValue)
	{
		return;
	}
	else
	{
		root = new BTNode();
		root->parent = NULL;
		root->m_value = nValue;
		CreatBTree(root->m_left);
		CreatBTree(root->m_right);
	}
}




void INORDER_TREE_WALK(BTNode *&pRoot) {
	if (pRoot != NULL) {
		INORDER_TREE_WALK(pRoot->m_left);
		cout << pRoot->m_value << " ";
		INORDER_TREE_WALK(pRoot->m_right);
	}
}


void Inorder_Parent(BTNode *&pRoot) {//将它们的父节点求出来
	if (pRoot != NULL) {
		Inorder_Parent(pRoot->m_left);
		if (pRoot->m_left != NULL) {
			pRoot->m_left->parent = pRoot;
		}
		if (pRoot->m_right != NULL) {
			pRoot->m_right->parent = pRoot;
		}
		Inorder_Parent(pRoot->m_right);
	}
}


BTNode *TREE_SEARCH(BTNode *&pRoot, int x) {//查询
	if (pRoot == NULL || x == pRoot->m_value) {
		return pRoot;
	}
	else {
		if (x < pRoot->m_value) {
			return TREE_SEARCH(pRoot->m_left, x);
		}
		else {
			return TREE_SEARCH(pRoot->m_right, x);
		}
	}
}


//插入
BTNode *ITERATIVE_TREE_SEARCH(BTNode *pRoot, int x) {//递推法
	while (pRoot != NULL && x != pRoot->m_value) {
		if (x < pRoot->m_value) {
			pRoot = pRoot->m_left;
		}
		else {
			pRoot = pRoot->m_right;
		}
	}
	return pRoot;
}

void TREE_INSERT(BTNode *&pRoot, int v) {//递归
	BTNode *y = new BTNode();
	BTNode *z = new BTNode();
	z->m_value = v;
	z->m_left = NULL;
	z->m_right = NULL;
	z->parent = NULL;

	BTNode *x = pRoot;
	while (x != NULL) {
		y = x;//y始终是x的父节点
		if (v < x->m_value) {
			x = x->m_left;
		}
		else {
			x = x->m_right;
		}
	}

	z->parent = y;

	if (y == NULL) {//树为空
		pRoot = z;
	}
	else {
		if (v < y->m_value) {
			y->m_left = z;
		}
		else {
			y->m_right = z;
		}
	}

}

void TRANSPLANT(BTNode *&pRoot, BTNode *&u, BTNode *&v) {
	if (u->parent == NULL) {
		pRoot = v;
	}
	else {
		if (u == u->parent->m_left) {
			u->parent->m_left = v;
		}
		else {
			u->parent->m_right = v;
		}
	}

	if (v != NULL) {
		v->parent = u->parent;
	}
}


BTNode *TREE_MINIMUM(BTNode *&pRoot) {
	while (pRoot->m_left != NULL) {
		pRoot = pRoot->m_left;
	}
	return pRoot;
}

void TREE_DELETE(BTNode *&pRoot, int v) {
	BTNode *y = new BTNode();
	BTNode *z = ITERATIVE_TREE_SEARCH(pRoot, v);
	if (z->m_left == NULL) {
		TRANSPLANT(pRoot, z, z->m_right);
	}
	else {
		if (z->m_right == NULL) {
			TRANSPLANT(pRoot, z, z->m_left);
		}
		else {//z有左右两个子树
			y = TREE_MINIMUM(pRoot->m_right);
			if (y->parent != z) {
				TRANSPLANT(pRoot, y, y->m_right);
				y->m_right = z->m_right;
				y->m_right->parent = y;
			}
			TRANSPLANT(pRoot, z, y);
			y->m_left = z->m_left;
			y->m_left->parent = y;
		}
	}
}



int main() {
	BTNode *pRoot = NULL;
	CreatBTree(pRoot);
	Inorder_Parent(pRoot);
	int k, v;
	cin >> k;
	TREE_INSERT(pRoot, k);
	INORDER_TREE_WALK(pRoot);
	cout << endl;
	cin >> v;
	TREE_DELETE(pRoot, v);
	//BTNode *z = ITERATIVE_TREE_SEARCH(pRoot, v);
	//cout << z->m_left->m_value;
	INORDER_TREE_WALK(pRoot);
	cout << endl;
	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值