キュー、幅優先をプログラムで書いているのですが、左右に子ノードがあった場合の処理がわかりません。
//■■■■■■■■キューに子ノードがあれば入れる処理を追加する
の部分です。
if(q1.front()->l_child!=NULL)
q1.push_back(&Node[0]);
if(q1.front()data->r_child!=NULL)
q1.push_back(&Node[0]);
と追加してみたのですが、
queue.cpp:66:2: error: use of undeclared identifier
'q1'
q1.push_back(&Node[0]);
^
queue.cpp:69:9: error: use of undeclared identifier
'q1'
while(!q1.empty()) //キューが空ではない
^
queue.cpp:72:9: error: use of undeclared identifier
'q1'
pnode=q1.front();
^
queue.cpp:74:3: error: use of undeclared identifier
'q1'
q1.pop.front();
^
queue.cpp:76:6: error: use of undeclared identifier
'q1'
if(q1.front()->l_child!=NULL)
^
queue.cpp:77:4: error: use of undeclared identifier
'q1'
q1.push_back(&Node[0]);
^
queue.cpp:78:6: error: use of undeclared identifier
'q1'
if(q1.front()data->r_child!=NULL)
^
queue.cpp:79:4: error: use of undeclared identifier
'q1'
q1.push_back(&Node[0]);
^
とエラーが出てしまいます(MacBook Pro,ターミナルで実行)。
左子ノード、右子ノードがそれぞれある場合をif文で考えるものだと思ったのですが、これではいけないのでしょうか?
/プログラム/
#include<iostream>
#include<deque>
#include<cstring>
using namespace std;
struct node{ //ノードを表す構造体
struct node *l_child; //左子ノード
struct node *r_child; //右子ノード
char data; //文字
};
struct queue{
struct queue *next;
struct node *n;
};
int main()
{
struct node Node[11];
//木の初期設定 Node[0].l_child=&Node[1]; Node[0].r_child=&Node[5]; Node[0].data='P'; Node[1].l_child=&Node[2]; Node[1].r_child=NULL; Node[1].data='M'; Node[2].l_child=&Node[3]; Node[2].r_child=&Node[4]; Node[2].data='S'; Node[3].l_child=NULL; Node[3].r_child=NULL; Node[3].data='A'; Node[4].l_child=NULL; Node[4].r_child=NULL; Node[4].data='A'; Node[5].l_child=NULL; Node[5].r_child=&Node[6]; Node[5].data='L'; Node[6].l_child=NULL; Node[6].r_child=&Node[7]; Node[6].data='E'; Node[7].l_child=&Node[8]; Node[7].r_child=&Node[9]; Node[7].data='R'; Node[8].l_child=NULL; Node[8].r_child=NULL; Node[8].data='T'; Node[9].l_child=&Node[10]; Node[9].r_child=NULL; Node[9].data='E'; Node[10].l_child=NULL; Node[10].r_child=NULL; Node[10].data='E'; q1.push_back(&Node[0]); //木の幅優先探索(キューを利用) while(!q1.empty()) //キューが空ではない { struct node *pnode; pnode=q1.front(); cout << pnode->data << "\n"; q1.pop.front(); //■■■■■■■■キューに子ノードがあれば入れる処理を追加する } return 0;
}
回答1件
あなたの回答
tips
プレビュー