Dnamic set DT dnamic set DT is a structure tat stores a set of elements. Eac element as a (unique) ke and satellite data. Te structure supports te following operations. Searc(S, k) Return te element wose ke is k. Insert(S, ) dd to S. Delete(S, ) Remove from S (te operation receives a pointer to ). Minimum(S) Return te element in S wit smallest ke. Maimum(S) Return te element in S wit largest ke. Successor(S, ) Return te element in S wit smallest ke tat is larger tan.ke. Predecessor(S, ) Return te element in S wit largest ke tat is smaller tan.ke.
Motivation In a binar searc tree, all operation take Θ() time in te worst case, were is te eigt of te tree. Te optimal eigt of a binar searc tree is log n. Even if we start wit a balanced tree, insertion/deletion operations can make te tree unbalanced. n VL tree is a special kind of a binar searc tree, wic is alwas kept banalced.
VL tree n VL tree is a binar searc tree suc tat for ever node, eigt(.left) eigt(.rigt) 1 (we assume tat eigt(null) = 1) 1 1 4 8 1 1 16 14 6 are named after teir inventors, Georg delson-velsk and Evgenii Landis.
Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1.
Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1. n 1 =.
Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1. n 1 =. n = + 1 + 1 = 4. 1
Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1. n 1 =. n = + 1 + 1 = 4. n = 4 + + 1 = 7. n k = n k 1 + n k + 1. 1
Te eigt of an VL tree n k = F k+ 1 were F k is te k-t Fibonacci number. Te proof is b induction: ase: n = 1 = F 1, n 1 = = F 4 1. n k = n k 1 +n k +1 = (F k+ 1)+(F k+1 1)+1 = F k+ 1 It is known tat ( F k = 1+ 5 ) k ( 5 1 ) k 5 ( 1+ 5 ) k 1 5 Terefore, ( 1+ ) k+ ( 5 1 1+ ) k+ 5 n k 1 1.45 5 5 ( ) k log (1+ 5)/ 5(nk + 1.45) 1.441 log n k. Te eigt of an VL tree wit n nodes is 1.441 log n.
Implementation node in an VL tree as te same fields defined for binar searc tree (ke, left, rigt, p). dditionall, ever node as a field eigt wic stores te eigt of te node.
Searcing Te Searc operation is andled eactl like in regular binar searc trees. Time compleit: Θ() = Θ(log n).
Insertion/Deletion Insertion and deletion are done b first appling te insertion/deletion algoritm of binar searc trees. fter te insertion/deletion, te tree ma not be balanced, so we need to correct it. Te time compleit of insertion/deletion in VL tree is Θ(log n).
Insertion fter a new leaf is inserted, te eigt of some of its ancestors increase b 1. Te eigts of te oter nodes are uncanged. 4 1 4 1 1 1 4 8 6 1 1 16 14 1 1 4 8 6 15 1 14 17 1 16 19 18
Insertion Te eigt fields of te nodes can be updated b going up te tree from te inserted leaf, and for eac ancestor v of te leaf perform v.eigt = 1 + ma(v.left.eigt, v.rigt.eigt) 4 1 4 1 1 1 4 8 6 1 1 14 16 1 1 4 8 6 1 1 14 15 17 16 19 18
Insertion Some of te ancestors of te leaf ma become unbalanced. 4 1 4 1 1 1 4 8 6 1 1 16 14 1 1 4 8 6 15 1 14 17 1 16 19 18
Let be te lowest node on te pat from te new leaf to te root wic is unbalanced (if doesn t eist we are done). Tere are 4 cases. In Case 1 suppose tat te new leaf is in te subtree of.left.left. Let =.left. Let be te eigt of te rigt cild of. Since was balanced before te insertion, te eigt of before te insertion was 1// + 1. -1//+1 C New leaf
Te insertion eiter increases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of is + 1 before te insertion, and + afterward. Te eigt of before te insertion is +, and + afterward. + +1 C New leaf
Te insertion eiter increases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of is + 1 before te insertion, and + afterward. Te eigt of before te insertion is +, and + afterward. + + + +1 C New leaf
fter te insertion, as a cild wit eigt + 1. Tis cild must be te left cild. Te eigt of before te insertion is. Te eigt of te rigt cild of is. + + + +1 C +1 New leaf
To fi te imbalance of, perform te following operation called rigt rotation. Te rotation operation doesn t cange te inorder of te nodes. Terefore, te new tree is a valid binar searc tree. + + + +1 C +1 +1 C
Te eigts of and after te rotations are + 1 and +. fter te rotation, and are balanced. + + + + +1 C +1 +1 +1 C
ssume ad a parent te before te rotation, and denote it b u. Let w be te sibling of before te rotation. Te eigt of w is + 1/ + / +. If te eigt of w is + 1, ten u is unbalanced after te insertion (before te rotation). w +1/+/+ + +1 u +4 +/+/+4 + + C w u +1/+/+ +1 +/+/+4 + +1 +1 C
fter te rotation, te eigt te sibling of w (node ) is +, wic is equal to te eigt of te sibling of w before te rotation. Terefore, u is balanced. Te eigt of u after te rotation is te same as te eigt before te insertion. Repeating tese arguments, ever ancestor of u is balanced and as same eigt as before te insertion. w +1/+/+ + +1 u +4 +/+/+4 + + C w u +1/+/+ +1 +/+/+4 + +1 +1 C
Eample 1 1 8 16 4 16 4 1 14 8 14 6 6 1 In tis eample, =.
In Case, suppose tat te new leaf is in te subtree of.left.rigt. Let =.left. Performing a rotation on and does not work. + + + + +1 C + +1 +1 C
Let z =.rigt. Perform a double rotation on,, z. Te double rotation doesn t cange te inorder of te nodes. fter te double rotation,,, z are balanced. Moreover, te eigt of z is te same as te eigt of before te insertion, and terefore all ancestors of z are balanced. + + z + + +1 z D +1 +1 +1-1 C D C -1-1
Case is wen te new leaf is in te subtree of.rigt.rigt, and Case 4 is wen te new leaf is in te subtree of.rigt.left. Case and Case 4 are smmetric to Case 1 and Case. Case is sown below. + + + + +1 +1 C +1 C +1
Deletion fter a node is deleted, te eigts of some of its ancestors decrease b 1. Te eigts of te oter nodes are uncanged. single ancestor of te deleted node can become unbalanced. 5 1 1 4 8 11 1 6 1 9 1 7
Let be te unbalanced node (if doesn t eist we are done). ssume tat te deleted node is in te subtree of.rigt. Let =.left. Since was balanced before te deletion, te eigt of.rigt before te deletion was 1// + 1. -1//+1 C
Te deletion eiter decreases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of.rigt is 1 before te insertion, and afterward. Te eigt of is + 1 (te insertion doesn t cange te eigt). C - -1
Te deletion eiter decreases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of.rigt is 1 before te insertion, and afterward. Te eigt of is + 1 (te insertion doesn t cange te eigt). +1 C - -1
Since as eigt and it is balanced, one of s cildren as eigt 1 and te oter cild as eigt 1/. In Case 1, assume tat te eigt of.left is 1, and te eigt of.rigt is 1/. +1 C - -1-1 -1/-
In Case 1 we perform a rigt rotation on and. fter te rotation, te eigt of is / 1 and te eigt of is + 1/. fter te rotation, and are balanced. +1 +1/ C - -1-1 /-1-1 -1/- -1/- C -
ssume ad a parent te before te rotation, and denote it b u. Let w be te sibling of before te rotation. Te eigt of w is / + 1/ +. u is balanced after te insertion (before te rotation). u +/+/+ u w /+1/+ +1 C - -1 w /+1/+ -1 +1/ /-1-1 -1/- -1/- C -
fter te rotation, u can become unbalanced. Tis occurs wen te eigt of w is +, and te eigt of after te rotation is. If te eigt of w is, and te eigt after te rotation is, ten te eigt of u is + and + 1 afterwards. Tis can cause an imbalance in an ancestor of u. In te two cases above, additional rotation is needed. u +/+/+ u w /+1/+ +1 C - -1 w /+1/+ -1 +1/ /-1-1 -1/- -1/- C -
In Case, assume tat te eigt of.left is, and te eigt of.rigt is 1. Let z =.rigt. In tis case we perform a double rotation of,, z. +1 z - z D -1 - -1 - -1-1 -/- -/- C D - -/- C -/-