LISP Problem with Binary Search Tree (Please help!!)
Posted: Thu Oct 07, 2010 9:10 am
I hv wrote a function to search a value if it's in a binary search tree (the output is T when it's in the binary tree, otherwise NIL):
When I set the input as (5 4 (9 nil 10)) and run (search 9), the variable right becomes (9 NIL 10) instead of 10. The worst is that When i search 10, the program stopped n showed no response.
It's totally fine when the input is (5 4 (9 7 10)).
I hv struggled with this problem for 5 hours, pleae help!!
Code: Select all
(defun search1 (a)
(if (listp temp) (and (setq node (car temp))
(setq left (cadr temp)) (setq right (caddr temp))))
(if (listp node)
(setq node1 (car node)) (setq node1 node))
(cond ((= a node1) (and (setq temp input) (= 1 1)))
((> a node1) (and (setq temp right)
(if (atom temp)(if (= temp a) (and (setq temp input) (= 1 1)) (and (setq temp input) (= 1 2))) (search1 a))))
((< a node1) (and (setq temp left)
(if (atom temp)(if (= temp a) (and (setq temp input) (= 1 1)) (and (setq temp input) (= 1 2))) (search1 a))))))
(defun search (a)
(setq temp input)
(if (= 1 1) (search1 a)))
It's totally fine when the input is (5 4 (9 7 10)).
I hv struggled with this problem for 5 hours, pleae help!!
