; From ANSI Common LISP
; If you have questions or comments about this code, or you want
; Something I didn't include, send mail to [email protected]
; This code is copyright 1995 by Paul Graham, but anyone who wants
; To use it is free to do so.
(Defun BST-remove (obj bst <)
(If (null BST)
Nil
(Let (ELT (node-elt bst )))
(If (eql obj elt)
(Percolate BST)
(If (funcall <obj elt)
(Make-node
: ELT
: L (BST-Remove OBJ (node-l BST) <)
: R (node-r BST ))
(Make-node
: ELT
: R (BST-Remove OBJ (node-r BST) <)
: L (node-l BST )))))))
(Defun percolate (BST)
(Let (L (node-l BST) (R (node-r BST )))
(Cond (null L) R)
(Null R) L)
(T (if (zerop (random 2 ))
(Make-node: ELT (node-ELT (BST-max L ))
: R
: L (BST-Remove-max L ))
(Make-node: ELT (node-ELT (BST-min R ))
: R (BST-Remove-min R)
: L ))))))
(Defun BST-Remove-min (BST)
(If (null (node-l BST ))
(Node-r BST)
(Make-node: ELT (node-elt bst)
: L (BST-Remove-min (node-l BST ))
: R (node-r BST ))))
(Defun BST-Remove-max (BST)
(If (null (node-r BST ))
(Node-l BST)
(Make-node: ELT (node-elt bst)
: L (node-l BST)
: R (BST-Remove-max (node-r BST )))))
Binary Tree Delete LISP