数据结构_简单的多叉树

简单的多叉树,可以实现从叶子节点遍历到根 和 从根遍历叶子节点的过程:


#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;
}

程序运行的效果截图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值