POJ 2255 Tree Recovery

    技术2024-11-10  19

    通过二叉树的前序和中序遍历结果,计算出后续遍历结果。

    思路为拆分成左子树和右子树进行递归。

    可以恢复出二叉树后再后续遍历,也可以在重建过程中进行输出,这里为了代码供以后参考,重建了二叉树,并在过程中打印输出。

     

     

    /*Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5033 Accepted: 3350 Description 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 / / F To 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! Input 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. Output For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root). Sample Input DBACEGF ABCDEFG BCAD CBAD Sample Output ACBFGED CDAB */ #include <stdio.h> #include <string.h> #define MAX_TREE_NODE_NUMBER 26 typedef struct _TREE_NODE_ST_ { int iLeftChild; int iRightChild; }TREE_NODE_ST; typedef struct _TREE_ST_ { int iNodeNum; TREE_NODE_ST astNode[MAX_TREE_NODE_NUMBER]; }TREE_ST; TREE_ST gstTree; char gacPreOrd[MAX_TREE_NODE_NUMBER+1]; char gacInOrd[MAX_TREE_NODE_NUMBER+1]; char gacPostord[MAX_TREE_NODE_NUMBER+1]; /*DBACEGF ABCDEFG ACBFGED D / / / / B E / / / / / / A C G / / F */ void SubTreeRecovery(int viRootIndexInPre,int viPreStartIndex,int viPreEndIndex,int viInStartIndex,int viInEndIndex) { int iLoopNodePre; int iLoopNodeIn; int iLoopNodeIn1; int iMinIndexInPre; int iMaxIndexInPre; /*get child*/ for (iLoopNodeIn = viInStartIndex; iLoopNodeIn <= viInEndIndex; iLoopNodeIn++) { if (gacPreOrd[viRootIndexInPre] == gacInOrd[iLoopNodeIn] ) { /*Left subtree*/ iMinIndexInPre = 0xFF; iMaxIndexInPre = -1; for (iLoopNodeIn1 = viInStartIndex; iLoopNodeIn1 < iLoopNodeIn; iLoopNodeIn1++) { for (iLoopNodePre = viPreStartIndex; iLoopNodePre <= viPreEndIndex; iLoopNodePre++) { if (gacInOrd[iLoopNodeIn1] == gacPreOrd[iLoopNodePre]) { if (iMinIndexInPre > iLoopNodePre) { iMinIndexInPre = iLoopNodePre; } if (iMaxIndexInPre < iLoopNodePre) { iMaxIndexInPre = iLoopNodePre; } } } } if (0xFF != iMinIndexInPre) { gstTree.astNode[gacPreOrd[viRootIndexInPre]-'A'].iLeftChild = gacPreOrd[iMinIndexInPre]-'A'; SubTreeRecovery(iMinIndexInPre,iMinIndexInPre,iMaxIndexInPre,viInStartIndex,iLoopNodeIn-1); printf("%c",gacPreOrd[iMinIndexInPre]); } /*Right subtree*/ iMinIndexInPre = 0xFF; iMaxIndexInPre = -1; for (iLoopNodeIn1 = iLoopNodeIn+1; iLoopNodeIn1 <= viInEndIndex; iLoopNodeIn1++) { for (iLoopNodePre = viPreStartIndex; iLoopNodePre <= viPreEndIndex; iLoopNodePre++) { if (gacInOrd[iLoopNodeIn1] == gacPreOrd[iLoopNodePre]) { if (iMinIndexInPre > iLoopNodePre) { iMinIndexInPre = iLoopNodePre; } if (iMaxIndexInPre < iLoopNodePre) { iMaxIndexInPre = iLoopNodePre; } } } } if (0xFF != iMinIndexInPre) { gstTree.astNode[gacPreOrd[viRootIndexInPre]-'A'].iRightChild = gacPreOrd[iMinIndexInPre]-'A'; SubTreeRecovery(iMinIndexInPre,iMinIndexInPre,iMaxIndexInPre,iLoopNodeIn+1,viInEndIndex); printf("%c",gacPreOrd[iMinIndexInPre]); } break; } } return; } void PostPrintTree(int viNodeIndex) { if (MAX_TREE_NODE_NUMBER > gstTree.astNode[viNodeIndex].iLeftChild) { PostPrintTree(gstTree.astNode[viNodeIndex].iLeftChild); } if (MAX_TREE_NODE_NUMBER > gstTree.astNode[viNodeIndex].iRightChild) { PostPrintTree(gstTree.astNode[viNodeIndex].iRightChild); } printf("%c",viNodeIndex+'A'); return; } int TreeRecoverymain(void) { char cRoot; while(EOF != scanf("%s",gacPreOrd )) { scanf("%s",gacInOrd); memset(&gstTree,0x7f,sizeof(TREE_ST)); cRoot = gacPreOrd[0]; gstTree.iNodeNum = strlen(gacPreOrd); SubTreeRecovery(0,0,gstTree.iNodeNum-1,0,gstTree.iNodeNum-1); printf("%c/n",cRoot); //PostPrintTree(cRoot-'A'); //printf("/n"); } return 0; }

    最新回复(0)