This preview shows pages 1–2. Sign up to view the full content.
Trees
Trees are a very common form of data. In fact, lists are simply trees with at most one branch at
each node.
Binary Search Trees
We often want to be able to build a data structure that allows us to look up information based on
some key value. One simple idea would be to just create a list of all the data items. The
disadvantage is that it can then take a long time to find the items at the end of the list, since we
have to look at all the prior items first. Many data structures can be used for this problem, one of
which is a
binary search tree
.
For simplicity, let's assume we just want to keep track of a bunch of numbers, remembering
which ones we've seen. From this, we could easily generalize our results to, for example, keeping
track of team player information indexed by player number, or to indexing on names rather than
numbers.
The following are some binary search trees for the numbers 1,3,4,6,9:
•
4
•
/
\
•
1
6
•
\
\
•
3
9
•
•
3
•
/
\
•
1
6
•
/ \
•
4
9
•
•
3
•
/
\
•
1
4
•
\
•
6
•
\
•
9
•
Each node holds a data item (here, number) and two possiblyempty subtrees. All the nodes in
the left branch are less than the current node. All the nodes in the right branch are greater than
the current node. The ordering of the nodes depends on how the tree is built.
Write Java Methods to do the following:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '07
 H.Shaban
 Binary Search, Computer Architecture

Click to edit the document details