65,210
社区成员
发帖
与我相关
我的任务
分享
// 6.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
class BST;
template<class T>
class node
{
friend void Iterator( node<T>* r);
friend class BST<T>;
private:
T data;
node<T> *lp,*rp;
public:
node(){lp=rp=NULL;}
node(T d,node<T> *l=NULL,node<T> *r=NULL):data(d),lp(l),rp(r){}
T getdata(){return data;}
};
template<class T>
class BST
{
friend void Iterator( node<T>* r);
private:
node<T> *root;
int value;
node<T>* find(const T& x,node<T> *r)
{
if(r==NULL)return NULL;
if(x<r->data)
return find(x,r->lp);
else if(x>r->data)
return find(x,r->rp);
else return r;
}
void insert(const T& x,node<T> *&root)
{
if(root==NULL)
{
root=new node<T>(x);
cout<<"have inserted "<<x<<endl;
}
else if(x<root->data)
insert(x,root->lp);
else if(x>root->data)
insert(x,root->rp);
else ; // when the data equle to x,do nothing
}
node<T>*& min(node<T>* &r)
{
if(r->lp!=NULL)
return min(r->lp);
else if(r->rp!=NULL)
return min(r->rp);
else return r;
}
void remove(const T& x,node<T> * &root)
{
if(root==NULL)
{
cout<<"have not this data "<<x<<endl;
return;
}
else if(x<root->data)
remove(x,root->lp);
else if(x>root->data)
remove(x,root->rp);
else if(root->lp==NULL&&root->rp==NULL)
{
node<T> *q=root;
root=NULL;
delete q;
}
else if(root->lp==NULL)
{
node<T> *q=root;
root=root->rp;
delete q;
}
else if(root->rp==NULL)
{
node<T> *q=root;
root=root->lp;
delete q;
}
else // 这里有问题!!!!!!!!
{
node<T> *p=min(root->rp);
root->data=p->getdata();
remove(root->data,p);
}
}
public:
BST(int v)
{
root=NULL;
cout<<"input the element when "<<v<<" stop"<<endl;
T x;cin>>x;
while(x!=v)
{
insert(x,root);cin>>x;
}
}
node<T>* find(const T&x)
{
return find(x,root);
}
void insert(const T& x)
{
if(root==NULL)
{ root->data=x;cout<<"have inserted "<<x<<endl;}
else insert(x,root);
}
node<T> *& min()
{
if(root==NULL)
return NULL;
return min(root);
}
void remove(const T& x) //if lp is null and rp is null,remove it
{ //else if lp is null,replace it with its rp;
if(root==NULL) //if they are both not null,replace it with the smallest data of rp
{
cout<<"it is null"<<endl;return ;
}
remove(x,root);
}
node<T>* getroot()
{
return root;
}
};
template<class T>
void Iterator( node<T>* r)// 中序遍历
{
while(r!=NULL)
{
cout<<r->getdata()<<" ";
Iterator(r->lp);
Iterator(r->rp);
return;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
BST<int> bst(100);
bst.insert(2);
bst.insert(3);
cout<<(bst.find(2))->getdata()<<endl;
cout<<"the tree is"<<endl;
Iterator( bst.getroot());
bst.remove(2);
bst.remove(7);
bst.remove(3); //这里会出错
cout<<"the tree is"<<endl;
Iterator( bst.getroot());
cin.get();
cin.get();
return 0;
}
/*关于二叉排序树的删除节点问题,难点在于当该节点的左右子树都不为空的时候!
左右子树都不为空,有两种方法。1.用该节点的直接先驱节点去替代,然后删除直接前驱节点
2.用该节点直接后继节点去替代,然后删除直接后继节点*/
//我用了第一种方法!你可以尝试用第二种方法试试!
#include <iostream>
using namespace std;
template<class T>
class BST;
template<class T>
class node
{
friend void Iterator( node<T>* r);
friend class BST<T>;
private:
T data;
node<T> *lp,*rp; //左右孩子指针
public:
node(){lp=rp=NULL;} //初始化
node(T d,node<T> *l=NULL,node<T> *r=NULL):data(d),lp(l),rp(r){}
T getdata(){return data;}
};
template<class T>
class BST
{
friend void Iterator( node<T>* r);
private:
node<T> *root; //树根指针
int value;
node<T>* find(const T& x,node<T> *r)
{
if(r==NULL)
return NULL;
if(x<r->data)
return find(x,r->lp);
else if(x>r->data)
return find(x,r->rp);
else return r;
}
void insert(const T& x,node<T> *&root)
{
if(root==NULL)
{
root=new node<T>(x);
cout<<"have inserted "<<x<<endl;
}
else if(x<root->data)
insert(x,root->lp);
else if(x>root->data)
insert(x,root->rp);
else
cout<<"the data has already inserted!"<<endl; // when the data equle to x,do nothing
}
node<T>*& min(node<T>* &r)
{
if(r->lp!=NULL)
return min(r->lp);
else if(r->rp!=NULL)
return min(r->rp);
else return r;
}
void remove(const T& x,node<T> * &root)
{
if(root==NULL)
{
cout<<"have not this data "<<x<<endl;
return;
}
else if(x<root->data) //在左子树中查找
remove(x,root->lp);
else if(x>root->data) //在右子树中查找
remove(x,root->rp);
//找到x值
else if(root->lp==NULL&&root->rp==NULL) //删除的节点为叶子节点
{
node<T> *q=root;
root=NULL;
delete q;
}
else if(root->lp==NULL) //删除的节点左子树为空
{
node<T> *q=root;
root=root->rp;
delete q;
}
else if(root->rp==NULL) //删除的节点右子树为空
{
node<T> *q=root;
root=root->lp;
delete q;
}
else // 这里有问题!!!!!!!! //左右子树都不为空,有两种方法。1.用该节点的直接先驱节点去替代,然后删除直接前驱节点!
//2.用该节点直接后继节点去替代,然后删除直接后继节点
{
node<T> *p,*s;
p=root;
s=root->lp ; //从左子树中找直接前驱
while(s->rp)
{
p=s;
s=s->rp;
}
root->data=s->data;
if(p!=root) //当root的左子树s的右子树不为空时
p->rp=s->lp;
else //当root的左子树s的右子树为空时
p->lp=s->lp;
delete s;
//node<T> *p=min(root->rp);
// root->data=p->getdata();
//remove(root->data,p);
}
}
public:
BST(int v)
{
root=NULL;
cout<<"input the element when "<<v<<" stop"<<endl;
T x;
cin>>x;
while(x!=v)
{
insert(x,root);
cin>>x;
}
}
node<T>* find(const T&x)
{
return find(x,root);
}
void insert(const T& x)
{
if(root==NULL)
{ root->data=x;cout<<"have inserted "<<x<<endl;}
else insert(x,root);
}
node<T> *& min()
{
if(root==NULL)
return NULL;
return min(root);
}
void remove(const T& x) //if lp is null and rp is null,remove it
{ //else if lp is null,replace it with its rp;
if(root==NULL) //if they are both not null,replace it with the smallest data of rp
{
cout<<"it is null"<<endl;return ;
}
remove(x,root);
}
node<T>* getroot()
{
return root;
}
};
template<class T>
void Iterator( node<T>* r)// 中序遍历
{
while(r!=NULL)
{
Iterator(r->lp);
cout<<r->getdata()<<" ";
Iterator(r->rp);
return;
}
}
int main()
{
BST<int> bst(100);
bst.insert(2);
bst.insert(3);
cout<<(bst.find(2))->getdata()<<endl;
cout<<"the tree is"<<endl;
Iterator( bst.getroot());
bst.remove(2);
bst.remove(7);
bst.remove(3);
cout<<"\nthe tree is"<<endl;
Iterator( bst.getroot());
cin.get();
cin.get();
return 0;
}