Deleting a Node from a BST<br />Allan Caine<br />November 11, 2009<br />
Four Cases<br />2<br />
Both Subtrees Empty<br />X<br />Empty<br />Empty<br />Empty<br />3<br />
Left Subtree non-EmptyRight Subtree Empty<br />X<br />L<br />L<br />Empty<br />4<br />
Left Subtree EmptyRight Subtree non-Empty<br />X<br />R<br />R<br />Empty<br />5<br />
Left and Right Subtrees non-Empty<br />X<br />X<br />R<br />L<br />L<br />New Root<br />6<br />?<br />?<br />
Continuing with the Recursive Case<br />Root<br />7<br />New Root<br />New Root<br />These nodesare the same.<br />New Roo...
Remove Root Code<br />(define (remove-root abst)<br />  (cond<br />    [(empty? abst) empty]<br />    [(and (empty? (node-...
The Search Routine<br />(define (remove-key n abst)<br />  (cond<br />    [(empty? abst) empty]<br />    [(= n (node-key a...
Upcoming SlideShare
Loading in...5
×

Deleting a Node from a Binary Search Tree (Scheme

4,420

Published on

Explains how to delete a node from a binary search tree. The code is written in Scheme. The presentation is a supplement to my CS115 lecture.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,420
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Deleting a Node from a Binary Search Tree (Scheme

  1. 1. Deleting a Node from a BST<br />Allan Caine<br />November 11, 2009<br />
  2. 2. Four Cases<br />2<br />
  3. 3. Both Subtrees Empty<br />X<br />Empty<br />Empty<br />Empty<br />3<br />
  4. 4. Left Subtree non-EmptyRight Subtree Empty<br />X<br />L<br />L<br />Empty<br />4<br />
  5. 5. Left Subtree EmptyRight Subtree non-Empty<br />X<br />R<br />R<br />Empty<br />5<br />
  6. 6. Left and Right Subtrees non-Empty<br />X<br />X<br />R<br />L<br />L<br />New Root<br />6<br />?<br />?<br />
  7. 7. Continuing with the Recursive Case<br />Root<br />7<br />New Root<br />New Root<br />These nodesare the same.<br />New Root<br />L’<br />R’<br />Empty<br />Empty<br />Empty<br />Empty<br />L<br />New Root<br />New Root<br />L’<br />?<br />?<br />?<br />?<br />
  8. 8. Remove Root Code<br />(define (remove-root abst)<br /> (cond<br /> [(empty? abst) empty]<br /> [(and (empty? (node-left abst))<br /> (empty? (node-right abst))) empty]<br /> [(empty? (node-left abst)) (node-right abst)]<br /> [(empty? (node-right abst)) (node-left abst)]<br /> [else (make-node (node-key (node-right abst))<br /> (node-value (node-right abst))<br /> (node-left abst)<br /> (remove-root (node-right abst)))]))<br />8<br />
  9. 9. The Search Routine<br />(define (remove-key n abst)<br /> (cond<br /> [(empty? abst) empty]<br /> [(= n (node-key abst)) (remove-root abst)]<br /> [(&lt; n (node-key abst)) (make-node (node-key abst)<br /> (node-value abst)<br /> (remove-key n (node-left abst))<br /> (node-right abst))]<br /> [else (make-node (node-key abst)<br /> (node-value abst)<br /> (node-left abst)<br /> (remove-key n (node-right abst)))]))<br />9<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×