AVL Tree
AVL Tree
AVL Tree
On
AVL Tree
SUBMITTED BY:
190032070 V. SUBHASH
CH.NARESH
<Assistant Professor>
KL UNIVERSITY
Green fields, Vaddeswaram – 522 502
DEPARTMENT OF BASIC ENGINEERING SCIENCES
CERTIFICATE
I express the sincere gratitude to our director Dr. A Jagadeesh for his
administration towards our academic growth.
190032070 V.SUBHASH
190032076 M.CHANDRAMAVARMA
AVL tree implements the Map abstract data type just like a regular
binary search tree, the only difference is in how the tree performs. To
implement our AVL tree we need to keep track of a balance factor for
each node in the tree. We do this by looking at the heights of the left and
right subtrees for each node..
INDEX
S.NO TITLE PAGE NO
1 Introduction 6-7
4 Class Diagram 10
5 Implementation 11-42
6 Outputs/ScreenShots 43-46
7 Conclusion 47
INTRODUCTION
The AVL trees, also called "Height Balanced Trees" were first
introduced by two Russians named Adelson-Velskii and Landis. AVL
trees are maintained in such a way that the trees always remain within
one level of being perfectly balanced. ... The BALANCE FACTOR of a
node T in a binary tree bf(T) = h-L - h-R.
AVL tree is a self-balanced binary search tree. That means, an AVL tree
is a binary search tree but it is a balanced tree. A binary tree is said to be
balanced, if the difference between the heights of left and right subtrees
of every node in the tree is either -1, 0 or +1. In other words, a binary tree
is said to be balanced if for every node, height of its children differ by at
most one. In an AVL tree, every node maintains an extra information
known as balance factor to take care of the self-balancing nature of the
tree.
AIM
To Implement AVL tree and its operations using java
Advantages:-
1. Since AVL trees are height balance trees, operations like insertion and deletion
have low time complexity. Let us consider an example:
2. To insert a node with a key Q in the binary tree, the algorithm requires seven
comparisons, but if you insert the same key in AVL tree, from the above 1st figure,
you can see that the algorithm will require three comparisons.
Disadvantages:-
Future enhancements:-
AVL tree (height-balanced tree) A binary search tree such that for each node the
heights of the left and right subtrees differ by at most one. ... During insertion or
deletion, a node in an AVL tree may become critical or unbalanced and then the
tree has to be reorganized to maintain its balanced property.
SYSTEM REQUIREMENTS
⮚ SOFTWARE REQUIREMENTS:
The major software requirements of the project are as follows:
Language : Java
IDE : Eclipse
Operating system: Windows Xp or later.
⮚ HARDWARE REQUIREMENTS:
The hardware requirements that map towards the software are as follows:
RAM : 8 Gb or More.
import java.util.*;
this.value = val;
return 0;
return N.height;
if (node == null) {
return(new Node(value));
else
return leftRotate(node);
node.left = leftRotate(node.left);
return rightRotate(node);
node.right = rightRotate(node.right);
return leftRotate(node);
return node;
Node x = y.left;
Node T2 = x.right;
// Perform rotation
x.right = y;
y.left = T2;
// Update heights
return x;
Node y = x.right;
Node T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
return y;
}
if (N == null)
return 0;
if (root != null) {
preOrder(root.left);
preOrder(root.right);
current = current.left;
return current;
return root;
// to be deleted
else {
Node temp;
if (root.left != null)
temp = root.left;
else
temp = root.right;
// No child case
if(temp == null) {
temp = root;
root = null;
temp = null;
else {
root.value = temp.value;
if (root == null)
return root;
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether
return rightRotate(root);
root.left = leftRotate(root.left);
return rightRotate(root);
return leftRotate(root);
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
if(root == null) {
System.out.println("(XXXXXX)");
return;
current.add(root);
int elements = 1;
sb.append(' ');
String textBuffer;
// Iterating through height levels.
textBuffer = sb.toString();
for(Node n : current) {
System.out.print(textBuffer);
if(n == null) {
System.out.print(" ");
next.add(null);
next.add(null);
} else {
System.out.printf("(%6d)", n.value);
next.add(n.left);
next.add(n.right);
}
System.out.print(textBuffer);
System.out.println();
for(Node n : current) {
System.out.print(textBuffer);
if(n == null)
System.out.print(" ");
else
System.out.printf("%s %s",
n.left == null ? " " : "/", n.right == null ? " " : "\\");
System.out.print(textBuffer);
System.out.println();
current = next;
while (true) {
System.out.println("(1) Insert");
System.out.println("(2) Delete");
try {
String s = bufferRead.readLine();
if (Integer.parseInt(s) == 1) {
else if (Integer.parseInt(s) == 2) {
}
else {
continue;
t.print(root);
catch(IOException e) {
e.printStackTrace();
}
OUTPUTS
Screen Shots:
CONCLUSION
AVL trees are often compared with red–black trees because both
support the same set of operations and take time for the basic operations. For
lookup-intensive applications, AVL trees are faster than red–black trees because
they are more strictly balanced.[4] Similar to red–black trees, AVL trees are height-
balanced. Both are, in general, neither weight-balanced nor -balanced for any
;[5] that is, sibling nodes can have hugely differing numbers of descendants.
The algorithm for intersection or difference is similar, but requires the Join2 helper
routine that is the same as Join but without the middle key. Based on the new
functions for union, intersection or difference, either one key or multiple keys can be
inserted to or deleted from the AVL tree. Since Split calls Join but does not deal with
the balancing criteria of AVL trees directly, such an implementation is usually called
the "join-based" implementation.