CMSC 341: Binary Search Trees
CMSC 341: Binary Search Trees
CMSC 341: Binary Search Trees
A BST of integers
42 20 A 27 25 B Describe the values which might appear in the subtrees labeled A, B, C, and D
8/3/2007 UMBC CMSC 341 BST 3
50 32 35 C D 60 99
SearchTree ADT
The SearchTree ADT
A search tree is a binary search tree which stores homogeneous elements with no duplicates. It is dynamic. The elements are ordered in the following ways
inorder -- as dictated by operator< preorder, postorder, levelorder -- as dictated by the structure of the tree
8/3/2007
BST Implementation
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> { private static class BinaryNode<AnyType> { // Constructors BinaryNode( AnyType theElement ) { this( theElement, null, null ); } BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt ) { element = theElement; left = lt; right = rt; } AnyType element; BinaryNode<AnyType> left; BinaryNode<AnyType> right; } // The data in the node // Left child // Right child
8/3/2007
8/3/2007
8/3/2007
Performance of contains
Searching in randomly built BST is O(lg n) on average
but generally, a BST is not randomly built
8/3/2007
Implementation of printTree
public void printTree() { printTree(root); } private void printTree( BinaryNode<AnyType> t ) { if( t != null ) { printTree( t.left ); System.out.println( t.element ); printTree( t.right ); } }
8/3/2007
8/3/2007
10
Worst Case
Average Case
Predecessor in BST
Predecessor of a node v in a BST is the node that holds the data value that immediately precedes the data at v in order. Finding predecessor
v has a left subtree
then predecessor must be the largest value in the left subtree (the rightmost node in the left subtree)
Successor in BST
Successor of a node v in a BST is the node that holds the data value that immediately follows the data at v in order. Finding Successor
v has right subtree
successor is smallest value in right subtree (the leftmost node in the right subtree)
Building a BST
Given an array/vector of elements, what is the performance (best/worst/average) of building a BST from scratch?
8/3/2007
17
Tree Iterators
As we know there are several ways to traverse through a BST. For the user to do so, we must supply different kind of iterators. The iterator type defines how the elements are traversed.
InOrderIterator<T> inOrderIterator(); PreOrderIterator<T> preOrderIterator(); PostOrderIterator<T> postOrderIterator(); LevelOrderIterator<T> levelOrderIterator();
8/3/2007 UMBC CMSC 341 BST 18
// An InOrderIterator that uses a list to store // the complete in-order traversal import java.util.*; class InOrderIterator<T> { Iterator<T> _listIter; List<T> _theList; T next() { /*TBD*/ boolean hasNext() { /*TBD*/
8/3/2007
20
8/3/2007
21
8/3/2007
22
8/3/2007
23
Stack-Based InOrderIterator
T next(){ BinarySearchTree.BinaryNode<T> topNode = null; try { topNode = _theStack.pop(); }catch (EmptyStackException e) { return null; } if(topNode.right != null){ fillStack(topNode.right); } return topNode.element; } boolean hasNext(){ return !_theStack.empty(); } }
8/3/2007
24
8/3/2007
25