A Binary Search Tree Implementation
A Binary Search Tree Implementation
Implementation
Chapter 26
Chapter Contents
Getting Started • Whose Node Has One
• An Interface for the Binary Child
Search Tree • Whose Node has Two
• Duplicate Entries Children
• Beginning the Class
• In the Root
Definition • Iterative Implementation
Searching and Retrieving • Recursive Implementation
3
Getting Started
5
An Interface for the
Binary Search Tree
import java.util.Iterator;
public class BinarySearchTree extends BinaryTree
implements SearchTreeInterface
{ public BinarySearchTree()
{ super();
} // end default constructor
public BinarySearchTree(Comparable rootEntry)
{ super();
setRootNode(new BinaryNode(rootEntry));
} // end constructor
... Note
Notethat
thatititisisserializable
serializable
because
becauseits
itsbase
baseclass
class
BinaryTree
BinaryTreeisisserializable.
serializable. 9
Searching and Retrieving
10
Searching and Retrieving
The search algorithm
Algorithm bstSearch(binarySearchTree, desiredObject)
// Searches a binary search tree for a given object.
// Returns true if the object is found.
if (binarySearchTree is empty)
return false
else if (desiredObject == object in the root of binarySearchTree)
return true
else if (desiredObject < object in the root of binarySearchTree)
return bstSearch(left subtree of binarySearchTree, desiredObject)
else
return bstSearch(right subtree of binarySearchTree, desiredObject)
11
Traversing
The SearchTreeInterface provides the
method getInorderIterator
• Returns an inorder iterator
Our class is a subclass of BinaryTree
• It inherits getInorderIterator
• This iterator traverses entries in ascending
order
• Uses the entries' method compareTo
12
Adding an Entry
13
Adding an Entry Recursively
15
Adding an Entry Recursively
Three cases
• The node has no children, it is a leaf (simplest
case)
• The node has one child
• The node has two children
18
Removing an Entry, Node a Leaf
27
Recursive Implementation
Details similar to adding an entry
The public remove method calls a private
recursive remove method
• The public remove returns removed entry
• Private remove must return root of revised
tree
• Thus use parameter that is an instance of
ReturnObject to return the value the public
remove needs
28
Efficiency of Operations
Operations add, remove, getEntry require a
search that begins at the root
Maximum number of comparisons is
directly proportional to the height, h of the
tree
These operations are O(h)
Thus we desire the shortest binary search
tree we can create from the data
29
Efficiency of Operations
Shorter
Shortertree
treehas
has
efficiency
efficiencyO(log
O(logn)
n) 30
Importance of Balance
Completely balanced
• Subtrees of each node have exactly same
height
Height balanced
• Subtrees of each node in the tree differ in
height by no more than 1
Completely balanced or height balanced
trees are balanced
31
Importance of Balance
33
Implementation of the ADT
Dictionary
Interface for a dictionary
import java.util.Iterator;
public interface DictionaryInterface
{ public Object add(Object key, Object value);
public Object remove(Object key);
public Object getValue(Object key);
public boolean contains(Object key);
public Iterator getKeyIterator();
public Iterator getValueIterator();
public boolean isEmpty();
public int getSize();
public void clear();
} // end DictionaryInterface
34
Implementation of the ADT
Dictionary
Class of data entries (can be private,
internal to class Dictionary)
private class Entry implements Comparable, java.io.Serializable
{ private Object key;
private Object value;
private Entry(Object searchKey, Object dataValue)
{ key = searchKey;
value = dataValue; } // end constructor
public int compareTo(Object other)
{ Comparable cKey = (Comparable)key;
return cKey.compareTo(((Entry)other).key); } // end compareTo
< The class also defines the methods getKey, getValue, and setValue;
no setKey method is provided. >
...
} // end Entry 35
Implementation of the ADT
Dictionary
Beginning of class Dictionary
import java.util.Iterator;
public class Dictionary implements DictionaryInterface,
java.io.Serializable
{ private SearchTreeInterface bst;
public Dictionary()
{ bst = new BinarySearchTree();|
} // end default constructor
...
} // end Dictionary
36