Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University.

Similar presentations


Presentation on theme: "CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University."— Presentation transcript:

1 CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University

2 Outline Balanced Binary Trees (AVL Trees) –Section 4.4.

3 Review: Left-Left (LL) Unbalance at A A B AR Before Insertion A B After Insertion, Before Rotation AR h BRBL BF=0 BF=1 hh BF=2 BF=1 BL BR h h+1 AR h A node is inserted into the left child of the left child of A – hence the name left-left. h+2

4 Review: Right-Right (RR) Unbalance at A A B BRBL AL hh BF=0 h A AL h B BL h BR h+1 Before Insertion After Insertion BF=-1 BF=-2 BF=-1 A node is inserted into the right child of the right child of A – hence right-right.

5 LL Fix: Single Right Rotation at A A B After Insertion, Before Rotation h BF=2 BF=1 BR h h+1 AR B A BR BL h+1h h BF=0 After Insertion and Rotation h+2

6 RR Fix : Single Left Rotation at A After Insertion, Before Rotation A BF= -2 AL h B BF=-1 BL h BR h+1 B BF=0 A ALBL hh BR h+1 BF=0 After Insertion and Rotation

7 Left-Right Unbalance (LR) h Before Insertion After Insertion AR A BF=1 BF=0 B BLBR hh A BF=2 BF= -1 B BL h CL C CR BF=X AR h A node is inserted into the right child of the left child of A – hence left-right.

8 LR Fix: Double LR Rotation at A After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF=X AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 BF=? h+1 Double LR Rotation consists of a single left rotation at B and a single right rotation at A.

9 LR Fix: Case 1 (C’s BF = 0) After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF=0 AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 h+1 hh If C’s BF=0, CL and CR are of the same height h. So, B’s BF = A’s BF = 0.

10 LR Fix: Case 2 (C’s BF = 1) After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF=1 AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 BF= -1 h+1 h-1 h If C’s BF=1, CL’s height is h and CR’s height is h-1. So, B’s BF = 0, A’s BF = -1.

11 LR Fix: Case 3 (C’s BF = -1) After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF= -1 AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 BF=1BF= 0 h+1 h h-1 h If C’s BF = -1, CL’s height is h-1 and CR’s height is h. So, B’s BF = 1, A’s BF = 0.

12 Double LR Rotation: Height Preservation The height of the tree rooted in A before the insertion is h+2. The height of the tree rooted in C after the insertion and double LR rotation is also h+2. So, no further rotations are needed up the insertion path.

13 Right Unbalance (RL) and Double RL Rotation RL Unbalance is symmetric to LR unbalance. Double RL rotation is symmetric to double LR rotation: whenever we rotate left in LR, we rotate right in RL; whenever we have to rotate right in LR, we have to rotate left in RL. Like the double LR rotation, the double RL rotation preserves the height.

14 Right-Left (RL) Unbalance at A A B BRBL AL hh BF=0 h A AL h B BL h h+1 Before Insertion After Insertion BF=-1 BF=-2 BF=1 BR h

15 RL Fix: Double RL Rotation at A A AL h B h+1 After Insertion, Before Rotation BF=-2 BF=1 BR h C CLCR BF=X C B BRCR A ALCL After Insertion and Rotation h h BF=0 BF=?

16 RL Fix: Computing BF’s The computation of BF’s in the RL rotation follows the same three cases as with the LR rotation summarized in the table below. Before RotationAfter Rotation C’s BF = 0; CL’s height = h; CR’s height = h. A’s BF = 0; B’s BF = 0. C’s BF = 1; CL’s height = h; CR’s height = h-1. A’s BF = 0; B’s BF = -1. C’s BF = -1, CL’s height = h-1; CR’s height = h. A’s BF = 1; B’s BF = 0.

17 AVL Search Tree: Implementation The AVL Tree Node data type should have the following data members: –key –data –parent –left child –right child –balance factor –height

18 AVL Search Tree: Implementation The insertion algorithm should insert the node into the tree in the same way as was done with the binary search tree. The balance factor and the height of the newly inserted node are set to 0. After the insertion is done, the algorithm starts climbing up the tree from the parent of the newly inserted node.

19 Fixing the AVL Search Tree After Insertion At each node up the insertion path beginning from the parent of the newly inserted node, execute the following steps: –Update the node’s height; –Update the node’s balance factor; –If the node’s balance factor is 2 or -2, determine which unbalance it is (LL, LR, RR, RL), execute the appropriate rotation and return; –If the node’s balance factor is 1, 0, or -1, get its parent and repeat the above steps; The algorithm terminates either after a rotation is executed or the null node is reached (the null node is the root’s parent).

20 Computing the Heights of the AVL Tree Nodes The height of the AVL tree node can be computed in constant time by looking up the heights of its children. The balance factor can also be computed in constant time as the difference b/w the left height and the right height.


Download ppt "CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University."

Similar presentations


Ads by Google