Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Data Structures & Algorithms

Topic: AVL Tree


By

Dr. Prakash Singh Tanwar

Associate Professor

Lovely Professional University, Punjab

By:Dr. Prakash Singh Tanwar


Example
Insert the following elements in AVL Tree
44, 17, 32, 78, 50, 88, 48, 62, 54.

By:Dr. Prakash Singh Tanwar


Example
Insert the following elements in AVL Tree
44, 17, 32, 78, 50, 88, 48, 62, 54.

By:Dr. Prakash Singh Tanwar


Example
Insert the following elements in AVL Tree
44, 17, 32, 78, 50, 88, 48, 62, 54.

By:Dr. Prakash Singh Tanwar


Example
Insert the following elements in AVL Tree
44, 17, 32, 78, 50, 88, 48, 62, 54.

By:Dr. Prakash Singh Tanwar


AVL Tree
An AVL tree node
#include<bits/stdc++.h>
using namespace std;

// An AVL tree node


class Node
{
public:
int key;
Node *left;
Node *right;
int height;
};

By:Dr. Prakash Singh Tanwar


AVL Tree
Height of Node
int height(Node *N)
{
if (N == NULL)
return 0;
return N->height;
}

By:Dr. Prakash Singh Tanwar


AVL Tree
Max of two values
int max(int a, int b)
{
return (a > b)? a : b;
}

By:Dr. Prakash Singh Tanwar


AVL Tree
New Node
Key Node* newNode(int key)
5 {
left right Node* node = new Node();
NULL NULL node->key = key;
height node->left = NULL;
1 node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
Node
}

By:Dr. Prakash Singh Tanwar


AVL Tree
Right Rotate
y Node *rightRotate(Node *y) x
{
Node *x = y->left;
x Node *T2 = x->right; y
// Perform rotation
x->right = y;
y->left = T2;
T2 // Update heights T2
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Return new root
return x;
}
By:Dr. Prakash Singh Tanwar
AVL Tree
Left Rotate
Node *leftRotate(Node *x)
{ y
x
Node *y = x->right;
Node *T2 = y->left;
// Perform rotation x
y
y->left = x;
x->right = T2;
// Update heights T2
T2
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// Return new root
return y;
}

By:Dr. Prakash Singh Tanwar


AVL Tree
Balance Factor

int getBalance(Node *N)


{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

By:Dr. Prakash Singh Tanwar


AVL Tree
Insert Node
Node* insert(Node* node, int key)
{
// 1. Perform the normal BST insertion
// 2. Update height of this ancestor node
/* 3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced */
// If this node becomes unbalanced, then
// there are 4 cases
// 1. Left Left Case
// 2. Right Right Case
// 3. Left Right Case
// 4. Right Left Case
// return the (unchanged) node pointer
}

By:Dr. Prakash Singh Tanwar


AVL Tree
1. Perform the normal BST insertion
Node* insert(Node* node, int key)
{
// 1. Perform the normal BST insertion
if (node == NULL)
return(newNode(key));

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;

}

By:Dr. Prakash Singh Tanwar


AVL Tree
2. Update height of this ancestor node

Node* insert(Node* node, int key)


{

//2. Update height of this ancestor node
node->height = 1 + max(height(node->left),
height(node->right));

}

By:Dr. Prakash Singh Tanwar


AVL Tree
3. Get the balance factor of this ancestor node to check
whether this node became unbalanced
Node* insert(Node* node, int key)
{

/*3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced
*/
int balance = getBalance(node);

}

By:Dr. Prakash Singh Tanwar


AVL Tree
Rotation according to various cases
Node* insert(Node* node, int key)
{

//i). Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
//ii) Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
//iii) Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
//iv) Right Left Case
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}

}
By:Dr. Prakash Singh Tanwar
AVL Tree
4. Return the node

Node* insert(Node* node, int key)


{

return node;
}

By:Dr. Prakash Singh Tanwar


AVL tree
Display
void preOrder(Node *root)
{
if(root != NULL)
{
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
}
}

By:Dr. Prakash Singh Tanwar


AVL tree
main
int main() 30
{
Node *root = NULL; 40
20
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
10 25 50
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
cout << "Preorder traversal of the AVL tree is \n";
preOrder(root);
return 0;
}

By:Dr. Prakash Singh Tanwar


AVL tree
20 20
10 10 10

20 20 10 30 10 30

40
30
20
30 30

10 30
40 20 40
20
40

50 10 25 50
10
50

By:Dr. Prakash Singh Tanwar


Thank You

By:Dr. Prakash Singh Tanwar

You might also like