Binary Tree Delete LISP

Source: Internet
Author: User


; 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.