Showing posts with label AVL TREE. Show all posts
Showing posts with label AVL TREE. Show all posts

Tuesday, May 26, 2015

AVL TREE

AIM

To implement the AVL tree with insert & delete operations using Java.

ALGORITHM

Step 1:              Start the program by defining the functions.

Step 2:              Insert the elements to the AVL tree.

Step 3:              Check the tree if it is balanced or not.

Step 4:              The balance factor is one of 0, 1 and -1.

Step 5:              If it is not balanced, balance the tree using
(i)           Left-left
(ii)         Left-right
(iii)       Right-left
(iv)       Right-right
Balancing

Step 6:              And if the tree is balanced and then the insert() and the delete() operations are performed.

Step 7:              Stop the program.





PROGRAM
AVL TREE
import java.io.*;
class node
{
      public int data;
      public node LC,RC;
      public int bf;
}
class avltree
{    
      node root = null;
      public boolean insert()
      {
                int newelt=0;
                try
                {
                System.out.println("Enter the element:");
                DataInputStream din=new DataInputStream(System.in);
                newelt=Integer.parseInt(din.readLine());
                }
                catch(Exception e){}
            if(root==null)
            {
                  node y=new node();
                  y.data=newelt;
                  y.bf=0;
                  y.LC=null;
                  y.RC=null;
                  root=y;
                  return true;
              }
            node f,a,q,p;        
            node b,c;            
            int d;               
            node y=new node();
            boolean found, unbalanced;
            f=null;
            a=root;
            p=root;
            q=null;
            found=false;
            while (p!=null && found!=true)
            {
                  if(p.bf!=0) {a=p;f=q;}
                  if(newelt<p.data){q=p;p=p.LC;}
                  else if(newelt>p.data){q=p;p=p.RC;}
                  else {y=p;found=true;}
            }          
            if(found==false)
                {
                  y.data=newelt;
                  y.bf=0;
                  y.LC=null;
              y.RC=null;
         if(newelt<q.data)
                  q.LC=y;
         else
                  q.RC=y;                 
         if(newelt >a.data)
                  {p=a.RC;b=p;d=-1;}
         else    
                  {p=a.LC;b=p;d=1;}
         while(p!=y)
         {
                  if(newelt > p.data)
                        {p.bf=-1;p=p.RC;}
                  else
                        {p.bf=1;p=p.LC;}
         }       
         unbalanced=true;
         if((a.bf==0)||((a.bf+d)==0))
                  {a.bf+=d;unbalanced=false;}    
         if(unbalanced==true)
         {
           if(d==1)
            {
            if(b.bf==1)
            {
                  System.out.println("LL imbalance");
                  a.LC=b.RC;
                  b.RC=a;
                  a.bf=0;
                  b.bf=0;
            }
            else
            {
                  System.out.println("LR imbalance");
                  c=b.RC;
                  b.RC=c.LC;
                  a.LC=c.RC;
                  c.LC=b;
                  c.RC=a;

Tuesday, February 18, 2014

AVL TREE

PROGRAM:

import java.io.*;
class avlnode
{
public int data,bf;
public avlnode lchild,rchild,parent;
}
class avl
{
avlnode root=null;
avlnode p=null;
avlnode prev=null;
public void insert(int ele)
{
avlnode temp=new avlnode();
temp.data=ele;
temp.lchild=null;
temp.rchild=null;
temp.parent=null;
p=new avlnode();
prev=new avlnode();
p=root;
if(root==null)
{
root=temp;
}
else
{
while(p!=null)
{
prev=p;
if(p.data>temp.data)
{
p=p.lchild;
}
else
{
p=p.rchild;
}
}
if(prev.data>temp.data)
{
prev.lchild=temp;
prev.lchild.parent=prev;
prev.bf+=1;
}
else
{
prev.rchild=temp;
prev.rchild.parent=prev;
prev.bf-=1;
}
while((prev!=null) && (prev.bf!=2) && (prev.bf!=-2))
{
p=prev;
prev=prev.parent;

if(prev!=null)
{
if(p==prev.lchild)
prev.bf+=1;
else
prev.bf-=1;
}
}

System.out.println("The tree before rotation");
dispre(root);
if((prev!=null) && (prev.bf==2))
{
switch(p.bf)
{
case 1:
System.out.println("left-left rotation");
ll_rotate();
dispre(root);
break;
case -1:
System.out.println("left-right rotation");
lr_rotate();
dispre(root);
break;
}
}

if((prev!=null) && (prev.bf==-2))
{
switch(p.bf)
{
case 1:
System.out.println("right-left rotation");
rl_rotate();
dispre(root);
break;
case -1:
System.out.println("right-right rotaion");
rr_rotate();
dispre(root);
break;
}
}
}
}
public void ll_rotate()
{
p.parent=prev.parent;
if(prev.parent!=null)
{
prev.parent.lchild=p;
}
p.rchild=prev;
prev.lchild=null;
if(prev==root)
root=p;
prev.bf=0;
p.bf=0;
}
public void rr_rotate()
{
p.parent=prev.parent;

if(prev.parent!=null)
{
prev.parent.rchild=p;
}

p.lchild=prev;
prev.rchild=null;
if(prev==root)
root=p;
prev.bf=0;
p.bf=0;
}
public void lr_rotate()
{
avlnode t=p.rchild;
if(t.lchild!=null)
p.rchild=t.lchild;
else
p.rchild=null;
if(t.rchild!=null)
prev.lchild=t.rchild;
else
prev.lchild=null;
if(prev.parent!=null)
{
t.parent=prev.parent;
prev.parent.lchild=t;
}
else
{
t.parent=null;
root=t;
}
t.rchild=prev;
t.lchild=p;
t.bf=0;
p.bf=0;
prev.bf=0;
}
public void rl_rotate()
{
avlnode t=p.lchild;
if(t.rchild!=null)
p.lchild=t.rchild;
else
p.lchild=null;
if(t.lchild!=null)
prev.rchild=t.lchild;
else
prev.rchild=null;
if(prev.parent!=null)
{
t.parent=prev.parent;
prev.parent.rchild=t;
}
else
{
t.parent=null;
root=t;
}
t.lchild=prev;
t.rchild=p;
t.bf=0;
p.bf=0;
prev.bf=0;
}
public void dispre(avlnode currentnode)
{
if(currentnode!=null)
{
dispre(currentnode.lchild);
System.out.println(currentnode.data+" bf:"+currentnode.bf);
dispre(currentnode.rchild);
}
}
}
class avl_main
{
public static void main(String[] args)throws IOException
{
System.out.println("AVL Tree");
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter your choice");
avl tree=new avl();
int ans;
do
{
System.out.println("Enter a data");
int val=Integer.parseInt(in.readLine());
tree.insert(val);
System.out.println("Want to continue press 1");
ans=Integer.parseInt(in.readLine());
}while(ans==1);
}
}