Tweet
# include<stdio.h>
# include<malloc.h>
typedef struct NODE
{
char Info;
struct NODE *Left_Child;
struct NODE *Right_Child;
}ND;
int depth = 0;
void Output ( ND*, int );
int Depth (ND *, int );
ND *Create_Tree (char , ND *);
void Output(ND *T, int Level)
{
int i;
if (T)
{
Output(T->Right_Child, Level+1);
printf("\n");
for (i = 0; i < Level; i++)
printf(" ");
printf("%c", T->Info);
Output(T->Left_Child, Level+1);
}
}
int Depth (ND *Node, int Level)
{
if (Node != NULL)
{
if (Level > depth)
depth = Level;
Depth (Node->Left_Child, Level + 1);
Depth (Node->Right_Child, Level + 1);
}
return (depth);
}
ND * Create_Tree (char Info, ND *Node)
{
if (Node == NULL)
{
Node = (ND *) malloc(sizeof(ND));
Node->Info = Info;
Node->Left_Child = NULL;
Node->Right_Child = NULL;
return (Node);
}
if (Info < Node->Info)
Node->Left_Child = Create_Tree (Info, Node->Left_Child);
else
if (Info > Node->Info)
Node->Right_Child = Create_Tree (Info, Node->Right_Child);
return(Node);
}
void main()
{
int Number = 0;
char Info ;
char choice;
int depth;
ND *T = (ND *) malloc(sizeof(ND));
T = NULL;
printf("\n Input choice 'b' to break:");
choice = getchar();
while(choice != 'b')
{
fflush(stdin);
printf("\n Input information of the node: ");
scanf("%c", &Info);
T = Create_Tree(Info, T);
Number++;
fflush(stdin);
printf("\n Input choice 'b' to break:");
choice = getchar();
}
printf("\n Number of elements in the list is %d", Number);
printf("\n Tree is \n");
Output(T, 1);
depth = Depth(T, 0);
printf("\n Depth of the above tree is: %d", depth);
}
How to find depth of a Binary Tree in C Programming ?
Posted by
LAHAUL SETH
~
How to find depth of a Binary Tree in C Programming ?
2012-02-26T21:00:00+05:30
LAHAUL SETH
Programming in C
|
Tree
|
Comments
How to find depth of a Binary Tree in C Programming ?
2012-02-26T21:00:00+05:30
LAHAUL SETH
Programming in C
|
Tree
|