Monday, 21 March 2016

Write an Algorithm and a Program that accepts a Binary Tree as input and checks whether it is a Height Balanced Tree.

#include<stdio.h>
#include<alloc.h>
struct btree
{
int data;
struct btree*left;
struct btree*right;
};
void add(struct btree**q,int n)
{
struct bttree*t,*temp;
t=malloc(sizeof(struct btree));
t->data=n;
t->left = NULL;
t->right =NULL;
if(*q==NULL)
{
*q=t;

}
else
{
temp=*q;
while(temp->left!=NULL && temp->right != NULL)
{
if(temp->data<=n)
temp = temp->right;
else
temp=temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n==NULL) return 0;
return max(height(n->left),height(n->right))+1
}
int is balanced(node*n)
{
if(n == NULL) return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return is balanced(n->left) && isbalaced(n->right));
}
inorder(struct btree*s)
{
if(s!= NULL)
{
inoroder(s->left);
{
if(temp->data<=n)
temp=temp->right;
else
temp= temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n == NULL) return 0;
return max(height(n->left), height(n->right))+1;
}
int isBalanced(node*n)
{
if(n ==NULL)return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return isbalanced(n->NULL) && isBalanced(n->right);
}
inorder(struct*s)
{
if(s!=NULL)
{
inorder(-s>left);
add(&p,10);
add(&p,5);
add(&p,60);
add(&p,70);
printf("\n\n in order=\n");
inorder(p);
printf("\n\n npre order=\n");
preorder(p);
printf("\n\n npost order=\n");
postorder(p);

h=hight(root);
printf("Hight:%d",h);
bal=isBalanced(root);
if(bal==1)
printf("it is balanceed");
getch();
}

}
}




#include<stdio.h>
#include<alloc.h>
struct btree
{
int data;
struct btree*left;
struct btree*right;
};
void add(struct btree**q,int n)
{
struct bttree*t,*temp;
t=malloc(sizeof(struct btree));
t->data=n;
t->left = NULL;
t->right =NULL;
if(*q==NULL)
{
*q=t;

}
else
{
temp=*q;
while(temp->left!=NULL && temp->right != NULL)
{
if(temp->data<=n)
temp = temp->right;
else
temp=temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n==NULL) return 0;
return max(height(n->left),height(n->right))+1
}
int is balanced(node*n)
{
if(n == NULL) return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return is balanced(n->left) && isbalaced(n->right));
}
inorder(struct btree*s)
{
if(s!= NULL)
{
inoroder(s->left);
{
if(temp->data<=n)
temp=temp->right;
else
temp= temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n == NULL) return 0;
return max(height(n->left), height(n->right))+1;
}
int isBalanced(node*n)
{
if(n ==NULL)return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return isbalanced(n->NULL) && isBalanced(n->right);
}
inorder(struct*s)
{
if(s!=NULL)
{
inorder(-s>left);
add(&p,10);
add(&p,5);
add(&p,60);
add(&p,70);
printf("\n\n in order=\n");
inorder(p);
printf("\n\n npre order=\n");
preorder(p);
printf("\n\n npost order=\n");
postorder(p);

h=hight(root);
printf("Hight:%d",h);
bal=isBalanced(root);
if(bal==1)
printf("it is balanceed");
getch();
}

}
}





No comments:

Post a Comment