質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

再帰

情報工学における再帰とは、プログラムのあるメソッドの処理上で自身のメソッドが再び呼び出されている処理の事をいいます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1295閲覧

再帰呼び出しを使った2分木 Segmentation fault

Giovannaaa

総合スコア10

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

再帰

情報工学における再帰とは、プログラムのあるメソッドの処理上で自身のメソッドが再び呼び出されている処理の事をいいます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/06/06 10:45

編集2021/06/06 10:46

再帰呼び出しを使う2分木の質問です。初めにこちらのプログラムは正しいでしょうか?Binarry_Tree関数に配列の値を引数にとると、Segmentation fault 11というエラーがでてしまいます。
また、下のような出力結果を出したい時どうすれば良いでしょうか?Print_Tree関数は未完成です。
Input Number: 8 (root)
[ 8] is joined at アドレス; Parent: 0
Input Number: 5
[ 5] is joined at アドレス; Left child of Parent: 親のアドレス
Input Number: 7
[ 7] is joined at アドレス; Right child of Parent: 親のアドレス
コメントアウトは無視してください。 よろしくお願いします。

C++

1#include <iostream> 2using namespace std; 3 4struct TREE{ 5 int data; 6 TREE *Parent; 7 TREE *LeftChild; 8 TREE *RightChild; 9}; 10 11void Binary_Tree(TREE *,int ); 12void Add_New_Vertex(TREE *,int ); 13void Print_Tree(TREE *,int); 14 15 16int main() 17{ 18 TREE *root = new TREE; 19 20 int a[] = {8,5,7,3,10,9,6,1,20}; 21 int i = 0; 22 //int x=5; 23 24 root->data=a[i]; 25 root->Parent = NULL; 26 root->LeftChild = NULL; 27 root->RightChild = NULL; 28 29 for(i=1;i < 9; i++){ 30 x=a[i]; 31 Binary_Tree(root,x); 32 } 33 //Binary_Tree(root,x); 34 35 /*for(int j=1; i<9; i++){ 36 Print_Tree(root,a[i]); 37 */ 38 39 //cout<< root->data <<" " << root <<" " << root->Parent <<" " << root->LeftChild <<" " <<" " << root->RightChild << endl; 40 //cout<< root->LeftChild->data <<" " << root->LeftChild <<" " << root->LeftChild->Parent <<" " << root->LeftChild <<" " <<" " << root->RightChild << endl; 41 //cout<< root->LeftChild->RightChild->data <<" " << root->LeftChild->RightChild <<" " << root->LeftChild->RightChild->Parent <<" " << root->LeftChild <<" " <<" " << root->RightChild << endl; 42} 43 44void Binary_Tree(TREE *p,int x) 45{ 46 47 if(x < p->data && p->LeftChild != NULL){ 48 Binary_Tree(p->LeftChild,x); 49 }else if(x > p->data && p->RightChild !=NULL){ 50 Binary_Tree(p->RightChild,x); 51 }else{ 52 Add_New_Vertex(p,x); 53 } 54 55} 56 57 58void Add_New_Vertex(TREE *p,int x) 59{ 60 TREE *add = new TREE; 61 62 add->data = x; 63 if(x < p->data){ 64 p->LeftChild = add; 65 p->RightChild = NULL; 66 add->Parent = p; 67 }else if(x > p->data){ 68 p->RightChild = add; 69 p->LeftChild = NULL; 70 add->Parent = p; 71 } 72} 73 74 void Print_Tree(TREE *p,int x ){ 75 76 if(x < p->data && p->LeftChild != NULL){ 77 cout << p->data << endl; 78 Print_Tree(p->LeftChild,x); 79 }else if(x > p->data && p->RightChild !=NULL){ 80 cout << p->data << endl; 81 Print_Tree(p->RightChild,x); 82 } 83 cout << p->data << endl; 84 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

質問のコードの Segmenttation fault はどのようにして解決したのでしょうか?
解決したコードを質問に追記してください。

質問内容に書いてるような配列の要素順に出力し、LeftChildならLeftChildと、RightChildならRightChildと出力するにはどうすれば良いでしょうか?

次のコードは参考になりますか?

C++

1#include <iostream> 2using namespace std; 3 4struct TREE { 5 int data; 6 TREE *Parent; 7 TREE *LeftChild; 8 TREE *RightChild; 9}; 10 11void Binary_Tree(TREE *, int); 12TREE *Add_New_Vertex(TREE *, int); 13void Print_Tree(TREE *, int); 14 15int main() 16{ 17 int a[] = { 8, 5, 7, 3, 10, 9, 6, 1, 20 }; 18 TREE *root = Add_New_Vertex(NULL, a[0]); 19 for (int i = 1; i < 9; i++) 20 Binary_Tree(root, a[i]); 21 for (int i = 0; i < 9; i++) 22 Print_Tree(root, a[i]); 23} 24 25void Binary_Tree(TREE *p, int x) 26{ 27 if (x < p->data) { 28 if (p->LeftChild != NULL) 29 Binary_Tree(p->LeftChild, x); 30 else 31 p->LeftChild = Add_New_Vertex(p, x); 32 } else if (x > p->data) { 33 if (p->RightChild != NULL) 34 Binary_Tree(p->RightChild, x); 35 else 36 p->RightChild = Add_New_Vertex(p, x); 37 } 38} 39 40TREE *Add_New_Vertex(TREE *p, int x) 41{ 42 TREE *add = new TREE; 43 add->data = x; 44 add->Parent = p; 45 add->LeftChild = add->RightChild = NULL; 46 return add; 47} 48 49void Print_Tree(TREE *p, int x) 50{ 51 if (x < p->data) 52 Print_Tree(p->LeftChild, x); 53 else if (x > p->data) 54 Print_Tree(p->RightChild, x); 55 else { 56 cout << "[ " << x << "] is joind at " << p; 57 if (p->Parent == NULL) 58 cout << "; Parent: 0\n"; 59 else if (p == p->Parent->LeftChild) 60 cout << "; Left child of Parent: " << p->Parent << endl; 61 else 62 cout << "; Right child of Parent: " << p->Parent << endl; 63 } 64}

実行結果

text

1[ 8] is joind at 0x7fffcf7f6eb0; Parent: 0 2[ 5] is joind at 0x7fffcf7f6ee0; Left child of Parent: 0x7fffcf7f6eb0 3[ 7] is joind at 0x7fffcf7f6f10; Right child of Parent: 0x7fffcf7f6ee0 4[ 3] is joind at 0x7fffcf7f6f40; Left child of Parent: 0x7fffcf7f6ee0 5[ 10] is joind at 0x7fffcf7f6f70; Right child of Parent: 0x7fffcf7f6eb0 6[ 9] is joind at 0x7fffcf7f6fa0; Left child of Parent: 0x7fffcf7f6f70 7[ 6] is joind at 0x7fffcf7f6fd0; Left child of Parent: 0x7fffcf7f6f10 8[ 1] is joind at 0x7fffcf7f7000; Left child of Parent: 0x7fffcf7f6f40 9[ 20] is joind at 0x7fffcf7f7030; Right child of Parent: 0x7fffcf7f6f70

理解できたかどうかコメントをお願いします。
理解できない場合、どこが分からないのかをコメントしてください。

投稿2021/06/06 16:52

kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Giovannaaa

2021/06/07 01:49

ありがとうございます。理解することができました。
guest

0

こちらのプログラムは正しいでしょうか?Binarry_Tree関数に配列の値を引数にとると、Segmentation fault 11というエラーがでてしまいます。

実行時エラーが出てるなら正しいわけがない。

C++

1void Add_New_Vertex(TREE* p, int x) 2{ 3 TREE* add = new TREE; 4 5 add->data = x; 6 add->LeftChild = NULL; // コレと 7 add->RightChild = NULL; // コレを忘れてない? 8 ...

下のような出力結果を出したい時どうすれば良いでしょうか?Print_Tree関数は未完成です。

// ひんと: とにかく全部プリントする void PrintTree(TREE* p) { if ( p != NULL ) { data と 親アドレスをプリントする PrintTree(p->LeftChild); PrintTree(p->RightChild); } }

投稿2021/06/06 11:07

編集2021/06/06 11:33
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Giovannaaa

2021/06/06 11:28

回答ありがとうございます。 付け足したのですが、コメントアウトをしている出力オブジェクトを使ってもrootの値は出力されるのですが、下のLeft,RightChildの値は出力されません。 よろしくお願いします。
episteme

2021/06/06 11:31

出力していないからでしょ(追記しました)
Giovannaaa

2021/06/06 11:41

ありがとうございます。試してみたのですが、 8 0x0 20 0x7fd15d405930 このような出力結果になってしまいます。
episteme

2021/06/06 11:42

デバッグを丸投げするおつもりか?
Giovannaaa

2021/06/06 11:57

何回もすみません。 for文を回さず、Binarry_Tree(root,5)とするとコメントアウトしている出力オブジェクトは正しく実行結果がでます。この引数にa[i]という配列を渡すとsegmentation fault 11が出ます。なぜなのでしょうか?
episteme

2021/06/06 12:26

配列を渡す? Binarry_Tree(root、a) したんですか? コンパイルエラーじゃありませんか?
episteme

2021/06/06 12:33 編集

void Binary_Tree(TREE *p,int x) に p = NULL が渡されると p->data, p->LeftChild, p->RightChild が segmentation fault するでしょね。
Giovannaaa

2021/06/06 13:28

指摘していただいたおかげで、解決できました。ありがとうございます。 最後に質問なのですが、質問内容に書いてるような配列の要素順に出力し、LeftChildならLeftChildと、RightChildならRightChildと出力するにはどうすれば良いでしょうか? ヒントでもいいので教えて欲しいです。よろしくお願いします。
episteme

2021/06/06 14:27

int a[] = {8,5,7,3,10,9,6,1,20}; for ( i = 0; i < 9; ++i ) { TREE の中から p->data == a[i] である p を見つけてプリントする }
Giovannaaa

2021/06/06 14:39

ヒントの内容をずっと考えているのですが、pを移動していくにはどうすれば良いのでしょうか?
episteme

2021/06/06 15:04 編集

ここまでできててわからんわけない。 てか、いくつ質問すれば気がすむのかな? Segmentation fault は解決したんでしょ?
Giovannaaa

2021/06/07 01:50

ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問