【问题标题】:BST-Smallest SchemeBST-最小方案
【发布时间】:2015-11-02 00:59:11
【问题描述】:

所以我正在尝试编写一个返回二叉搜索树中最小值的代码。我知道这是树的最左边的值,并且知道我需要它递归地向左运行,直到什么都没有。但是我的代码不起作用,我不知道为什么。任何帮助将不胜感激。

(define (bst-smallest bs-tree)
  (cond ((null? bs-tree)
     (display "undefined"))
    ((< (bst-value bs-tree) (bst-value (bst-left bs-tree))) 
     (bst-value (bst-left bs-tree)))
    (else 
     (bst-smallest (bst-left bs-tree)))
    ))

【问题讨论】:

    标签: list tree scheme binary-tree binary-search-tree


    【解决方案1】:

    你只需要一直走到树的左边,直到你不能再往前走。在您的代码中,第二个条件不正确 - 无需测试值,我们知道最左边的元素将是 按构造 的最小值。试试这个:

    (define (bst-smallest bs-tree)
      (cond ((null? bs-tree)
             (display "undefined"))
            ((null? (bst-left bs-tree))
             (bst-value bs-tree))
            (else
             (bst-smallest (bst-left bs-tree)))))
    

    【讨论】:

    • 谢谢!这更有意义!
    猜你喜欢
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多