Your SlideShare is downloading. ×
0
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Deleting a Node from a Binary Search Tree (Scheme
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Deleting a Node from a Binary Search Tree (Scheme

4,395

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.

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,395
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Deleting a Node from a BST<br />Allan Caine<br />November 11, 2009<br />
  • 2. Four Cases<br />2<br />
  • 3. Both Subtrees Empty<br />X<br />Empty<br />Empty<br />Empty<br />3<br />
  • 4. Left Subtree non-EmptyRight Subtree Empty<br />X<br />L<br />L<br />Empty<br />4<br />
  • 5. Left Subtree EmptyRight Subtree non-Empty<br />X<br />R<br />R<br />Empty<br />5<br />
  • 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. 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. 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. The Search Routine<br />(define (remove-key n abst)<br /> (cond<br /> [(empty? abst) empty]<br /> [(= n (node-key abst)) (remove-root abst)]<br /> [(&amp;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 />

×