Download CS 310: Binary Search Trees and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!
CS 310: Binary Search Trees
Chris Kauffman
Week 11-
Logistics
Reading
I (^) Weiss Ch. 7 Recursion I (^) Weiss Ch. 19 BSTs
Today
I (^) Tree traversals I (^) Mention Tree Iterators I (^) Comparable vs Comparator I (^) Binary Search Trees
HW
I (^) Due Thursday I (^) Questions?
HW
I (^) Will post and discuss on Thursday I (^) Last HW of the course
Comparisons
How does java guarantee comparability?
Comparable
Data can implement Comparable
int c = x.compareTo(y); // neg for x before y // 0 for x equals y // pos for x after y
Comparator
Use a Comparator object to do comparisons
Comparator cmp = new ...; int c = cmp.compare(x,y); // neg for x before y // 0 for x equals y // pos for x after y
I (^) Generalizes subtraction: c = x-y; I (^) Presence of both hints at a fundamental problem
Comparators allow multiple sorting criteria
public class RCComp { public int compare(RowCol a, RowCol b) { // compare a.row to b.row // if tied, compare a.col to b.col } } public class CRComp { public int compare(RowCol a, RowCol b) { // compare a.col to b.col // if tied, compare a.row to b.row } }
main(){ RowCol x = ...; RowCol y = ...; Comparator rc = new RCComp(); if(rc.compare(x,y) < 0){ // x before y according to rc } else if(rc.compare(x,y) > 0){ // x after y according to rc } else{ // x equals y according to rc } Comparator cr = new CRComp(); if(cr.compare(x,y) < 0){ // x before y according to cr } else if(cr.compare(x,y) > 0){ // x after y according to cr } else{ // x equals y according to cr } }
Define bst.find()
I (^) find(T x) is publicly accessible tree.find("Mario"); I (^) Define find(T x, Node t) which works on a given start node I (^) Compare via Comparable: if(x.compareTo(t.data) < 0) Give 2 versions I (^) Recursive I (^) Iterative
public class BinarySearchTree <T extends Comparable> { protected Node root; // Return x if in tree, // null otherwise public T find( T x ){ Node result = find(x, this.root); if(result == null){ return null;} else{ return result.data; } }
// Find node containing x // starting at node t // Return null if not found private static Node find(T x, Node t){ // DEFINE ME } }
Recursive find(x,node)
Use key of data to search through tree I (^) Left for less than I (^) Right for greater than
// pseudocode Node find(T x, Node t){ if(t == null){ return null; } int diff = x.compareTo(t.data); if(diff < 0){ // x < t.data return find(x,t.left); } else if(diff > 0){ // x > t.data return find(x,t.right); } else { // x==t.data return t.data; // found } }
Complexity of BST Find
I (^) What is the worst-case complexity of find(x) in terms of tree properties? I (^) Construct a tree with this this worst case complexity
Examples of Insert
Play with MyBST.java in JGrasp and look at the pretty pictures after multiple insert(x) calls
Exercise: Recursive insert(x,t)
From weiss/nonstandard/BinarySearchTree.java class BinarySearchTree { Node root; public void insert( T x ){ root = insert( x, root ); } private static Node insert( T x, Node t ) { if( t == null ) t = new Node( x ); else if( x.compareTo( t.data ) < 0 ) t.left = insert( x, t.left ); else if( x.compareTo( t.data ) > 0 ) t.right = insert( x, t.right ); else throw new DuplicateItemException( x.toString( ) ); return t; } }
Compare: Iterative insert(x,t)
class BinarySearchTree { Node root; public void insert( T x ){ if(this.root == null){ // New root? this.root = new Node(x); // Yes return; // } // Node t = this.root; // Root exists while(true){ // Traverse tree int diff = x.compareTo(t.data); // if(diff == 0){ // Check duplicate throw new DuplicateItemException( x.toString( ) ); } if(diff < 0){ // Go left if(t.left == null){ // Found insertion point? t.left = new Node(x); // Yesreturn; // } // else{ // No: Go farther left t = t.left; } } else{ // Go right if(t.right == null){ // Found insertion point? t.right = new Node(x); // Yes return; // } // else{ // No: Go farther left t = t.right; } } } } }
Prelims
Consider Mario Tree
I (^) Describe which cases exist tree.remove(x)? I (^) Which of these do you anticipate being easy/hard to code for?
BST General remove(x): Cases
Cases for t.remove(x)
- x not in tree I (^) Leave tree as is or raise an exception
- x at a node with no children I (^) Get rid of node containing x
- x at a node with 1 child I (^) "Pass over" node containing x
- x at a node with 2 children I (^) Find a next node in sorting order I (^) Replace x with next nodes data I (^) Remove next node I (^) Next is minimum of right subtree
Warm-up Questions
- What is the Binary Search Tree property?
- Are all trees binary trees? Do all binary trees have the BST property? (give counter-examples)
- Where is the biggest data element in a BST? The smallest?
- What are the runtime complexities of BST tree.find(x) and tree.insert(x)?
- Which kinds of nodes are easy to remove from BSTs? Which kinds are more difficult?
- What is a useful strategy for removing difficult nodes?
Children Cases for remove(t,x)
One Child: Remove 5
- Find node t with data x
- Replace with only child
Two Children: Remove 2
- Find node t with data x
- Find min node of t.right: min must have 0/1 child
- Replace t.data with min.data
- Remove min