Counting of binary trees

Source: Internet
Author: User

The traversal of a binary tree is also often used to count two-fork trees.

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXSIZE typedef char ELEMTYPE  ;      typedef struct NODE {elemtype data;      struct Node *lchild;  struct Node *rchild;    }*bittree,bitnode; void CreateBitTree2 (Bittree *t,char str[]);//non-recursive creation of two-fork-tree void Destroybittree (Bittree *t);//Destroy binary tree int leafnum (bittree T);// Statistics the number of leaf nodes in a binary tree int notleafcount (Bittree t);//Statistics The number of non-leaf nodes in the binary tree int bittreedepth (Bittree t);//Calculate the depth of the binary tree #include "linkbitr      Ee.h "void CreateBitTree2 (Bittree *t,char str[])//non-recursive create two fork tree {char ch;      Bittree Stack[maxsize];      int top =-1;      int flag,k;      Bitnode *p;      *t = null,k = 0;      ch = str[k];              while (ch! = ') ' {switch (ch) {case ' (': stack[++top] = p;              flag = 1;          Break              Case ') ': top--;          Break              Case ', ': flag = 2;          Break Default:p = (BitTree) malloc (sizeof (Bitnode));              P->data = ch;              P->lchild = NULL;              P->rchild = NULL;              if (*t = = NULL) {*t = P;                      } else {switch (flag) {Case 1:                      Stack[top]->lchild = p;                  Break                      Case 2:stack[top]->rchild = p;                  Break      }}} ch = str[++k]; }} void Destroybittree (Bittree *t)//Destroy binary tree {if (*t) {if ((*t)->lchild) {De          Stroybittree (& (*t)->lchild);          } if ((*t)->rchild) {Destroybittree (& ((*t)->rchild));          } free (*t);      *t = NULL; }} int Leafnum (Bittree T)//Statistics The number of leaf nodes in the binary tree {if (!      T) {return 0; } else if (! T-> Lchild &&!      T->rchild) {return 1;      } else {return leafnum (t->lchild) +leafnum (t->rchild); }} int Notleafcount (Bittree T)//Count the number of non-leaf nodes in the binary tree {if (!      T) {return 0; } else if (! T->lchild &&!      T->rchild) {return 0;      } else {return Notleafcount (t->lchild) +notleafcount (t->rchild) +1;      }} int bittreedepth (Bittree t)//Calculate the depth of the binary tree {if (T = = NULL) {return 0; } else {return bittreedepth (t->lchild) > Bittreedepth (t->rchild)? 1+BITTREEDEP      Th (T->lchild): 1+bittreedepth (T->rchild);      }} #include "LinkBiTree.h" int main (void) {Bittree t,root;      int num,depth;      printf ("Build a two-fork tree based on parentheses: (A (b (c,d), E (f (, G), H (i))) \ n");      CreateBitTree2 (&t, "(A (b (c,d), E (f (, G), H (i)))");      num = Leafnum (T);      printf ("number of leaf nodes =%2d\n", num);  num = Notleafcount (T);    printf ("Number of non-leaf nodes =%2d\n", num);      depth = bittreedepth (T);      printf ("The depth of the binary tree =%2d\n", depth);      printf ("Build A two-fork tree based on parentheses: (A (B (D (, H (J)), E (, I (k,l)), C (f,g))) \ n");      CreateBitTree2 (&root, "(A (B (D (, H (J)), E (, I (k,l)), C (f,g)))");      num = Leafnum (root);      printf ("number of leaf nodes =%2d\n", num);      num = Notleafcount (root);      printf ("Number of non-leaf nodes =%2d\n", num);      Depth = bittreedepth (root);      printf ("The depth of the binary tree =%2d\n", depth);      Destroybittree (&t);      Destroybittree (&root);  return 0;   }
The results of the operation are as follows:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Counting of binary trees

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.