// tree.cpp : Defines the entry point for the console application.
//
#include "tree.h"
#include "queue.h"
#include <stdlib.h>
void hierarchical(TreeNode<char>* t)
// 层次遍历以结点t为根的多元树
{
SeqQueue myqueue; // 队列对象
if (t==NULL) return;
myqueue.QInsert(t); // 把根插入队列
while(myqueue.QueueEmpty()== 0 /* 输入判断条件 */ ) // 队列非空
{
TreeNode<char> *p=myqueue.QDelete(); //取出队首结点
cout<<p->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<<"下面是实验指定的树"<<endl;
Tree<char>t;
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<char>* troot=t.TreeRoot();
//层次遍历的指针是指向结点,而不是指向树
cout<<"层次遍历:"<<endl;
hierarchical(troot);
cout<<endl<<"先根遍历:"<<endl;
t.DisplayTree();
cout<<endl<<"后根遍历:"<<endl;
t.DisplayTree2();
//自己的树
cout<<endl<<endl<<"下面是自己的树"<<endl;
Tree<char>t1;
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<char>* troot1=t1.TreeRoot();
//层次遍历的指针是指向结点,而不是指向树
cout<<"层次遍历:"<<endl;
hierarchical(troot1);
cout<<endl<<"先根遍历:"<<endl;
t1.DisplayTree();
cout<<endl<<"后根遍历:"<<endl;
t1.DisplayTree2();
cout<<endl;
system("pause");
}