CS 135 Winter 2011
Brad Lushman
Assignment:
7
Due:
Wed., March 16, 2011, 9:30am
Language level:
Intermediate Student
Allowed recursion:
Structural, Generative, Accumulative
Files to submit:
bstremove.rkt
,
applyinorder.rkt
,
contract.rkt
Warmup exercises:
HtDP 17.3.1, 17.6.4, 19.1.5, 20.1.1, 20.1.2, 24.0.7, 24.0.8
Practise exercises:
HtDP 17.6.5, 17.6.6, 19.1.6, 20.1.3, 21.2.3, 24.0.9
Here are the assignment questions you need to submit.
1. Perform the Assignment 7 questions using the online evaluation “Stepping Problems” tool
linked to the course web page and available at
https://www.student.cs.uwaterloo.ca/
˜
cs135/stepping/
The instructions are the same as A03 and A04; check there for more information, if neces
sary.
Reminder: You should not use DrRacket’s Stepper to help you with this question, for a few
reasons. First, as mentioned in class, DrRacket’s evaluation rules are slightly different from
the ones presented in class; you are to use the ones presented in class. Second, in an exam
situation, of course, you will not have the Stepper to help you.
Third, you can reenter
steps as many times as necessary to get them correct, so you might as well maximize the
educational benefit.
2. For the purposes of presenting examples, we will use the following binary search tree through
out this question:
(
define
t
(
makenode
5 ””
(
makenode
3 ””
(
makenode
2 ””
(
makenode
1 ””
empty empty
)
empty
)
(
makenode
4 ””
empty empty
))
(
makenode
7 ””
(
makenode
6 ””
empty empty
)
empty
)))
On the last assignment, you wrote a function to add a new (key, value) pair to an existing
binary search tree. Removing a node from a binary search tree is a somewhat harder problem,
which you will explore here. There are several cases to consider:
•
If the node to be removed has no children, then it can simply be removed. For example:
(
checkexpect
(
bstremove t
1
)
(
makenode
5 ””
(
makenode
3 ””
(
makenode
2 ””
empty empty
)
(
makenode
4 ””
empty empty
))
(
makenode
7 ””
(
makenode
6 ””
empty empty
)
empty
)))
CS 135 — Winter 2011
Assignment 7
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
•
If the node to be removed has exactly one child, we can replace it with its only child.
For example:
(
checkexpect
(
bstremove t
7
)
(
makenode
5 ””
(
makenode
3 ””
(
makenode
2 ””
(
makenode
1 ””
empty empty
)
empty
)
(
makenode
4 ””
empty empty
))
(
makenode
6 ””
empty empty
)))
•
If the node (with key
k
) to be removed has two children, we have to do more work.
We must first find the node’s
inorder successor
—this is the node in the tree with the
smallest key larger than
k
. We then replace the node with its inorder successor, and
instead remove the inorder successor. Since the inorder successor will not have a left
child (why?), we will be able to remove it according to one of the first two cases. For
example:
(
checkexpect
(
bstremove t
5
)
(
makenode
6 ””
(
makenode
3 ””
(
makenode
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '07
 VASIGA
 Computer Science, Recursion, list representation

Click to edit the document details