(defun is-bst (usertree) (if (null (bst-finder usertree)) (write-line "The Tree is a Binary Search Tree.") (write-line "The Tree is NOT a Binary Search Tree.") ) ) (defun bst-finder (usertree) (if (not (null (car (car (cdr usertree))))) (cond ((null usertree) t) ;is tree emtpy ? ((< (car usertree) (car (car (cdr usertree)))) t) ;is left > top ? ((bst-finder (car (cdr usertree))) t) ;try again left ) nil ;if we reach the end ;of the branch, return null ) (if (not (null (car (car (cdr (cdr usertree)))))) (cond ((null usertree) t) ;is tree emtpy ? ((> (car usertree) (car (car (cdr (cdr usertree))))) t) ;is right < top ? ((bst-finder (car (cdr (cdr usertree)))) t) ;try again right ) nil ;if we reach the end ;of the branch, return null ) ) ;============================================================================================== (write (list 8 '(3 (1 () ()) (6 (4 () ())( 7 () ()))) '(10 (()) (14 (13) ())))) (write-line "") (is-bst (list 8 '(3 (1 () ()) (6 (4 () ())( 7 () ()))) '(10 (()) (14 (13) ())))) (print (list 8 '(3 (5 () ()) (6 (4 () ())( 7 () ()))) '(10 (()) (2 (13) ())))) (write-line "") (is-bst (list 8 '(3 (5 () ()) (6 (4 () ())( 7 () ()))) '(10 (()) (2 (13) ()))))
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more