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)

Source: Internet
Author: User
Tags define null

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.