To define a binary tree, you can first write its recursive definition in plain English:
a binary tree with value of type 'x' is either an internal node with a value of type 'x' and two child trees with values of type 'x' or an empty leaf node.
Then it's easy to translate that into Haskell:
data BinaryTree x = InternalNode x (BinaryTree x) (BinaryTree x) | LeafNode
The 2-3-4 trees differ from binary trees by having 3 kinds of internal nodes instead of one, so you need more alternatives.
Your question is a little bit vague, as it's not clear if you want to define the tree structure itself or functions that act on a tree. Since a 2-3-4-tree is just a B-Tree, you can use Data.Tree directly and write functions working on it and enforcing the constraints you like.
If you have to define the tree data type yourself, I'd suggest to define a data type for the nodes that holds (2|3|4)-tuples of nodes and data.
发布评论
评论(2)
http: //en.wikipedia.org/wiki/2-3-4_tree 是一个很好的起点。 http://en.wikipedia.org/wiki/B-tree
要定义一棵二叉树,可以先用简单的英语写出它的递归定义:
一棵值为“x”类型的二叉树,要么是一个值为“x”类型的内部节点,要么是两个值为“x”类型的子树'x' 或空叶节点。
然后很容易将其转换为 Haskell:
2-3-4 树与二叉树不同,它具有 3 种内部节点而不是一种,因此您需要更多选择。
http://en.wikipedia.org/wiki/2-3-4_tree is a good point to start. There are also some clues at http://en.wikipedia.org/wiki/B-tree
To define a binary tree, you can first write its recursive definition in plain English:
a binary tree with value of type 'x' is either an internal node with a value of type 'x' and two child trees with values of type 'x' or an empty leaf node.
Then it's easy to translate that into Haskell:
The 2-3-4 trees differ from binary trees by having 3 kinds of internal nodes instead of one, so you need more alternatives.
您的问题有点模糊,因为不清楚您是否想定义树结构本身或作用于树的函数。由于 2-3-4-tree 只是一个 B-Tree,因此您可以使用直接使用
Data.Tree
编写对其进行处理的函数并强制执行您喜欢的约束。如果您必须自己定义树数据类型,我建议为保存 (2|3|4) 节点和数据元组的节点定义数据类型。
Your question is a little bit vague, as it's not clear if you want to define the tree structure itself or functions that act on a tree. Since a 2-3-4-tree is just a B-Tree, you can use
Data.Tree
directly and write functions working on it and enforcing the constraints you like.If you have to define the tree data type yourself, I'd suggest to define a data type for the nodes that holds (2|3|4)-tuples of nodes and data.