【问题标题】:Binary tree traversal tree doesn't complete [closed]二叉树遍历树未完成[关闭]
【发布时间】:2017-11-17 13:42:50
【问题描述】:

我为我的演示文稿制作了一个二叉树程序,试图使用文件输入进行后序遍历,结果在插入步骤开始时树只有一个节点,然后重复直到它读到文件末尾。 任何想法这里出了什么问题?

这是我在 txt 中使用的数据

5709611981 N 0623sweet@gmail.com A

5909680109 N 35563@bodin.ac.th A

5909680059 N a.supitcha@hotmail.com A

5909610114 N aladen009@hotmail.com A

5909610031 否 aomsin3475@gmail.com A

5909520024 N apisittest@hotmail.com A

5909680018 N apstaan​​@gmail.com A

5709650567 S apuonn_ap@hotmail.com A

5709650062 Sathachai-riders@hotmail.com A

5909610064 N babe-anusorn@hotmail.com A

5909650193 S baifeern@gmail.com A

5709650021 S ball.bus@hotmail.com A

5909610460 N bambee18341@gmail.com A

5909650011 S bell12546789@gmail.com A

5809650798 S big__007@hotmail.com A

代码:

class Person{
private:
    string id;
    string section;
    string status;
    string email;
public:
    Person();
    Person(string id,string section,string email,string status);
    string getID();
    string getSection();
    string getStatus();
    string getEmail();
    void setID(string newid);
    void setEmail(string newEmail);
    void setSection(string newsection);
    void setStatus(string newstatus);
    };

    class BinarySearchTree
    {
private:
    struct tree_node
    {
        tree_node* left;
        tree_node* right;
        Person data;
    };
    tree_node* root;

public:
    BinarySearchTree()
    {
        root = NULL;
    }

    bool isEmpty() const { return root == NULL;}
    void print_postorder();
    void postorder(tree_node*);
    void print_preorder();
    void preorder(tree_node*);
    void insert(Person);
    void remove(string);
    void search(string key);
    void changeStatus(string key,string newstatus);
    };

    Person::Person()
    {

    }

    Person::Person(string newid,string newsection,string newemail,string newstatus){
id = newid;
section = newsection;
email = newemail;
status = newstatus;
     }
     string Person::getID(){
return id;
       }
     string Person::getSection(){
return section;
       }
     string Person::getEmail(){
return email; 
       }
     string Person::getStatus(){
return status;
       }
     void Person::setStatus(string newstatus){
status = newstatus;
       }
     void Person::setID(string newid){
status = newid;
       }
     void Person::setEmail(string newemail){
status = newemail;
       }
     void Person::setSection(string newsection){
status = newsection;
       }
       void BinarySearchTree::insert(Person p){
tree_node* t = new tree_node;
tree_node* parent;
t->data = p;
t->left = NULL;
t->right = NULL;
parent = NULL;


if(isEmpty()) root = t;
else{
    tree_node* curr;
    curr = root;
    while(curr)
    {
        parent = curr;
        if(t->data.getID() > curr->data.getID()){
        curr=curr->right;
        }
        else{
            curr = curr->left;
        }
    }

    if(t->data.getID() < parent->data.getID()){
        parent->left = t;
    }
    else{
        parent->right = t;
            }
         }
      } 

              void BinarySearchTree::print_postorder(){
    postorder(root);
}

void BinarySearchTree::postorder(tree_node* p)
{   
    if(p != NULL)
    {
        if(p->left){
            postorder(p->left);
        }
        if(p->right){
            postorder(p->right);
        }
        cout<<" "<<p->data.getID() << " " << endl ;
    }
    else {
    cout<<" NULL P " << endl ;
    return;
    }
}

void BinarySearchTree::search(string key){
    bool found = false;

    tree_node* curr;
    tree_node* parent;
    curr = root;
    while(curr != NULL){
        if(curr->data.getID() == key){
            found = true ;
            cout << "Contact Email : " << curr->data.getEmail() << endl;
        }
        else
        {
            parent = curr;
            if(key>curr->data.getID()){
                curr = curr->right;
            }
            else curr = curr->left;
        }
    }
    if(!found){
        cout<<" This student is not in this class. " << endl;
        return;
    }


}

void fillTree(BinarySearchTree b)
{
ifstream file;
file.open("classlist60.txt");
if(!file){
    cout << "File error." << endl;
}

string id;
string section;
string email;
string status;
Person p;
int count = 0;
int halt = 0 ;
while(file >> id >> section >> email >> status)
{
    p.setID(id);
    p.setSection(section);
    p.setEmail(email);
    p.setStatus(status);
    count++;
    if(status == "W"){
        halt++;
    }
    b.insert(p);
}
b.print_postorder();

cout << endl <<  " Total registered students :" << count << endl;
cout <<  " Num of withdrawal Students :" << halt << endl;
file.close();
}

int main(){
BinarySearchTree b;
string id;
string email;
fillTree(b);

cout << endl << " Search Email for student ID : " ;
cin >> id;
b.search(id);

return 0;
}

结果是:

结果应该是Postorder中学生的所有细节,可以通过Input搜索结果树几乎是空的

【问题讨论】:

  • 您的文字图片isn't very helpful。它无法大声朗读或复制到编辑器中,并且索引不是很好,这意味着有相同问题的其他用户不太可能在这里找到答案。请edit您的帖子直接合并相关文本(最好使用复制+粘贴以避免转录错误)。

标签: c++ algorithm tree binary-search-tree


【解决方案1】:

在您的 insert 函数中,您将父节点链接到节点 t,但您没有将节点 t 链接到它正在替换的节点。因此,您最终会丢失父母所指向的内容。您需要在父节点和子节点之间插入新节点。

这个想法和插入一个链表是一样的。在插入之前,您有:

          parent
          /    \
       child  child

您的代码正在这样做:

          parent
          /    \
       child    t

但你想要的是这样的:

          parent
          /    \
       child    t
                 \
               child

这是对代码的简单修改:

if(t->data.getID() < parent->data.getID()){
    t->left = parent->left;  // link to parent's previous child
    parent->left = t;
}
else{
    t->right = parent->right;  // link to parent's previous child
    parent->right = t;
        }
     }
  } 

【讨论】:

  • 哇,感谢这项工作,但我仍然无法在树中显示数据,它似乎是一个空白区域。它是postorder() 部分内的打印空间,但不是p-&gt;data.getID()
  • @Karospapermoon 仔细阅读您的二传手,直到您看到为止。
猜你喜欢
  • 1970-01-01
  • 2012-01-01
  • 2022-11-11
  • 2020-12-16
  • 1970-01-01
  • 2021-11-20
  • 2017-09-16
  • 1970-01-01
相关资源
最近更新 更多