Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Binary Search Tree

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

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.