Anish Mathuria Faculty Block 1, 1105 Binary Search Trees A binary search tree is a binary tree if a node has value N, all values in its left sub- tree are less than N, and all values in its right sub-tree are greater than N. This property called binary search tree property holds for each and every node in the tree 5 4 8 1 3 7 11 BINARY SEARCH TREE Example 17 6 19 11 15 NOT A BINARY SEARCH TREE Why? Example Operations Search for an element Minimum, Maximum Predecessor, Successor Insert, Delete BST Implementation 9 5 10 96 99 94 97 key left right 9 5 94 10 97 96 99 root BST Operations - Search Given a Binary Search Tree B, we search for a key k, first at the root, then either in the left subtree or the right subtree Search (B, k) v := root(B) if v = NULL or k = v.key then return v if k < v.key then Search (v.left, k) else Search (v.right, k) BST Operations Search (2) An Iterative search can also be defined Search (B, k) v := root(B) while v null and k v.key do if k < v.key then v := v.left else v := v.right return v Example Search for 13 BST Operations Maximum The Binary Search Tree property guarantees that the maximum is the right most child of the tree Maximum (B) v := root(B) while v.right NULL do v := v.right return v Minimum can similarly be defined Example Minimum Maximum BST Operations Insertion The insertion of an item with a new key k into a Binary Search Tree requires three steps Create a node to hold the item Determine the place in the BST where the new node is to be inserted. To do this: Search for the key k in the BST. If the key is found, false is returned and the operation terminated, otherwise, the search will encounter a null value in one of the leaf nodes of the BST Attach the new node to this leaf node, either as a left or right child Example: insertion 12 5 9 18 19 15 17 2 13 Insert 13 in the tree u Insert Psuedo-code Assuming node u is created and initialised Insert (B, u) v := root(B) if v = NULL then // an empty tree root (B) := u while v NULL do if u.key = v.key then // item with key already exists return false p := v if u.key < v.key then v := v.left else v := v.right if u.key < p.key then p.left := u else p.right := u return true Exercise Insert 95 into the following tree 10 96 99 94 97 Exercise Insert the following numbers into a Binary Search Tree: 50, 16, 89, 7, 45, 70, 66, 10 and 95 Exercise 50 16 89 95 70 66 45 7 10 Insert the following numbers into a Binary Search Tree: 50, 16, 89, 7, 45, 70, 66, 10 and 95 BST Operations - Delete When we delete a node, we need to consider how we take care of the children of the deleted node This has to be done without violating the search tree property Delete Three cases... Node to be deleted is a leaf node case 0 Node to be deleted has one child case 1 Node to be deleted has two children case 2 Find the node to be deleted If the node to be deleted is a leaf node then set the parents link to null A D B G C E I F H J Delete A A Node to be deleted is a leaf node Exercise Delete 5 from the following tree 94 10 97 5 24 11 17 5 94 10 97 24 11 17 Delete node has one child find the node to be deleted The orphan is adopted by the grandparent D B G C E I F H J Exercise Delete 24 from the following tree 94 10 97 5 24 11 17 24 94 10 97 5 11 17 Delete node has two children Find the node to be deleted Node to be deleted is replaced by the left most node (i.e. the last left child in the path) in the right sub- tree, called the successor D C G E I F H J H Promote left most node in right sub-tree delete node successor successor takes place of delete node successors parent adopts successors right child. Before delete After delete Exercise Delete 10 from the following tree 94 10 97 5 24 11 17 94 11 97 5 24 11 17 Correctness of Tree Delete How do we know case 2 should go to case 0 or case 1 instead of back to case 2? Because when node to be deleted has 2 children, its successor is the minimum in its right subtree, and that successor has no left child (hence 0 or 1 child) Equivalently, we could swap with predecessor instead of successor. It might be good to alternate to avoid creating lopsided tree Deletion Pseudo-code You have to write this complex pseudocode Level and Height The level of its node is its distance from the root The root is at level 0 The children of the root are at level 1 Their children are at level 2 The height of the tree is the level of a leaf in the longest path from the root plus 1 This is not the standard definition E B F A D H C G I Efficiency of BST Operations The operations search, insert, and delete require at most h comparisons, where h is the height of the tree Maximum height Searching, insertion, deletion take n comparisons Minimum height Searching, insertion, deletion take log n + 1 comparisons Insert 9 Insert 1 Insert 8 Insert 4 Insert 5 Insert 6 Insert 7 Its a linked list! Example of maximum height BST 8 1 9 5 4 height = 7 6 7 Insert 6 Insert 4 Insert 8 Insert 1 Insert 5 Insert 7 Insert 9 Example of minimum height BST 8 4 6 9 1 height = 3 5 7 Maximum Height Tree Given a binary tree with n nodes, what is the maximum value it could have for its height? Ans: n Minimum Height Tree Given a binary tree with n nodes, what is the minimum value it could have for its height? Assume that n = 2 k 1 for some k For this choice of n, the minimum height tree will be a Full Tree All internal (non-leaf) nodes have 2 children All leaves are on the same, last level of the tree Example of minimum height tree The binary tree below is a full tree of height 3, and it has 2 3 -1 = 7 nodes So how can we relate n (7) to the height h (3)? Level 0: 1 node = 2 0 Level 1: 2 nodes = 2 1 Level 2: 4 nodes = 2 2
Level h 1: 2 h-1 nodes The total number of nodes is the sum of the nodes at each level Recall (from the last slide) that n = 2 0 + 2 1 + + 2 h-1 for some h Note that h is the height of the tree, so if we can solve for h we are done Using math, we know that 2 0 + 2 1 + + 2 h-1 = 2 h 1 (geometric sum) Now we have n = 2 h 1 Now we also know that a Full Tree of height h has 2 h -1 nodes Taking the log 2 of both sides we get h = log 2 (n+1) Note the formula on previous slide is for a tree with 2 k -1 nodes Binary trees can have any number of nodes will this change the formula? Not significantly (omit proof) The minimum height for any binary tree with n nodes is h = 1 + log n 2 Insert 7 Insert 4 Insert 1 Insert 9 Insert 5 Example of minimum height BST 7 4 9 1 5 height = log(n)+1 = log(5)+1 = 3 Binary Search Trees Advantage: If the tree has minimum height, then every operation takes log n + 1 comparisons Problem: The tree might get very deep. For example, when inserting ordered numbers to the tree, the resulting height will be exactly n Thus, our goal is to keep the height of a binary search tree shallow Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree