问题 L: DS_6.12 最近共同祖先(by Yan)

问题 L: DS_6.12 最近共同祖先(by Yan)
时间限制: 15 Sec 内存限制: 128 MB
提交: 5 解决: 5
[提交][状态][讨论版]
题目描述
从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。求两个不同结点ch1,ch2的最近共同祖先。

第一行:扩展先序序列
第二行:ch1,ch2两个不同结点值,用一个空格间隔。

输入
输出
样例输入
ABC##DE#G##F###
C F
样例输出
B

#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);
void searchfirstcommonroot(SeQueue *A,char s[]);

void prints(char s[]);
void printSeQueueA(SeQueue *A);


int main(){
BiTree k,r;BiTree q,p,h;char ch1,ch2;char s[3];//A,S,B
A=InitQueue();S=InitQueue();
int j;
CreateBiTree(&k);
scanf("\n%c %c",&ch1,&ch2);
//printf("\n%c %c\n",ch1,ch2);
s[0]=ch1;s[1]=ch2;
PostOrdertoQueue(k);


//while(!EmptyQueue(A)){
//  OutQueue(A,&q); 
//  printf("%c",q->data);
//}
//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 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");
//}


//printSeQueueA(A);
searchfirstcommonroot(A,s);

//prints(s);
//printSeQueueA(A);







}
void prints(char s[]){
    for(int i=0;i<2;i++){
        printf("%c",s[i]);
    }
}

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);
}   

}
}
void printSeQueueA(SeQueue *A){
    BiTree q;
    while(!EmptyQueue(A)){
    OutQueue(A,&q); 
    printf("%c",q->data);
}
} 
void searchfirstcommonroot(SeQueue *A,char *s) {
    A->front=0;
    SeQueue *P=InitQueue();
    BiTree h;BiTree x;int j;    
    SeQueue *R[2];
    for(int i=0;i<2;i++){
        R[i]=InitQueue();
    }
//printf("\n%d\n",11111111) ;

//printf("A->front=%d,h=%c",A->front,h=s[0]);
//if(A->data[1]->Lchild->data==h||A->data[1]->Rchild->data==h){
//  printf("true");
//}
//else printf("false") ;
for(int i=A->front;i<A->rear;i++) {
    if(A->data[i]->data==s[0]||A->data[i]->data==s[1]){     
        x=A->data[i];
        InQueue(P,x);
    }
}
//printf("\n执行到这句!\n");

//while(!EmptyQueue(P)){
//      OutQueue(P,&h);
//      printf("%c",h->data) ;
//  }
//printf("\n");
//P->front=0;
for(int i=0;i<2;i++){ 
    for(j=A->front,h=P->data[i];j<A->rear;j++){ 

        if(A->data[j]->Lchild==h||A->data[j]->Rchild==h){
            h=A->data[j];
            InQueue(R[i],h);

    }

}
}

//for(int i=P->front;i<P->rear;i++){
//  printf("%c:",P->data[i]->data);
//  while(!EmptyQueue(R[i])){
//      OutQueue(R[i],&h);
//      printf("%c",h->data) ;
//  }       
//  printf("\n");
//}


int flag=0; 
for(int i=0;i<R[0]->rear;i++){
    for(int j=0;j<R[1]->rear;j++){

    if(R[0]->data[i]==R[1]->data[j]){
        printf("%c",R[0]->data[i]->data);flag=1;break;
    }
}
if(flag==1) break;
}
}
//void searchfirstcommonroot2(SeQueue *A,char s[]) {
//  char h;BiTree x;int j;  
//  SeQueue *R[2];
//  for(int i=0;i<2;i++){
//      R[i]=InitQueue();
//  }
//printf("\n%d\n",11111111) ;
//
//for(int i=0;i<2;i++){ 
//  for(j=A->front,h=s[i];j<A->rear;j++){
//      if(A->data[j]->Lchild->data==h||A->data[j]->Rchild->data==h){
//          h=A->data[j]->data;
//          printf("\n");
//          x=A->data[j];
//          InQueue(R[i],x);
//                      
//  }
//}
//}
//printf("\n%d\n",22222) ;
int min=R[1]->rear<R[2]->rear?R[1]->rear:R[2]->rear;
int max=R[1]->rear>R[2]->rear?R[1]->rear:R[2]->rear;
//for(int i=0;i<R[0]->rear;i++){
//  for(int j=0;j<R[1]->rear;j++){
//  
//  if(R[0]->data[i]==R[1]->data[j]){
//      printf("%c",R[0]->data[i]->data);break;
//  }
//}
//}
//}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值