100% found this document useful (1 vote)
40 views15 pages

Avl Tree and Its Applications

Uploaded by

P. Sanjay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
40 views15 pages

Avl Tree and Its Applications

Uploaded by

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

AVL TREE

ALGORITHMS AND ITS


APPLICATIONS
PRESENTED BY
Y. SINDU UJWALA 24011DA811
I. RUCHITHA 24011DA812
AVL Tree is defined as a self-balancing
Binary Search Tree (BST) where the
difference between heights of left and right
subtrees for any node cannot be more than
one.

Tree is said to be balanced if balance factor


of each node is in between -1 to 1,
otherwise, the tree will be unbalanced and
need to be balanced
Balance Factor (k) = height (left(k)) - height (right(k))

.
Why AVL Tree?
- An AVL tree is a self-balancing BST that ensures
the height of the tree remains relatively small by
rotating nodes when the balance factor becomes
too large.

- AVL trees guarantee a search time of O(log n),


making them much faster than BSTs. Whereas,
these BSTs can become unbalanced, leading to
poor search performance (O(n) in the worst case).

- Insertion and deletion operations in AVL trees are


designed to maintain the balance of the tree,
ensuring that search performance remains
efficient.
There are basically four types of rotations which are
as follows:
AVL Rotations
1.Left Rotation: When a node is added into the
right subtree of the right subtree, if the tree gets out
of balance, we do a single left rotation.
We perform
rotation in AVL
tree only in
case if Balance 2.Right Rotation:
If a node is added to the left subtree of the left subtree, the AVL tree
Factor is other may get out of balance, we do a single right rotation.
than -1, 0,
and 1.
3. Right-Left Rotation:
:
AVL Rotations A right-left rotation is a combination in which first right rotation
takes place after that left rotation executes.

We perform
rotation in AVL
tree only in
case if Balance 4. Left-Right Rotation:
A left-right rotation is a combination in which first left rotation
Factor is other takes place after that right rotation executes.

than -1, 0,
and 1.
Node leftRotate(Node x) { //x=10
Node y = x.right; // y is the right child of x
L Rotation //y=20
Algorithm Node z = y.left; // z is the left child of y //z=30

10 y.left = x; // y becomes the new root, x becomes the


left child of y
20 x.right = z; // x’s right child becomes z
15
// Recalculate heights
Before left rotation
x.height = Math.max(height(x.left), height(x.right)) +
1;
20
y.height = Math.max(height(y.left), height(y.right)) + 1; / \
10 15

return y; // Return new root y after rotation


}
}
Node rightRotate(Node y) {
R Rotation Node x = y.left; // x is the left child of y

Algorithm Node z = x.right; // z is the right child of x

30 x.right = y; // x becomes the new root, y becomes the right child of x


/ y.left = z; // z becomes the left child of y
20
/
10 // Recalculate the heights
y.height = Math.max(height(y.left), height(y.right)) + 1;
x.height = Math.max(height(x.left), height(x.right)) + 1;

return x; // Return the new root (x)


}
Node insert(Node node, int key) {
if (node == null)
Insertion return new Node(key);
Algorithm if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
6 else
return node;
node.height = 1+Math.max(height(node.left),height(node.right));
9 int balance = getBalance(node);
4
if (balance > 1 && key < node.left.key)
return rightRotate(node);
if (balance < -1 && key > node.right.key)
3 return leftRotate(node);
5
if (balance > 1 && key > node.left.key){
node.left = leftRotate(node.left);
return rightRotate(node);
Insert 1 }
if (balance < -1 && key < node.right.key) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
//Tree balancing
int getBalance(Node N) {
return (N == null) ? 0 : height(N.left) - height(N.right);

Tree }
// Get height of a node
Balancing, int height(Node N) {

Height, }
return (N == null) ? 0 : N.height;

Finding // Get the node with the smallest key in the tree
Minimum Node minValueNode(Node node) {

Algorithm Node current = node;


while (current.left != null)
current = current.left;
return current;
}
Search
Node search(Node node, int key) {
Algorithm
if (node == null || node.key == key) {

return node;
}
if (key < node.key) {

return search(node.left, key);

}
return search(node.right, key);
}
Deletion Node delete(Node root, int key) {
if (root == null)
Algorithm return root;
10 if (key < root.key)
root.left = delete(root.left, key);
else if (key > root.key)
5 20 root.right = delete(root.right, key);
else {
if ((root.left == null) || (root.right == null)) {
Node temp = (root.left != null) ? root.left : root.right;
2 9 30 if (temp == null) {
root = null;
} else {
7 root = temp;
}
} else {
Node temp = minValueNode(root.right);
root.key = temp.key;
root.right = delete(root.right, temp.key);
}
if(root==null)
return root;
root.height=Math.max(height(root.left), height(root.right))+1;
int balance=getBalance(root);
if(balance>1 && getBalance(root.left)>=0)
return rightRotate(root);
if(balance >1&&getBalance(root.left)<=0){
root.left=leftRotate(root.left);
return rightRotate(root);
}
if(balance<-1 &&getBalance(root.right)>0){
root.right=rightRotate(root.right);
return leftRotate(root);
}
return root;
}
APPLICATIONS

•Databases: AVL trees are widely used in database systems for maintaining balanced search trees. They ensure
efficient operations like insertion, deletion, and searching with a time complexity of O(log⁡n),O(\log n) and
O(logn), which is crucial for indexing large datasets.

•Memory Management: AVL trees help allocate and free memory blocks efficiently in dynamic memory
systems. By keeping memory allocations balanced, they reduce fragmentation and speed up memory access
operations.

•Network Routing: AVL trees are utilized in routing tables for storing and efficiently retrieving routes. Their
self-balancing nature ensures quick access and updates, crucial for managing network paths dynamically.

•File Systems: File directories often rely on AVL trees to manage hierarchical data structures. This ensures quick
lookups, additions, and deletions, maintaining system performance as files are frequently accessed or modified.

•Gaming: AVL trees handle dynamic data in games, such as managing player rankings or high scores. Their
balanced structure ensures that retrieving or updating scores happens in minimal time, enhancing the gaming
experience.

You might also like