Balanced Binary Trees 1 Binary searc trees provide O(log N) searc times provided tat te nodes are distributed in a reasonably balanced manner. Unfortunately, tat is not always te case and performing a sequence of deletions and insertions can often exacerbate te problem. Wen a BST becomes badly unbalanced, te searc beavior can degenerate to tat of a sorted linked list, O(N). Tere are a number of strategies for dealing wit tis problem; most involve adding some sort of restructuring to te insert/delete algoritms. Tat can be effective only if te restructuring reduces te average dept of a node from te root of te BST, and if te cost of te restructuring is, on average, O(log N). We will examine one suc restructuring algoritm
2 AVL tree*: a binary searc tree in wic te eigts of te left and rigt subtrees of te root differ by at most 1, and in wic te left and rigt subtrees are temselves AVL trees. Eac AVL tree node as an associated balance factor indicating te relative eigts of its subtrees (left-iger, equal, rigt-iger). Normally, tis adds one data element to eac tree node and an enumerated type is used. How effective is tis? Te eigt of an AVL tree wit N nodes never exceeds 1.44 log N and is typically muc closer to log N. *G. M. Adelson-Velskii and E. M. Landis, 192.
Examples Tis is an AVL tree... Notation: - means te subtrees of te node are te same eigt means tat te rigt subtree of te node is iger (by one) tan te left subtree (rigt-iger) / means tat te left subtree of te node is iger (by one) tan te rigt subtree (left-iger) / 3...and tis is not. / /
Unbalance from Insertion 4 Consider inserting te value 4 into te AVL tree: 0 2 0 30 8 Te result would be unbalanced at te node containing 2: 0 2 0 Te unbalance is repaired by applying one of two types of rotation to te unbalanced subtree 30 4 8
Rebalancing via Subtree Restructuring Te subtree rooted at 2 is rigt-iger. 0 2 0 We restructure te subtree, resulting in a balanced subtree: 30 4 8 0 2 30 4 0 Te transformation is relatively simple, requiring only a few operations, and results in a subtree tat as equal balance. 8
AVL Insertion Case: rigt-iger Tere are two unbalance cases to consider, eac defined by te state of te subtree tat just received a new node. For simplicity, assume for now tat te insertion was to te rigt subtree (of te subtree). Let be te root of te newly unbalanced subtree, and suppose tat its rigt subtree is now rigt-iger: rigt In tis case, te subtree rooted at rigt was previously equally balanced (wy?) and te subtree rooted at was previously rigtiger (wy?). T 1 Te eigt labels follow from tose observations. T 2 +1 T 3 Balance can be restored by rotating te values so tat rigt becomes te subtree root node and becomes te left cild.
AVL Left Rotation Te manipulation just described is known as a left rotation and te result is: rigt rigt T 1 T 2 +1 T 3 Overall eigt of eac subtree is now te same. T 1 T 2 T 3 Tat covers te case were te rigt subtree as become rigt-iger te case were te left subtree as become left-iger is analogous and solved by a rigt rotation.
Unbalance from Insertion 8 Consider inserting te value 1 into tis AVL tree: 0 2 0 20 8 Te result would be unbalanced at te node containing 2: Te unbalance is repaired by applying one of two types of rotation to te unbalanced subtree 20 2 0 0 1 8
Rebalancing via Subtree Restructuring 9 0 Te subtree rooted at 2 is left-iger. 2 0 20 1 8 We restructure te subtree, resulting in a balanced subtree: 0 20 Te transformation is again relatively simple, requiring only a few operations, and results in a subtree tat as equal balance. 1 2 0 8
AVL Rigt Rotation Te previous manipulation is known as a rigt rotation and te result is: 10 // left left / +1 T 3 T 2 T 1 Overall eigt of eac subtree is now te same. T 3 T 2 T 1 Tat covers te case were te left subtree as become left-iger.
AVL Insertion Case: left-iger Now suppose tat te rigt subtree as become left-iger: 11 T 1 rigt-left? / rigt Te insertion occurred in te left subtree of te rigt subtree of. In tis case, te left subtree of te rigt subtree (rooted at rigt-left) may be eiter left-iger or rigt-iger, but not balanced (wy?). -1 T 2 or T 3 T 4 Surprisingly (peraps), tis case is more difficult. Te unbalance cannot be removed by performing a single left or rigt rotation.
AVL Double Rotation Applying a single rigt rotation to te subtree rooted at rigt produces 12 / rigt rigt-left rigt-left?? rigt -1 T 4 T 2 T 2 or T 3 T 3 T 4 a subtree rooted at rigt-left tat is now rigt-iger
AVL Double Rotation 13 Now, applying a single left rotation to te subtree rooted at produces rigt-left? Balance factors ere depend on original balance factor of rigt-left. rigt? rigt-left? rigt T 2 T 3 T 4 T 1 T 2 T 3 T 4 a balanced subtree. -1 or Te case were te left subtree of is rigt-iger is andled similarly (by a double rotation: left: rigt).
Unbalance from Deletion 14 Deleting a node from an AVL tree can also create an imbalance tat must be corrected. Te effects of deletion are potentially more complex tan tose of insertion. Te basic idea remains te same: delete te node, track canges in balance factors as te recursion backs out, and apply rotations as needed to restore AVL balance at eac node along te pat followed down during te deletion. However, rebalancing after a deletion may require applying single or double rotations at more tan one point along tat pat. As usual, tere are cases Here, we will make te following assumptions: - te lowest imbalance occurs at te node root (a subtree root) - te deletion occurred in te left subtree of root
AVL Deletion Case: rigt-iger 1 Suppose we ave te subtree on te left prior to deletion and tat on te rigt after deletion: rigt rigt 1 1 2 3-1 2 3-1 rigt -1 Note: "rigt-iger" refers to te balance factor of te root of te rigt subtree (labeled rigt ere). 1 2 3-1 -1 Ten a single left rotation at will rebalance te subtree.
AVL Deletion Case: equal-eigt Suppose te rigt subtree root as balance factor equal-eigt: 1 rigt Again, a single left rotation at root will rebalance te subtree. / rigt 1 2 3 1-1 2 3 Te difference is te resulting balance factor at te old subtree root node,, wic depends upon te original balance factor of te node rigt.
AVL Deletion Case: left-iger If te rigt subtree root was left-iger, we ave te following situation: 1 1 rigt-left? 2 3-1 or -2? / rigt 4-1 Deleting a node from te left subtree of now will cause to become double rigt iger. As you sould expect, te resulting imbalance can be cured by first applying a rigt rotation at te node rigt, and ten applying a left rotation at te node. However, we must be careful because te balance factors will depend upon te original balance factor at te node labeled rigt-left
AVL Deletion Case: left-iger, left-iger If te rigt-left subtree root was also left-iger, we obtain: 18 rigt-left / rigt rigt 1 rigt-left / 4-1 2 3-1 -2-1 1-1 2-1 3-2 4-1
AVL Deletion Case: left-iger, rigt-iger If te rigt-left subtree root was rigt-iger, we obtain: 19 rigt-left / rigt / rigt 1 rigt-left 4-1 2 3-2 -1 1 2-2 3 4-1 -1-1 -1 And, finally, if te rigt-left subtree root was equal-eigt, we'd obtain a tree were all tree of te labeled nodes ave equal-eigt.
AVL Deletion Cases: Summary 20 We ave considered a number of distinct deletion cases, assuming tat te deletion occurred in te left subtree of te imbalanced node. Tere are an equal number of entirely similar, symmetric cases for te assumption te deletion was in te rigt subtree of te imbalanced node. Drawing diagrams elps Tis discussion also as some logical implications for ow insertion is andled in an AVL tree. Te determination of te balance factors in te tree, following te rotations, involves similar logic in bot cases.