用值填充 ML 中的普通二叉树
让我们说:
datatype bin_tree = Empty |
Node of value * bin_tree * bin_tree
我将如何填充二叉树(不是左比根小而右比根大的二叉搜索树)。只是插入二叉树中每个节点的列表中的值。
Where let's say:
datatype bin_tree = Empty |
Node of value * bin_tree * bin_tree
How would I go about filling a binary tree (not a binary search tree where left is smaller than root and right bigger). Just values from a list inserted at each node in a binary tree.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用已声明的值构造函数。
如果我们暂时假设
value
是int
,那么我们可以将树表示为:
或者,等效地,在一行上:
You use the value constructors you've declared.
If we assume for a moment that
value
isint
instead, then we for instance have that the treeis represented by:
Or, equivalently, on one line:
如果不了解更多关于您希望如何根据给定列表构建树的信息,那么实际上不可能为您提供帮助。然而,这里有一个创建平衡树的示例。它采用第一个元素并将其用作节点值,然后通过采用“左”列表中的所有“偶数”元素和所有“ “右”列表中的奇数个元素:
结果:
It's not really possible to help you, without knowing more about how you wan't your tree constructed from a given list. However here is an example that creates a balanced tree. It takes the first element and uses it as the node value, and then it splits the rest of the list into two sub lists of equal size (if possible), by taking all "even" element in the "left" list and all "odd" elements in the "right" list:
The result:
这是一个答案用 Java 完成同样的问题。这可能会有所帮助:)。
Here is an answer to the same question done in Java. This will probably help a good bit :).