2017-03-20 23 views
2

このコードがどこで失敗しているのか分かりません。これはクラスの割り当てのためのもので、最低の高さでその場所にバイナリツリーに挿入する必要があります。C++バイナリツリー挿入/高さ

高さ関数に明らかなセグメント化エラーが発生しています。

class Node { 
    public: 
     Node(); 
     Node(int); 

    private: 
     friend class bT; 
     int data; 
     Node* left; 
     Node* right; 
}; 

class bT { 
    public: 
     bT(); 
     virtual void insert(int); 
     int height() const; 
     unsigned size() const; 

    protected: 
     Node* root; 

    private: 
     void insert(Node*&, int); 
     int height(Node*) const; 
}; 

そして、私のメインのファイル:

int bT::height() const { 
    //Returns -1 when root does not exist 
    if (root == NULL) 
     return -1; 

    //Calls private recursive method. Uses taller height 
    int lheight = height(root->left); 
    int rheight = height(root->right); 

    if (lheight > rheight) 
     return lheight; 
    else 
     return rheight; 
} 


void bT::insert(int x) { 
    //Inserts new node at root if root does not exist 
    if (root == NULL){ 
     Node node(x); 
     root = &node; 
    } 

    //Calls private recursive method to insert. 
    else { 
     int lheight = height(root->left); 
     int rheight = height(root->right); 

     if (lheight <= rheight) 
      insert(root->left, x); 
     else 
      insert(root->right, x); 
    } 
} 

int bT::height(Node* r) const { 
    //Base Case: Returns 0 when node does not exist 
    if (r == NULL) 
     return 0; 

    //Calls private recursive method. Uses taller height 
    int lheight = height(r->left); 
    int rheight = height(r->right); 

    if (lheight > rheight) 
     return 1 + lheight; 
    else 
     return 1 + rheight; 
} 

void bT::insert(Node*& r, int x){ 
    //Base Case: Sets r = new node when node does not exist 
    if (r == NULL) { 
     Node node(x); 
     r = &node; 
    } 

    //Recursive Call: Calls insert function for node with lower height 
    else { 
     int lheight = height(r->left); 
     int rheight = height(r->right); 

     if (lheight <= rheight) 
      insert(r->left, x); 
     else 
      insert(r->right, x); 
    } 
} 
+0

あなたの 'ルート'は[ダングリングポインタ](http://stackoverflow.com/q/17997228/5980430)です。あなたの挿入メソッドのコードが間違っています。 –

+0

@appleappleありがとうございますが、私はそのページを読んでいますが、私はまだ混乱しています。クラスヘッダーを変更せずにこの問題を解決するにはどうすればよいですか(それは私たちに与えられ、明示的に変更しないように指示しています)。 – Tommy

+0

@ 5980430私のデフォルトのコンストラクタでNodeのために、私はNULLへの左と右のポインタを初期化します。 – Tommy

答えて

1

このコードは、あなたのinsert方法では、簡単な修正は、動的に変化するだろうポインタ


if (root == NULL){ 
    Node node(x); 
    root = &node; 
} 
//root point to invalid address now 

ぶら下がり引き起こします割り当て。


if (root == NULL){ 
    Node* node = new Node(x); 
    root = node; 
} 

あなたが(そしてそれにはデストラクタはありません)クラス宣言を変更することはできませんので、私はあなたがこれを学ぶしていないと思いますが、あなたはメモリリークに注意する必要があります。

+0

@トミー私はそれをテストし、それは[かなりうまくいく](http://coliru.stacked-crooked.com/a/5087c804d4fa3f24)。 'left'''''と' 'root''を初期化し、上記コードの' two'を置き換えてもよろしいですか? –

+0

@トミー私はそれが十分だと思う。私の前のコメントのコードをチェックしましたか?ところで、完全なバイナリツリー*が必要な場合は、間違った場所に挿入します。 –

+0

ああ?どこに間違って挿入していますか?あなたの忍耐のためにそんなにありがとうbtw笑 – Tommy

関連する問題