Binary Search Trees Binary Search Trees Stores keys
Binary Search Trees
Binary Search Trees • Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently. • A Binary Search Tree (BST) is a binary tree with the following properties: For every node X: 1. All the keys in its left subtree are smaller than or equal to the key value in node X 2. All the keys in its right subtree are larger than the key value in node X 2
Binary Search Trees: Examples root 14 C A 10 D 8 16 11 15 18 NOTE! When nodes of a BST are traversed by Inorder traversal the keys appear in sorted order 3
Searching for a key in a BST P L R How do you search BST for node with key x ? 4
Searching for a key in a BST search(root, x) compare x to key of root: - if x = key return true - if x < key => search in L - if x > key => search in R - if no subtree, return false search L or R in the exact same way Example: 14 10 8 16 11 15 x=10 is ? ? ? X=19 is ? ? ? Time complexity O(height of the tree) 5
Binary search – Object nodes public class BSTNode { Comparable data; BSTNode left; BSTNode right; } 6
Searching for a key in a BST public boolean search(Comparable obj) { int comp = data. compare. To(obj); if (comp==0) return true; 6 if (comp>0 && left!=null) return left. search(obj); else if (comp<0 && right!=null) 2 return right. search(obj); return false; } 0 4 10 22 7 14 9 13 19 7
Inserting a new key in a BST How to insert a new key? Example: 10 The same procedure used for search also applies: – Determine the location by searching. – Search will fail. – Insert new key where the search failed. 8 3 2 Insert 4? 9 4 5 8
Inserting a new key in a BST Design decision: if the key is found? • • throw exception do nothing multiple entries (our choice) replace current entry 9
Inserting a new key in a BST public boolean insert(Comparable obj) { int comp = data. compare. To(obj); if (comp>0) { if (left!=null) return left. insert(obj); else {left = new BSTNode(obj, null); return true; } } else // new element greater { if (right!=null) return right. insert(obj); else {right = new BSTNode(obj, null); return true; } } } // Main describes non-recursive method 10
Deleting from a BST To delete node with key x first you need to search for it. Once found, apply one of the following three cases CASE A: x is a leaf p p q r x delete x BST property maintained 11
Deleting from a BST cont. Case B: x is interior with only one subtree r r x q q delete x L L BST property maintained 12
Deleting from a BST cont. Case C: x is interior with two subtrees r r x r delete x q s r delete x Z q W Z ts W t BST property maintained 13
Deleting from a BST cont. Case C cont: … or you can also do it like this q < x < r r Þ q is smaller than the smaller element in Z Þ r is larger than the largest element in W q W t r s Z Can you see other possible ways ? 14
Deleting from a BST cont. Case C cont: … or you can also do it like this r r x delete x q r W Z ts r OR s t q r W Z s 15
Deleting from a BST cont. • Remove node with two children (22) – find next node in Inorder (23) – copy next node data to this node (22 23 ) 10 – remove next node instead (23) 23 22 6 2 0 7 4 14 9 13 26 19 23 16 28
Building a BST Build a BST from a sequence of nodes read one a time Example: Inserting 1) Insert C C A B L M (in this order!) 2) Insert A C C A 17
Building a BST 3) Insert B C 5) Insert M A C B L A C 4) Insert L B M L A B 18
Building a BST Is there a unique BST for letters A B C L M ? NO! Different input sequences result in different trees Inserting: A B C L M A Inserting: C A B L M C B C L A L M B M 19
BST Applications • Sorting with a BST – Given a BST can you output its keys in sorted order? Yes, print the keys using Inorder traversal How can you find the minimum? How can you find the maximum? Find the Leftmost node Find the Rightmost node 20
BST Applications • Find kth element in BST – Given a BST can you output the Kth element? 21 Data Structures & Problem Solving using JAVA/2 E Mark Allen Weiss © 2002 Addison Wesley
Complexity of Searching BST • It depends on the shape of the tree. • For full or complete binary trees: – best time analysis – worst time analysis ………… O(1) O(log N) • Balanced Binary Tree: – The heights of the two child subtrees of any node differ by at most one. 22
Complexity of Searching BST • • Unfortunately, inserting and removing are uncontrolled in BST This may lead to a BST like this one: A – best time analysis – worst time analysis O(1) O(N) B C L M 23
Assuring Performance of BSTs inserting and removing must rebalance tree to maintain performance 10 Balanced trees are called AVL trees – we will not be studying these trees. 22 10 22 26 26 26
- Slides: 24