You are on page 1of 34

AVL TREES

AVL (Height-balanced Trees)


A perfectly balanced binary tree is a binary tree such that:
The height of the left and right subtrees of the root are equal The left and right subtrees of the root are perfectly balanced binary trees

Perfectly Balanced Binary Tree

AVL (Height-balanced Trees)


An AVL tree (or height-balanced tree) is a binary search tree such that:
The height of the left and right subtrees of the root differ by at most 1 The left and right subtrees of the root are AVL trees Node balance factor of -1 if node left high, 0 if node is equal high and +1 is node is right high

AVL Trees
To orient yourself, drop the key of the node and use / \ - // \\ to represent the balance factor of that node: \ : right high
/ : left high - : equally high \\ : right imbalance // : left imbalance
h(right subtree) = 1 + h(left subtree)
h(left subtree) = 1 + h(right subtree)

h(right subtree) = h(left subtree)


h(right subtree) > 1 + h(left subtree) 5 h(left subtree) > 1 + h(right subtree)

AVL Trees

Non-AVL Trees

AVL Trees
An AVL tree is not a full tree nor it is a minimum level tree. Insertions and deletions are done exactly in the same way as in a BST. However after each operation we need to check the AVL property as the tree may become imbalanced:

/ new node

// / / 8 h different by 2!

AVL Trees
When inserting, if AVL property is not violated we are done. Actually there is a good chance that it will not:

\ \ \ -

/ / \ /

/ -

/
-

We have some latitude since we are concerned with height difference

AVL Trees: Practice


\ / -

/ -

\
10

When will the insertion of a new node cause the tree to become imbalanced?

AVL Trees: Inserting a node


The tree becomes imbalanced if and only if the newly insterted node is a left (right) descendant of a node that previously had a left (right) high (heavy) subtree and the height of the left subtree increases. In the picture that follows: Us: new nodes that can cause imbalance Bs: new nodes that keep the tree balanced
11

AVL Trees: Inserting a node


\ / B B -

/ -

\ -

12

UU UU

BBBB U U UU U UUU

Insertion Into AVL Tree

13

Insertion Into AVL Trees

14

Insertion Into AVL Trees

15

Insertion Into AVL Trees

16

Insertion Into AVL Trees

17

Insertion Into AVL Trees

18

AVL Tree Rotations


Reconstruction procedure: rotating tree

left rotation and right rotation


Suppose that the rotation occurs at node x Left rotation: certain nodes from the right subtree of x move to its left subtree; the root of the right subtree of x becomes the new root of the reconstructed subtree Right rotation at x: certain nodes from the left subtree of x move to its right subtree; the root of the left subtree of x becomes the new root of the reconstructed subtree
19

AVL Tree Rotations

20

AVL Tree Rotations

21

AVL Tree Rotations

22

AVL Tree Rotations

23

AVL Tree Rotations

24

AVL Tree Rotations

25

AVL Tree Rotations

26

AVL Tree Rotations

27

AVL Tree Rotations

28

AVL Tree Rotations

29

AVL Tree Rotations

30

AVL Tree Rotations

31

AVL Tree Rotations

32

AVL Tree Rotations

33

Deletion From AVL Trees


Case 1: the node to be deleted is a leaf Case 2: the node to be deleted has no right child, that is, its right subtree is empty Case 3: the node to be deleted has no left child, that is, its left subtree is empty Case 4: the node to be deleted has a left child and a right child

34

You might also like