c++实现多叉树树形显示(适合家谱的显示)

摘要:
多叉树的树形显示核心代码voidpositionadd{if(!

c++实现多叉树树形显示(适合家谱的显示)第1张

c++实现多叉树树形显示(适合家谱的显示)第2张

多叉树(左兄弟右孩子二叉树)的树形显示

核心代码

void positionadd(Multiway_tree*root, int n)
{
	if (!root)return;
	Multiway_tree*pcur = root;
	stack<Multiway_tree*>s;
	while (pcur || !s.empty())
	{
		if (pcur)
		{
			if (pcur == root->brother)break;
			pcur->positon += n;
			s.push(pcur);
			pcur = pcur->child;
		}
		else
		{
			pcur = s.top();
			s.pop();
			pcur = pcur->brother;
		}
	}
}
void Multiway_tree::display(Multiway_tree *root, const int distance)
{

	if (root == NULL)
	return;

	root->positon = 0;
	root->positionmark = " ";
	int *namenum = new int[sum];
	stack<Multiway_tree*>s;
	Multiway_tree *pcur;
	pcur = root;
	int gener = 0;
	
	while (pcur || !s.empty())
	{
		if (pcur)
		{
			if (pcur->generation > gener)gener = pcur->generation;
			if (gener)
				s.push(pcur);
			pcur = pcur->child;
		}
		else
		{
			pcur = s.top();
			s.pop();
			pcur = pcur->brother;
		}
	}
	

	Multiway_tree **p1 = new Multiway_tree*[gener + 1];

	for (int i = 0; i < gener + 1; ++i)
		p1[i] = 0;
	pcur = root;
	while (pcur || !s.empty())
	{
		if (pcur)
		{

			if (!(p1[pcur->generation]))p1[pcur->generation] = pcur;
			else { Multiway_tree*t = p1[pcur->generation]; while (t->next)t = t->next; t->next = pcur; }
			
			namenum[pcur->num] = pcur->name.size() ;

			if (pcur->parent)
				if (pcur->parent->child == pcur)
				{
					Multiway_tree*t = pcur;
					if (pcur->brother)pcur->positionmark = "/";
					else pcur->positionmark = "|";
					t->parent->childrennum++;
					while (t->brother)
					{
						t->parent->childrennum++;
						t->brother->positionmark = "|";
						t = t->brother;
					}
					if (t != pcur)t->positionmark = "\";
				}

			s.push(pcur);
			pcur = pcur->child;
		}
		else
		{
			pcur = s.top();
			s.pop();
			pcur = pcur->brother;
		}
	}

	pcur = root;
	pcur->positon = 0;
	while (pcur || !s.empty())
	{
		if (pcur)
		{
			if (pcur->parent)
			{
				int n = 0;
				if (pcur->parent->child == pcur)
				{
					Multiway_tree*t = pcur;
					pcur->positon = 0;
					while (t->brother)
					{
						t->brother->positon = t->positon + namenum[t->num] + distance;
						t = t->brother;
					}

					int g = t->parent->positon + (namenum[t->parent->num]) / 2 - (t->positon + namenum[t->num]) / 2;

					if (t->parent->childrennum == 1)pcur->positon = pcur->parent->positon + namenum[pcur->parent->num] / 2 - namenum[pcur->num] / 2;
					else pcur->positon = g;
					t = pcur;
					while (t->brother)
					{
						t->brother->positon += g;
						t = t->brother;
					}

				}

			}
			s.push(pcur);
			pcur = pcur->child;
		}
		else
		{
			pcur = s.top();
			s.pop();
			pcur = pcur->brother;
		}
	}
	for (int i = gener; i > 0; --i)
	{

		for (Multiway_tree*t1 = p1[i]; t1->next; t1 = t1->next)
		{
			if (t1->parent->child != t1)continue;
			for (Multiway_tree*t2 = t1->next; t2; t2 = t2->next)
			{
				if (t2->parent == t1->parent)continue;
				else
				{
					Multiway_tree*t1n = t1;
					while (t1n->brother)t1n = t1n->brother;
					Multiway_tree*t2n = t2;
					while (t2n->brother)t2n = t2n->brother;
					if (!
						((t1->positon >= (t2n->positon + namenum[t2n->num])) || (t2->positon >= (t1n->positon + namenum[t1n->num])))
						)
					{
						int d = 0;
						abs(t1->parent->child->positon - t2n->positon - namenum[t2n->num]) > abs(t2->parent->child->positon - t1n->positon - namenum[t1n->num])
							? abs(t2->parent->child->positon - t1n->positon - namenum[t1n->num]) : abs(t1->parent->child->positon - t2n->positon - namenum[t2n->num]);


						Multiway_tree*t1p = t1, *t2p = t2;
						while (t1p->parent != t2p->parent)
						{

							t1p = t1p->parent; t2p = t2p->parent;

						}
						int n = 0;

						Multiway_tree*temp = t1p;

						for (temp = t1p, n = 0; temp != t2p && temp; temp = temp->brother)
							++n;
						if (temp != t2p)
						{
							for (temp = t2p, n = 0; temp != t1p && temp; temp = temp->brother)
								++n;
							d = abs(t1->parent->child->positon - t2n->positon - namenum[t2n->num]);
						}
						else {
							d = abs(t2->parent->child->positon - t1n->positon - namenum[t1n->num]);
						}

						//4???

						int averaged = (d + distance) / n + 1;


						if (t1p->parent->childrennum % 2 == 0)
						{
							int childn = 0;
							temp = t1p->parent->child;
							while (temp)
							{
								childn++;
								int t = averaged*(childn - t1p->parent->childrennum / 2) - averaged / 2;
								positionadd(temp, t);
								temp = temp->brother;

							}
						}

						else
						{

							int childn = 0;
							temp = t1p->parent->child;
							while (temp)
							{
								childn++;
								int t = averaged*(childn - (t1p->parent->childrennum + 1) / 2);

								positionadd(temp, t);

								temp = temp->brother;

							}


						}

					}



				}


			}

		}
	}

	int minposition = INT_MAX;
	pcur = root;
	while (pcur || !s.empty())
	{
		if (pcur)
		{


			if (minposition > pcur->positon)
				minposition = pcur->positon;

			s.push(pcur);
			pcur = pcur->child;
		}
		else
		{
			pcur = s.top();
			s.pop();
			pcur = pcur->brother;
		}
	}
	positionadd(root, -1 * minposition + 1);
	COORD info = GetLargestConsoleWindowSize(GetStdHandle(STD_OUTPUT_HANDLE));
	if (root->positon < info.X / 4);

	positionadd(root, info.X / 4 - root->positon);


	string *output = new string[(gener + 1) * 3];
	for (int i = 0; i < (gener + 1) * 3; ++i)
		output[i].append(119, ' ');
	for (int i = 1; i < gener + 1; ++i)
	{

		for (Multiway_tree *temp = p1[i]; temp; temp = temp->next)
		{
			string t = temp->name ;
			output[i].replace(temp->positon, namenum[temp->num], t);
			output[i + gener].replace(temp->positon + namenum[temp->num] / 2, 1, (temp->positionmark));

			output[i + gener * 2].replace(temp->positon + namenum[temp->num] / 2, to_string(temp->num).size(), to_string(temp->num));

		}
	}
	for (int i = 1; i < gener + 1; ++i)
	{

		cout << output[i + gener * 2] << endl << output[i] << endl << endl;
		if (i != gener)cout << output[i + 1 + gener] << endl << endl;
	}
	
}

下载地址

https://download.csdn.net/download/li_haoren/10336965

perorder.txt inorder.txt 分别是对本质的二叉树的前序和中序遍历保存的文件

免责声明:文章转载自《c++实现多叉树树形显示(适合家谱的显示)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇pyflink从入门到入土Lua初学习 9-14_03 数据结构 ---&amp;gt; 队列 (自己写的 跟书上差不多)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

内存数据库-H2简介与实践

该模式下,H2数据库可以部署在不同的JVM或不同的物理机中,多个应用可以通过连接H2服务器同时连接到H2数据库。混合模式示意图如下:1.3H2数据库JDBCURL格式H2数据库支持多种连接方式和连接设置,连接URL格式如下,URL中的设置大小写不敏感。...

实用干货丨如何使用Prometheus配置自定义告警规则

前言普罗米修斯是一个用于监控和报警的开源系统。在普罗米修斯的术语中,它所监视的事物被称为目标。在本文中,我们将逐步展示如何安装Prometheus来监控/创建报警,并根据自定义事件配置自定义报警规则。当条件满足时,它将发出警报集成Alertmanager来处理客户端应用程序发送的警报。警报管理器将与发送警报通知的电子邮件帐户集成。了解普罗米修斯操作员根据普罗...

vSphere HA 原理与配置

应当基于可用性需求和群集的特性选择vSphereHA接入控制策略。...

js 预览 excel,js-xlsx的使用

js-xlsx简介SheetJS生成的js-xls x是一个非常方便的工具库,只能使用纯js读取和导出excel。它功能强大,支持多种格式,支持xls、xlsx和ods等十几种格式。本文以xlsx格式为例。官方github:https://github.com/SheetJS/js-xlsx支持演示在线演示地址:http://demo.haoji.me/20...

IDEA的设置打不开,点了没反应解决办法

把它去掉用回英文d就可以了。...

jquery跨域请求数据

Jquery跨域请求数据Jquery跨请求数据。事实上,这很容易。请遵循以下步骤:首先,编写js,通过get获取远程数据。请注意,回调参数应添加在链接之后,这意味着将回调函数地址传输到远程页面。',{params},函数cb{alert;alert;},'json');第二:编写处理程序。publicvoidProcessRequest{context.Re...