Basic application of binary tree (achievement + number of leaves + depth + traversal)

Source: Internet
Author: User

#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-&GT;RR, S1+1+p, S2+1+p, n-1-p); } return root;}        void Post_order (Bitree p)//post-traversal {if (p) {Post_order (P-&GT;LL);        Post_order (P-&GT;RR);    printf ("%c", p->ch);        }}void In_order (Bitree p)//middle order Traversal {if (p) {Post_order (P-&GT;LL);        printf ("%c", p->ch);    Post_order (P-&GT;RR);        }}void Pre_order (Bitree p)//first-order traversal {if (p) {printf ("%c", p->ch);       Post_order (P-&GT;LL); Post_order (P-&GT;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-&GT;LL);        Deep2=deep_bitree (P-&GT;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-&GT;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)

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.