Yufei Tao ITEE University of Queensland
We ave already learned a static version of te BST. In tis lecture, we will make te structure dynamic, namely, allowing it to support updates (i.e., insertions and deletions). Te dynamic version we will learn is called te AVL-tree.
Recall: Binary Searc Tree (BST) A BST on a set S of n integers is a binary tree T satisfying all te following requirements: T as n nodes. Eac node u in T stores a distinct integer in S, wic is called te key of u. For every internal u, it olds tat: Te key of u is larger tan all te keys in te left subtree of u. Te key of u is smaller tan all te keys in te rigt subtree of u.
Recall: Balanced Binary Tree A binary tree T is balanced if te following olds on every internal node u of T : Te eigt of te left subtree of u differs from tat of te rigt subtree of u by at most 1. If u violates te above requirement, we say tat u is imbalanced.
AVL-Tree An AVL-tree on a set S of n integers is a balanced binary searc tree T were te following olds on every internal node u u stores te eigts of its left and rigt subtrees.
Example An AVL-tree on S = {, 10,, 20, 0,, 60, 7, 80} 2 7 2 2 1 1 10 0 60 80 1 0 1 0 20 For example, te numbers and 2 near node indicate tat its left subtree as eigt, and its rigt subtree as eigt 2. By storing te subtree eigts, an internal node knows weter it as become imbalanced. Te left subtree eigt of an internal node can be obtained in O(1) time from its left cild (ow?). Similarly for te rigt.
Next, we will explain ow to perform updates. Te most crucial step is to remedy a node u wen it becomes imbalanced. It suffices to consider a scenario called 2-level imbalance. In tis situation, two conditions apply: Tere is a difference of 2 in te eigts of te left and rigt subtrees of u. All te proper descendants of u are balanced. Before delving into te insertion and deletion algoritms, we will first explain ow to rebalance u in te above situation.
2-Level Imbalance Tere are two cases: + 2 + 2 Due to symmetry, it suffices to explain only te left case, wic can be furter divided into a left-left and a left-rigt case, as sown next.
2-Level Imbalance + 1 or + 1 + 1 Left-left Left-Rigt
Rebalancing Left-Left By a rotation: a + 2 + 1 b x + 1 + 1 b x C A x a + 1 A B B C Only pointers to cange (te red ones). Te cost is O(1). Recall x = or + 1.
Rebalancing Left-Rigt By a double rotation: + 2 a + 2 a + 1 c + 1 b + 1 b + 1 c x y D b x y a + 1 A A B C D B C Only 5 pointers to cange (see above). Hence, te cost is O(1). Note tat x and y must be or 1. Furtermore, at least one of tem must be (wy?).
We are now to explain te insertion algoritm
Insertion Suppose tat we need to insert a new integer e. First create a new leaf z storing te key e. Tis can be done by descending a root-to-leaf pat: 1 Set u te root of T 2 If u is a leaf node: 2.1 If te key of u > e, make z te left cild of u, and done. 2.2 Oterwise, make z te rigt cild of u, and done. Oterwise (i.e., u is an internal node): 2.1 If te key of u > e, set u to te left cild. 2.2 Oterwise, set u to te rigt cild. 4 Repeat from Line 2. Finally, update te subtree eigt values on te nodes of te root-to-z pat in te bottom-up order. Te total cost is proportional to te eigt of T, i.e., O(log n).
Example Inserting 5: 7 2 2 10 0 60 80 1 1 20 5 Te red eigt values are modified by tis insertion.
Example An insertion may cause te tree to become imbalanced! 7 7 10 0 60 80 10 0 60 80 20 5 20 5 1 6 In te left tree, nodes 10 and become imbalanced, wereas in te rigt, node is now imbalanced.
Imbalance in an Insertion... Only te nodes along te pat from te insertion pat (from te root to te newly added leaf) can become imbalanced. It suffices remedy only te lowest imbalanced node.
Left-Left Example a + 2 + 1 b x + 1 b + 1 x C A x a + 1 A B B C 7 7 10 2 0 0 60 80 0 1 1 60 80 20 1 0 5 1 10 20 5 1
Left-Rigt Example a + 2 a + 2 + 1 c + 1 b + 1 b + 1 c x y D b x y a + 1 A A B C D B C 4 2 0 2 7 2 1 2 2 10 0 1 2 60 80 10 20 5 7 20 5 6 60 80 6
Insertion Time... It will be left as an exercise for you to prove: Only 2-level imbalance can occur in an insertion. Once we ave remedied te lowest imbalanced node, all te nodes in te tree will become balanced again. Te total insertion time is terefore O(log n).
We now proceed to explain te deletion algoritm.
Deletion Suppose tat we want to delete an integer e. First, find te node u wose key equals e in O(log n) time (troug a predecessor query). Case 1: If u is a leaf node, simply remove it from (te AVL-tree) T. Case 1 Example Remove 60: 0 10 20 5 7 6 60
Deletion Now suppose tat node u (containing te integer e to be deleted) is not a leaf node. We proceed as follows: Case 2: If u as a rigt subtree: Find te node v storing te successor s of e. Set te key of u to s Case 2.1: If v is a leaf node, ten remove it from T. Case 2.2: Oterwise, it must old tat v as a rigt cild w, wic is a leaf (wy?), but not left cild. Set te key of v to tat of w, and remove w from T. Case : If u as no rigt subtree: It must old tat u as a left cild v, wic is a leaf. (wy?) Set te key of u to tat of v, and remove v from T.
Case 2.1 Example Delete : 0 0 60 10 20 5 7 10 20 5 7 6 60 6
Case 2.2 Example Delete 0: 0 5 10 20 5 7 10 20 6 7 6 60 60
Case Example 0 0 10 20 5 7 10 20 5 60 6 60 6
Deletion... In all te above cases, we ave essentially descended a root-to-leaf pat (call it te deletion pat), and removed a leaf node. We can now update te subtree eigt values for te nodes on tis pat in te bottom-up order. Te cost so far is O(log n). Recall tat te successor of an integer can be found in O(log n) time.
Imbalance in a Deletion... Only te nodes along te deletion pat may become imbalanced. We fix eac of tem in te bottom-up order in exactly te same way as described for insertions, namely, using eiter a rotation or double rotation. Unlike an insertion, we may need to remedy more tan one imbalanced node.
Left-Left Example 1 Delete 0: 0 5 10 20 5 10 20 Node 5 becomes imbalanced a left-left case andle by a rotation: 5 10 20 10 5 20
Left-Left Example 2 Delete 0: 0 5 10 20 5 10 20 25 25 Node 5 becomes imbalanced also a left-left case andle by a rotation: 5 10 5 10 20 20 25 25
Left-Rigt Example Delete : 0 0 7 10 20 5 7 10 20 5 17 25 6 17 25 6 27 27 Node 7 becomes imbalanced a left-rigt case andled by a double rotation. See te next slide.
Left-Rigt Example 0 0 7 6 10 20 5 10 20 5 7 17 25 6 17 25 27 27 Node 0 is still imbalanced a left-rigt case andled by a double rotation. See next.
Left-Rigt Example 0 20 6 0 10 20 5 7 10 17 25 6 17 25 27 5 7 27 Final tree after te deletion. Note tat tis deletion required fixing 2 imbalanced nodes.
Deletion Time... It will be left as an exercise for you to prove tat Only 2-level imbalance can occur in an insertion. Since we spend O(1) time fixing eac imbalanced nodes, te total deletion time is O(log n).
We now conclude our discussion on te AVL-tree, wic provides te following guarantees: O(n) space consumption. O(log n) time per predecessor query (ence, also per dictionary lookup). O(log n) time per insertion O(log n) time per deletion. All te above complexities old in te worst case.