gpt4 book ai didi

list - BST-最小方案

转载 作者:行者123 更新时间:2023-12-02 03:20:58 27 4
gpt4 key购买 nike

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

(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)))
))

最佳答案

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

(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)))))

关于list - BST-最小方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33469132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com