#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define NULL 0
#define MAXSIZE 30
typedef struct BITNODE//define binary tree data structure
{
char data;
struct Bitnode *lchild,*rchild;
} Bitnode;
void PreCreate (Bitnode *& T)//First order traversal builds two-fork tree, #代表空树
{
Char ch;
Ch=getchar ();
if (ch== ' # ')
T=null;
Else
{
if (! ( t= (Bitnode *) malloc (sizeof (Bitnode))))
printf ("error!");
t->data=ch;
PreCreate (T->lchild);
PreCreate (T->rchild);
}
}
int Getleafnum (Bitnode *root)//Statistics binary Tree leaf node number
{
int count=0;//The total number of leaves, number of Yeko. Number of leaves in right sub-number
int left_count=0;
int right_count=0;
/* Determine if the root node is null
If the root node is not empty, determine if the root node is a leaf, then the total number of leaves +1 and return,
The number of leaves and the right number of leaves of the left subtree are not counted and added back
If the root node is empty, the number of leaves is 0 and returns
*/
if (root)
{
if (root->lchild==null&&root->rchild==null)
count++;
Else
{
Left_count=getleafnum (Root->lchild);
Right_count=getleafnum (Root->rchild);
Count=left_count+right_count;
}
}
Else
{
count=0;
}
return count;
}
int gettreedepth (Bitnode *root)//Statistical binary tree depth
{
int depth=0;
int left_depth=0;
int right_depth=0;
/*
Determines whether the root node is empty,
If the root node is empty, the depth is set to 0 and returns
If the root node is not empty, the left subtree depth is counted, the right subtree depth is counted, the two are added together with 1 (1-bit root node) and returned
*/
if (root)
{
Left_depth=gettreedepth (Root->lchild);
Right_depth=gettreedepth (Root->rchild);
depth=1+ (left_depth>right_depth?left_depth:right_depth);
}
Else
{
depth=0;
}
return depth;
}
int main ()
{
Bitnode * BITREE=NULL;
PreCreate (Bitree);//First order traversal create two fork tree
printf ("Number of Leaves:%d\n", Getleafnum (Bitree));
printf ("The two Fork Tree depth:%d\n", Gettreedepth (Bitree));
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sequence traversal creates a two-fork tree, counting the number of leaf nodes and the statistical depth of the two-tree (creating a two-fork tree # represents an empty tree, the sequence cannot be mistaken)