2012-02-28 100 views
-2

该程序应该为二叉树节点进行插入,删除。 问题是当我在删除后显示树元素时,程序显示错误。请帮助我的计划中发生什么错误。有没有人知道这个程序有什么问题?

#include <iostream.h> 
#include <stdlib.h> 
#include <string> 

class Tnode 
{ 
public: 
    class Tnode *left; 
    class Tnode *right; 
    int info; 
}; 

class tree: public Tnode 
{ 
public: 
    int top; 
    Tnode *root; 

    tree() 
    { 
     root=NULL; 
     top=0; 
    } 

    void insert(int ch) 
    { 
     Tnode *temp,*temp1; 
     if(root== NULL) 
     { 
      root=new Tnode; 
      root->info=ch; 
      root->left=NULL; 
      root->right=NULL; 
      return; 
     } 
     temp1=new Tnode; 
     temp1->info=ch; 
     temp1->right=temp1->left=NULL; 
     temp=search(root,ch); 
     if(temp->info>ch) 
      temp->left=temp1; 
     else 
      temp->right=temp1; 
    } 

    Tnode *search(Tnode *temp,int ch) 
    { 
     if(root== NULL) 
     { 
      cout <<"no node present"; 
      return NULL; 
     } 
     if(temp->left==NULL && temp->right== NULL) 
      return temp; 

     if(temp->info>ch) 
     { 
      if(temp->left==NULL) return temp; 
      search(temp->left,ch); 
     } 
     else 
     { 
      if(temp->right==NULL) return temp; 
      search(temp->right,ch); 
     } 
    } 

    void display(Tnode *temp) 
    { 
     if(temp==NULL) 
      return; 

     display(temp->left); 
     cout<<temp->info; 
     display(temp->right); 
    } 

    Tnode *getposition(Tnode *root, int x) 
    { 
     Tnode *temp; 
     temp=root; 
     while(temp&&temp->info != x) 
      ((temp->info>x)?(temp=temp->left):(temp=temp->right)); 
     return(temp); 
    } 

    Tnode *getleft_right_most(Tnode *temp) 
    { 
     Tnode *m=temp->left; 
     while(temp->right) 
      temp=temp->right; 
      return temp; 
    } 

    Tnode *getfather(Tnode *root, Tnode *temp) 
    { 
     Tnode *h=root; 
     while(h->left!=temp&&h->right!=temp) 
      ((h->info>temp->info)?h=h->left : h=h->right); 
     return h; 
    } 

    Tnode *del(Tnode *temp, Tnode *f) 
    { 
     if(temp->left&&temp->right) 
     if(temp->right) 
     { 
      (f->left==temp)?f->left=temp->right : f->left=temp->right; 
      temp->right=0; 
     } 
     if(temp->left) 
     { 
      (f->right==temp)?f->right=temp->left : f->right=temp->right; 
      temp->left=0; 
     } 

     free(temp); 
    } 

    Tnode *delroot(Tnode *root) 
    { 
     Tnode *d=root; 
     if((d->right)&&!(d->left)) 
     { 
      root=d->right; 
      d->right=0; 
     } 
     else if((d->left)&&!(d->right)) 
     { 
      root=d->left; 
      d->left=0; 
     } 
     else 
      root=0; 
     return d; 
    } 

    Tnode *delprocess(Tnode *root, int key) 
    { 
     Tnode *d=root; 
     Tnode *temp,*f,*t,*Re; 
     if(root->info==key) 
     { 
      Re=delroot(d); 
      return(Re); 
     } 
     else 
     { 
      temp=getposition(d,key); 
      if(temp->left!=0&&temp->right!=0) 
      { 
       t=getleft_right_most(temp); 
       temp->info=t->info; 
       temp=t; 
      } 

      f=getfather(d,temp); 
      Re=del(temp,f); 
      return(Re); 
     } 
    } 
}; 

main() 
{ 
    tree t1; 

    int ch,n,i; 
    while(1) 
    { 
     cout <<"\n1.INSERT\n2.DELETE NUMBER\n3.DISPLAY\n4.EXIT\nEnter your          choice:"; 
     cin >> ch; 
     switch(ch) 
     { 
     case 1: do{ 
       cout <<"\nenter the elements you want to insert:"; 
        cin >> ch; 
       cout<<"\nto exit insert the number -1."; 
         if(ch!=-1) 
         t1.insert(ch); 
         }while(ch!=-1); 
         break; 
     case 2: t1.display(t1.root);break; 
     case 3: cout<<"\nto delete a number of your insertion enter it : "; 
        cin>>n; 
       t1.root=t1.delprocess(t1.root,n); 
       cout<<"\nthe tree after deletion is : "; 
       t1.display(t1.root); break; 
     case 4: exit(1); 
     } 
    } 
} 
+3

显示错误?你可以说这些吗?我可以用类似的方式回答:你的程序有什么问题?好吧,它有一个bug ... – 2012-02-28 23:32:46

回答

2

有没有<iostream.h>。你必须使用<iostream>。并用<cstdlib>替换<stdlib.h>

此外,您正在使用coutcin,它们都在命名空间std中定义。因此,在文件的开头使用std::coutusing namespace std;,但我不推荐后者。

因此,与以下的人取代你的前三行和你的程序将编译:

#include <iostream> 
#include <cstdlib> 
#include <string> 

using namespace std; 

但是,如果您有没有问题,编译代码,你的错误是完全不同的东西,比你应该添加具体的错误信息给你的问题。

0

你没有一个小错误,你有太多事情出错了。我建议你用一个调试器获得一个IDE,然后逐步检查你的代码,看看发生了什么是你期望的。

例如,在Tnode *search你叫递归搜索,但你忘了使用返回值:

//    search(temp->left,ch); 
       return search(temp->left,ch); 
//    ^^^^^^ 
//    search(temp->right,ch); 
       return search(temp->left,ch); 
//    ^^^^^^ 

还有一句:在Tnode *getleft_right_most,你可能想要这个:

Tnode *m=temp->left; 
// while(temp->right) 
//  temp=temp->right; 
//  return temp; 
    while(m->right) 
     m=m->right; 
     return m; 

这是这是你的编译器可以帮助你的东西。使用-Wall(或启用所有警告),它会指向你,你创建了变量m,并忘记使用它。

Tnode *del,有一个if声明没有身体:

if(temp->left&&temp->right) 
if(temp->right) 

编译器将只取第二个if语句,并用它作为尸体失踪:

if(temp->left&&temp->right) { 
    if(temp->right) 
    { // ... 
    } 
} 

Tnode *delroot,你可能想要

return root; 

而不是return d。您也不处理root拥有leftright分支的情况。

等等,其余的错误是你的锻炼。

我不想提及你交换了选项2和3,但这很烦人。

最后提示:因为您创建了tree类,因此您不需要(也不应该)将root作为参数传递给成员函数。

2

有相当多的毛病是:

  1. #include <iostream.h>需求是#include <iostream>
  2. main()需求是int main()
  3. tree::del需要返回一个值。
  4. main()中的交换机案例与交换机上方的消息输出不匹配。
  5. tree::search如果控制权转到最后的if else块中,则不返回值。
  6. 因为Tnode s分配使用new,他们应该使用delete,而不是free释放。
  7. 青睐纯智能指针; Tnode*可能是std::shared_ptr<Tnode>这将允许您避免调用delete
  8. tree不需要继承Tnode
  9. Tnode应该是struct,因为它只有公共成员并且没有功能。
  10. 有意义的变量名帮助理解(例如是f简称爸爸?)
  11. 一般类不应该有公共成员变量,使类数据的私密性与公共存取。
  12. 喜欢在初始化列表中初始化类成员而不是在构造函数体中赋值
  13. 要一致;例如坚持要么NULL0,而不是两者(尽管用C++ 11编译器,肯定坚持nullptr
  14. 消除成员函数参数中的类成员变量的歧义;您在两种情况下都使用root
  15. 关心对齐和格式设置;有几个地方意味着你期望控制块比它大。格式化的一般状态给人一种直接印象(在这种情况下非常准确)缺乏照顾,并且应该使任何读者立即怀疑逻辑。

而且还有可能更多...

+0

+1 ,,尤其是\ [15 \]:我只是试图清理OP的代码。第二十次错位后我放弃了...... – Zeta 2012-02-29 08:04:47