資源簡介
函數填空:層次遍歷多元樹(在文件tree.cpp中3個空)、先根遍歷、后根遍歷的遞歸函數(在文件tree.h中2個空);

代碼片段和文件信息
//?tree.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“tree.h“
#include?“queue.h“
#include?
void?hierarchical(TreeNode*?t)
//?層次遍歷以結點t為根的多元樹
{
SeqQueue?myqueue;?//?隊列對象
if?(t==NULL)?return;
myqueue.QInsert(t);?//?把根插入隊列
while(myqueue.QueueEmpty()==?0??/*?輸入判斷條件?*/?)?//?隊列非空
{
TreeNode?*p=myqueue.QDelete();?//取出隊首結點
cout<data<<“??“;?//訪問該結點
p=?p->FirstChild();/*?輸入表達式?*/??//先找大兒子(左兒)
if?(p!=NULL)?myqueue.QInsert(p);?//?大兒子入隊
while?(p!=NULL)?//?把大兒子的右兄弟依次入隊
{
p=?p->NextSibling();/*?輸入表達式?*/
if?(p!=NULL)?myqueue.QInsert(p);
}
}
}
void?main(void)
{
//指定的樹
cout<<“下面是實驗指定的樹“< Treet;
t.InsertChild(‘A‘);
for(int?i=0;i<3;i++)
{
t.Root();
t.InsertChild(‘B‘+i);//插入B、C和D
}
t.Root();//插入E
t.FirstChild();
t.InsertChild(‘E‘);
for(i=0;i<2;i++)//插入F和G
{
t.Root();
t.FirstChild();
t.NextSibling();
t.InsertChild(‘F‘+i);
}
t.Root();//插入H
t.FirstChild();
t.NextSibling();
t.NextSibling();
t.InsertChild(‘H‘);
????TreeNode*?troot=t.TreeRoot();??
//層次遍歷的指針是指向結點,而不是指向樹
cout<<“層次遍歷:“< hierarchical(troot);
cout< t.DisplayTree();
cout< t.DisplayTree2();
????//自己的樹
cout< Treet1;
t1.InsertChild(‘A‘);
t1.Root();
t1.InsertChild(‘B‘);//插入B
for(i=0;i<2;i++)
{
t1.Root();//插入C和D
t1.FirstChild();
t1.InsertChild(‘C‘+i);
}
for(i=0;i<3;i++)//插入E、F和G
{
t1.Root();
t1.FirstChild();
t1.FirstChild();
t1.InsertChild(‘E‘+i);
}
????TreeNode*?troot1=t1.TreeRoot();??
???//層次遍歷的指針是指向結點,而不是指向樹
cout<<“層次遍歷:“< hierarchical(troot1);
cout< t1.DisplayTree();
cout< t1.DisplayTree2();
cout< system(“pause“);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1060??2002-04-15?11:23??1\Queue.h
?????文件???????2091??2010-05-23?16:28??1\tree.cpp
?????文件?????217171??2010-05-23?14:21??1\tree.exe
?????文件???????5603??2010-05-23?13:57??1\tree.h
?????目錄??????????0??2010-05-23?16:41??1
-----------?---------??----------?-----??----
???????????????225925????????????????????5
評論
共有 條評論