为什么我的Common Lisp Binary搜索树函数不能正常工作?

问题描述 投票:0回答:1

我必须创建一个lisp程序,该程序可以检查二进制搜索树是否实际上是BST。

这就是我所做的:

(defun BST (lst)
(if (null lst) nil
    (let ((curr (car lst)) (left (car (cdr lst))) (right (cdr (cdr lst)))) 
         (cond
             ((and (null left) (null right)) t)
             ((and (numberp (car left)) (> (car left) curr)) nil)
             ((and (numberp (car right)) (<= (car right) curr)) nil)
             ((null right) (BST left))
             ((null left) (BST right))
             (t (and (BST right) (BST left)))))))

(print (BST '(8 (3 (1 () ()) (6 (4 () ())(7 () ()))) (10 () (14 (13) ())))))

结果为t,但是如果我将10修改为小于8的值,则结果仍然为true。实际上,我的函数似乎完全忽略了树的右侧,仅对8-> 3-> 1路径中的更改做出反应。任何帮助,将不胜感激,谢谢。

lisp binary-tree common-lisp binary-search-tree clisp
1个回答
0
投票

您的问题在于抽象。空无一人。

© www.soinside.com 2019 - 2024. All rights reserved.