一棵二叉搜索树由一棵二叉树来组织,可以用链表数据结构来表示,每个结点除了Key和卫星数据,还包含指针left,right,p,分别指向左子树,右子树和双亲。
二叉树中的key总是满足二叉搜索树性质:
设x是二叉搜索树的一个结点,若y是x左子树中的一个结点,则y.key< x.key;若y是x右子树中的一个结点,则y.key>=x.key。(相等时,放在左子树还是右子树看个人习惯,此文是放在右子树中)
#include<iostream>
using namespace std;
//template<class A>
struct BSTNode{
//A value;//卫星数据
int key;//排序关键字
BSTNode *lchild = nullptr;//指向左孩子
BSTNode *rchild = nullptr;//指向右孩子
BSTNode *p = nullptr;//指向双亲
};
struct Tree{
BSTNode *root = nullptr;
};
//中序遍历,先序遍历,后序遍历,O(n)
void inorder_tree_walk(BSTNode *x){
if(x != nullptr){
inorder_tree_walk(x->lchild);
cout << x->key<<" ";
inorder_tree_walk(x->rchild);
}
}
void preorder_tree_walk(BSTNode *x){
if(x != nullptr){
cout << x->key <<" ";
preorder_tree_walk(x->lchild);
preorder_tree_walk(x->rchild);
}
}
void postorder_tree_walk(const BSTNode *x){
if(x != nullptr){
postorder_tree_walk(x->lchild);
postorder_tree_walk(x->rchild);
cout << x->key <<" ";
}
}
//迭代查找
BSTNode* iterative_tree_search(BSTNode *x,int v){
while(x !=nullptr && x->key != v){
if(v < x->key)
x = x->lchild;
else
x = x->rchild;
}
return x;
}
//递归查找
BSTNode* tree_search(BSTNode *x,int v){
if(x == nullptr || x->key == v)
return x;
if(v < x->key)
return tree_search(x->lchild,v);
else
return tree_search(x->rchild,v);
}
//最小关键字元素
BSTNode* tree_minimum(BSTNode *x){
while(x->lchild != nullptr)
x = x->lchild;
return x;
}
//最大关键字元素
BSTNode* tree_maximum(BSTNode *x){
while(x->rchild != nullptr)
x = x->rchild;
return x;
}
//后继,前驱,O(h)
BSTNode* tree_successor(BSTNode *x){
if(x->rchild != nullptr)
return tree_minimum(x->rchild);
BSTNode *y = x->p;
while(y != nullptr && x == y->rchild){
x = y;
y = y->p;
}
return y;
}
BSTNode* tree_predecessor(BSTNode *x){
if(x->lchild != nullptr)
return tree_minimum(x->rchild);
BSTNode *y = x->p;
while(y != nullptr && x == y->lchild){
x = y;
y = y->p;
}
return y;
}
//插入
void tree_insert(Tree &T,BSTNode *z){
BSTNode *y = nullptr;
BSTNode *x = T.root;
while(x != nullptr){
y = x;
if(z->key < x->key)
x = x->lchild;
else
x = x->rchild;
}
z->p = y;
if(y == nullptr)
T.root = z;
else if(z->key < y->key)
y->lchild = z;
else
y->rchild = z;
}
/*删除*/
//将v的双亲换成u的双亲
void transplant(Tree &T,BSTNode *u,BSTNode *v){
if(u->p == nullptr)
T.root = v;
else if(u == u->p->lchild)
u->p->lchild = v;
else
u->p->rchild = v;
if(v != nullptr)
v->p = u->p;
}
void tree_delete(Tree &T,BSTNode *z){
if(z->lchild == nullptr)
transplant(T,z,z->rchild);
else if(z->rchild == nullptr)
transplant(T,z,z->lchild);
else{
BSTNode *y = tree_minimum(z->rchild);
if(y->p != z){
transplant(T,y,y->rchild);
y->rchild = z->rchild;
y->rchild->p = y;
}
transplant(T,z,y);
y->lchild = z->lchild;
y->lchild->p = y;
}
}
//测试代码
int main(){
Tree T;
int k,n;
cout<<"个数:";
cin>>n;
while(n--){//输入#结束
cin>>k;
BSTNode *a = new BSTNode();
a->key = k;
tree_insert(T,a);
}
cout<<"中序输出:";
inorder_tree_walk(T.root);//输出
BSTNode *median;
cout<<"\n输入要删除的节点:";
int v;
cin>>v;
median = iterative_tree_search(T.root,v);//查找key==8的节点
cout<<v<<"的后继"<<tree_successor(median)->key<<endl;
cout<<v<<"的前驱"<<tree_predecessor(median)->key<<endl;
cout<<"删除"<<v<<"得到:";
tree_delete(T,median);//,然后删除该节点
inorder_tree_walk(T.root);//输出
median = tree_minimum(T.root);
cout<<"\nmininum:"<<median->key<<endl;
cout<<"删除最小值得到:";
tree_delete(T,median);
inorder_tree_walk(T.root);//输出
median = tree_maximum(T.root);
cout<<"\nmaxinum:"<<median->key<<endl;
cout<<"删除最大值得到:";
tree_delete(T,median);
inorder_tree_walk(T.root);//输出
cout<<endl;
}