二叉查找树的插入、删除、遍历和查找等C++实现

本文介绍了一种使用C++实现二叉查找树的方法,包括节点创建、插入、搜索、删除等核心操作,并提供了完整的代码示例。

#include <iostream>
using namespace std;

template<class T>
class BSTNode
{
public:
BSTNode(){lChild = rChild = NULL;}
BSTNode(T &x){element = x; lChild = rChild = NULL;}
//private:
int element;
BSTNode<T> *lChild,*rChild;
};

template<typename T>
BSTNode<T>* CreateBST(BSTNode<T> *t, T &x) //递归创建二叉查找树
{
BSTNode<T> *b = new BSTNode<T>(x);
if(!t)
return b;
else if(b->element <= t->element)
{
t->lChild = CreateBST(t->lChild, b->element);
}
else
{
t->rChild = CreateBST(t->rChild, b->element);
}

return t;
}

template<typename T>
void InOrder(BSTNode<T> *t) //中序遍历
{
if(t)
{
InOrder(t->lChild);
cout << t->element << " ";
InOrder(t->rChild);
}
}

template<typename T>
BSTNode<T>* Insert(BSTNode<T> *t, BSTNode<T> *b) //插入结点b
{
BSTNode<T> *root = t;
BSTNode<T> *temp = NULL;
while(t)
{
temp = t;
if(b->element <= t->element)
{
t = t->lChild;
}
else
{
t = t->rChild;
}
}

if(!temp)
return b;
else
{
if(b->element <= temp->element)
{
temp->lChild = b;
}
else
{
temp->rChild = b;
}
}
return root;
}

template<typename T>
BSTNode<T>* Search(BSTNode<T> *t, T key) //查询值为key的结点
{
while(t && t->element!=key)
{
if(key <= t->element)
{
t = t->lChild;
}
else
{
t = t->rChild;
}
}

return t;
}

template<typename T>
BSTNode<T>* Minimum(BSTNode<T> *t) //返回最小元素
{
while(t->lChild)
{
t= t->lChild;
}

return t;
}

template<typename T>
BSTNode<T>* Maximum(BSTNode<T> *t) //返回最大元素
{
while(t->rChild)
{
t = t->rChild;
}
return t;
}

template<typename T>
BSTNode<T>* findParent(BSTNode<T> *t,BSTNode<T> *node) //寻找node结点的父结点
{
BSTNode<T> *parent = NULL;
if(t == node)
{
return NULL;
}
else
{
while(t)
{
if(!t->lChild && !t->rChild)
{
return NULL;
}
else if(t->lChild == node || t->rChild == node)
{
parent = t;
break;
}
else if(node->element <= t->element)
{
t = t->lChild;
}
else
{
t= t->rChild;
}
}
}

return parent;
}

template<typename T>
BSTNode<T>* Delete(BSTNode<T> *t, int key) //删除值为key的结点
{
BSTNode<T> *z = Search(t,key);
BSTNode<T> *x=NULL,*y= NULL;

if(z->lChild == NULL || z->rChild == NULL)
{
y = z;
}
else
{
y = Minimum(z->rChild);
}
if(y->lChild)
{
x = y->lChild;
}
else
{
x = y->rChild;
}
if(x)
{
BSTNode<T> *parent = findParent(t,y);
if(!parent)
{
return x;
}
else if(parent->lChild == y)
{
parent->lChild = x;
}
else if(parent->rChild == y)
{
parent->rChild =x;
}
}
if(y != z)
{
z->element = y->element;
}

delete y;

return t;

}


int main()
{
int x;
cout << "Input Numbers: " << endl;
BSTNode<int> *bstree = NULL;
while((cin>>x))
{
BSTNode<int> *b = new BSTNode<int>(x);
bstree = Insert(bstree,b);
cout << "Input Numbers: " << endl;
}

InOrder(bstree);

BSTNode<int> *r = Search(bstree, 13);
if(r)
{
cout << endl << r->element << endl;
}

BSTNode<int> *min = Minimum(bstree);
cout << endl << "The minimum is : " << min->element << endl;

BSTNode<int> *max= Maximum(bstree);
cout << endl << "The maximum is: " << max->element << endl;

bstree = Delete(bstree,5);
InOrder(bstree);

return 0;
}

 

转自:http://blog.sina.com.cn/s/blog_63351e510100ldfx.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值