CMSC 341 Binary Search Trees 832007 UMBC CMSC
CMSC 341 Binary Search Trees 8/3/2007 UMBC CMSC 341 BST
Binary Search Tree n n A Binary Search Tree is a Binary Tree in which, at every node v, the values stored in the left subtree of v are less than the value at v and the values stored in the right subtree are greater. The elements in the BST must be comparable. Duplicates are not allowed in our discussion. Note that each subtree of a BST is also a BST. 8/3/2007 UMBC CMSC 341 BST 2
A BST of integers 42 50 20 A 27 25 60 32 99 35 B C D Describe the values which might appear in the subtrees labeled A, B, C, and D 8/3/2007 UMBC CMSC 341 BST 3
Search. Tree ADT n The Search. Tree ADT q q q 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 n n 8/3/2007 inorder -- as dictated by operator< preorder, postorder, levelorder -- as dictated by the structure of the tree UMBC CMSC 341 BST 4
BST Implementation public class Binary. Search. Tree<Any. Type extends Comparable<? super Any. Type>> { private static class Binary. Node<Any. Type> { // Constructors Binary. Node( Any. Type the. Element ) { this( the. Element, null ); } Binary. Node( Any. Type the. Element, Binary. Node<Any. Type> lt, Binary. Node<Any. Type> rt ) { element = the. Element; left = lt; right = rt; } Any. Type element; Binary. Node<Any. Type> left; Binary. Node<Any. Type> right; // The data in the node // Left child // Right child } 8/3/2007 UMBC CMSC 341 BST 5
BST Implementation (2) private Binary. Node<Any. Type> root; public Binary. Search. Tree( ) { root = null; } public void make. Empty( ) { root = null; } public boolean is. Empty( ) { return root == null; } 8/3/2007 UMBC CMSC 341 BST 6
BST “contains” Method public boolean contains( Any. Type x ) { return contains( x, root ); } private boolean contains( Any. Type x, Binary. Node<Any. Type> t ) { if( t == null ) return false; int compare. Result = x. compare. To( t. element ); if( compare. Result < 0 ) return contains( x, t. left ); else if( compare. Result > 0 ) return contains( x, t. right ); else return true; // Match } 8/3/2007 UMBC CMSC 341 BST 7
Performance of “contains” n Searching in randomly built BST is O(lg n) on average q n but generally, a BST is not randomly built Asymptotic performance is O(height) in all cases 8/3/2007 UMBC CMSC 341 BST 8
Implementation of print. Tree public void print. Tree() { print. Tree(root); } private void print. Tree( Binary. Node<Any. Type> t ) { if( t != null ) { print. Tree( t. left ); System. out. println( t. element ); print. Tree( t. right ); } } 8/3/2007 UMBC CMSC 341 BST 9
BST Implementation (3) public Any. Type find. Min( ) { if( is. Empty( ) ) throw new Underflow. Exception( ); return find. Min( root ). element; } public Any. Type find. Max( ) { if( is. Empty( ) ) throw new Underflow. Exception( ); return find. Max( root ). element; } public void insert( Any. Type x ) { root = insert( x, root ); } public void remove( Any. Type x ) { root = remove( x, root ); } 8/3/2007 UMBC CMSC 341 BST 10
The insert Operation private Binary. Node<Any. Type> insert( Any. Type x, Binary. Node<Any. Type> t ) { if( t == null ) return new Binary. Node<Any. Type>( x, null ); int compare. Result = x. compare. To( t. element ); if( compare. Result < 0 ) t. left = insert( x, t. left ); else if( compare. Result > 0 ) t. right = insert( x, t. right ); else ; // Duplicate; do nothing return t; } 8/3/2007 UMBC CMSC 341 BST 11
The remove Operation private Binary. Node<Any. Type> remove( Any. Type x, Binary. Node<Any. Type> t ) { if( t == null ) return t; // Item not found; do nothing int compare. Result = x. compare. To( t. element ); if( compare. Result < 0 ) t. left = remove( x, t. left ); else if( compare. Result > 0 ) t. right = remove( x, t. right ); else if( t. left != null && t. right != null ){ // 2 children t. element = find. Min( t. right ). element; t. right = remove( t. element, t. right ); } else t = ( t. left != null ) ? t. left : t. right; return t; } 8/3/2007 UMBC CMSC 341 BST 12
Implementations of find Max and Min private Binary. Node<Any. Type> find. Min( Binary. Node<Any. Type> t ) { if( t == null ) return null; else if( t. left == null ) return t; return find. Min( t. left ); } private Binary. Node<Any. Type> find. Max( Binary. Node<Any. Type> t ) { if( t != null ) while( t. right != null ) t = t. right; return t; } 8/3/2007 UMBC CMSC 341 BST 13
Performance of BST methods n What is the asymptotic performance of each of the BST methods? Best Case Worst Case Average Case contains insert remove find. Min/ Max make. Empty assignment 8/3/2007 UMBC CMSC 341 BST 14
Predecessor in BST n 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. n Finding predecessor q v has a left subtree n q v does not have a left subtree n 8/3/2007 then predecessor must be the largest value in the left subtree (the rightmost node in the left subtree) predecessor is the first node on path back to root that does not have v in its left subtree UMBC CMSC 341 BST 15
Successor in BST n 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. n Finding Successor q v has right subtree n q v does not have right subtree n 8/3/2007 successor is smallest value in right subtree (the leftmost node in the right subtree) successor is first node on path back to root that does not have v in its right subtree UMBC CMSC 341 BST 16
Building a BST n Given an array/vector of elements, what is the performance (best/worst/average) of building a BST from scratch? 8/3/2007 UMBC CMSC 341 BST 17
Tree Iterators n 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. q q 8/3/2007 In. Order. Iterator<T> in. Order. Iterator(); Pre. Order. Iterator<T> pre. Order. Iterator(); Post. Order. Iterator<T> post. Order. Iterator(); Level. Order. Iterator<T> level. Order. Iterator(); UMBC CMSC 341 BST 18
Using Tree Iterator public static void main (String args[] ) { Binary. Search. Tree<Integer> tree = new Binary. Search. Tree<Integer>(); // store some ints into the tree In. Order. Iterator<Integer> itr = tree. in. Order. Iterator( ); while ( itr. has. Next( ) ) { Object x = itr. next(); // do something with x } } 8/3/2007 UMBC CMSC 341 BST 19
The In. Order. Iterator is a Disguised List Iterator // An In. Order. Iterator that uses a list to store // the complete in-order traversal import java. util. *; class In. Order. Iterator<T> { Iterator<T> _list. Iter; List<T> _the. List; T next() { /*TBD*/ } boolean has. Next() { /*TBD*/ } In. Order. Iterator(Binary. Search. Tree. Binary. Node<T> root) { /*TBD*/ } } 8/3/2007 UMBC CMSC 341 BST 20
List-Based In. Order. Iterator Methods //constructor In. Order. Iterator( Binary. Search. Tree. Binary. Node<T> root ) { fill. List. Inorder( _the. List, root ); _list. Iter = _the. List. iterator( ); } // constructor helper function void fill. List. Inorder (List<T> list, Binary. Search. Tree. Binary. Node<T> node) { if (node == null) return; fill. List. Inorder( list, node. left ); list. add( node. element ); fill. List. Inorder( list, node. right ); } 8/3/2007 UMBC CMSC 341 BST 21
List-based In. Order. Iterator Methods Call List Iterator Methods T next() { return _list. Iter. next(); } boolean has. Next() { return _list. Iter. has. Next(); } 8/3/2007 UMBC CMSC 341 BST 22
In. Order. Iterator Class with a Stack // An In. Order. Iterator that uses a stack to mimic recursive traversal class In. Order. Iterator { Stack<Binary. Search. Tree. Binary. Node<T>> _the. Stack; //constructor In. Order. Iterator(Binary. Search. Tree. Binary. Node<T> root){ _the. Stack = new Stack(); fill. Stack( root ); } // constructor helper function void fill. Stack(Binary. Search. Tree. Binary. Node<T> node){ while(node != null){ _the. Stack. push(node); node = node. left; } } 8/3/2007 UMBC CMSC 341 BST 23
Stack-Based In. Order. Iterator T next(){ Binary. Search. Tree. Binary. Node<T> top. Node = null; try { top. Node = _the. Stack. pop(); }catch (Empty. Stack. Exception e) { return null; } if(top. Node. right != null){ fill. Stack(top. Node. right); } return top. Node. element; } boolean has. Next(){ return !_the. Stack. empty(); } } 8/3/2007 UMBC CMSC 341 BST 24
More Recursive BST Methods n bool is. BST ( Binary. Node<T> t ) returns true if the Binary tree is a BST n const T& find. Min( Binary. Node<T> t ) returns the minimum value in a BST n int count. Full. Nodes ( Binary. Node<T> t ) returns the number of full nodes (those with 2 children) in a binary tree n int count. Leaves( Binary. Node<T> t ) counts the number of leaves in a Binary Tree 8/3/2007 UMBC CMSC 341 BST 25
- Slides: 25