一、问题描述
画二叉树的时候,总是无法对齐圆点得到对称的节点。例如:
|
1 2 3 4 5 6 7 8 9 10 |
graph test { graph [dpi=80] node [shape=circle]; 1 -- 2, 3; 2 -- 4, 5 [color=red,penwidth=3.0]; 3 -- 6, 7 [color=red,penwidth=3.0]; 4 -- 8; } |
画出来的效果:

红色标出来的 4 个线条,长度不一样,子节点看起来不对称,就导致整个二叉树看起来也不够美观。
二、解决方案
可以在子节点中加一个中间节点,把线条的 weight 设置成 10,然后隐藏中间的节点和连线:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
graph bin_tree { node [shape=circle]; 1 -- 2, 3; 2 -- 4; // 隐藏中间节点的连线 2 -- m2 [weight=10 style="dashed"]; 2 -- 5; 3 -- 6; // 隐藏中间节点的连线 3 -- m3 [weight=10 style="dashed"]; 3 -- 7; 4 -- 8; // 隐藏中间节点 m2, m3 [label="" style="dashed"] } |
效果:

weight 属性的作用是设置线条的权重,权重越大,线条越垂直。
如果希望完全隐藏中间节点,只要把对应节点和线条的属性设置为 invis 即可:
|
1 |
style="dashed" -> style="invis" |





![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
评论