Data structure experiment: Binary Tree descending traversal and hierarchical Traversal

Source: Internet
Author: User

Data structure experiment: Binary Tree descending traversal and hierarchical Traversal Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ This topic describes the Pre-and Middle-order traversal of a binary tree, and calculates the post-order traversal of the binary tree. There are multiple groups of input data. The first line is an integer T (t <1000), indicating that there are T groups of test data. Each group contains two strings with a length less than 50. The first string represents the first sequential traversal sequence of the binary tree, and the second string represents the central sequential traversal sequence of the binary tree. Output the post-sequential traversal sequence of the output binary tree in each group. The second row outputs the sample input of the hierarchical traversal sequence of the binary tree.
2abdegcfdbgeafcxnliulnixu
Sample output
dgebfcaabcdefglinuxxnuli
Message source ma6174
Sample program

These linked lists make you cry ..


#include <stdio.h>#include <string.h>#include <stdlib.h>struct node{    char data;    struct node *l,*r;};struct node *creat(int n,char *str1,char *str2)//二叉树的先序遍历的第一个节点是子树的根,中序遍历中根节点的左边全都是左子树的中序,右边是右子树的中序{    struct node *root;    char *p;    if(n==0)//当节点为零时表明数据全部进入二叉树;        return NULL;    root=(struct node *)malloc (sizeof(struct node));    root->data=str1[0];//先序的第一个为根节点    for(p=str2;p!='\0';p++)    {        if(*p==str1[0])//在中序中找到根节点,从而分为左右两个部分;            break;    }    int t=p-str2;//左子树节点的个数    root->l=creat(t,str1+1,str2);//递归创建左子树;    root->r=creat(n-t-1,str1+t+1,p+1);//递归创建右子树    return root;};void houxu(struct node *root){    if(root)    {        houxu(root->l);        houxu(root->r);        printf("%c",root->data);    }}void bianli(struct node *root){    int cnt=0;    int r1=1;    struct node *q[60];//指针控制    q[0]=root;    while(cnt<r1)//每一个q[cnt]都是子数树根;    {        if(q[cnt])//为二叉树所以最多有2个子树,且只有当二叉树不为空时才可能有数据,为空时即可跳过,不打印        {            printf("%c",q[cnt]->data);            q[r1++]=q[cnt]->l;            q[r1++]=q[cnt]->r;        }        cnt++;    }}int main(){    int n;    char str1[60],str2[60];    struct node *root;    root=(struct node *)malloc(sizeof(struct node));    scanf("%d",&n);    getchar();    while(n--)    {        scanf("%s %s",str1,str2);        int n=strlen(str1);        root=creat(n,str1,str2);        houxu(root);        printf("\n");        bianli(root);        printf("\n");    }    return 0;}

Zookeeper

Data structure experiment: Binary Tree descending traversal and hierarchical 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.