二进制搜索的高度为:
-0,如果树为空
-1 +(左子树的高度和右子树的高度的最大值)。
例如:(高度为空)=> 0
(高度(make节点2为“ 2”(make节点1为“ one”)为空))=> 2
我想知道如何找到每个子树的高度以及递归的工作方式。谢谢!
实现非常简单,您只需要将已经用语言编写的内容翻译成代码即可。我不会为您带来乐趣,但我将向您介绍该过程的一般概念,因此您可以通过自己的方式获得答案:
(define (height tree)
(if <???> ; if the tree is empty
<???> ; then return the height of the empty tree
(<???> ; otherwise return 1 plus the maximum between
(max (height <???>) ; the height of the left subtree
(height <???>))))) ; and the height of the right subtree
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句