CS350: Data Structures AVL Trees YORK COLLEGE OF PENNSYLVANIA YORK COLLEGE OF PENNSYLVANIA 'GHI

!"#$%&'())*+,-.)/.&0123454670!"#$%&'())*+,-.)/.&0123454670 YORK COLLEGE OF PENNSYLVANIA 'GHI&1015?26511?? @A9/**/")*&&1015?2D50633@A9/**/")*&&1015?2D50633'GONJHK&P@JJHG York College of Pennsylvania 05?3352775?30? 05?3352775?30? MFIF&'&1015?2D50633 05?3352775?30? EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

CS350: Data Structures © James Moscola COLLEGE CATALOG 2009–2011 COLLEGE CATALOG 2009–2011 COLLEGE CATALOG 2009–2011

College Catalog 2009–2011College Catalog 2009–2011 College Catalog 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56 Balanced Search Trees

• Binary search trees are not guaranteed to be balanced given random insertions and deletions - Inserting a sorted lists of elements into a BST produces the worst case -- O(N) - Performance of an unbalanced can degrade as more elements are inserted

• Balanced operations, such as insert, insure that the a tree always remains balanced - An operation is not complete until it returns the tree to a balanced state

Balanced Unbalanced tree tree

CS350: Data Structures 2 AVL Trees

• A type of balanced

• Named for its discoverers -- Adelson, Velskii, and Landis

• Definition:

An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.

CS350: Data Structures 3 AVL Trees

3 12 The height of each node is listed in blue

2 8 1 16

1 4 10 0 0 14 -1

0 2 6 0

An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.

CS350: Data Structures 4 AVL Trees

3 12 The height of each node is listed in blue

2 8 1 16 height differs by at most 1

1 4 10 0 0 14 -1

0 2 6 0

An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.

CS350: Data Structures 5 AVL Trees

3 12 The height of each node is listed in blue

2 8 1 16 height differs by at most 1

1 4 10 0 0 14 -1

0 2 6 0

An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.

CS350: Data Structures 6 AVL Trees

The after inserting node 1 ? 12 What are the node heights? ? 8 1 16

? 4 10 0 0 14 -1

? 2 6 0

? 1

CS350: Data Structures 7 AVL Trees

The binary tree after inserting node 1 4 12 What are the node heights? 3 8 1 16

2 4 10 0 0 14 -1

1 2 6 0

0 1

CS350: Data Structures 8 AVL Trees

4 12

3 8 1 16 height differs by more than 1 therefore, node 8 is invalid 2 4 10 0 0 14 -1

1 2 6 0

0 1

CS350: Data Structures 9 AVL Trees

4 12

height differs by 3 8 1 16 more than 1 height differs by therefore, node 12 more than 1 is unbalanced therefore, node 8 is unbalanced 2 4 10 0 0 14 -1

1 2 6 0

0 1

CS350: Data Structures 10 AVL Trees

• When a node in the tree no longer satisfies the invariant required to be an AVL tree the tree must be rebalanced around that node

• There are four ways in which an insertion into a tree may cause an imbalance to occur at some node X: (1) An insertion in the left subtree of the left child of X (2) An insertion in the right subtree of the left child of X symmetric (3) An insertion in the left subtree of the right child of X (4) An insertion in the right subtree of the right child of X

CS350: Data Structures 11 Creating an Imbalance -- Case #1

(1) An insertion in the left subtree of the left child of X

N2 1 N2 2

N1 0 -1 N1 1 -1

-1 -1 C 0 -1 C

A B B A root of A subtree Node N1 and root of C differ increases height by more than 1 therefore N2 as does N1 and N2 violates AVL rules

CS350: Data Structures 12 Fixing the Imbalance -- Case #1

Perform a single right rotation N1 becomes new root node

N2 now right N2 2 1 N1 child of N1

0 N1 1 -1 0 N2

0 -1 C -1 -1 A B B C A

B subtree now left child of N2

CS350: Data Structures 13 Creating an Imbalance -- Case #4 (symmetric with 1)

(4) An insertion in the right subtree of the right child of X

N2 N12

N1 N1

A A

root of C subtree B C increases height B as does N1 and N2 C Node N1 and root of A differ by more than 1 therefore N2 violates AVL rules

CS350: Data Structures 14 Fixing the Imbalance -- Case #4 (symmetric with 1)

Perform a single left rotation N1 becomes new root node

N2 now left N2 N1 child of N1

N1 N2

A C B A B C

B subtree now right child of N2

CS350: Data Structures 15 Creating an Imbalance -- Case #2

(2) An insertion in the right subtree of the left child of X

N3 1 N3 2

N1 0 -1 N1 1 -1

-1 -1 C -1 0 C

A B A B Node N1 and root of C differ root of B subtree by more than 1 therefore N3 increases height violates AVL rules as does N1 and N3

CS350: Data Structures 16 Fixing the Imbalance -- Case #2

We know the node N2 exists because a node was just inserted into the B subtree

N3 2 N3 2

N1 1 -1 N1 1 -1

-1 0 C -1 0 N2 C

A A -1 -1

B

B1 B2

CS350: Data Structures 17 Fixing the Imbalance -- Case #2

Perform a left-right double rotation Step #1 - Perform a single left rotation tree is still N3 2 between N1 and N2 N3 2 unbalanced

N1 1 -1 N2 1 -1

-1 0 N2 C 0 N1 -1 C

A -1 -1 -1 -1 B2

B1 B2 A B1

CS350: Data Structures 18 Fixing the Imbalance -- Case #2

Perform a left-right double rotation Step #2 - Perform a single right rotation N3 2 between N2 and N3 N2 1

N2 1 -1 N1 0 0 N3

0 N1 -1 C -1 -1 -1 -1

-1 -1 B2 A B1 B2 C

A B1

CS350: Data Structures 19 Fixing the Imbalance -- Case #2

Another look at the left-right double rotation without the intermediate steps

N3 N2

N1 N1 N3

N2 C

A A B1 B2 C

B1 B2

CS350: Data Structures 20 Fixing the Imbalance -- Case #3 (symmetric with 2)

Here is a look at the right-left double rotation without the intermediate steps

N3 N2

N1 N3 N1

A N2

C A B1 B2 C

B1 B2

CS350: Data Structures 21 AVL Tree Insertion Example

3 12

2 8 1 16

1 4 10 0 0 14 -1

0 2 6 0

CS350: Data Structures 22 AVL Tree Insertion Example

• Inserting node 1 ? 12 - First, recursively search for the location which to insert the node - Next, insert the node ? 8 1 16 - Finally, unwind the recursion update node heights along the way. Rebalance tree where ? 4 10 0 0 14 -1 necessary • To update node’s height, check its children ? 2 6 0

? 1

CS350: Data Structures 23 AVL Tree Insertion Example

? 12

? 8 1 16

? 4 10 0 0 14 -1

? 2 6 0 node 1 is a leaf node so its height is 0

0 1

CS350: Data Structures 24 AVL Tree Insertion Example

? 12

? 8 1 16

node 2 - add 1 to the height ? 4 10 0 0 14 -1 of its highest child

1 2 6 0

0 1

CS350: Data Structures 25 AVL Tree Insertion Example

? 12

node 4 - add 1 to the height ? 8 1 16 of its highest child

2 4 10 0 0 14 -1

1 2 6 0

0 1

CS350: Data Structures 26 AVL Tree Insertion Example

node 8 violates AVL ? 12 properties, so rebalance

? 8 1 16 height differs by more than 1 2 4 10 0 0 14 -1

1 2 6 0

0 1

CS350: Data Structures 27 AVL Tree Insertion Example

Case #1: Must perform single ? 12 right rotation between node 8 and node 4 ? 8 1 16

2 4 10 0 0 14 -1

1 2 6 0

0 1

CS350: Data Structures 28 AVL Tree Insertion Example

After rotation, update height of node 8 ? 12 Node 4 is balanced, so continue unwinding recursion 2 4 1 16

1 2 8 1 0 14 -1

0 1 6 0 10 0

CS350: Data Structures 29 AVL Tree Insertion Example

node 12 - add 1 to the 3 12 height of its highest child

2 4 1 16

1 2 8 1 0 14 -1

0 1 6 0 10 0

CS350: Data Structures 30 Other Operations: find / remove

• The find operation is the same as the unbalanced binary search tree

• The remove operation works similarly to the remove operation from the unbalanced binary search tree with a few modifications - When a node is removed, the heights of its ancestors may need to be updated as the recursion is unwound -- fix imbalances as they are encountered just like with insertion

CS350: Data Structures 31 Analysis of AVL Tree Operations

of AVL Tree operations

worst case average

find O(log N) O(log N)

insert O(log N) O(log N)

remove O(log N) O(log N)

CS350: Data Structures 32