Skip to content

Instantly share code, notes, and snippets.

@mrprofessor
Created October 14, 2016 12:08
Show Gist options
  • Save mrprofessor/e8c15fc147e9bebeb30b10b8b4ee65ca to your computer and use it in GitHub Desktop.
Save mrprofessor/e8c15fc147e9bebeb30b10b8b4ee65ca to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct list
{
int num;
struct list *next;
};
int count=0;
addNode(int n,struct list **head)
{
struct list *temp;
struct list *newnode=(struct list*)malloc(sizeof(struct list));
newnode->num=n;
newnode->next=NULL;
if(*head==NULL)
{
*head=newnode;
}
else
{
temp=*head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
display(struct list **head)
{
struct list *temp=*head;
while(temp!=NULL)
{
printf("%d ",temp->num);
temp=temp->next;
}
}
binaryTree(struct node **root,struct list **head,int index,int size)
{
int i;
struct list *temp1;
if(index<size)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
temp1=*head;
for(i=0;i<index;i++)
{
temp1=temp1->next;
}
temp->data=temp1->num;
*root=temp;
// printf("%d ",temp1->num);
binaryTree(&(temp->left),head,2*index+1,size);
binaryTree(&(temp->right),head,2*index+2,size);
}
}
displayTree(struct node **root)
{
struct node *temp=*root;
if(temp!=NULL)
{
printf("\n");
printf("%d ",temp->data);
if(temp->left==NULL)
printf("(N,");
else
printf("(%d,",temp->left->data);
if(temp->right==NULL)
printf("N)");
else
printf("%d)",temp->right->data);
displayTree(&(temp->left));
displayTree(&(temp->right));
}
}
int FindHeight(struct node **root)
{
struct node *temp=*root;
if(temp==NULL)
return -1;
temp=*root;
return max(FindHeight(&temp->left),FindHeight(&temp->right))+1;
}
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
FindHeightNode(struct node **root,int x)
{
int height;
struct node *temp=*root;
if(temp!=NULL)
{
if(temp->data==x)
{
height=FindHeight(&temp);
printf("\n\tHeight of node %d :%d\n",x,height);
}
FindHeightNode(&(temp->left),x);
FindHeightNode(&(temp->right),x);
}
}
FindDepthNode(struct node **root,int currentLevel,int x)
{
int d;
if(*root==NULL)
return;
struct node *temp=*root;
if(temp!=NULL)
{
if(temp->data==x)
{
printf("\n\tDepth of %d is %d\n",x,currentLevel);
}
FindDepthNode(&(temp->left),currentLevel+1,x);
FindDepthNode(&(temp->right),currentLevel+1,x);
}
}
main(int argc,char* argv[])
{
struct list *head=NULL;
struct node *root=NULL;
FILE *fp;
if(argv[1]==NULL)
{
printf("\n\n\tAdd file name\n");
return ;
}
fp=fopen(argv[1],"r");
int size=0,n;
while((fscanf(fp,"%d",&n))!=EOF)
{
addNode(n,&head);
size++;
}
printf("\n\n\tList contains\n");
display(&head);
printf("\n");
binaryTree(&root,&head,0,size);
printf("\n\tBinary tree is represented below\n");
displayTree(&root);
int height;
height=FindHeight(&root);
printf("\n\tHeight of the tree: %d\n",height);
int number,h,x;
printf("\n\tEnter the number whose height is to be known:");
scanf("%d",&number);
FindHeightNode(&root,number);
printf("\n\tEnter the number whose depth is to be known :\n");
scanf("%d",&x);
FindDepthNode(&root,0,x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment