83.png

    1. /*
    2. 设一个二叉树各节点的值互不相同,其先序遍历序列和中序遍历序列分别存于两个一维数组A、B中,试编写算法建立该二叉树的二叉链表
    3. 分析:
    4. 这是一个典型的已知中序和先序求二叉树的案例,具体实现步骤如下:
    5. 1、先根据先序序列确定树的根节点
    6. 2、根据根节点在中序在中序序列中划分出二叉树的左右子树包含哪些节点,然后根据左右子树节点在先序序列中的次序确定子树的
    7. 的根节点,即回到步骤一。
    8. 如此重复,直到每颗子树仅有一个节点为止
    9. */
    10. struct biTree {
    11. char data;
    12. struct biTree *lchild;
    13. struct biTree *rchild;
    14. };
    15. #include <stdio.h>
    16. #include <stdlib.h>
    17. biTree *preInCreate(char *arrIn,char *arrPre,int l1,int h1,int l2,int h2) {
    18. //l1 h1 为中序的第一和最后一个节点下标,l2 h2 为先序的第一和最后一个节点下标
    19. int llen, rlen,i;//左子树、右子树长度
    20. struct biTree *root = (struct biTree *)malloc(sizeof(struct biTree));
    21. root->data = *(arrPre + l2);
    22. for (i = l1; *(arrIn + i) != root->data; i++);//找到根节点在中序序列的位置
    23. llen = i - l1;//记录左边节点个数
    24. rlen = h1 - i;//记录根节点右边节点个数
    25. if (llen) {
    26. root->lchild = preInCreate(arrIn,arrPre,l1,l1+llen-1,l2+1,l2+llen);//把左边的序列有看做一个新的继续找根节点
    27. }
    28. else {
    29. root->lchild = NULL;
    30. }
    31. if (rlen) {
    32. root->rchild = preInCreate(arrIn, arrPre, h1-llen+1, h1, h2-llen+1, h2);//把右边的序列有看做一个新的继续找根节点
    33. }
    34. else {
    35. root->rchild = NULL;
    36. }
    37. return root;
    38. }
    39. int main() {
    40. char arrIn[] = { 'D','B','E','A','F','C','G' },
    41. arrPre[] = {'A','B','D','E','C','F','G'};
    42. struct biTree *root;
    43. void inOrder(biTree *);
    44. void preOrder(biTree *);
    45. root = preInCreate(arrIn,arrPre,0,6,0,6);
    46. inOrder(root);
    47. printf("\n");
    48. preOrder(root);
    49. return 0;
    50. }