I have a problem of understand an example code of Binary search trees to store an object that I’m studying on a CL guide ANSI…
First we create a structure :
Code: Select all
(defstruct (node (:print-function
(lambda(n s d )
(format s "#<~A>" (node-elt n)))))
elt (l nil) (r nil))
Code: Select all
(defun bst-insert (obj bst <)
(if (null bst)
(make-node :elt obj)
(let ((elt (node-elt bst)))
(if (eql obj elt)
bst
(if (funcall < obj elt)
(make-node
:elt elt
:l (bst-insert obj (node-l bst) <)
:r (node-r bst))
(make-node
:elt elt
:r (bst-insert obj (node-r bst) <)
:l (node-l bst)))))))
(setf nums nil )
And iterate on a list of numbers to create Tree…
Code: Select all
(dolist ( x '( 5 8 4 2 1 9 6 7 3))
(setf nums ( bst-insert x nums #'<)))
But ther’re many steps that are obscure for me:
For example the function never create an istance of node structure…infact “(make-node :elt obj)†set a value for elt slot but not create an instance of node!!…then in “else†conditions how function can call: (let ((elt (node-elt bst)))â€..????? bst isn’ t an instance of node!!!it’s like :
(let((elt (node-elt #<5>…) that raise error!!…for me it’s obscure code, can someone help me with??
Thanks in advance