210 likes | 413 Views
AVL TREES. COP 3502. AVL Trees. We know that the search time for a node in a balanced binary search tree is O(log n) We’re dividing our search space in half each time we search the left or right branch. 5. 8. 2. 1. 4. 7. 3. AVL Trees.
E N D
AVL TREES COP 3502
AVL Trees • We know that the search time for a node in a balanced binary search tree is O(log n) • We’re dividing our search space in half each time we search the left or right branch. 5 8 2 1 4 7 3
AVL Trees • But if trees get out of balance, or have deep search paths • Their search performance deteriorates • In the worst case instead of having an O(log n) search time • The search time is O(n)
AVL Trees • So what we want is a tree that stays relatively balanced so that we can maintain the O(log n) search time, • BUT doesn’t require too much work in maintaining the balance so that we can still have O(log n) insertion time. • 2 Russian mathematicians, Adelson-Velski and Landis, created this type of almost balanced trees – • known as AVL trees
AVL Trees • height of a binary tree: • the length of the longest path from the root to a leaf. • (the height of an empty tree is -1) • (the height of a leaf is 0) • The AVL tree property is that for any node N, the height of N’s left and right subtrees must be equal or differ by 1. Height = 2 Height = 3 5 5 H = 2 H = 1 8 8 2 2 H = -1 H = 0 1 1 4 7 3
AVL Trees • height of a binary tree: • the length of the longest path from the root to a leaf. • (the height of an empty tree is -1) • (the height of a leaf is 0) • The AVL tree property is that for any node N, the height of N’s left and right subtrees must be equal or differ by 1. • The Balance Factor is the difference in heights of the left and right subtrees at any node. 5 BF = 1 5 BF= 2 8 2 H = 1 H = 0 H = 1 8 2 H = -1 4 7 1 3
Non-AVL Trees C 30 B 30 15 50 A 27 50 4 27 15 C 20 4 20 A B
AVL Trees • Now that we know what an AVL tree is, • now the question is how do we maintain this AVL tree property when new nodes are inserted or deleted? • When an imbalance is introduced to a tree, it is localized to 3 nodes and their 4 subtrees. • Denote the 3 nodes as A, B, C in their inorder listing. • Here are the 4 possibilites of the imbalances that could occur: C C A A B A B C A B C B
AVL Trees • All 4 imbalance cases can be solved by converting to the following tree: C C A A B A B C A B C B B A C
Case 1: Case 2: Case 3: Case 4: Single Rotation Double Rotation Double Rotation Single Rotation C A A C A C B B B B C A C A B B B B A C A C A C B B A C A C
AVL Tree Insert • So now the question is, how can we use these rotations to actually perform an insert on an AVL tree? • Here are basic steps: • Do a normal binary search tree insert • Restore the tree based on this new leaf node, steps for restoration: • Calculate the heights of the left and right subtrees, use this to set the potentially new height of the node. • If they are within one of each other, recursively restore the parent node. • If not, then perform the appropriate rotations on that particular node, THEN recursively restore the heights of the parent node. • Note: No recursive call is made if the node in question is the root node and has no parents. • Note: one rebalancing will always do the trick, though we must make the recursive calls to move up the tree so that the heights stored at each node are properly recalculated.
AVL Tree Insert Examples • The most simple insert into an AVL Tree that causes a rebalance is inserting a 3rd node into an AVL tree that creates a tree of height 2. • In this example, consider inserting the value 5: 9 C H = 1 7 H = -1 7 B H = 0 5 9 H = -1 5 A
AVL Tree Insert Examples • In this example, consider inserting the value 20: • In this situation, the nodes 27 and 15 are balanced and we don’t discover an imbalance until we trace up to 30. At this point, we label the nodes A, B and C based on our trace up the tree. The three values we passed were 27, 15 and 30, respectively. Thus, our labels are A = 15, B = 27, and C = 30. 30 C 30 27 B C H = 2 H = 0 15 50 A 27 50 15 30 B A C H = 0 H = 1 4 27 B A 15 4 20 50 H = -1 H = 0 20 4 20
AVL Tree Insert Examples • In this example, consider inserting the value 46: 32 A H = 1 H = 3 16 48 C 8 24 40 B 56 36 44 52 60 46
AVL Tree Insert Examples • In this example, consider inserting the value 46: 32 A 16 40 B 8 24 36 48 C 56 44 46 52 60
AVL Tree Insert Examples • In this example, consider inserting the value 46: 40 B 32 A 48 C 36 56 16 44 46 52 60 8 24
AVL Tree Insert Examples • In this example, consider inserting the value 61: 32 16 48 BF = 2 8 24 40 56 A H = 2 H = 0 4 36 44 52 60 B 58 62 C 61
AVL Tree Insert Examples • In this example, consider inserting the value 61: 32 16 48 60 8 24 40 B 4 36 44 A 56 62 C 52 58 61
AVL Tree example • Show the resulting tree after inserting 15 into the tree below: 30 36 20 43 31 10 25 4 18 29 15
AVL Tree example - ANSWER • Show the resulting tree after inserting 15 into the tree below: Imbalanced, Balance Factor = 2 30 C H = 1 H = 3 36 20 B 43 31 A 10 25 4 18 29 15
AVL Tree example - ANSWER • Show the resulting tree after inserting 15 into the tree below: 20 B A 10 30 C 4 18 25 36 15 29 31 43