This preview shows pages 1–4. Sign up to view the full content.
B
INARY
S
EARCH
T
REES
Contents
z
Introduction
{
Test Yourself
#1
z
Implementing Binary Search Trees
{
The lookup method
{
The
insert method
±
Test Yourself #2
{
The delete method
±
Test Yourself #3
z
Summary
Introduction
An important special kind of binary tree is the
binary search tree (BST)
. In a BST, each node stores
some information including a unique
key value
, and perhaps some associated data. A binary tree is a
BST iff, for every node n in the tree:
z
All keys in n's left subtree are less than the key in n, and
z
all keys in n's right subtree are greater than the key in n.
Note: if duplicate keys are allowed, then nodes with values that are equal to the key in node n can be
either in n's left subtree or in its right subtree (but not both). In these notes, we will assume that
duplicates are not allowed.
Here are some BSTs in which each node just stores an integer key:
These are not BSTs:
Page 1 of 14
Binary Search Trees
2008/3/27
http://pages.cs.wisc.edu/~cs3671/topics/BinarySearchTrees/
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document In the left one 5 is not greater than 6. In the right one 6 is not greater than 7.
Note that more than one BST can be used to store the same set of key values. For example, both of
the following are BSTs that store the same set of integer keys:
The reason binarysearch trees are important is that the following operations can be implemented
efficiently using a BST:
z
insert a key value
z
determine whether a key value is in the tree
z
remove a key value from the tree
z
print all of the key values in sorted order
TEST
YOURSELF #1
Question 1: Which of the following binary trees are BSTs? If a tree is
not
a BST, say why.
Question 2: Using which kind of traversal (preorder, postorder, inorder, or levelorder) visits the
nodes of a BST in sorted order?
solution
Implementing BSTs
To implement a binary search tree, we will use two classes: one for the individual tree nodes, and
one for the BST itself. The following class definitions assume that the BST will store only key
values, no associated data.
A
10
cat
15
/ \
/
/
\
/
\
B
C
5
bat
rat
5
22
/
/
\
\
3
ant
20
30
Page 2 of 14
Binary Search Trees
2008/3/27
http://pages.cs.wisc.edu/~cs3671/topics/BinarySearchTrees/
The type parameter
K
is the type of the key. Because most of the BST operations require comparing
key values, we declare that
K extends Comparable
, meaning that type
K
must implement a method
To implement a BST that stores some data with each key, we would use the following class
definitions (changes are in red):
class BinaryTreenode<K extends Comparable> {
// *** fields ***
private K key;
private BinaryTreenode<K> left, right;
// *** methods ***
// constructor
public BinaryTreenode(K k, BinaryTreenode<K> l, BinaryTreenode<K> r) {
key = k;
left = l;
right = r;
}
// access to fields
public K getKey() {return key;}
public BinaryTreenode<K> getLeft() {return left;}
public BinaryTreenode<K> getRight() {return right;}
// change fields
public void setKey(K k) {key = k;}
public void setLeft(BinaryTreenode<K> l) {left = l;}
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.
This note was uploaded on 03/27/2008 for the course MATH 211 taught by Professor Onlineresources during the Spring '06 term at Wisconsin.
 Spring '06
 OnlineResources

Click to edit the document details