#include<stdio.h>
#include<stdlib.h>
int * createCompleteBinaryTree(int n);
void printCompleteBinaryTree(int * tree, int n);
int isBST(int * tree, int n);
int main()
{
int n, * tree;
printf("Enter the number of elements in the tree\n");
scanf("%d",&n);
tree = createCompleteBinaryTree(n);
printCompleteBinaryTree(tree,n);
if(isBST(tree,n))
printf("The tree is a BST\n");
else
printf("The tree is not a BST\n");
return 0;
}
int * createCompleteBinaryTree(int n)
{
int i;
int * tree = (int *) malloc((n)*sizeof(int));
printf("Enter the elements in the tree\n");
for(i=0;i<n;i++)
{
scanf("%d",(tree+i));
}
return tree;
}
void printCompleteBinaryTree(int * tree, int n)
{
int i;
printf("The elements in the tree in level order are");
for(i=0;i<n;i++)
printf(" %d",*(tree+i));
printf("\n");
}
int isBST(int * tree, int n)
{
int i,j=1,sum=0,m=0;
m=n/2;
for(i=0;i<m;i++)
{
sum=*(tree+i);
if(sum<=*(tree+j))
{
if(j>=n)
break;
else
return 0;
}
if(sum>=*(tree+j+1))
{
if((j+1)>=n)
break;
else
return 0;
}
else{
if(j>=n)
break;
j=j+2;
}}
return 1;
}
No comments:
Post a Comment