Professional Documents
Culture Documents
AVL Tree
AVL Tree
Lecture 7
AVL Trees
Done By
BASHAR ABDULQAWI QASSEM
(IT)
Supervisor
Eng. Salah Alssayany
2023
Data Structures II AVL Trees
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of
every node is either -1, 0 or +1.
Consider the following keys inserted in the given order in the binary
search tree.
The height of the tree grows linearly in size when we insert the
keys in increasing order of their value. Thus, the search operation,
at worst, takes O(n).
Let us now look at the same keys but inserted in a different order.
Height Balanced
Here, the keys are the same, but since they are inserted in
a different order, they take different positions, and the
height of the tree remains balanced. Hence search will not
take more than O(log n) for any element of the tree. It is
now evident that if insertion is done correctly, the tree’s
height can be kept balanced.
If the balance factor of any node in an AVL tree becomes less than
-1 or greater than 1, the tree has to be balanced by making a
simple modifications in the tree called rotation.
AVL Rotations
In AVL tree, after performing operations 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. Whenever the tree becomes
imbalanced due to any operation we
use rotation operations to make the tree balanced.
There are four rotations and they are classified into two types.
Single Rotation
Left - Left Rotation (LL Rotation)
In LL Rotation, every node moves one position to right from the current position. To
understand LL Rotation, let us consider the following insertion operation in AVL Tree...
Double Rotation
Left Right Rotation (LR Rotation)
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In
LR Rotation, at first, every node moves one position to the left and one position to right
from the current position. To understand LR Rotation, let us consider the following insertion
operation in AVL Tree...
This rotation is performed when a new node is inserted at the left child of the right
subtree.
1. Search
2. Insertion
3. Deletion
Step 1 - Insert the new element into the tree using Binary Search Tree insertion
logic.
Step 2 - After insertion, check the Balance Factor of every node.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree
is said to be imbalanced. In this case, perform suitable Rotation to make it
balanced and go for next operation.
RR Rotation at 1
RR Rotation at 3
RR Rotation at 5
RR Rotation at 5
Summary:
AVL trees are self-balancing binary search trees.
Balance factor is the fundamental attribute of AVL trees
The balance factor of a node is defined as the difference
between the height of the left and right subtree of that node.
The valid values of the balance factor are -1, 0, and +1.
The insert and delete operation require rotations to be
performed after violating the balance factor.
The time complexity of insert, delete, and search operation is
O(log N).
AVL trees follow all properties of Binary Search Trees.
The left subtree has nodes that are lesser than the root node.
The right subtree has nodes that are always greater than the
root node.
AVL trees are used where search operation is more frequent
compared to insert and delete operations.
r->l = insert(r->l, v); cout << t->d << " "; cout << " Inorder
Traversal:" << endl<<"\t\t\t[ ";
r = balance(r); }
AVL.inorder(r);
} else if (v >= r->d) { int main() {
cout <<" ]";
r->r = insert(r->r, v); int c, i;
//AVL.printTree(r, "", true);
r = balance(r); AVL_tree AVL;
break;
} return r; while (1) {cout<<endl;for(int
i=0;i<119;i++) case 4:
} {cout<<"Ä";}cout<<endl;
cout << "Preorder
void AVL_tree::show(AVL *p, int l) cout << " 1.Insert Element Traversal:" << endl<<"\t\t\t[ ";
{ into the tree" << endl;
AVL.preorder(r);
int i; cout << " 2.show Balanced
AVL Tree" << endl; cout <<" ]";
if (p != NULL) {
cout << " 3.InOrder traversal" // AVL.printTree(r, "", true);
show(p->r, l+ 1); << endl;
break;
cout<<" "; cout << " 4.PreOrder
traversal" << endl; case 5:
if (p == r)
cout << " 5.PostOrder cout << "Postorder
cout << " Root -> "; Traversal:" << endl<<"\t\t\t[ ";
traversal" << endl;
for (i = 0; i < l&& p != r; i++) AVL.postorder(r);
cout << " 6.Exit" << endl;
cout << " "; cout<<" ]";
cout << " \n \t\t\t\t\tEnter your
cout << p->d; Choice: ";
// AVL.printTree(r, "", true);
show(p->l, l + 1); cin >> c;
break;
} switch (c) {
case 6:
} case 1:
exit(1);
void AVL_tree::inorder(AVL *t) { cout << "\t\t\t\t\tEnter
break;
value to be inserted: ";
if (t == NULL) default:
cin >> i;
return; cout << " \a Wrong
r = AVL.insert(r, i);
Choice ,, try again" << endl;
inorder(t->l);
break;
}
cout << t->d << " ";
case 2:
}
inorder(t->r);
if (r == NULL) {
return 0;
}
cout << "\t\t\t\t\t\tTree is
}
void AVL_tree::preorder(AVL *t) { Empty" << endl;
if (t == NULL) continue;
return; }
cout << t->d << " "; cout << " Balanced AVL
Tree:" << endl<<"\t\t\t[ ";
preorder(t->l);
AVL.show(r, 1);
preorder(t->r);
cout<<"
} ]"<<endl<<endl<<endl;
😍 🤗 🌝