CS2020: Data Structures and Algorithms (Accelerated)
Problems 5–6
Due: February 2nd, 13:59
Overview.
You have two tasks this week. The ﬁrst involves implementing a basic binary search
tree. In another problem set later this semester, you will improve your search tree so that it always
remains balanced (using a new weightbalancing technique diﬀerent from that seen in class). The
second task involves using a balanced search tree to implement an airport scheduling system.
Collaboration Policy.
As always, you are encouraged to work with other students on solving
these problems. However, you
must
write up your solution
by yourself
. In addition, when you
write up your solution, you
must
list the names of every collaborator, that is, every other person
that you talked to about the problem (even if you only discussed it brieﬂy). Any deviation from
this policy will be considered cheating, and will be punished severely, including referral to the NUS
Board of Discipline.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentProblem 5.
Basic Binary Search Trees
For this question, your task is to implement a basic binary search tree in Java. Notably, your
search tree must satisfy the binary search tree property: every key in a node’s left subtree must
be smaller than the key stored at the node itself, and every key in a node’s right subtree must be
larger than the key stored at the node itself. Inserting an element into the tree and searching for
an element in the tree should take time
O
(
h
), where
h
is the height of the tree.
Your solution to this problem set should consist of a class which implements the
ITreeNode
interface distributed with the problem set. A tree node should contain a key, along with a reference
to its parent (if any), its left child (if any) and its right child (if any). The
ITreeNode
interface
contains the following methods:
•
getLeftChild
,
getRightChild
,
getParent
: returns the left child, the right child, or the
parent (or null, if the speciﬁed child/parent does not exist).
•
getWeight
: returns the total number of keys stored in the subtree rooted at the tree node
(see below)
•
getKey
: returns the key associated with the tree node
•
getTreeKeys
: returns a sorted list of every key in the subtree rooted at the tree node
•
insert
: inserts an integer into the subtree rooted at the tree node
•
search
: queries whether a given integer appears in the subtree rooted at the tree node
Each of these functions should be implemented as eﬃciently as possible.
Notice that one of the required functions returns the
weight
of a node. We deﬁne the weight of
a tree node as the total number of keys that are stored in that subtree. For example, the weight of
a leaf node is 1. In general, the weight of a node
v
is equal to:
v.left.weight + v.right.weight
+ 1
(where we deﬁne
v.left.weight = 0
if
v
has no left child, and
v.right.weight = 0
if
v
has
no right child). While the weight can be recalculated on every call to
getWeight
, that can be quite
expensive (costing
O
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '11
 SethGilbert
 Algorithms, Binary Search, Data Structures

Click to edit the document details