简单的多叉树,可以实现从叶子节点遍历到根 和 从根遍历叶子节点的过程:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
enum
{
SCHOOL,
GRADE,
CLASS
};
class Node
{
public :
Node():father(NULL),level(0)
{
ChildList.clear();
this->content = "";
}
Node(int level, string content):father(NULL)
{
ChildList.clear();
this->level = level;
this->content = content;
}
public:
vector<Node> ChildList;
Node* father;
int level;
string content;
};
void Reverse(Node root)
{
if(root.ChildList.size() == 0)
return;
else
{
for(int i =0; i<(root.ChildList.size()); i++)
{
Reverse(root.ChildList[i]);
cout<<root.ChildList[i].content<<" ";
cout<<endl;
}
}
}
void Reverse_toRoot(Node leaf)
{
//cout<<leaf.content<<" ";
if(leaf.father == NULL)
return;
else
{
cout<<leaf.content<<" ";
Reverse_toRoot(*leaf.father);
}
}
int main()
{
Node root;
Node Schoola(SCHOOL,"北京大学");
root.ChildList.push_back(Schoola);
root.ChildList[0].father = &root;
Node Gradea(GRADE,"一年级");
Node Gradeb(GRADE,"二年级");
Node Gradec(GRADE,"三年级");
root.ChildList[0].ChildList.push_back(Gradea);
root.ChildList[0].ChildList.push_back(Gradeb);
root.ChildList[0].ChildList.push_back(Gradec);
root.ChildList[0].ChildList[0].father = &root.ChildList[0];
root.ChildList[0].ChildList[1].father = &root.ChildList[0];
root.ChildList[0].ChildList[2].father = &root.ChildList[0];
Node Classa(CLASS,"一班");
Node Classb(CLASS,"二班");
Node Classc(CLASS,"三班");
root.ChildList[0].ChildList[0].ChildList.push_back(Classa);
root.ChildList[0].ChildList[0].ChildList.push_back(Classb);
root.ChildList[0].ChildList[0].ChildList.push_back(Classc);
root.ChildList[0].ChildList[0].ChildList[0].father = &root.ChildList[0].ChildList[0];
root.ChildList[0].ChildList[0].ChildList[1].father = &root.ChildList[0].ChildList[0];
root.ChildList[0].ChildList[0].ChildList[2].father = &root.ChildList[0].ChildList[0];
root.ChildList[0].ChildList[1].ChildList.push_back(Classa);
root.ChildList[0].ChildList[1].ChildList.push_back(Classb);
root.ChildList[0].ChildList[1].ChildList.push_back(Classc);
root.ChildList[0].ChildList[1].ChildList[0].father = &root.ChildList[0].ChildList[1];
root.ChildList[0].ChildList[1].ChildList[1].father = &root.ChildList[0].ChildList[1];
root.ChildList[0].ChildList[1].ChildList[2].father = &root.ChildList[0].ChildList[1];
root.ChildList[0].ChildList[2].ChildList.push_back(Classa);
root.ChildList[0].ChildList[2].ChildList.push_back(Classb);
root.ChildList[0].ChildList[2].ChildList.push_back(Classc);
root.ChildList[0].ChildList[2].ChildList[0].father = &root.ChildList[0].ChildList[2];
root.ChildList[0].ChildList[2].ChildList[1].father = &root.ChildList[0].ChildList[2];
root.ChildList[0].ChildList[2].ChildList[2].father = &root.ChildList[0].ChildList[2];
Reverse(root);
cout<<endl<<endl<<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl<<endl;
//构建一棵树,因为只创建了7个实例
Classc.father = &Gradec;
Classb.father = &Gradec;
Classa.father = &Gradec;
Gradec.father = &Schoola;
Gradeb.father = &Schoola;
Gradea.father = &Schoola;
Schoola.father = &root;
Reverse_toRoot(Classc);
cout<<endl;
Reverse_toRoot(root.ChildList[0].ChildList[2].ChildList[2]);
cout<<endl;
return 0;
}
程序运行的效果截图