Java Classes
Download
Report
Transcript Java Classes
A Binary Search
Tree
Implementation
Chapter 27
Slides by Steve Armstrong
LeTourneau University
Longview, TX
2007, Prentice Hall
Chapter Contents
• Getting Started
An Interface for the Binary Search Tree
Duplicate Entries
Beginning the Class Definition
• Searching and Retrieving
• Traversing
• Adding an Entry
Recursive Implementation
Iterative Implementation
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Chapter Contents
• Removing an Entry …
Whose Node is a leaf
Whose Node Has One Child
Whose Node has Two Children
In the Root
Recursive Implementation
Iterative Implementation
• Efficiency of Operations
Importance of Balance
Order in Which Nodes Are Added
• Implementation of the ADT Dictionary
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Getting 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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Getting Started
Fig. 27-1 A binary search tree of names.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
An Interface for the
Binary Search Tree
• Includes common operations of a tree
• Also includes basic database operations
Search
Retrieve
Add
Remove
Traverse
• View source code for
SearchTreeInterface
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
An Interface for the
Binary Search Tree
Fig. 27-2 Adding an entry that matches an entry
already in a binary tree … continued →
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
An Interface for the
Binary Search Tree
Fig. 27-2 Adding an entry that matches an entry
already in a binary tree.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Duplicate Entries
If duplicates are allowed,
place the duplicate in the
entry's right subtree.
Fig. 27-3 A binary search tree with duplicate entries.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Beginning the Class Definition
• View source code of BinarySearchTree
• Note
Derived from BinaryTree
Call by constructor to protected method
setRootNode (inherited from BinaryTree)
• Other methods
contains
getEntry
add
remove
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Searching 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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Searching and Retrieving
• View search algorithm
Root node of tree or subtree enables
manipulation of descendant nodes
• Note implementation in methods
getEntry
findEntry
• Method contains simply calls getEntry
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Adding an Entry
Fig. 27-4 (a) A
binary search tree;
(b) the same tree
after adding Chad.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Adding an Entry Recursively
Fig. 27-5 Recursively adding Chad to smaller subtrees
of a binary search tree … continued →
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Adding an Entry Recursively
Fig. 27-5 (ctd.)
Recursively adding
Chad to smaller
subtrees of a binary
search tree.
Click to view recursive
method addEntry
Click to view iterative
method addEntry
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing 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 has no children, it is a leaf (simplest
case)
The node has one child
The node has two children
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry, Node a Leaf
Fig. 27-6 (a) Two possible configurations of leaf
node N; (b) the resulting two possible
configurations after removing node N.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry,
Node Has One Child
Fig. 27-6 (a) Two possible configurations of leaf node N; (b) the
resulting two possible configurations
after removing node N.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry,
Node Has Two Children
Fig. 27-8 Two possible configurations of node N
that has two children.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry,
Node Has Two Children
Fig. 27-9 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.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry,
Node Has Two Children
Fig. 27-10 The largest entry a in node N's left subtree
occurs in the subtree's rightmost node R.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry,
Node Has Two Children
Fig. 27-11 (a) A binary search tree; (b)
after removing Chad;
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry,
Node Has Two Children
Fig. 27-11 (c) after removing Sean;
(d) after removing Kathy.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Removing an Entry in the Root
Fig. 27-12 (a) Two possible configurations of a root that
has one child; (b) after removing the root.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
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
• View source code for recursive implementation
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Iterative 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
• View source code for iterative
implementation
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficiency of Operations
Fig. 27-13 Two
binary search trees
that contain the
same data.
Shorter tree has
efficiency O(log n)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Importance of Balance
Fig. 27-14 Some binary trees that are height balanced.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Importance 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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Implementation of the ADT
Dictionary
• Recall interface for a dictionary (Ch. 17)
• Consider an implementation of the ADT
dictionary that uses a binary search tree
• Note methods
add
remove
Use of iterator
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X