漂亮地打印一棵树
假设我有一个定义如下的二叉树数据结构
type 'a tree =
| Node of 'a tree * 'a * 'a tree
| Nil
我有一个树的实例如下:
let x =
Node
(Node (Node (Nil,35,Node (Nil,40,Nil)),48,Node (Nil,52,Node (Nil,53,Nil))),
80,Node (Node (Nil,82,Node (Nil,83,Nil)),92,Node (Nil,98,Nil)))
我试图将树漂亮地打印成易于解释的东西。最好,我想在控制台窗口中打印树,如下所示:
_______ 80 _______
/ \
_ 48 _ _ 92 _
/ \ / \
35 52 82 98
\ \ /
40 53 83
让我的树以该格式输出的简单方法是什么?
Let's say I have a binary tree data structure defined as follows
type 'a tree =
| Node of 'a tree * 'a * 'a tree
| Nil
I have an instance of a tree as follows:
let x =
Node
(Node (Node (Nil,35,Node (Nil,40,Nil)),48,Node (Nil,52,Node (Nil,53,Nil))),
80,Node (Node (Nil,82,Node (Nil,83,Nil)),92,Node (Nil,98,Nil)))
I'm trying to pretty-print the tree into something easy to interpret. Preferably, I'd like to print the tree in a console window like this:
_______ 80 _______
/ \
_ 48 _ _ 92 _
/ \ / \
35 52 82 98
\ \ /
40 53 83
What's an easy way to get my tree to output in that format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你想让它非常漂亮,你可以从 此博客条目使用 WPF 绘制它。
但我可能很快也会编写一个 ASCII 解决方案。
编辑
好吧,哇,这很难。
我不确定它是否完全正确,而且我忍不住认为可能有更好的抽象。但无论如何...享受吧!
(请参阅代码末尾的一个相当漂亮的大型示例。)
If you want it to be very pretty, you could steal about 25 lines of code from this blog entry to draw it with WPF.
But I'll code up an ascii solution shortly too, probably.
EDIT
Ok, wow, that was hard.
I'm not certain it's entirely correct, and I can't help but think there's probably a better abstraction. But anyway... enjoy!
(See the end of the code for a large example that is rather pretty.)
如果你不介意把头转向一侧,你可以先打印树的深度,一个节点到一行,递归地将深度传递到树上,并在之前的行上打印
深度*N
空格节点。这是Lua代码:
测试:
If you don't mind turning your head sideways, you can print the tree depth first, one node to a line, recursively passing the depth down the tree, and printing
depth*N
spaces on the line before the node.Here's Lua code:
Test:
也许这可以帮助:在 ML 中绘制树
Maybe this can help: Drawing Trees in ML
虽然这不是完全正确的输出,但我在 http://www 找到了答案.christiankissig.de/cms/files/ocaml99/problem67.ml:
Although it's not exactly the right output, I found an answer at http://www.christiankissig.de/cms/files/ocaml99/problem67.ml :
这是一种直觉,我确信像 Knuth 这样的人有这个想法,我太懒了
检查。
如果您将树视为一维结构,您将得到一个数组
(或向量)长度为 L
这很容易通过“按顺序”递归树遍历来构建:左,根,右
当树不平衡时,必须进行一些计算来填补空白
2 维
1 维:
可以使用此数组构建漂亮的打印树
(也许有递归的东西)
首先使用 L/2 位置处的值,X 位置是
L/2 值 * 默认长度(这里是 2 个字符)
添加漂亮的分支将导致更多的位置算术,但这在这里很简单
您可以连接字符串中的值,而不是使用数组,连接将
事实上计算最佳 X 位置并允许不同的值大小,
制作一棵更紧凑的树。
在这种情况下,您必须计算要提取的字符串中的单词数
价值观。例如:对于第一个元素,使用字符串的 L/2 个单词代替
数组的 L/2 元素。字符串中的 X 位置与树中的位置相同。
This is an intuition, I'm sure someone like Knuth had the idea, I'm too lazy
to check.
If you look at your tree as an one dimensional structure you will get an array
(or vector) of length L
This is easy to build with an "in order" recursive tree traversal: left,root,right
some calculations must be done to fill the gaps when the tree is unbalanced
2 dimension
1 dimension :
The pretty printed tree can be build using this array
(maybe with something recursive)
first using values at L/2 position, the X position is the
L/2 value * the default length (here it is 2 characters)
Adding pretty branches will cause more positional arithmetics but it's trivial here
You can concatenate values in a string instead using an array, concatenation will
de facto calculate the best X postion and will allow different value size,
making a more compact tree.
In this case you will have to count the words in the string to extract
the values. ex: for the first element using the L/2th word of the string instead
of the L/2 element of the array. The X position in the string is the same in the tree.