//二叉搜索树的查、删、加:
#include <iostream>
#include<stack>
#include<queue>
using namespace std;
class node //结点类
{
private:
int data;
node *lchild,*rchild;
public:
friend class binarytree_search;
friend void visit(node *p);
node()
{
}
node(int i)
{
data=i;
lchild=rchild=NULL;
}
node * get_left_child()
{
return lchild;
}
node * get_right_child()
{
return rchild;
}
void binarytree_search_construction()
{
node *p1=new node;p1->data=99;p1->lchild=p1->rchild=NULL;
node *p2=new node;p2->data=110;p2->lchild=p2->rchild=NULL;
node *p3=new node;p3->data=105;p3->lchild=p3->rchild=NULL;
node *p4=new node;p4->data=250;p4->lchild=p4->rchild=NULL;
node *p5=new node;p5->data=200;p5->lchild=p5->rchild=NULL;
node *p6=new node;p6->data=300;p6->lchild=p6->rchild=NULL;
p1->rchild=p2;
p2->lchild=p3;
p4->lchild=p5;
p4->rchild=p6;
lchild=p1;
rchild=p4;
}
bool isleaf()
{
if(lchild==NULL&&rchild==NULL) return true;
else return false;
}
};
void visit(node* p)
{
cout<<p->data<<" ";
}
class binarytree_search //二叉搜索树类
{
private:
node *root;
public:
binarytree_search() //初始化:P103 3-18(a)
{
node *rot=new node;
rot->data=122;
rot->binarytree_search_construction();
root=rot;
}
node * get_root()
{
return root;
}
node* get_father(node* cnode) //返回cnode的父结点
{
queue<node*> que;
node* p=root;
if(p) que.push(p);
while(!que.empty())
{
p=que.front();
if(p->lchild==cnode||p->rchild==cnode) return p;
que.pop();
if(p->lchild) que.push(p->lchild);
if(p->rchild) que.push(p->rchild);
}
}
void in_find(node* p) //中序遍历
{
if(p)
{
in_find(p->lchild);
visit(p);
in_find(p->rchild);
}
}
/** \brief
*
* \param t int
* \return node*
*node * Search(int t)
{
node* p=root;
cout<<p->data<<endl;
while(p&&t!=p->data)
{
if(t<p->data)
{
if(p->lchild)
p=p->lchild;
cout<<p->data<<endl;
}
if(t>p->data)
{
if(p->rchild)
p=p->rchild;
cout<<p->data<<endl;
}
}
if(p)
{
cout<<"find it"<<endl;
return p;
}
else cout<<"404 not found"<<endl;
return NULL;
}
*/
node * Search(node *p,int t)
{
node * cur=p;
while(p&&(t!=cur->data))
{
cur=(t<cur->data?Search(cur->lchild,t):Search(cur->rchild,t));
}
if(!cur) cout<<"not found"<<endl;
return cur;
}
node * Insert(node *&root,int t) //??????????
{
if(!root)
{
node *root=new node;
root->data=t;
return root;
}
else if(t<root->data)
{
root=Insert(root->lchild,t);
}
else if(t>root->data)
{
root=Insert(root->rchild,t);
}
else
{
if(t==root->data)
cout<<"该元素已存在"<<endl;
return NULL;
}
}
void Insert(int t)
{
node *p=root,*pre=NULL;
while(p)
{
pre=p;
if(p->data<t) p=p->rchild;
else p=p->lchild;
}
if(!root) root=new node(t);
else if(pre->data<t) pre->rchild=new node(t);
else if(pre->data>t) pre->lchild=new node(t);
else
{
cout<<"该元素已存在"<<endl;
}
}
void Delete_merge(node *&t) //???
{
node *temp=t;
if(t)
{
if(!t->rchild) t=t->lchild;
else if(!t->lchild) t=t->rchild;
else
{
temp=t->lchild;
while(temp->rchild) temp=temp->rchild;
temp->rchild=t->rchild;
temp=t;
t=t->lchild;
}
delete temp;
}
}
binarytree_search Delete_merge(binarytree_search bst,int i)
{
node *t=bst.Search(bst.get_root(),i);
node *f=bst.get_father(t);
node *temp=t;
if(t)
{
if(!t->rchild) f->rchild=t->lchild;
else if(!t->lchild) f->rchild=t->rchild;
else
{
temp=t->lchild;
while(temp->rchild) temp=temp->rchild;
temp->rchild=t->rchild;
f->rchild=temp;
delete t;
}
}
return bst;
}
void Delete_copy(node *&t)
{
node *pre,*temp=t;
if(t->rchild) t=t->lchild;
else if(t->lchild) t=t->rchild;
else
{
temp=t->lchild;
pre=t;
while(temp->rchild)
{
pre=temp;
temp=temp->rchild;
}
t->data=temp->data;
if(pre==t) pre->lchild=temp->lchild;
else pre->rchild=temp->rchild;
}
delete temp;
}
};
int main()
{
binarytree_search bt;
int t1=300,t2=666;
cout<<"在树中查找"<<t1<<":"<<endl;
bt.Search(bt.get_root(),t1); //查找
bt.in_find(bt.get_root()); //插入
cout<<endl;
//bt.Insert(bt.get_root(),666); //引用错误?
cout<<"在树中插入"<<t2<<":"<<endl;
bt.Insert(t2);
bt.in_find(bt.get_root());
cout<<endl;
cout<<"在树中删除"<<t1<<":"<<endl;
//bt.Delete_merge(bt.get_root()); //合并删除
bt=bt.Delete_merge(bt,t1);
bt.in_find(bt.get_root());
return 0;
}
二叉搜索树
最新推荐文章于 2024-01-29 23:24:42 发布