2017-11-17 1 views
0

私は、プレゼンテーション用のバイナリツリープログラムを作成して、ファイル入力を使ってポストオーダートラバーサルを作成しようとしました。挿入ステップを開始するとツリーが始まり、ノードが1つしかなく、ファイルの末尾に移動します。 ここで何が問題になったのでしょうか?バイナリツリートラバーサルツリーが完了しない

これは私が

5909680059 N [email protected] [email protected] TXTに

5709611981 N [email protected]

5909680109 Nを使用するデータです。 comの

5909610114 N [email protected]

5909610031 N [email protected]

5909520024 N [email protected]

5909680018 N apstaan​​@gmail.com

5709650567 S [email protected]

5709650062 S [email protected]

5909610064 N [email protected]

5909650193 S [email protected]

5709650021 S [email protected]

5909610460 N [email protected]

5909650011 S [email protected]

5809650798 S [email protected]

コード:

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; 
} 

結果は次のとおりです。 enter image description here

結果は後順の学生のすべての詳細であるために仮定し、入力を介して検索することができあなたのinsert機能では、ツリーは

+0

あなたのイメージのイメージはあまり役に立ちません(// meta.u nix.stackexchange.com/q/4086)。大声で読み上げたりエディタにコピーしたりすることはできません。インデックスはあまりうまくいきません。つまり、同じ問題を抱えている他のユーザーが答えを見つけにくいということです。関連するテキストを直接組み込むには、投稿を編集してください(できれば、コピーエラーを避けるためにコピー+貼り付けを使用することをおすすめします)。 –

答えて

2

ほとんど空になって判明、あなたはノード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; 
     } 
    } 
    } 
+0

うわー、ありがとう、ありがとう、しかし、私はまだツリーのデータを表示することができません空白のように見えます。それは 'postorder()'部分の中のプリントスペースですが、 'p-> data.getID()'ではありません。 –

+1

@Karospapermoonあなたのセッターを見るまで慎重に読んでください。 – molbdnilo

関連する問題