You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Binary tree where the difference between the height of left subtree and right subtree for every node is not more than k(usually k=1). This difference is called balanced factor. bf=|height of LST - height of RST|.
So for every node height of LST - height of RST should be {-1,0,1}.
AVL TREE
AVL Tree is a self-balancing BST (i.e.,basically a BST which remains balanced even though you insert or delete values).
Ways of Balancing AVL Tree
To maintain the balance in AVL Tree(i.e to be AVL tree always) while inserting and deleting, we perform Rotations. Depending on different imbalance cases, we have 4 basic types of rotations –
// INSERTION IN AVL TREE STARTSpublicNodeinsertNodeRECURR(Noder, intkey) {
// if the root is null, create a new node and return itif (r == null)
returnnewNode(key);
// if the given key is less than the root node,// recur for the left subtreeelseif (key < r.data)
r.left = insertNodeRECURR(r.left, key);
// otherwise, recur for the right subtreeelseif(key > r.data)
r.right = insertNodeRECURR(r.right, key);
else// key== root.data
{
System.out.println("Dublicate value not allowed");
returnr;
}
// HERE COME'S AVL TREE PART STARTSintbf = getBalancedFac(r);
// Left Caseif (bf > 1 && key < r.left.data)
returnrightRotate(r);
// Right Caseelseif(bf < -1 && key > r.right.data)
returnleftRotate(r);
// Left-Right Caseelseif(bf > 1 && key > r.left.data)
{
r.left=leftRotate(r.left);
returnrightRotate(r);
}
// Right-Left Caseelseif(bf < -1 && key < r.right.data)
{
r.right=rightRotate(r.right);
returnleftRotate(r);
}
// HERE COME'S AVL TREE PART ENDSreturnr;
}
// INSERTION IN AVL TREE ENDS
// DELETION IN AVL TREE STARTSpublicNodedelete(Noder,intval) {
if(r==null) returnnull;
if(val<r.data) {
r.left=delete(r.left,val);
}
elseif(val>r.data) {
r.right=delete(r.right,val);
}
else {
if(r.left!=null && r.right!=null) {
intlmax=maxLeft(r.left);
r.data=lmax;
r.left=delete(r.left,lmax);
}
elseif(r.left!=null) {
returnr.left;
}
elseif(r.right!=null) {
returnr.right;
}
else {
returnnull;
}
}
// HERE COME'S AVL TREE DELETION PART STARTSintbf=getBalancedFac(r);
// bf=2 means deletion from right subtree and now remaining is left imbalance OR L-R imbalance/* * bf(r.left)=1 means remaing structure in this is:- 30 bf=2 / 20 bf=1 / 10 bf=0 * bf(r.left)=0 means remaing structure in this is:- 30 bf=2 / 20 bf=0 / \ 10 25 bf=0 for both */if(bf==2 && getBalancedFac(r.left)>=0)
returnrightRotate(r);
elseif(bf==2 && getBalancedFac(r.left)==-1 ) {
r.left=leftRotate(r.left);
returnrightRotate(r);
}
// bf=-2 means deletion from left subtree and now remaining is right imbalance OR R-L imbalance/* * bf(r.right)=-1 means remaing structure in this is:- 10 bf=-2 \ 20 bf=-1 \ 30 bf=0 * bf(r.right)=0 means remaing structure in this is:- 10 bf=-2 \ 20 bf=0 / \ 15 30 bf=0 for both */elseif(bf==-2 && getBalancedFac(r.right)<=0)
returnleftRotate(r);
elseif(bf==-2 && getBalancedFac(r.right)==1 ) {
r.right=rightRotate(r.right);
returnleftRotate(r);
}
// HERE COME'S AVL TREE DELETION PART ENDSreturnr;
}
publicstaticintmaxLeft(Nodenode) {
if(node.right!=null) {
returnmaxLeft(node.right);
}
else {
returnnode.data;
}
}
// DELETION IN AVL TREE ENDS