二叉排序树

#include<iostream>

using namespace std;

struct treenode

{

       treenode*lchild;

       treenode*rchild;

       intdata;

};

class Bitree

{

public:

 

 

Bitree();

treenode *getroot()

{

       returnB;

}

void midle(treenode *B);

void insertNode(int k);

void delNode(int k);

treenode* search(int k);

private:

void Bitree::delNode(treenode *B,treenode *f,int k);//删除值为k的结点

       treenode*Bitree::search(treenode *B,int k);//查找值为k的结点

 void Bitree:: buildtree(treenode *&s,treenode *&B);

       treenode*Bitree::searchparent(treenode *B,int k);//查找p的父亲结点

   treenode *Bitree::insertNode(int k,treenode *B);//插入值为k的结点

 

    treenode *B;//二叉排序树头结点

 

 

 

 

};

 

void Bitree::buildtree(treenode*&s,treenode *&B)//要用引用啊

{

       if(B==NULL)

       B=s;

       else{

if(s->data<B->data)      

{

       buildtree(s,B->lchild);

}

else

{

buildtree(s,B->rchild);

}

}

}

Bitree::Bitree( )

{

       intk;

cout<<"输入你要建立的二叉排序树的值,ctrl+Z结束"<<endl;

B=NULL;

 

while(cin>>k)

{

       treenode*s;

       s=newtreenode;

       s->data=k;

       s->lchild=s->rchild=NULL;

 

buildtree(s,B);

}

}

void Bitree:: midle(treenode *B)

{

       if(B==NULL)

              return;

       else

       {

       midle(B->lchild);

       cout<<B->data<<"";

       midle(B->rchild);

}

}

 

void Bitree::insertNode(int k)

{

 

insertNode( k,B);

}

 

treenode *Bitree::insertNode(int k,treenode*B)

{

       if(B==NULL)

              returnNULL;

       else

       {

              if(k<B->data)

                     returninsertNode(k,B->lchild);

              elsereturn insertNode(k,B->rchild);

       }

}

 

 

treenode *Bitree::search(treenode *B,int k)

 

{

       if(B==NULL)

       returnNULL;

       else

       {

 

if(k<B->data)

search(B->lchild,k);

else if(k>B->data)

 

search(B->rchild,k);

else

return B;

 

}

}

 

treenode* Bitree::search( int k)

{

 

return      search(B,k);

}

treenode* Bitree::searchparent(treenode*B,int k)

{

       if(B==NULL)

              returnNULL;

       elseif(B->lchild->data ==k||B->rchild->data==k)

              returnB;

       elseif(k<B->data)

       returnsearchparent(B->lchild, k);

       else

       returnsearchparent(B->rchild,k);

 

 

}

      

void Bitree::delNode(int k)

{

treenode *p,*f;

 

p=search(B,k);

f=searchparent(B,k);

if(p==NULL||f==NULL)

{

cout<<"要删除的值不存在"<<endl;

return ;

}

delNode(p,f,k);

 

}

void Bitree::delNode(treenode *p,treenode*f,int k)

{

       if(p==B)

       {

       B=NULL;

       }

       else

       {

 

 

       if(!p->lchild&&!p->rchild)

       {

              if(f->lchild==p)

              {

              f->lchild=NULL;

              }

              else

              {

                     f->rchild=NULL;

              }

              deletep;

       }

       elseif(!p->rchild)

       {

              if(f->lchild==p)

              {

                     f->lchild=p->lchild;

              }

              elseif(f->rchild==p)

              {

                     f->rchild=p->lchild;

              }

              deletep;

 

       }

       elseif(!p->lchild)

       {

              if(f->lchild==p)

              {

                     f->lchild=p->rchild;

              }

         else if(p->rchild==p)

         {

                f->rchild=p->rchild;

         }

         delete p;

       }

      

 

else

{

       treenode*par;

       par=p;

       treenode*s;

       s=p->rchild;

       while(s->lchild!=NULL)

       {

              par=s;

              s=s->lchild;

       }

       p->data=s->data;

       if(par==s)

       {

              par->rchild=s->rchild;

       }

 

              else

                     par->lchild=s->rchild;

              deletes;

       }

 

}

}

 

 

void main()

{

       BitreeBisortree;

       intk;

       charch;

       while(1)

       {

       cout<<"输入你你要查找的值"<<endl;

       cin.clear();

 

       cin>>k;

treenode *p=Bisortree.search(k);

       if(p==NULL)

       {

              cout<<"查着的值不存在"<<endl;

       }

       elsecout<<"你查找的值在二叉排序树里"<<endl;

       cout<<endl;

       cout<<"亲你是否要继续查找(Y|N)"<<endl;

              cin>>ch;

              if(ch!='Y')

              break;

       }

 

       while(Bisortree.getroot()!=NULL)

              {

              cout<<"输入你要删除的值"<<endl;

       cin>>k;

       Bisortree.delNode(k);

 

       cout<<"是否要继续删除"<<endl;

       cin>>ch;

  cout<<"Y|N"<<endl;

 

       {

              if(ch!='Y')

                     break;

       }

              }

              cout<<"中序遍历输出删除后的值"<<endl;

 Bisortree.midle(Bisortree.getroot());

              cout<<endl

                     <<endl;

       while(1)

              {

              cout<<"亲输入你要插入的值"<<endl;

              cin.clear();

              cin>>k;

             

       Bisortree.insertNode(k);

       cin.clear();

       cout<<"亲是否要继续插入(N||Y)";

       cin>>ch;

      

    if(ch!='Y')

        break;

              }

       cout<<"输出插入数值后的中序遍历"<<endl;

    Bisortree.midle(Bisortree.getroot());

       cout<<"啊哈哈任务终于完成了"<<endl;

       cin.get();

}

 

 

 

 

 

 

 

 

你可能感兴趣的:(二叉排序树)