Time Complexity of Basic BST Operations • Search, Insert, Delete – These operations visit the nodes along a root-toleaf path – The number of nodes encountered on unique path depends on the shape of the tree and the position of the node in the tree Worst-case running time of each operation? O(h) where h is the height of the tree • The height varies –A “worst-shape” BST –A “best-shape” BST –An “average-shape” BST Different Shapes of Tree Balanced BST Can Do better • BSTs are limited because of their bad worstcase performance O(n). A BST with this worstcase structure is no more efficient than a regular linked list • Balanced search trees are trees whose heights in the worst case is O(lg n) Balanced BST Can Do better 0 1 2 3 . 10 . 13 . h-1 If the tree is perfectly balanced, we can store more than 16,000 elements and require at most 14 nodes to be checked to locate an element. How many nodes (in the worst case) would we have to check in a linked list? What Does it Mean to Balance a BST? • Tentative Rule – Require that the left and right subtrees of the root node have the same height We can do better What Does it Mean to Balance a BST? (cont’d) • Another Tentative Rule – Require that every node have left and right subtrees of the same height Too restrictive only perfectly balanced trees of 2k – 1 nodes would satisfy this criterion What Does it Mean to Balance BST? (cont’d) • The Rule – Require that, for every node, the height of the left and right subtrees can differ by at most one Balancing a BST • There are a number of techniques to properly balance a binary tree – Approach 1: take all the elements, place them in an array, sort them, and then reconstruct the tree (global) (example, and algorithm) – Approach 2: constantly restructuring the tree when new elements arrive or elements are deleted and lead to an unbalanced tree (i.e., selfbalancing trees) Approach 1: Reorder Data and Build BST • When all data arrive, store all data in an array, sort the array. What is the root of the tree? – the middle element of the array • Designate for the root of the BST the middle element of the array (i.e., the middle element of the array is the first element inserted into the BST) • Continue inserting recursively on the left and right subarrays until all elements in the array have been inserted into the BST • 1 2 3 4 5 6 7 (construct the tree) Approach 1: Reorder Data and Build BST (cont’d) • This approach has one serious drawback – All data must be put in an array before the BST can be created – Unsuitable or very inefficient when the BST has to be used while the data to be included in the BST are still coming Approach 2: Self balancing with AVL Tree • AVL trees offers another way to balance tree • AVL trees maintain balance of BSTs while they are being created via insertions of data • Tree rebalancing can be performed locally if only a portion of the tree is affected after insertion or deletion. AVL Trees • AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most by one – Each node contains a value (-1, 1, 0) indicating which subtree is "heavier” Balance Factor – Each node is marked with a balance factor indicating which subtree is "heavier” – Balance Factor: height (right subtree) minus height (left subtree) – A balanced tree 1 (-1, 1, 0) -1 0 1 0 1 0 AVL Implementation issues: – Insert and Delete are modified. They restructure the tree to make it balanced (if necessary) Fixing Imbalances An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1 There are two cases of imbalances: -2 1 -1 0 -2 2 or 2 -1 1 0 CASE 1 Imbalance caused by inserting node in left subtree of left child or right subtree of right child (i.e., insertion occurs on the “outside”) 0 or 0 CASE 2 Imbalance caused by inserting node in right subtree of left child or left subtree of right child (i.e., insertion occurs on the “inside”) Fixing Imbalances (cont’d) Fixing an imbalance is done by rotating the tree There are two types of rotation: single rotation for CASE 1 imbalances double rotation for CASE 2 imbalances consists of two single rotations The rotations must always preserve the BST property Fixing Imbalances: Case 1 node with imbalance 6 D 4 4 C 2 A B right rotate node 4 about 6 2 A 6 B C D This is a single right rotation. A single left rotation is symmetric. Fixing Imbalances: Case 1 (cont’d) left rotate node Q about P This is a single left rotation Fixing Imbalances: Case 2 node with imbalance node with imbalance 6 6 D 4 2 A 2 4 B C STEP 1: left rotate node 4 about 2 4 D A B C STEP2: right rotate A node 4 about 6 2 6 B C D Fixing Imbalances: Case 2 (cont’d) STEP 1: Right rotate R about Q STEP 2: Left rotate R about P Note that P can be part of a larger AVL tree; it can be a child of some other node in the tree