#include "stdio.h"
#include "string.h"
#include "BiTNode.h"
Establishing two-pronged tree in the first order
Bitree procreate (Bitree T)
{
Char ch;
scanf ("%c", &ch);
if (ch = = ' # ')
{
T = NULL;
return NULL;
}
Else
{
T = (Bitnode *) malloc (sizeof (Bitnode));
if (! T
{
printf ("error!");
return NULL;
}
T->data = ch;
T->lchild = Procreate (T->lchild);
T->rchild = Procreate (T->rchild);
}
return T;
}
First-order traversal of binary tree
void preorder (Bitree T)
{
if (T)
{
printf ("%c", t->data);
Preorder (T->lchild);
Preorder (T->rchild);
}
}
Middle sequence Traversal binary tree
void Inorder (Bitree T)
{
if (T)
{
Inorder (T->lchild);
printf ("%c", t->data);
Inorder (T->rchild);
}
}
Sequential traversal of binary tree
void Postorder (Bitree T)
{
if (T)
{
Postorder (T->lchild);
Postorder (T->rchild);
printf ("%c", t->data);
}
}
Find the number of leaf nodes
int Sumleaf (Bitree T)
{
int sum = 0, M, N;
if (T)
{
if ((! T->lchild) && (! T->rchild))
sum + +;
m = Sumleaf (T->lchild);
sum + = m;
n = sumleaf (t->rchild);
sum + = n;
}
return sum;
}
Finding the depth of a binary tree
int Depth (Bitree T)
{
int dep = 0, DEPL, Depr;
if (! T) dep = 0;
Else
{
Depl=depth (T->lchild);
Depr=depth (T->rchild);
dep=1+ (DEPL>DEPR?DEPL:DEPR);
}
return DEP;
}
int main ()
{
Bitree T;
int sum, DEP;
printf ("Order to create a two-fork tree, please enter the sequence, empty use # instead of \ n");
t = Procreate (t);
printf ("Result of the first Order traversal:");
Preorder (T);
printf ("\ n the result of the sequence Traversal:");
Inorder (T);
printf ("\ nthe result of post-sequential traversal:");
Postorder (T);
printf ("\ n number of Leaves:");
Sum=sumleaf (T);
printf ("%d", sum);
Dep=depth (T);
printf ("\ n Depth:%d \ n", DEP);
return 0;
}
BiTNode.h's Content
#ifndef bitnode_h_included
#define Bitnode_h_included
#define Elemtype Char
Defining Data structures
typedef struct BITNODE
{
Elemtype data;
struct Bitnode *lchild, *rchild;
}bitnode,*bitree;
#endif//bitnode_h_included
Establishment of Binary tree