Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures – LECTURE Balanced trees

Similar presentations


Presentation on theme: "Data Structures – LECTURE Balanced trees"— Presentation transcript:

1 Data Structures – LECTURE Balanced trees
Motivation Types of balanced trees AVL trees Properties Balancing operations Data Structures, Spring 2006 © L. Joskowicz

2 Motivation Binary search trees are useful for efficiently implementing dynamic set operations: Search, Successor, Predecessor, Minimum, Maximum, Insert, Delete in O(h) time, where h is the height of the tree When the tree is balanced, that is, its height h = O(lg n), the operations are indeed efficient. However, the Insert and Delete alter the shape of the tree and can result in an unbalanced tree. In the worst case, h = O(n)  no better than a linked list! Data Structures, Spring 2006 © L. Joskowicz

3 Balanced trees Find a method for keeping the tree always balanced
When an Insert or Delete operation causes an imbalance, we want to correct this in at most O(lg n) time  no complexity overhead. Add a requirement on the height of subtrees The most popular balanced tree data structures: AVL trees: subtree height difference of at most 1 Red-Black trees: subtree height difference ≤ 2(lg n + 1) 2-3 B-trees: same subtree height, varying # of children Data Structures, Spring 2006 © L. Joskowicz

4 AVL: Adelson-Velsky and Landis, 1962
AVL trees An AVL tree is a binary tree with one balance property: For any node in the tree, the height difference between its left and right subtrees is at most one. Sh-1 Sh-2 h-2 h-1 h Sh x Sh = Sh–1 + Sh–2 + 1 Size of tree: AVL: Adelson-Velsky and Landis, 1962 Data Structures, Spring 2006 © L. Joskowicz

5 Examples 12 12 8 16 14 4 10 2 6 1 8 16 4 10 14 2 6 Not an AVL tree (nodes 8 and 12) An AVL tree Data Structures, Spring 2006 © L. Joskowicz

6 AVL tree properties What is the minimum and maximum height h of an AVL tree with n nodes? Study the recursion: T(n) = T(n-1) + T(n-2) + 1 Minimum height: fill all levels except the last one root Tright Tleft h-1 h = lg n or h-2 Data Structures, Spring 2006 © L. Joskowicz

7 Maximal height of an AVL tree
All levels have a difference of height of 1. The smallest AVL tree of depth 1 has 1 node. The smallest AVL tree of depth 2 has 2 nodes. In general, Sh = Sh-1+ Sh (S1 = 1; S2 = 2) Sh-1 Sh-2 h-2 h-1 h Sh x The proof shows that the number of nodes in exponential in the height, even for the minimal tree. Since exp and log are inverse functions, the height is logarithmic in the number of nodes, therefore the insert is O(log n) Data Structures, Spring 2006 © L. Joskowicz

8 Maximal AVL height: complexity
Study the Fibonacci series recursion T(n) = T(n-1) + T(n-2) (T1 = 0; T2 = 2) Special case of linear recurrence equation! 2T(n-2) ≤ T(n) ≤ 2T(n-1) 2└n/2┘ ≤ T(n) ≤ 2n-1 The solution is T(n) = cn where c is a constant (prove guess!) T(n) = T(n-1) + T(n-2) cn = cn-1 +cn-2 c2 = c  h = lg1.6n  O(lg n) golden ratio! S(n) = T(n) +1 same complexity Data Structures, Spring 2006 © L. Joskowicz

9 Balancing AVL trees Before the operation, the tree is balanced.
After an insertion or deletion operation, the tree might become unbalanced  fix subtrees that became unbalanced. The height of any subtree has changed by at most 1. Thus, if a node is not balanced, the difference between its children heights is 2. Four possible cases with a height difference of 2. Data Structures, Spring 2006 © L. Joskowicz

10 Cases of imbalance (1) k2 k1 B C A Case 1: The left subtree is higher than the right subtree because of the left subtree of the left child Case 4: The right subtree is higher than the left subtree because of the right subtree of the right child k1 k2 B A C Data Structures, Spring 2006 © L. Joskowicz

11 Cases of imbalance (2) Case 2: The left subtree is higher than the right subtree because of the right subtree of the left child k2 k1 C A B Case 3: The right subtree is higher than the left subtree because of the left subtree of the right child k1 k2 C A B Data Structures, Spring 2006 © L. Joskowicz

12 Fixing Case 1: right rotation
k2 k2 k1 A B C right rotation k1 C B A The rotation takes O(1) time and restores the balance Insertion – The height of subtree A is increased. After the rotation, the tree has the same height as before the insert. Deletion – The height of subtree C is decreased. After the rotation, the tree has a lower height by 1 from before Remark: One may ask, If after insert and the balance operation, the height of the tree does not change, when does the height actually increase ?? The answer: the height changes only when the tree has balance of 0 in all nodes on the path from the root to the newly added leaf (thus no re-balance is needed). So, when balance operation is performed, the height doesn’t change, and when the height changes, the balance remains (without a balance operation). Data Structures, Spring 2006 © L. Joskowicz

13 Case 1: Insertion example
12 12 8 16 14 10 4 6 2 1 k2 k1 C A B k2 8 16 k1 C 4 10 14 A 2 6 B 1 Insert 1 Data Structures, Spring 2006 © L. Joskowicz

14 Fixing Case 4: left rotation
k2 k1 k2 C B A k1 left rotation k1 k2 A B C Analysis as in Case 1 Data Structures, Spring 2006 © L. Joskowicz

15 Case 4: deletion example
k1 k2 delete 5 7 9 k1 k2 left rotation 5 7 9 5 k2 k1 4 7 9 Data Structures, Spring 2006 © L. Joskowicz

16 Fixing Case 2 – first try …
k2 k1 B A C k2 right rotation k1 C A B A single rotation is not enough A series of rotations on the subtree of k1 to reduce Case 2 to Case 1! Data Structures, Spring 2006 © L. Joskowicz

17 Fixing Case 2: right then left rotation
left rotation on k1 right rotation on k3 k3 k3 k1 A C k2 B1 B2 k3 k1 A C k2 B1 B2 k1 k2 C A B1 B2 Note that the height of B and C is not exactly known and that’s why they are located between the two dashed lines and not on one of them. In an insert operation, one of them is located on the lower line and one on the upper line, in the delete, at least one is located on the lower line (but it is possible that they both are). Data Structures, Spring 2006 © L. Joskowicz

18 Case 2: insertion example
12 6 16 14 8 4 5 2 10 k3 k1 C B1 k2 A 12 k1 12 8 16 14 10 6 4 5 k3 C A B1 k2 2 k3 8 16 k1 4 10 14 k2 C 2 6 A B1 5 no B2 left rotation on 4 right rotation on 8 Data Structures, Spring 2006 © L. Joskowicz

19 Fixing Case 3: right then left rotation
right rotation on k3 left rotation on k1 k1 k3 k1 A C k2 B1 B2 k3 k2 A C B1 B2 Analysis as in Case 2 Data Structures, Spring 2006 © L. Joskowicz

20 Insert and delete operations (1)
Insert/delete the element as in a regular binary search tree, and then re-balance. Observation: only nodes on the path from the root to the node that was changed may become unbalanced. Assume we go left: the right subtree was not changed, and stays balanced. This holds as we descend the tree. Data Structures, Spring 2006 © L. Joskowicz

21 Insert and delete operations (2)
After adding/deleting a leaf, go up, back to the root. Re-balance every node on the way as necessary. The path is O(lg n) long, and each node balance takes O(1), thus the total time for every operation is O(lg n). For the insertion we can do better: when going up, after the first balance, the subtree that was balanced has height as before, so all higher nodes are now balanced again. We can find this node in the pass down to the leaf, so one pass is enough. Data Structures, Spring 2006 © L. Joskowicz

22 Example: deletion requiring two passes
18 12 21 7 13 3 8 10 15 9 11 14 16 Deleting X upsets the balance at `A'. When we rotate (B up, A down) to fix this, the whole of this subtree - which is D's left subtree - gets shorter. This means the balance at `D' is upset. When we fix this with a rotation (G up, D down) This subtree (L's left subtree) gets shorter - the subtree heights at this stage are 4 on the left and 6 on the right. When we rotate to fix this (L down, M up), we will get a 5-5 balance at M - so now it is one shorter and its parent is out of balance. This can propagate right to the top of the tree 17 delete 3 Data Structures, Spring 2006 © L. Joskowicz

23 Summary Efficient dynamic operations on a binary tree require a balance tree whose height is O(lg n) Balanced height trees: AVL trees Red-black trees B-trees Insertion and deletion operations might require re-balancing in O(lg n) to restore balanced tree properties Re-balancing with left and/or right rotations on a case by case basis. Data Structures, Spring 2006 © L. Joskowicz


Download ppt "Data Structures – LECTURE Balanced trees"

Similar presentations


Ads by Google