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

Deleting a Node from a Binary Search Tree (Scheme

on

  • 3,831 views

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.

Statistics

Views

Total Views
3,831
Views on SlideShare
3,828
Embed Views
3

Actions

Likes
0
Downloads
28
Comments
0

2 Embeds 3

http://wildfire.gigya.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Deleting a Node from a Binary Search Tree (Scheme Deleting a Node from a Binary Search Tree (Scheme Presentation Transcript

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