回 帖 发 新 帖 刷新版面

主题:二叉树的删除C++

void Remove(bintreenode *&t,char x)//二叉树的删除(遵守二叉排序树删除思想做的)(把相同的元素全部删掉还没有做啊)
{
    bintreenode *s,*q,*p;
    int fag=0;
    if(t!=NULL)
    { 
       if(t->info!=x)
             f=t;        
       if(t->info==x)
        {
              p=t;
              if(f==NULL);
              else
                {
                  q=p;
                  if(f->llink==p&&p->llink==NULL&&p->rlink==NULL)
                    {
                      delete p;
                      f->llink=NULL;
                      fag=1;
                      return;
                    }
                  if(f->llink==p&&p->llink!=NULL&&p->rlink==NULL)
                    {
                      f->llink=p->llink;
                      delete p;
                      fag=1;
                      return;
                    }
                  if(f->rlink==p&&p->llink==NULL&&p->rlink==NULL)
                       {
                      delete p;
                      f->rlink=NULL;
                      fag=1;
                      return;
                    }
                  if(f->rlink==p&&p->llink!=NULL&&p->rlink==NULL)
                    {
                      f->rlink=p->llink;
                      delete p;
                      fag=1;
                      return;
                    } 

回复列表 (共3个回复)

沙发

                  s=p->llink;
                  if(s!=NULL)
                    {  
                      while(s->rlink!=NULL)
                        {
                          q=s;
                          s=s->rlink;
                        }
                      if(q==p)
                        q->llink=s->llink;
                      else 
                        q->rlink=s->llink;
                      p->info=s->info;
                      delete s;
                    } 
                  else
                    { 
                      if(f->rlink==p)
                        f->rlink=p->rlink;
                      else if(f->llink==p)
                        f->llink=p->rlink;
                      delete p;
                    }
                  fag=1;
                  return;
                }
            if(fag==0)
                {
                  if(f==NULL&&t!=NULL&&t->llink==NULL&&t->rlink==NULL)//删除头结点,右孩子和左孩子都为空
                    {
                      t=NULL;
                      delete p;
                      return;
                    }
                  else if(f==NULL&&t->llink!=NULL&&t->rlink==NULL)//删除头结点,右孩子为空,左孩子不为空
                    {
                      t=t->llink;
                      delete p;
                      return;
                    }
                  else if(f==NULL&&t->rlink!=NULL&&t->llink==NULL)//删除头结点,右孩子不为空,左孩子为空

板凳

            {
                      t=t->rlink;
                      delete p;
                      return;
                    }
                  else if(f==NULL&&t->rlink!=NULL&&t->llink!=NULL)//删除头结点,左右孩子不为空
                    { 
                      q=p;
                      s=p->llink;
                        if(s->rlink==NULL)//右孩子树的右孩子为空
                        {
                          s->rlink=p->rlink;
                          t=s;
                          delete p;
                          return;
                        }
                      if(s->llink==NULL)//右孩子树的左孩子为空,右孩子不为空(逻辑性最强的一部分)
                        {
                          int temp;
                          //把左子树的右子树最左的结点的左孩子或者自己本身与左孩子对换元素
                          p=p->llink;
                          s=p->rlink;
                          q=p; 
                          while(s->rlink!=NULL)
                                {
                                  q=s;
                                  s=s->rlink;
                                }
                          temp=p->info;
                          p->info=s->info;
                          s->info=temp;
                             if(q==p)
                            {q->llink=s->llink;//对应q,s没有移动的情况
                             q->rlink=NULL;
                            }
                          else    
                             q->rlink=s->llink;
                          if(p->rlink==NULL&&p->llink!=NULL) //为了定位s的位置
                              q=p->llink;                   //对应q,s没有移动的情况
                          else if(p->rlink!=NULL)

3 楼

                          q=p->rlink;
                          while(q->llink!=NULL)//把最后一个放在q指向左子树最左的
                             q=q->llink;
                          q->llink=s;
                          s->llink=NULL;
                          //以下是左右树对换
                          if(p->rlink!=NULL)
                            { 
                              p=t;          //对应左孩子的右孩子不为空的情况
                              s=p->llink;
                              q=s->rlink; 
                              s->rlink=NULL;
                              s->llink=q;
                              s->rlink=p->rlink;
                            }
                          else
                            {
                              p=t;         //对应左孩子的右孩子为空的情况
                              s=p->llink;
                              s->rlink=p->rlink;
                            }
                          t=s;
                          delete p;
                          return;
                        }
                      while(s->rlink!=NULL)//删除头结点,左右孩子不为空
                        {
                          q=s;
                          s=s->rlink;
                        }
                      if(q==p)
                          q->llink=s->llink;
                      else 
                          q->rlink=s->llink;
                      p->info=s->info;
                      delete s;
                      return;
                    }
                }
        }
       Remove(t->llink,x);
       f=t;//回到根节点,不然回到右孩子结点
       Remove(t->rlink,x);
    }  
}

[em20][em20][em20][em20][em20][em20][em20][em20][em3][em3][em3][em3][em3][em1][em1][em1][em1]

我来回复

您尚未登录,请登录后再回复。点此登录或注册