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

核心代码

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++实现多叉树树形显示(适合家谱的显示)的更多相关文章

  1. Xcode模拟器不显示SDK版本,反而显示设备ID的解决办法

    今天在应用程序中修改了Xcode app 的名称,结果导致Xcode模拟器不显示SDK版本,反而显示设备ID了,感觉特别的忧伤......如图: 进到Xcode->window->Devi ...

  2. iframe 内显示的网页 只显示改网页的某一部分!

    使用iframe调用指定网页的特定位置(显示目标网页某区域的我想要的内容) 有些时候我们并不需要显示iframe标签属性src指定的目标网页的所有内容,往往只需要显示某一特定区域.现有两种实现方法提供 ...

  3. ssh连接linux服务器只显示-bash-4.1#不显示路径解决方法

    ssh连接linux服务器只显示-bash-4.1#不显示路径时,我们只需要修改  ~/.bash_profile文件,如果不存在这个文件,那么新建一个,增加内容  export PS1='[\u@\ ...

  4. Linux 下shell显示-bash-4.1$不显示用户名路径的解决方法

    Linux CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法   问题描述:   CentOS下新增一个用户,登录进去之后shell脚本的信息如下:     而不是我们经常看 ...

  5. CentOS下shell显示-bash-4.1#不显示用户名路径的解决方法

    CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法 问题描述: CentOS下新增一个用户,登录进去之后shell脚本的信息如下: 而不是我们经常看到的username@hos ...

  6. 解决eclipse插件svn不显示svn信息和显示的信息为数字的问题

    1.选择window-->preferences如下图 通过上面步骤svn信息便显示了 2.解决显示的信息为数字问题 选择svn-label decoration format里面的author ...

  7. favicon.ico显示,favicon显示,favicon图标显示

    favicon.ico显示,favicon显示,favicon图标显示 >>>>>>>>>>>>>>>> ...

  8. 直接拨号、将电话号码传入拨号程序、调用拨号程序、调用系统浏览器浏览网页、调用系统程序查看联系人、显示系统设置界面和显示Wi-Fi设置界面代码

    直接拨号.将电话号码传入拨号程序.调用拨号程序.调用系统浏览器浏览网页.调用系统程序查看联系人.显示系统设置界面和显示Wi-Fi设置界面代码 拨打号码的代码如下: Intent callIntent= ...

  9. .net应用程序中添加chm帮助文档打开显示此程序无法显示网页问题

    在做.net大作业时添加了chm帮助文档结果在打开时显示“此程序无法显示网页问题”,但是把帮助文档拷到别的路径下却显示正常, 经过从网上查找,终于找到了答案: (1).chm文件的路径中不能含有“#” ...

随机推荐

  1. oracle里的查询转换

    oracle里的查询转换的作用 Oracle里的查询转换,有称为查询改写,指oracle在执行目标sql时可能会做等价改写,目的是为了更高效的执行目标sql 在10g及其以后的版本中,oracle会对 ...

  2. Vue源码学习(一):调试环境搭建

    最近开始学习Vue源码,第一步就是要把调试环境搭好,这个过程遇到小坑着实费了点功夫,在这里记下来 一.调试环境搭建过程 1.安装node.js,具体不展开 2.下载vue项目源码,git或svn等均可 ...

  3. 参数传递中编码问题(Get/Post 方式)(二)

    form有2中方法把数据提交给服务器,get 和post ,分别说下吧.(一)get 提交1.首先说下客户端(浏览器)的form表单用get 方法是如何将数据编码后提交给服务器端的吧. 对于get 方 ...

  4. CORS跨域资源共享漏洞

    CORS漏洞其中已经存在很久了,但是国内了解的人不是很多,文章更是少只有少,漏洞平台也没有此分类. 在DefConChina之后写了一篇算是小科普的文章. 定义CORS,Cross-Origin Re ...

  5. 查看自己U盘格式

    转自:https://zhidao.baidu.com/question/220844418.html 选中U盘后,鼠标右键单击,选项菜单中点击属性,出的的属性窗口中的“常规”项里边就有U盘的基本信息 ...

  6. BurpSuite系列(九)----Comparer模块(比较器)

    一.简介 Burp Comparer在Burp Suite中主要提供一个可视化的差异比对功能,来对比分析两次数据之间的区别.使用中的场景可能是: 1.枚举用户名过程中,对比分析登陆成功和失败时,服务器 ...

  7. Variable hoisting Function hoisting

    Variable hoisting Another unusual thing about variables in JavaScript is that you can refer to a var ...

  8. Codeforces 1109E 线段树

    思路及博客:https://www.cnblogs.com/uid001/p/10507346.html 代码: #include <bits/stdc++.h> #define LL l ...

  9. sequelize 批量添加和更新数据 bulkCreate

    sequelize提供了批量插入数据的方法:Model.bulkCreate([…object]). 例如: let  updatePhone = [{userName: '李白‘},{userNam ...

  10. Apache apxs命令

    一.简介 apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载 ...