August 19, 2024
P57 - Binary search trees (dictionaries)
Write a function to construct a binary search tree from a list of integer numbers.Example:* (construct '(3 2 5 7 1))(3 (2 (1 nil nil) nil) (5 nil (7 nil nil)))
Then use this function to test the solution of the problem P56.
Example:* (symmetric '(5 3 18 1 4 12 21))T* (symmetric '(3 2 5 7 1))T* (symmetric '(3 2 5 7))NIL
lisp
(defun construct (lista)
(let ((tree nil))
(dolist (item lista tree)
(setf tree (bst-add item tree))
)
)
)
(defun bst-add (item tree)
(cond
((null tree) (list item nil nil))
((< item (first tree)) (list (first tree)
(bst-add item (second tree))
(third tree)))
((> item (first tree)) (list (first tree)
(second tree)
(bst-add item (third tree))))
;; elemento ja' existe na arvore:
;; nao coloca repetido, joga ele fora
(t tree)
)
)