第4章第1节练习题13 根据先序序列和中序序列建立二叉树

问题描述

假设一颗二叉树中各节点的值互不相同,其先序遍历序列和中序遍历序列分别存于两个一维数组 A[] B[] 中,试编写算法建立该二叉树的二叉链表

算法思想

由先序和中序序列可以唯一确定一颗二叉树,算法的实现步骤如下:

  1. 根据先序序列确定树的根节点;
  2. 根据根节点在中序序列中划分出二叉树的左,右子树包含哪些节点;
  3. 根绝左右子树在先序序列中的相对次序确定子树的根节点,回到步骤1

其基本过程如下图所示:

二叉树过程

算法描述

BiTNode* InJudge(ElemType* A,ElemType* B,int l1,int h1,int l2,int h2)
{
    int i;
    int llen;
    int rlen;
    BiTNode* T;
    T=(BiTNode*)malloc(sizeof(BiTNode));
    T->data=A[l1];
    for(i=l2;B[i]!=T->data;i++);
    llen=i-l2;
    rlen=h2-i;
    if(llen!=0){
        T->lchild=InJudge(A,B,l1+1,l1+llen,l2,l2+llen-1);
    }else{
        T->lchild=NULL;
    }
    if(rlen!=0){
        T->rchild=InJudge(A,B,h1-rlen+1,h1,h2-rlen+1,h2);
    }else{
        T->rchild=NULL;
    }
    return T;
}

具体代码见附件。


附件

#include<stdio.h>
#include<stdlib.h>

#define MaxSize 10
typedef char ElemType;

typedef struct BiTNode{
    ElemType data;
    struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;

typedef struct{
    BiTNode* data[MaxSize];
    int front,rear;
}SqQueue;

//---------------------------------------------------------------

void InitQueue(SqQueue*);
void EnQueue(SqQueue*,BiTNode*);
BiTNode* DeQueue(SqQueue*);
int IsEmptyQueue(SqQueue*);

BiTNode* CreateBiTree(BiTNode*);
BiTNode* InJudge(ElemType*,ElemType*,int,int,int,int);
void LevelOrder(BiTNode*);

//---------------------------------------------------------------

int main(int argc,char* argv[])
{
    BiTNode* T;
    ElemType A[MaxSize]={'A','B','C','D','E','F','G','H','I'};
    ElemType B[MaxSize]={'B','C','A','E','D','G','H','F','I'};
    T=InJudge(A,B,0,8,0,8);
    LevelOrder(T);
    printf("\n");
    return 0;
}

//---------------------------------------------------------------

BiTNode* InJudge(ElemType* A,ElemType* B,int l1,int h1,int l2,int h2)
{
    int i;
    int llen;
    int rlen;
    BiTNode* T;
    T=(BiTNode*)malloc(sizeof(BiTNode));
    T->data=A[l1];
    for(i=l2;B[i]!=T->data;i++);
    llen=i-l2;
    rlen=h2-i;
    if(llen!=0){
        T->lchild=InJudge(A,B,l1+1,l1+llen,l2,l2+llen-1);
    }else{
        T->lchild=NULL;
    }
    if(rlen!=0){
        T->rchild=InJudge(A,B,h1-rlen+1,h1,h2-rlen+1,h2);
    }else{
        T->rchild=NULL;
    }
    return T;
}

void LevelOrder(BiTNode* T)
{
    BiTNode *p=T;
    SqQueue Q;
    InitQueue(&Q);
    EnQueue(&Q,p);
    while(IsEmptyQueue(&Q)!=0){
        p=DeQueue(&Q);
        printf("%c",p->data);
        if(p->lchild!=NULL){
            EnQueue(&Q,p->lchild);
        }
        if(p->rchild!=NULL){
            EnQueue(&Q,p->rchild);
        }
    }
}

//---------------------------------------------------------------

void InitQueue(SqQueue* Q)
{
    Q->front=0;
    Q->rear=0;
}

void EnQueue(SqQueue* Q,BiTNode* T)
{
    if((Q->rear+1)%MaxSize==Q->front){
        return;
    }
    Q->data[Q->rear++]=T;
}

BiTNode* DeQueue(SqQueue* Q)
{
    if(Q->front==Q->rear){
        return NULL;
    }
    return Q->data[Q->front++];
}

int IsEmptyQueue(SqQueue* Q)
{
    if(Q->front==Q->rear){
        return 0;
    }
    return -1;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值