1. Finding the number of nodes in a binary tree
2. Find the number of leaf nodes of binary tree
3. Find the depth of the binary tree
4. Find the number of nodes in the K-tier of the binary tree
#include <stdio.h> #include <stdlib.h> #define ElementType char typedef struct NODE {ElementType data;
struct Node *lchild;
struct Node *rchild;
}binarytree;
Create a two-fork tree node binarytree* createbinarytree (data) {binarytree* T = (binarytree*) malloc (sizeof (BinaryTree)); if (!t) {printf ("insufficient space.")
\ n ");
return NULL;
} t->lchild = NULL;
T->rchild = NULL;
T->data = data;
return t; }/* To find a recursive solution to the number of nodes in a binary tree: (1) If the binary tree is empty, the number of nodes is 0 (2) If the binary tree is not empty, the number of binary tree nodes = number of left subtree nodes + right subtree node number + 1 */int getnodenum (binarytree* BT)
{if (BT = = NULL) return 0; int leftnum = Getnodenum (bt->lchild); Left Dial hand tree node number int rightnum = Getnodenum (bt->rchild);
Number of right subtree nodes return (leftnum + rightnum + 1); }/* To find the recursive solution of the number of binary tree leaf nodes: (1) If the binary tree is empty, return 0 (2) If the binary tree is not empty and the left and right subtrees are empty, return 1 (3) If the binary tree is not empty, and the left and left subtrees are not both empty, return the number of leaf nodes plus the number of leaf nodes/int G in the subtree.
Etleafnodenum (binarytree* bt) {if (BT = = NULL) return 0; if (Bt->lchild = = NULL && BT->RCHild = = NULL) return 1; int leftnum = Getleafnodenum (bt->lchild); Record number of leaf nodes int rightnum = Getleafnodenum (bt->rchild);
The number of leaf nodes in the right subtree return (Leftnum + rightnum);
}/* To find the depth recursive solution of the Binary tree: (1) If the binary tree is empty, the binary tree depth is 0 (2) If the binary tree is not empty, the binary tree depth = max (left subtree depth, right subtree depth) + 1 */int getdepth (binarytree* BT) {
if (BT = = NULL) return 0; int leftdepth = getdepth (bt->lchild); Left dial hand tree depth int rightdepth = getdepth (bt->rchild); Right subtree depth int maxDepth = (leftdepth > rightdepth)?
leftdepth:rightdepth;
Return (maxDepth + 1);
}/* The number of nodes in the K-layer of the binary tree is recursive solution: (1) If the binary tree is empty or k<1 returns 0 (2) If the binary tree is not empty and k==1, returns 1 (3) If the binary tree is not empty and k>1, the number of nodes that return record k-1 layer and the number of nodes in the right subtree k-1
*/int Getnodenumkthlevel (binarytree* bt, int k) {if (BT = = NULL | | K < 1) return 0;
if (k = = 1) return 1;
int leftnum = Getnodenumkthlevel (Bt->lchild, k-1);
int rightnum = Getnodenumkthlevel (Bt->rchild, k-1);
Return (Leftnum + rightnum); } int main (int argc, const char * argv[]) {binarytree* tree_a = createbinarytree (' A ');
binarytree* tree_b = Createbinarytree (' B ');
Tree_a->lchild = Tree_b;
binarytree* Tree_c = Createbinarytree (' C ');
Tree_a->rchild = Tree_c;
binarytree* tree_d = Createbinarytree (' D ');
Tree_b->lchild = tree_d;
binarytree* tree_f = Createbinarytree (' F ');
Tree_b->rchild = Tree_f;
binarytree* tree_e = Createbinarytree (' E ');
Tree_f->lchild = tree_e;
binarytree* tree_g = Createbinarytree (' G ');
Tree_c->lchild = Tree_g;
binarytree* tree_i = Createbinarytree (' I ');
Tree_c->rchild = tree_i;
binarytree* tree_h = Createbinarytree (' H ');
Tree_g->rchild = Tree_h;
int num = Getnodenum (tree_a);
printf ("Number of binary tree nodes:%d", num);
int depth = getdepth (tree_a);
printf ("\ n two fork tree depth:%d", depth);
int k = 3;
int kthnum = Getnodenumkthlevel (tree_a, k);
printf ("\ n two-tree%d-tier nodes:%d", K, Kthnum); int leafnum = Getleafnodenum (tree_a);
printf ("\ n two tree leaf node number:%d", leafnum);
return 0; }