二分木について勉強しています。その中である数を見つけるために関数findを作ったのですが、segmentation faultになってしまいます。同じような中身のものは(正解とされているのも)ではそのまま通ります。
何が原因なのでしょうか。
c++
1bool BinarySearchTree::find( int x, BinaryNode* p ) const 2{ 3 if(p -> key == x)return true; 4 else if(p -> key < x) return find(x,p -> left); 5 else if(p -> key > x) return find(x,p -> right); 6 else {return false;} 7}
以下はクラスの定義です。
c++
1#include <iostream> 2#include <string> 3#include <new> 4using namespace std; 5 6class BinaryNode // 2分木のノードのクラス 7{ 8 int key; // 各ノードには整数を格納する。 9 // マップ(キーと値の対応)にはなっていない。 10 11 BinaryNode* left; // 左の子ノードへのポインタ 12 BinaryNode* right; // 右の.. 13 14public: 15 BinaryNode( int x ): key(x), left(NULL), right(NULL) { }; 16 // コンストラクタでメンバーを初期化 17 18 friend class BinarySearchTree; // BinarySearchTree クラスから 19 // このクラスのプライベートメンバへの 20 // アクセスを許可する。 21}; 22 23 24 25class BinarySearchTree // 2分探索木のクラス 26{ 27 28 BinaryNode* root; // 2分木の根(root)を指すポインタ。 29 // デーメンバはこれだけ。 30 31public: 32 BinarySearchTree( ); 33 ~BinarySearchTree( ); 34 bool Insert( int x ); 35 bool Find( int x ) const; 36 void Show( ) const; 37 bool RemoveMin(); 38 bool RemoveMax(); 39 40 41private: 42 bool insert( int x, BinaryNode* & p ); 43 bool find( int x, BinaryNode* p ) const; 44 void show( BinaryNode* p ) const; 45 void makeEmpty( BinaryNode* p ); 46};
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/22 01:37