#include <stdio.h> #include <string.h> #include <string> #include <iostream> #include < algorithm>using namespace std;typedef struct node{char ch; struct node *ll; struct node *rr;} Binode, *bitree;struct node *creat_bitree (struct node *root, char *s1, char *s2, int n) {if (n<=0) return NULL; else {root=new binode; root->ch=s1[0]; int p = STRCHR (s2, s1[0])-S2; Find s1[0] elements in S2 root->ll= creat_bitree (root->ll, s1+1, S2, p); Root->rr= Creat_bitree (ROOT->RR, S1+1+p, S2+1+p, n-1-p); } return root;} void Post_order (Bitree p)//post-traversal {if (p) {Post_order (P->LL); Post_order (P->RR); printf ("%c", p->ch); }}void In_order (Bitree p)//middle order Traversal {if (p) {Post_order (P->LL); printf ("%c", p->ch); Post_order (P->RR); }}void Pre_order (Bitree p)//first-order traversal {if (p) {printf ("%c", p->ch); Post_order (P->LL); Post_order (P->RR); }}int deep_bitree (Bitree p)//Find the depth of the binary tree {int deep1, DEEP2; if (P==null) return 0; else {deep1=deep_bitree (P->LL); Deep2=deep_bitree (P->RR); if (Deep1 > Deep2) return (deep1+1); else return (deep2+1); }}void leaf_num (bitree p, int *cnt)//calculates the number of leaf nodes {if (p==null) return; else {leaf_num (p->ll, CNT); if (p->ll==null && p->rr==null) {(*cnt) + +; } leaf_num (P->RR, CNT); }}int Main () {char s1[200]; Char s2[200]; scanf ("%s", S1); scanf ("%s", S2); int Len =strlen (S1); Bitree Root; Bitree p; root = Creat_bitree (p, s1, S2, Len),//root is the root node of the two-fork tree that has been built p=root; Post_order (P); The post-order traversal of printf ("\ n") starts from the root point; P=root; int Deep=deep_bitree (p); printf ("%d\n", deep); int a=0; int *sum; sum=&a; P=root; Leaf_num (P, sum); printf ("%d\n", *sum); return 0;}
Basic application of binary tree (achievement + number of leaves + depth + traversal)