Title: A Binary Search Tree Implementation
1A Binary Search Tree Implementation
2Chapter Contents
- Getting Started
- Interface for the Binary Search Tree
- Duplicate Entries
- Beginning the Class Definition
- Searching and Retrieving
- Traversing
- Adding an Entry (Iterative, Recursive)
- Removing an Entry (Iterative, Recursive)
- Whose Node a) is a leaf, b) has one child, c) has
2 children - In the Root
- Efficiency of Operations
- Importance of Balance
- Order in Which Nodes Are Added
- Implementation of the ADT Dictionary
3Getting Started
- A binary search tree is a binary tree
- Nodes contain Comparable objects
- For each node in the tree
- The data in a node is greater than the data in
the node's left subtree - The data in a node is less than the data in the
node's right subtree
4Getting Started
Fig. 26-1 A binary search tree of names.
5An Interface for the Binary Search Tree
import java.util.Iterator public interface
SearchTreeInterface extends TreeInterface pu
blic boolean contains(Comparable entry) public
Comparable getEntry(Comparable entry) public
Comparable add(Comparable newEntry) public
Comparable remove(Comparable entry) public
Iterator getInorderIterator()
6An Interface for the Binary Search Tree
Fig. 26-2 Adding an entry that matches an entry
already in a binary tree continued ?
7An Interface for the Binary Search Tree
Fig. 26-2 (ctd.) Adding an entry that matches an
entry already in a binary tree.
8Duplicate Entries
If duplicates are allowed, place the duplicate in
the entry's right subtree.
Fig. 26-3 A binary search tree with duplicate
entries.
9Beginning the Class Definition
import java.util.Iterator public class
BinarySearchTree extends BinaryTree
implements SearchTreeInterface public
BinarySearchTree() super() public
BinarySearchTree(Comparable rootEntry) super()
setRootNode(new BinaryNode(rootEntry)) .
. .
Note that it is serializable because its base
class BinaryTree is serializable.
10Searching and Retrieving
- Like performing a binary search of an array
- For a binary array
- Search one of two halves of the array
- For the binary search tree
- You search one of two subtrees of the binary
search tree
11Searching and Retrieving
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
falseelse if (desiredObject object in the
root of binarySearchTree) return trueelse if
(desiredObject lt object in the root of
binarySearchTree) return bstSearch(left subtree
of binarySearchTree, desiredObject)else return
bstSearch(right subtree of binarySearchTree,
desiredObject)
12Traversing
- 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
13Adding an Entry
Fig. 26-4 (a) A binary search tree (b) the same
tree after adding Chad.
14Adding an Entry Recursively
Fig. 26-5 Recursively adding Chad to smaller
subtrees of a binary search tree continued ?
15Adding an Entry Recursively
Fig. 26-5 (ctd.) Recursively adding Chad to
smaller subtrees of a binary search tree.
16Adding an Entry Recursively
Fig. 26-6 p. 603 When adding Chad to Doug, (a)
addNode copies its argument (null) to its local
parameter rootNode (b) rootNode references a new
node that contains Chad
17Adding an Entry Recursively
Fig. 26-7 After adding a node to the subtree
passed to it, addNode returns a reference to the
subtree so it can be attached to the rest of the
original tree.
18Removing an Entry
- The remove method must receive an entry to be
matched in the tree - If found, it is removed
- Otherwise the method returns null
- Three cases
- The node is a leaf (simplest case)
- The node has one child
- The node has two children
19Removing an Entry, Node a Leaf
Fig. 26-8 (a) Two possible configurations of leaf
node N (b) the resulting two possible
configurations after removing node N.
20Removing an Entry, Node Has One Child
Fig. 26-9 (a) Four possible configurations of
node N with one child (b) resulting two
possible configurations after removing node N.
21Removing an Entry, Node Has Two Children
Fig. 26-10 Two possible configurations of node N
that has two children.
22Removing an Entry, Node Has Two Children
Fig. 26-11 Node N and its subtrees (a) entry a
is immediately before e, b is immediately after
e (b) after deleting the node that contained a
and replacing e with a.
23Removing an Entry, Node Has Two Children
Fig. 26-12 The largest entry a in node N's left
subtree occurs in the subtree's rightmost node R.
24Removing an Entry, Node Has Two Children
Fig. 26-13 (a) A binary search tree (b) after
removing Chad
25Removing an Entry, Node Has Two Children
Fig. 26-13 (c) after removing Sean (d) after
removing Kathy.
26Removing an Entry in the Root
Fig. 26-14 (a) Two possible configurations of a
root that has one child (b) after removing the
root.
27Iterative Implementation
- To locate the desired entry
- The remove method given entry to be matched
- If remove finds the entry, it returns the entry
- If not, it returns null
- The compareTo method used to make comparisons
with the entries in the tree - p. 612 if entry is found, currentNode is the node
to be removed from tree
28Recursive 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 returns root of revised tree
- Thus use instance of ReturnObject to return the
value the public remove needs - p. 616 remove() recurses in two places (left and
right subtrees) removeEntry() is only called
once (when comparison is zero)
29Efficiency of Operations
- Operations add, remove, getEntry require a search
that begins at the root - Maximum number of comparisons 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
30Efficiency of Operations
Fig. 26-15 Two binary search trees that contain
the same data.
Shorter tree has efficiency O(log n)
31Importance of Balance
- Completely balanced
- Each nodes subtrees have same height
- Height balanced (or simply balanced)
- Each nodes subtrees differ in height by no more
than 1 - A complete binary tree is one thats balanced and
fills left to right.
32Importance of Balance
Fig. 26-16 Some binary trees that are balanced
Only (a) is a complete binary tree
33Importance of Balance
- The order in which entries are added affect the
shape of the tree - If entries are added to an empty binary tree
- Best not to have them sorted first
- Tree is more balanced if entries are in random
order
34Implementation of the ADT Dictionary
- Interface for a dictionary
import java.util.Iteratorpublic 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()
35Implementation of 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 public int
compareTo(Object other) Comparable cKey
(Comparable)key return cKey.compareTo(((Entry)o
ther).key) lt The class also defines
getKey, getValue, and setValue no setKey
method is provided. gt . . .
36Implementation of the ADT Dictionary
- Beginning of class Dictionary
import java.util.Iteratorpublic class
Dictionary implements DictionaryInterface,
java.io.Serializable private
SearchTreeInterface bst public
Dictionary() bst new BinarySearchTree()
. . .