210 likes | 220 Views
Learn about AVL Trees in this lecture addendum, focused on insertion methods and rotations to maintain balance. Includes examples and detailed explanations of insertion processes, single and double rotations, and non-recursive insertion techniques.
E N D
AVL Trees (a few more slides) CSE 373 Data Structures Lecture 8.5
Insertion in AVL Trees • Insert at the leaf (as for all BST) • only nodes on the path from insertion point to root node have possibly changed in height • So after the Insert, go back up to the root node by node, updating heights • If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node AVL Trees addendum - Lecture 8.5
Insert in BST Insert(T : reference tree pointer, x : element) : integer { if T = null then T := new tree; T.data := x; return 1;//the links to //children are null case T.data = x : return 0; //Duplicate do nothing T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x); endcase } AVL Trees addendum - Lecture 8.5
Insert in AVL trees Insert(T : reference tree pointer, x : element) : { if T = null then T := new tree; T.data := x; height := 0; case T.data = x : return ; //Duplicate do nothing T.data > x : return Insert(T.left, x); if ((height(T.left)- height(T.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : return Insert(T.right, x); code similar to the left case Endcase T.height := max(height(T.left),height(T.right)) +1; return; } AVL Trees addendum - Lecture 8.5
Example of Insertions in an AVL Tree 2 20 Insert 5, 40 0 1 10 30 0 0 35 25 AVL Trees addendum - Lecture 8.5
Example of Insertions in an AVL Tree 2 3 20 20 1 1 1 2 10 30 10 30 0 0 0 1 0 0 5 35 5 35 25 25 0 40 Now Insert 45 AVL Trees addendum - Lecture 8.5
Single rotation (outside case) 3 3 20 20 1 2 1 2 10 30 10 30 0 2 0 0 0 1 5 35 5 40 25 25 0 0 35 45 40 1 Imbalance 0 45 Now Insert 34 AVL Trees addendum - Lecture 8.5
Double rotation (inside case) 3 3 20 20 1 3 1 2 10 30 10 35 0 2 0 0 1 1 5 Imbalance 40 5 40 25 30 0 25 34 1 0 45 35 45 0 Insertion of 34 0 34 AVL Trees addendum - Lecture 8.5
AVL Insertion: Outside Case j Consider a valid AVL subtree h+2 k h h+1 Z h h X Y AVL Trees addendum - Lecture 8.5
AVL Insertion: Outside Case j Inserting into X destroys the AVL property at node j (h+2) - h k Becomes h + 2 h Z h+1 h Y X AVL Trees addendum - Lecture 8.5
Outside Case Completed “Right rotation” done! (“Left rotation” is mirror symmetric) k h+2 j h+1 h+1 h h X Z Y AVL property has been restored! AVL Trees addendum - Lecture 8.5
AVL Insertion: Inside Case j Consider a valid AVL subtree h+2 k h h+1 Z h h X Y AVL Trees addendum - Lecture 8.5
AVL Insertion: Inside Case j Inserting into Y destroys the AVL property at node j k Becomes h + 2 h Z h h+1 X Y AVL Trees addendum - Lecture 8.5
AVL Insertion: Inside Case j Consider the structure of subtree Y… k h Z h h+1 X Y AVL Trees addendum - Lecture 8.5
AVL Insertion: Inside Case j Y = node i and subtrees V and W h+2 k h Z i h h+1 X h or h-1 W V AVL Trees addendum - Lecture 8.5
AVL Insertion: Inside Case j We will do a left-right “double rotation” . . . k Z i X W V AVL Trees addendum - Lecture 8.5
Double rotation : second rotation double rotation complete Balance has been restored h+2 i h+1 j k h+1 h h h or h-1 V Z W X AVL Trees addendum - Lecture 8.5
Non-recursive insertion or the hacker’s delight • Key observations; • At most one rotation • Balance factor: 2 bits are sufficient (-1 left, 0 equal, +1 right) • There is one node on the path of insertion, say S, that is “critical”. It is the node where a rotation can occur and nodes above it won’t have their balance factors modified AVL Trees addendum - Lecture 8.5
Non-recursive insertion • Step 1 (Insert and find S): • Find the place of insertion and identify the last node S on the path whose BF ≠ 0 (if all BF on the path = 0, S is the root). • Insert • Step 2 (Adjust BF’s) • Restart from the child of S on the path of insertion. (note: all the nodes from that node on on the path of insertion have BF = 0.)If the path traversed was left (right) set BF to –1 (+1) and repeat until you reach a null link (at the place of insertion) AVL Trees addendum - Lecture 8.5
Non-recursive insertion (ct’d) • Step 3 (Balance if necessary): • If BF(S) = 0 (S was the root) set BF(S) to the direction of insertion (the tree has become higher) • If BF(S) = -1 (+1) and we traverse right (left) set BF(S) = 0 (the tree has become more balanced) • If BF(S) = -1 (+1) and we traverse left (right), the tree becomes unbalanced. Perform a single rotation or a double rotation depending on whether the path is left-left (right-right) or left-right (right-left) AVL Trees addendum - Lecture 8.5
Non-recursive Insertion with BF’s Step 1 & 2 Step 3 +1 +1 20 S 20 -1 +1 -1 0 10 30 10 35 0 0 ->1 0 0 0 1 5 40 5 40 25 30 0 25 34 0 0->-1 0 45 35 45 0 Insertion of 34 0 34 AVL Trees addendum - Lecture 8.5