Feb 8, 2013 at 2:40am UTC
Some errors and warnings occur during the run!!!
Find the errors and fix them!!!!!
pls!
Feb 8, 2013 at 1:46pm UTC
Wouldn't it be better if you actually copy and pasted the errors here?
(damn... thought OP and the second poster was the same guy...)
Last edited on Feb 8, 2013 at 1:48pm UTC
Mar 27, 2013 at 11:33am UTC
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data,height;
node *left,*right;
};
typedef node *nodeptr;
class aj
{
struct node1
{
node *d;
node1 *link;
}*front,*rear;
int i,j;
public:
void enque(nodeptr );
void deque();
void bfs(nodeptr );
aj();
void add(int ,nodeptr &);
int bh(nodeptr );
int max(int ,int );
nodeptr rr(nodeptr );
nodeptr lr(nodeptr );
nodeptr lrr(nodeptr );
nodeptr rlr(nodeptr );
void del(int x,nodeptr &);
};
void aj::del(int x,nodeptr &p)
{
nodeptr d;
if(p==NULL)
cout<<"\nElement not found";
else if(x<p->data)
{
del(x,p->left);
if(bh(p->right)-bh(p->left)==2)
{
if(bh(p->right->right)>=bh(p->right->left))//RR
p=lr(p);
else//RL
p=rlr(p);
}
}
else if(x>p->data)
{
del(x,p->right);
if(bh(p->left)-bh(p->right)==2)
{
if(bh(p->left->left)>=bh(p->left->right))//LL
p=rr(p);
else//LR
p=lrr(p);
}
}
else if(p->left==NULL&&p->right==NULL)
{
d=p;
p=NULL;
delete d;
}
else if(p->left==NULL)
{
d=p;
p=p->right;
delete d;
}
else if(p->right==NULL)
{
d=p;
p=p->left;
delete d;
}
else
{
nodeptr t=p->right,q=p;
while(t->left!=NULL)//inorder successor evaluation
t=t->left;
int k=t->data;
del(k,p);
q->data=k;
}
if(p!=NULL)
{
int d1=max(bh(p->left),bh(p->right));
p->height=d1+1;
}
}
aj::aj()
{
front=NULL;
}
void aj::enque(nodeptr p)
{
node1 *temp=new node1;
temp->d=p;
temp->link=NULL;
if(front==NULL)
front=rear=temp;
else
{
rear->link=temp;
rear=temp;
}
}
void aj::deque()
{
node1 *temp=front;
front=front->link;
delete temp;
}
void aj::bfs(nodeptr p)
{
enque(p);
while(front!=NULL)
{
p=front->d;
deque();
if(p->left!=NULL)
enque(p->left);
if(p->right!=NULL)
enque(p->right);
cout<<endl<<p->data;
}
}
nodeptr aj::rr(nodeptr p)//for LL
{
nodeptr q=p->left;
p->left=q->right;
q->right=p;
p->height=max(bh(p->left),bh(p->right))+1;
q->height=max(bh(q->left),p->height)+1;
return q;
}
nodeptr aj::lrr(nodeptr p)//for LR
{
p->left=lr(p->left);
return rr(p);
}
nodeptr aj::lr(nodeptr p)//for RR
{
nodeptr q=p->right;
p->right=q->left;
q->left=p;
p->height=max(bh(p->left),bh(p->right))+1;
q->height=max(p->height,bh(q->right))+1;
return q;
}
nodeptr aj::rlr(nodeptr p)//for RL
{
p->right=rr(p->right);
return lr(p);
}
void aj::add(int x,nodeptr &p)
{
if(p==NULL)
{
p=new node;
p->data=x;
p->left=p->right=NULL;
}
else
{
if(x<p->data)
{
add(x,p->left);
if((bh(p->left))-(bh(p->right))==2)
{
if(x<p->left->data)//LL
p=rr(p);
else//LR
p=lrr(p);
}
}
else if(x>p->data)
{
add(x,p->right);
if((bh(p->right))-(bh(p->left))==2)
{
if(x>p->right->data)//RR
p=lr(p);
else//RL
p=rlr(p);
}
}
else
cout<<"\nData exist";
}
int m,n,d;
m=bh(p->left);
n=bh(p->right);
d=max(m,n);
p->height=d+1;
}
int aj::bh(nodeptr p)
{
int t;
if(p==NULL)
return -1;
else
{
t=p->height;
return t;
}
}
int aj::max(int v1,int v2)
{
return((v1>v2)?v1:v2);
}
int main()
{
int d;
nodeptr root=NULL;
aj a;
a.add(65,root);
a.add(85,root);
a.add(45,root);
a.add(55,root);
a.add(35,root);
a.add(95,root);
a.add(60,root);
a.add(80,root);
a.add(105,root);
cout<<"\nBfs trav::\n";
a.bfs(root);
cout<<"\nEnter data to delete::";
cin>>d;
a.del(d,root);
cout<<"\nBfs trav after deletion";
a.bfs(root);
system("pause");
return 0;
}
Trry this code....
Mar 28, 2013 at 10:31am UTC
//Another way of deletion in avl
//its easier to understand
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data,height;
node *left,*right;
};
typedef node *nodeptr;
class aj
{
struct node1
{
node *d;
node1 *link;
}*front,*rear;
int i,j;
public:
void enque(nodeptr );
void deque();
void bfs(nodeptr );
aj();
void add(int ,nodeptr &);
int bh(nodeptr );
int max(int ,int );
void inorder(nodeptr );
nodeptr rr(nodeptr );
nodeptr lr(nodeptr );
nodeptr lrr(nodeptr );
nodeptr rlr(nodeptr );
void del(int x,nodeptr &);
int delmin(nodeptr &);
void setheight(nodeptr &);
void check(nodeptr &);
void set(nodeptr &);
};
void aj::setheight(nodeptr &p)
{
if(p!=NULL)
{
setheight(p->left);
setheight(p->right);
int m,n,d;
m=bh(p->left);
n=bh(p->right);
d=max(m,n);
p->height=d+1;
}
}
void aj::check(nodeptr &p)
{
if(p!=NULL)
{
if(bh(p->left)-bh(p->right)==2||bh(p->right)-bh(p->left)==2)
set(p);
check(p->left);
check(p->right);
}
}
void aj::set(nodeptr &p)
{
if(bh(p->left)-bh(p->right)==2)
{
if(bh(p->left->left)>=bh(p->left->right))//LL
p=rr(p);
else//LR
p=lrr(p);
}
if(bh(p->right)-bh(p->left)==2)
{
if(bh(p->right->right)>=bh(p->right->left))//RR
p=lr(p);
else//RL
p=rlr(p);
}
}
void aj::del(int x,nodeptr &p)
{
nodeptr d;
if(p==NULL)
cout<<"\nElement not found";
else if(x<p->data)
del(x,p->left);
else if(x>p->data)
del(x,p->right);
else if(p->left==NULL&&p->right==NULL)
{
d=p;
p=NULL;
delete d;
}
else if(p->left==NULL)
{
d=p;
p=p->right;
delete d;
}
else if(p->right==NULL)
{
d=p;
p=p->left;
delete d;
}
else
p->data=delmin(p->right);
}
int aj::delmin(nodeptr &p)
{
int d;
nodeptr d1;
if(p->left==NULL)
{
d=p->data;
d1=p;
p=p->right;
delete d1;
}
else
{
d=delmin(p->left);
return d;
}
}
aj::aj()
{
front=NULL;
}
void aj::enque(nodeptr p)
{
node1 *temp=new node1;
temp->d=p;
temp->link=NULL;
if(front==NULL)
front=rear=temp;
else
{
rear->link=temp;
rear=temp;
}
}
void aj::deque()
{
node1 *temp=front;
front=front->link;
delete temp;
}
void aj::bfs(nodeptr p)
{
enque(p);
while(front!=NULL)
{
p=front->d;
deque();
if(p->left!=NULL)
enque(p->left);
if(p->right!=NULL)
enque(p->right);
cout<<endl<<p->data;
}
}
nodeptr aj::rr(nodeptr p)//for LL
{
nodeptr q=p->left;
p->left=q->right;
q->right=p;
p->height=max(bh(p->left),bh(p->right))+1;
q->height=max(bh(q->left),p->height)+1;
return q;
}
nodeptr aj::lrr(nodeptr p)//for LR
{
p->left=lr(p->left);
return rr(p);
}
nodeptr aj::lr(nodeptr p)//for RR
{
nodeptr q=p->right;
p->right=q->left;
q->left=p;
p->height=max(bh(p->left),bh(p->right))+1;
q->height=max(p->height,bh(q->right))+1;
return q;
}
nodeptr aj::rlr(nodeptr p)//for RL
{
p->right=rr(p->right);
return lr(p);
}
void aj::add(int x,nodeptr &p)
{
if(p==NULL)
{
p=new node;
p->data=x;
p->left=p->right=NULL;
}
else
{
if(x<p->data)
{
add(x,p->left);
if((bh(p->left))-(bh(p->right))==2)
{
if(x<p->left->data)//LL
p=rr(p);
else//LR
p=lrr(p);
}
}
else if(x>p->data)
{
add(x,p->right);
if((bh(p->right))-(bh(p->left))==2)
{
if(x>p->right->data)//RR
p=lr(p);
else//RL
p=rlr(p);
}
}
else
cout<<"\nData exist";
}
int m,n,d;
m=bh(p->left);
n=bh(p->right);
d=max(m,n);
p->height=d+1;
}
int aj::bh(nodeptr p)
{
int t;
if(p==NULL)
return -1;
else
{
t=p->height;
return t;
}
}
int aj::max(int v1,int v2)
{
return((v1>v2)?v1:v2);
}
void aj::inorder(nodeptr p)
{
if(p!=NULL)
{
inorder(p->left);
cout<<endl<<p->data;
inorder(p->right);
}
}
int main()
{
int d;
nodeptr root=NULL;
aj a;
a.add(65,root);
a.add(85,root);
a.add(45,root);
a.add(55,root);
a.add(35,root);
a.add(95,root);
a.add(60,root);
cout<<"\nBfs trav::\n";
a.bfs(root);
cout<<"\nEnter data to delete::";
cin>>d;
a.del(d,root);
cout<<"\nBfs trav after deletion";
a.bfs(root);
cout<<"\nBfs trav after balancing tree";
a.setheight(root);
a.check(root);
a.bfs(root);
system("pause");
return 0;
}