This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 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: bst-remove.rkt , apply-in-order.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 DrRackets Stepper to help you with this question, for a few reasons. First, as mentioned in class, DrRackets 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 re-enter 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 ( make-node 5 ( make-node 3 ( make-node 2 ( make-node 1 empty empty ) empty ) ( make-node 4 empty empty )) ( make-node 7 ( make-node 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: ( check-expect ( bst-remove t 1 ) ( make-node 5 ( make-node 3 ( make-node 2 empty empty ) ( make-node 4 empty empty )) ( make-node 7 ( make-node 6 empty empty ) empty ))) CS 135 Winter 2011 Assignment 7 1 If the node to be removed has exactly one child, we can replace it with its only child. For example: ( check-expect ( bst-remove t 7 ) ( make-node 5 ( make-node 3 ( make-node 2 ( make-node 1 empty empty ) empty ) ( make-node 4 empty empty )) ( make-node 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 nodes 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. Forchild (why?...
View Full Document
This note was uploaded on 10/02/2011 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.
- Winter '07