0% found this document useful (0 votes)
16 views

AVL TREES UNIT 3

DSA

Uploaded by

brindha235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

AVL TREES UNIT 3

DSA

Uploaded by

brindha235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

TREES

Syllabus

UNIT III NON-LINEAR DATA STRUCTURES- TREES


Tree ADT –Tree terminologies – Binary Tree ADT- Tree traversals –
Expression trees – Binary search tree ADT - AVL Trees –
Complete binary tree – Full binary tree-Heap – Priority queue using
Heap –B-Tree - B+Tree
AVL (Adelson-Velskii and Landis) tree
■ A balanced binary search tree where the height of the two subtrees (children) of a node differs by
at most one. Look-up, insertion, and deletion are O( log n), where n is the number of nodes in the
tree.
Definition of height (reminder)
■ Height: the length of the longest path from a node to a leaf.
– All leaves have a height of 0
– An empty tree has a height of –1
The AVL Tree Data Structure
An AVL tree is a self-balancing binary search tree.

Structural properties
1. Binary tree property (same as BST)
2. Order property (same as for BST)

3. Balance condition:
balance of every node is between -1 and 1

where balance(node) = height(node.left) – height(node.right)


Example #1: Is this an AVL Tree?

Balance Condition:
balance of every node is between -1 and 1 6

where balance(node) =
height(node.left) – height(node.right)
4 8

1 7 11

10 12
Example #2: Is this an AVL Tree?

6
Balance Condition:
balance of every node is between -1 and 1

where balance(node) = 4 8
height(node.left) – height(node.right)

1 5 7 11

2
AVL Trees
Good News:
Because height of AVL tree is O(log(n)), then find

But as we insert and delete elements, we need to:


AVL Trees

3
10

2 2
5 20

0 1 1 0
2 9 15 30

0 0
7 17
An AVL Tree

3 10 data
10 3 height
children
1 2
5 15
0 0 0 1
2 9 12 20
0 0
17 30
Not AVL Trees

3 0-2 = -2
10 2 (-1)-1 = -2
10
0 2
5 15 1
15
0 1
12 20 0
20
0 0
17 30

Note: height(empty tree) == -1


AVL tree operations
■ AVL find:
– Same as usual BST find

■ AVL insert:

AVL delete:

// An AVL tree node


struct Node
{
int key;
struct Node *left;
struct Node *right;
int height;
};
AVL Tree insert (more specific):

1. Insert the new node as in our generic BST (a new leaf)

2. For each node on the path from the root to the new leaf,
the insertion may (or may not) have changed the node’s
height

3. So after insertion in a subtree,


■ In AVL tree, after performing every operation like insertion and deletion we need to check the balance
factor of every node in the tree. If every node satisfies the balance factor condition then we conclude the
operation otherwise we must make it balanced. We use rotation operations to make the tree balanced
whenever the tree is becoming imbalanced due to any operation.

■ There are four scenarios where a rotation might be required when the tree is unbalanced( ie when height
difference is more than 1 )at node α

■ Case A : Insertion into the left sub-tree of left node of α (Single right rotation)

■ Case B : Insertion into the right sub-tree of right node of α (Single left rotation)

■ Case C : Insertion into the right sub-tree of left node of α (Double rotation)

■ Case D : Insertion into the left sub-tree of right node of α (Double rotation)
Generalizing the examples…

12

5 23

2 9 18 30

1 4 7 10
Generalizing our examples…

12

5 23

2 9 18 30

1 4 7 10
Generalizing our examples…

12

A C
Case A : Single Right rotation
■ Two nodes k2 and k1 are shown in the figure, here k2 is the root
node (and also the a node, k1 is its left child and Z shown in the
triangle is its right child. The nodes X and Y are the left and right
sub-trees of the node K1)A new node is inserted at left subtree of
K1and because of this the tree becomes unbalance at K2 The k2
node has been rotated single time towards right to become the
right child of k1 and Y has become the left child of k2
The sub routine to do right rotation is
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
* Update heights, then return new root.
*/
private static AvlNode rotateWithLeftChild( AvlNode k2 )
{
AvlNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
15

8 22

4 10 19 24

3 6 17 20

16
Case B: Single Left Rotation
■ The new node has been inserted as a child node of Z, One
rotation towards should make it balanced within limits of AVL
tree. The figure on the right is after rotation the node k1 one
time towards left. This time node Y has become the right child
node of the node k1
* Rotate binary tree node with right child.
* For AVL trees, this is a single rotation for case 4.
* Update heights, then return new root.
*/
private static AvlNode rotateWithRightChild( AvlNode k1 )
{
AvlNode k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = max( height( k2.right ), k1.height ) + 1;

return k2;
}
Example for right-right case:
insert(26)

15
15

8 24
8 22

24 4 10 22 25
4 10 19

3 6 3 6 19 23
23 25 26

26
Case C : LEFT Right Double rotation
■ The new node is inserted in the right sub-tree of left
child . ie the left child of node α is k1 and its right
sub-tree is node k2. Because of insertion the
balance is lost at k3 so there are two single rotations
performed

* Double rotate binary tree node: first left child


* with its right child; then node k3 with new left child.
* For AVL trees, this is a double rotation for case 2.
* Update heights, then return new root.
*/ private static AvlNode doubleWithLeftChild( AvlNode
k3 )
{
k3.left = rotateWithRightChild( k3.left);
return rotateWithLeftChild( k3 );
}
Case D : Right LEFT Double rotation

/** * Double rotate binary tree node: first right child


* with its left child; then node k1 with new right child.
* For AVL trees, this is a double rotation for case 3.
* Update heights, then return new root.
*/
private static AvlNode doubleWithRightChild( AvlNode k1 )
{

k1.right = rotateWithLeftChild( k1.right );


return rotateWithRightChild( k1 );
}
Pros and Cons of AVL Trees

Arguments for AVL trees:

1. All operations logarithmic worst-case because trees are always


balanced
2. Height balancing adds no more than a constant factor to the speed of
insert and delete

Arguments against AVL trees:

1. Difficult to program & debug [but done once in a library!]


2. More space for height field
3. Asymptotically faster but rebalancing takes a little time
4. If amortized logarithmic time is enough, use splay trees (also in the
text, not covered in this class)

You might also like