This preview shows pages 1–3. Sign up to view the full content.
CS 135 Winter 2012
Brad Lushman
Assignment: 6
Due:
Wednesday, March 7, 9:15am
Language level:
Beginning Student with List Abbreviations
Allowed recursion:
Pure structural
Files to submit:
bst.rkt
,
minimax.rkt
,
familytree.rkt
Warmup exercises:
HtDP 14.2.1, 14.2.2, 17.2.1, 17.3.1, 17.6.4, 17.8.3, 18.1.1, 18.1.2,
18.1.3, 18.1.4, 18.1.5
Practise exercises:
HtDP 14.2.3, 14.2.6, 17.1.2, 17.2.2, 17.6.2, 17.8.4, 17.8.8, 18.1.5
Here are the assignment questions you need to submit.
1. Recall from lectures, the deﬁnition of a binary search tree:
(
deﬁnestruct node
(
key value left right
))
;; A binary search tree (BST) is one of
;; * empty
;; * a (makenode Num String BST BST),
;; where for each node (makenode k v l r), every node in the subtree
;; rooted at l has a key less than k, and every node in the subtree rooted
;; at r has a key greater than k.
Write the function
addbst
that consumes a binary search tree
abst
, a key
k
, and a value
v
.
(
addbst abst k v
) produces the binary search tree resulting from adding the key
k
, with value
v
to
abst
. If
abst
already contains the key
k
it should be updated so that it associates the key
k
with the value
v
, instead of whatever value used to be associated with
k
.
Place your solution in the ﬁle
bst.rkt
.
2. Binary search trees are most useful for searching when every choice of search direction (left
or right) eliminates about half of the remaining nodes. The degree to which this occurs
depends on the shape of the tree. Trees that look mostly linear (i.e., trees with minimal
branching) do not signiﬁcantly speed up searching. On the other hand, “bushier” trees tend
to speed up searches considerably. There are various ways to characterize trees that do tend
to speed up searches. We will explore some of these in this problem.
(a) A binary tree is called
full
if every node possesses either 0 or 2 children. Full trees
by their very nature cannot be strictly linear. Write the Scheme function
full?
that
consumes a binary search tree and produces
true
iff the given binary search tree is full.
The empty tree, paradoxically, is considered full.
(b) Although the fullness property of a binary tree does provide for maximum branching,
by preventing any node from having exactly one child, this property alone does not
CS 135 — Winter 2012
Assignment 6
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document guarantee particulary fast searches. Declare a constant
badfulltree
that encodes a full
binary search tree on the numbers 1,2,3,4,5,6,7,8,9,10,11, with the maximum possible
height. (The height of a tree is deﬁned as the maximum number of nodes traversed
from the root to a leaf.)
(c) A binary tree is called
perfect
if it is full, and additionally if every leaf in the tree has
the same
depth
(i.e., distance from the root). Equivalently, a tree is perfect if every
node’s left subtree and right subtree have the same height. As noted above, the height
of a binary tree is the maximum distance from the root to a leaf, measured in nodes. In
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 04/02/2012 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.
 Winter '07
 VASIGA
 Recursion

Click to edit the document details