Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

3 AVL Search Tree: Insert Inserting into an AVL search tree is the same as inserting into a binary search tree. Caveat: An AVL search tree may become unbalanced after an insertion and must be rebalanced to preserve the AVL search tree property.

4 Insertion: Example 29 4 31 34 39 BF = 0 BF = -1 BF = 1 BF = 0

5 Insertion: Example 29 4 31 34 39 BF = 0 BF = -2 BF = 2 BF = 1 BF = 0 31 is inserted to the left of 34.

6 Observations on Unbalanced AVL Search Trees Let T be an AVL search tree. –If T is unbalanced, the balance factors are: - 2, -1, 0, 1, and 2. –If a node has a BF = 2 after insertion, it must have had a BF = 1 before insertion. –If a node has a BF = -2 after insertion, it must have had a BF = -1 before Insertion. –The good news is that only the BFs of the nodes on the path from the root to the inserted node (leaf) can change.

7 Balancing Unbalanced Nodes A unbalanced node has its BF equal to -2 or 2, but not both, of course. Two types of unbalances: Left and Right. The left unbalances are divided into Left- Left (LL) and Left-Right (LR). The right unbalances are divided into Right-Right (RR) and Right-Left (RL). The terminology will become clear in the next couple of slides.

8 Left Unbalance (LL) at A A B AR Before Insertion A B After Insertion 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

9 Fixing LL Unbalance: 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+1 hh BF=0 After Insertion and Rotation h+2

10 Fixing LL Unbalance: Height Preservation The height of the tree, rooted in A, before the insertion is h+2. The height of the tree, rooted in B after the insertion and the single right rotation, is the same, i.e. h+2. This ensures that we do not have to climb up the tree and restore the unbalances higher up the insertion path. Also, keep in mind that the height of BR may be negative one (-1), i.e., BR may be empty.

11 Right Unbalance (RR) 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.

12 Fixing RR Unbalance: 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

13 Single Left Rotation: Height Preservation The Single Left Rotation, since it is symmetrical to the single right rotation, also preserves the height. The height of the tree, rooted in A, before the insertion is that same as the height of tree rooted in B after the insertion and the rotation.

14 Left 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.

15 Fixing LR Unbalance: 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.


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

Similar presentations


Ads by Google