【发布时间】:2020-02-07 03:06:56
【问题描述】:
我有一个定义如下的二叉树:
data BSTree = Void | BSNode BSTree Integer BSTree
想写一个函数
subTree:: Integer -> Integer -> BSTree -> BSTree
返回所有具有
我尝试了以下
subTree:: Integer -> Integer -> BSTree -> BSTree
subTree a b Void = Void
subTree a b (BSNode leftTree key rightTree)
| key < a = BSNode Void key (subTree key b rightTree)
| b < key = BSNode (subTree a key leftTree) key Void
| a <= key && b > key = BSNode (subTree a key leftTree) key (subTree key b rightTree)
但没有得到正确的输出。有人能指出我的逻辑缺陷吗?
【问题讨论】:
-
提示:如果
key < a,您仍然返回一个包含key的BSNode。b < key也是如此。 -
与树的高度有什么关系?
-
道歉,标题上的大脑褪色。 @WillemVanOnsem 这是真的,但我不确定我应该改用什么。
-
@AdityaSubramanian:你在右子树上递归,没有在那个级别构造一个节点。
-
当然好!谢谢!