首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Tree 二叉树的建立 跟遍历

2013-01-26 
Tree二叉树的建立 和遍历http://acm.nyist.net/JudgeOnline/problem.php?pid221Tree时间限制:1000 ms |

Tree 二叉树的建立 和遍历

http://acm.nyist.net/JudgeOnline/problem.php?pid=221Tree时间限制:1000 ms | 内存限制:65535 KB难度:3描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.This is an example of one of her creations:                                                 D                                              / \                                             /   \                                            B     E                                           / \     \                                          /   \     \                                         A     C     G                                                    /                                                   /                                                  FTo record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.However, doing the reconstruction by hand, soon turned out to be tedious. So now she asks you to write a program that does the job for her! 输入The input will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)Input is terminated by end of file. 输出For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).样例输入DBACEGF ABCDEFGBCAD CBAD样例输出ACBFGEDCDAB题意:利用二叉树的先序遍历和中序遍历建立二叉树 并输出二叉树的后序遍历解析:建立二叉树首先建立节点类typedef struct node//定义结点{    char data;    struct node *lchild,*rchild;} Node,*BitTree;利用先序和中序建立树 可以用递归的方法每次找出先序遍历中各点在中序中的位置int search(char ino[],char c)//在中序序列中查找先序中该元素所在位置{    int i=0;    while(ino[i]!=c&&ino[i])  i++;    if(ino[i]==c)   return i;}如果在开始的位置则左子树为空 在最右边则右子树为空 在中间位置的话 则又分成两块 继续递归void CrtBT(BitTree &T,char pre[],char ino[],int ps,int is,int n)/*递归算法构造函数,建立二叉链表*/{    int k;    if(n==0)  T=NULL;    else    {        k=search(ino,pre[ps]);//找到在中序中的位置  分为两部分继续递归        T=(BitTree)malloc(sizeof(Node));        T->data=pre[ps];        if(k==is)     T->lchild=NULL;//如果中序中 字符左为空的话  左子树即为空        //先序前进一个  中序不变  字符程度为总长度 k减去  is  既 左边剩下的个数        else     CrtBT(T->lchild,pre,ino,ps+1,is,k-is);//        if(k==is+n-1)     T->rchild=NULL;        //同理        else     CrtBT(T->rchild,pre,ino,ps+1+(k-is),k+1,n-(k-is)-1);    }}后续遍历比较简单了void PostOrder(BitTree T){    if(T)    {        PostOrder(T->lchild);        PostOrder(T->rchild);        printf("%c",T->data);    }}整个代码为#include<stdio.h>#include<stdlib.h>#include<cstring>#define size 100typedef struct node//定义结点{    char data;    struct node *lchild,*rchild;} Node,*BitTree;int search(char ino[],char c)//在中序序列中查找先序中该元素所在位置{    int i=0;    while(ino[i]!=c&&ino[i])  i++;    if(ino[i]==c)   return i;}void CrtBT(BitTree &T,char pre[],char ino[],int ps,int is,int n)/*递归算法构造函数,建立二叉链表*/{    int k;    if(n==0)  T=NULL;    else    {        k=search(ino,pre[ps]);//找到在中序中的位置  分为两部分继续递归        T=(BitTree)malloc(sizeof(Node));        T->data=pre[ps];        if(k==is)     T->lchild=NULL;//如果中序中 字符左为空的话  左子树即为空        //先序前进一个  中序不变  字符程度为总长度 k减去  is  既 左边剩下的个数        else     CrtBT(T->lchild,pre,ino,ps+1,is,k-is);//        if(k==is+n-1)     T->rchild=NULL;        //同理        else     CrtBT(T->rchild,pre,ino,ps+1+(k-is),k+1,n-(k-is)-1);    }}void PostOrder(BitTree T){    if(T)    {        PostOrder(T->lchild);        PostOrder(T->rchild);        printf("%c",T->data);    }}int main(){    char pre[size],ino[size];    while(scanf("%s%s",pre,ino)!=EOF)    {        BitTree T=NULL;        CrtBT(T,pre,ino,0,0,strlen(pre));        PostOrder(T);        printf("\n");    }}


 

1楼mig_davidli前天 20:27
麻烦再编辑一下吧,html语句乱入代码了。。
Re: qq490691606昨天 14:41
回复mig_davidlin嗯 多谢提醒

热点排行