Ashrith
BAN USER#include <stdio.h>
#include <malloc.h>
struct node {
struct node *left;
int data;
struct node *right;
};
int countwtf =0;
struct node * pop();
void push(struct node *);
void levelorder(struct node *);
void insert(struct node **,int);
struct node *queue[30];
top = -1;
void main()
{
struct node *root=NULL;
insert(&root,10);
insert(&root,6);
insert(&root,13);
insert(&root,2);
insert(&root,12);
insert(&root,5);
insert(&root,14);
insert(&root,1);
insert(&root,8);
levelorder(root);
}
void levelorder(struct node *root)
{ int height=0;
push(root);
while(top!=-1)//empty queue impies no nodes at the level
{ int nodeatlevel = top+1 ;
/* no of nodes at current level */
//this while loop prints all nodes in current level
while(nodeatlevel>0)
{root=pop();
printf("%d\t",root->data);
/* we push nodes into queue to visit them in next level*/
push(root->left);
push(root->right) ;
nodeatlevel --;
}
height++; /*after all nodes of level are printed we increment its height */
printf("\n");
}
printf("height is %d",height);
}
struct node * pop()
{struct node *temp = queue[0];
int i=1;
while(i<=top)
{
queue[i-1]=queue[i]; i++;
}top--;return temp;
};
void push(struct node *p)
{
if(p!=NULL)
queue[++top]=p;
}
//inserts nodes in a BST
void insert(struct node **rt,int num)
{
if(*rt == NULL)
{
*rt = (struct node *)malloc(sizeof(struct node));
(*rt)->data = num;
(*rt)->left=NULL;
(*rt)->right = NULL;
return;}
if(num<((*rt)->data))
insert(&((*rt)->left),num);
if(num>((*rt)->data))
insert(&((*rt)->right),num);
}
you should return 1+ greater_of( Binary_Tree_height(node->left)
- Ashrith June 20, 2014, Binary_Tree_height(node->right) )