Binary Search Trees Motivation Searching for a particular record in an unordered list takes O(n), too slow for large lists (databases) If the list is ordered, can use an array implementation and use binary search; however, insertion is still O(n). Is there a solution that allows efficient search and insertion? Data Structures 1 Binary Search Trees
Binary Search Trees An efficient data structure that can perform insertions, deletions and searching for arbitrary elements in logarithmic time. Binary Search Tree Property A binary search tree can be empty. Each node of the tree has a unique key. All keys in the left subtree of node with value K have values less than K All keys in the right subtree of node with value K have values greater than or equal to K Data Structures 2 Binary Search Trees
Binary Search Tree Examples 28 17 45 11 20 30 60 3 15 Data Structures 3 Binary Search Trees
Binary Search Tree Examples Binary search trees can be balanced or unbalanced. Depends on the order of the values inserted into the tree. Data Structures 4 Binary Search Trees
Binary Search Tree Operations Find an Element Insert an Element Delete an Element Other Operations: Copy, Equality, Size of Binary Search Trees. Data Structures 5 Binary Search Trees
Implementation: Binary Search Tree import java. lang. Comparable ; / / Binary Search Tree implementation class BST<Key extends Comparable<? super Key>, E> implements D i c t i o n a r y <Key, E> { private BSTNode<Key, E> r o o t ; / / Root of the BST i n t nodecount ; / / Number of nodes i n the BST BST ( ) { r o o t = null ; nodecount = 0; public void c l e a r ( ) { r o o t = null ; nodecount = 0; / / I n s e r t a record i n t o the t r e e public void i n s e r t ( Key k, E e ) { r o o t = i n s e r t h e l p ( root, k, e ) ; nodecount ++; / / Remove a record from the t r e e. public E remove ( Key k ) { E temp = f i n d h e l p ( root, k ) ; / / F i r s t f i n d i t i f ( temp!= null ) { r o o t = removehelp ( root, k ) ; / / Now remove i t nodecount ; return temp ; Data Structures 6 Binary Search Trees
Binary Search Tree Property boolean checkbst (BSTNode<Integer, Integer> root, I n t e g e r low, I n t e g e r high ) { i f ( r o o t == null ) return true ; / / Empty subtree I n t e g e r rootkey = r o o t. key ( ) ; i f ( ( rootkey < low ) ( rootkey > high ) ) return false ; / / Out of range i f (! checkbst ( r o o t. l e f t ( ), low, rootkey ) ) return false ; / / L e f t side f a i l e d return checkbst ( r o o t. r i g h t ( ), rootkey, high ) ; Data Structures 7 Binary Search Trees
Binary Search Tree Operations: Insert Inserting 14.. 28 Inserting 14 28 17 45 17 45 11 20 30 60 11 20 30 60 3 15 3 15 14 Data Structures 8 Binary Search Trees
Binary Search Tree Operations: Insert Best and convenient to do this recursively. public void i n s e r t ( Key k, E e ) { r o o t = i n s e r t h e l p ( root, k, e ) ; nodecount ++; private BSTNode<Key, E> i n s e r t h e l p (BSTNode<Key, E> r t, Key k, E e ) { i f ( r t == null ) return new BSTNode<Key, E>(k, e ) ; i f ( r t. key ( ). compareto ( k ) > 0) r t. s e t L e f t ( i n s e r t h e l p ( r t. l e f t ( ), k, e ) ) ; else r t. s e t R i g h t ( i n s e r t h e l p ( r t. r i g h t ( ), k, e ) ) ; return r t ; Data Structures 9 Binary Search Trees
Binary Search Tree Operations: Find Find 15 Find 30 28 17 45 11 20 30 60 3 15 Data Structures 10 Binary Search Trees
Binary Search Tree Operations: Find public E f i n d ( Key k ) { return f i n d h e l p ( root, k ) ; private E f i n d h e l p (BSTNode<Key, E> r t, Key k ) { i f ( r t == null ) return null ; i f ( r t. key ( ). compareto ( k ) > 0) return f i n d h e l p ( r t. l e f t ( ), k ) ; else i f ( r t. key ( ). compareto ( k ) == 0) return r t. element ( ) ; else return f i n d h e l p ( r t. r i g h t ( ), k ) ; Data Structures 11 Binary Search Trees
Binary Search Tree Operations: Removing a Node More complicated than find or insert operations. Need to consider cases of empty tree, nodes with 1 child, or nodes with 2 children. Delete 28 Delete 45 28 30 28 45 17 45 17 45 11 20 30 60 11 20 60 3 15 3 15 Data Structures 12 Binary Search Trees
Removing an Element If element is a leaf node, adjust its parent pointer to become leaf node. If element s left subtree is NULL, make its parent point to its right subtree. If element s right subtree is NULL, make its parent point to its left subtree. Element with 2 non-empty subtrees: more complicated. Replace node with the largest node in left subtree, or, Replace node with the smallest node in right subtree Data Structures 13 Binary Search Trees
Binary Search Tree Operations Removing the Minimum Key private BSTNode<Key, E> deletemin (BSTNode<Key, E> r t ) { i f ( r t. l e f t ( ) == null ) return r t. r i g h t ( ) ; else { r t. s e t L e f t ( deletemin ( r t. l e f t ( ) ) ) ; return r t ; Data Structures 14 Binary Search Trees
Binary Search Tree Operations Getting the Minimum Key private BSTNode<Key, E> getmin (BSTNode<Key, E> r t ) { i f ( r t. l e f t ( ) == null ) return r t ; else return getmin ( r t. l e f t ( ) ) ; Data Structures 15 Binary Search Trees
Binary Search Tree Operations: Removing a Node / / Remove a record from the t r e e. public E remove ( Key k ) { E temp = f i n d h e l p ( root, k ) ; / / F i r s t f i n d i t i f ( temp!= null ) { r o o t = removehelp ( root, k ) ; / / Now remove i t nodecount ; return temp ; Data Structures 16 Binary Search Trees
Remove: removehelp() / / Remove a node with key value k private BSTNode<Key, E> removehelp (BSTNode<Key, E> r t, Key k ) { i f ( r t == null ) return null ; i f ( r t. key ( ). compareto ( k ) > 0) r t. s e t L e f t ( removehelp ( r t. l e f t ( ), k ) ) ; else i f ( r t. key ( ). compareto ( k ) < 0) r t. s e t R ight ( removehelp ( r t. r i g h t ( ), k ) ) ; else { / / Found i t i f ( r t. l e f t ( ) == null ) return r t. r i g h t ( ) ; else i f ( r t. r i g h t ( ) == null ) return r t. l e f t ( ) ; else { / / Two c h i l d r e n BSTNode<Key, E> temp = getmin ( r t. r i g h t ( ) ) ; r t. setelement ( temp. element ( ) ) ; r t. setkey ( temp. key ( ) ) ; r t. s e t R ight ( deletemin ( r t. r i g h t ( ) ) ) ; return r t ; Data Structures 17 Binary Search Trees
Cost of Binary Search Tree Operations Find, Insertion, Deletion costs are a function of the depth of the tree. Traversing a tree: what is the cost? What is the depth of the tree? Average Case, Worst case? Assumption Need to keep the tree balanced. Why? Data Structures 18 Binary Search Trees