将BinaryTree转换为树? [英] Converting a BinaryTree to a Tree?
本文介绍了将BinaryTree转换为树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个类:一个是可以有N个子树的树,而BinaryTree最多可以有2个子树。
类定义如下:
数据树a =空树| Tree a [Tree a]派生(Show,Ord,Eq)
数据BinTree a = EmptyBin |节点a(BinTree a)(BinTree a)派生(Show,Ord,Eq)
我可以将BinaryTree转换为Tree吗?
谢谢 解决方案
确定。 Tree
的结构允许我们将两个子树放入列表中:
convert EmptyBin = EmptyTree
convert(Node alr)= Tree a [convert l,convert r]
如果您想以另一种方式进行转换,可能会更复杂一些,具体取决于您想如何分支一长串子树,但可以使用 Ord a
上下文来帮助你。
I have 2 classes: One which is a Tree which can have N subtrees, and BinaryTree can have at most 2 subtrees.
The classes are defined like so:
data Tree a = EmptyTree | Tree a [Tree a] deriving (Show, Ord, Eq)
data BinTree a = EmptyBin | Node a (BinTree a) (BinTree a) deriving (Show, Ord, Eq)
Is there a way I could convert a BinaryTree into a Tree?
Thanks
解决方案
Sure. The structure of the Tree
allows us to put the two subtrees into the list:
convert EmptyBin = EmptyTree
convert (Node a l r) = Tree a [convert l,convert r]
If you wanted to convert the other way, that might be more complex, depending on how you wanted to branch a long list of subtrees, but you could use an Ord a
context to help you there.
这篇关于将BinaryTree转换为树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文