This preview shows page 1. Sign up to view the full content.
306
Chapter 9
Abstract Data Types and Algorithms
With each comparison, either we find the item or cut the tree in half by
moving to search in the left subtree or the right subtree. In half? Well, not
exactly. As shown in Figure 9.17, the shape of a binary tree is not always
well balanced. Clearly, the efficiency of a search in a binary search tree is
directly related to the shape of the tree. How does the tree get its shape?
The shape of the tree is determined by the order in which items are entered
into the tree. Let’s build a binary search tree.
Building a Binary Search Tree
The clue of how to build a binary search tree lies in the search algorithm we
just used. If we follow the search path and do not find the item, we end up at
the place where it would be
if it were there
. Let’s now build a binary search
tree using strings:
john
,
phil
,
lila
,
kate
,
becca
,
judy
,
june
,
mari
,
jim
, and
sarah
.
Because
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 01/13/2011 for the course CSE 1550 taught by Professor Marianakant during the Fall '10 term at York University.
 Fall '10
 MARIANAKANT

Click to edit the document details