问题 K: DS_6.11 根结点到叶子结点的路径(by Yan)

问题 K: DS_6.11 根结点到叶子结点的路径(by Yan)
时间限制: 15 Sec 内存限制: 128 MB
提交: 19 解决: 16
[提交][状态][讨论版]
题目描述
从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。输出从根结点到每个叶子结点的路径。
输出的元素间不用间隔,都是英文字符,每个路径均占一行。
输入
输出
样例输入
AB#DG###CE##FH###
样例输出
G:ABD
E:AC
H:ACF`

#include<stdio.h> 
#include<stdlib.h>
#define DataType char
#define MAXSIZE 50
#define MaxSize 50
//


int count=0;//统计叶子结点个数 
int count1=0;//统计结点个数 
typedef struct Node{
    DataType data;
    struct Node *Lchild;
    struct Node *Rchild;
}BiTNode,*BiTree;

typedef struct{
    BiTree data[MAXSIZE];
    int rear,front;//队头队尾指针 

}SeQueue;

int EmptyQueue(SeQueue *sq){
    int m;
    if((m=(sq->rear-sq->front))==0) return 1;
    else return 0;
}
SeQueue *InitQueue(){
    SeQueue *sq;
    sq=(SeQueue*)malloc(sizeof(SeQueue));
    sq->front=sq->rear=0; 
    return sq;
}

int InQueue(SeQueue *sq,BiTree x){
    int m;
    if((m=(sq->rear-sq->front))==MAXSIZE)
       return 0;
    else{
        sq->data[sq->rear]=x;sq->rear++;

        return 1;
    }
}

int OutQueue(SeQueue *sq,BiTree *x){
    int m;
    if((m=(sq->rear-sq->front))==0)
        return 0;
    else{
        *x=sq->data[sq->front];sq->front++;
        return 1;     
}
}

void FrontQueue(SeQueue *sq,BiTree *x){
    if(!EmptyQueue(sq))
       *x=sq->data[sq->front]; 
}

//队列 
typedef struct{
    BiTree data[MAXSIZE];
    int top; 
}SeqStack;



SeqStack *Init_SeqStack(){
    SeqStack *s;
    s=(SeqStack*)malloc(sizeof(SeqStack));
    s->top=-1;
    return s;
}
int Empty_SeqStack(SeqStack *s){
    if(s->top==-1) return 1;
    else return 0;
}

int Push_SeqStack(SeqStack *s,BiTree  x){
    if(s->top==MAXSIZE-1) return 0;
    else{
        s->top++;
        s->data[s->top]=x;
        return 1;
    }
}

int Pop_SeqStack(SeqStack *s,BiTree *x){
    if(Empty_SeqStack(s)) return 0;
    else {
        *x=s->data[s->top];
        s->top--;return 1;       
    }
}

BiTree Top_SeqStack(SeqStack *s){
    if(Empty_SeqStack(s)) return 0;
    else return (s->data[s->top]);
}
void CreateBiTree(BiTree *root) {
    char ch;
    ch=getchar();
    if(ch=='#') *root=NULL;
    else{
        *root=(BiTree)malloc(sizeof(BiTNode));
        (*root)->data=ch;
        CreateBiTree(&((*root)->Lchild));
        CreateBiTree(&((*root)->Rchild));
    }
}
void PostOrder(BiTree root){
    if(root){       
        PostOrder(root->Lchild);     
        PostOrder(root->Rchild);
        printf("%c",root->data);
}
}
//后序非递归遍历二叉树
void PostOrder2(BiTree root) {
    SeqStack *S;
    BiTree p,q;
    S=Init_SeqStack();p=root;q=NULL;
    while(p!=NULL||!Empty_SeqStack(S)){
        while (p!=NULL){
            Push_SeqStack(S,p);p=p->Lchild;
        }
        if(!Empty_SeqStack(S)){
            p=Top_SeqStack(S);
            if((p->Rchild==NULL)||(p->Rchild==q)){//判断栈顶结点的右子树是否为空,右子树是否刚访问过  
                Pop_SeqStack(S,&p);printf("%c",p->data);q=p;p=NULL;  

        }
        else p=p->Rchild;
    }

}
} 

SeQueue  *A;
SeQueue  *S;
//后续递归遍历压栈 

//void PreOrder_count(BiTree root) {        
//  if(root){
//      if(root->Lchild==NULL&&root->Rchild==NULL){
//          InQueue(S,root);//count++; 
//  }
//      PreOrder_count(root->Lchild);
//      PreOrder_count(root->Rchild);
//  }
//}

后序非递归遍历二叉树
//void PostOrderpushtoSeqStack(BiTree root) {
//  SeqStack *S;
//  BiTree p,q;
//  S=Init_SeqStack();p=root;q=NULL;
//  while(p!=NULL||!Empty_SeqStack(S)){
//      while (p!=NULL){
//          Push_SeqStack(S,p);p=p->Lchild;
//      }
//      if(!Empty_SeqStack(S)){
//          p=Top_SeqStack(S);
//          if((p->Rchild==NULL)||(p->Rchild==q)){//判断栈顶结点的右子树是否为空,右子树是否刚访问过  
//              Pop_SeqStack(S,&p);printf("%c",p->data);q=p;p=NULL;  
//          
//      }
//      else p=p->Rchild;
//  }
//  
//}
//} 
void PostOrdertoQueue(BiTree root);
int main(){
BiTree k,r;BiTree q,p,h;//A,S,B
A=InitQueue();S=InitQueue();
int j;
CreateBiTree(&k);
PostOrdertoQueue(k);
//while(!EmptyQueue(A)){
//  OutQueue(A,&q); 
//  printf("%c",q->data);
//}

//while(!EmptyQueue(S)){
//  OutQueue(S,&p); 
//  printf("%c",p->data);
//}

SeqStack* E[S->rear+1];

for(int i=0;i<S->rear+1;i++){
    E[i]=Init_SeqStack();
}
//printf("\nabc\n");
//printf("A->front=%d,S->front=%d",A->front,S->front) ;
//printf("A->rear=%d,S->rear=%d",A->rear,S->rear); 
//A->front=0;
//S->front=0;
//printf("\nA->front=%d,S->front=%d\n",A->front,S->front) ;
//printf("A->rear=%d,S->rear=%d",A->rear,S->rear) ;


//for(int )
//检测 
//printf("A->data[0]=%d--S->data[0]=%d",A->data[0],S->data[0]); h=S->data[0];
//if(h==A->data[1]->Lchild||h==A->data[1]->Rchild){
//  printf("\n%d\n",1);
//}
//else
//  printf("\n%d\n",0);
//


for(int i=S->front;i<S->rear;i++){ 
    for(j=A->front,h=S->data[i];j<A->rear;j++){
        if(A->data[j]->Lchild==h||A->data[j]->Rchild==h){
            h=A->data[j];
            Push_SeqStack(E[i],h);          
    }
}
}
for(int i=S->front;i<S->rear;i++){
    printf("%c:",S->data[i]->data);
    while(!Empty_SeqStack(E[i])){
        Pop_SeqStack(E[i],&r);
        printf("%c",r->data) ;
    }       
    printf("\n");
}

}
//
void PostOrdertoQueue(BiTree root){
    if(root){

        PostOrdertoQueue(root->Lchild);      
        PostOrdertoQueue(root->Rchild);
        InQueue(A,root);
        if(root->Lchild==NULL&&root->Rchild==NULL){
            InQueue(S,root);
}   

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值