C 给定RSON和LSON时的前序-后序树遍历

C 给定RSON和LSON时的前序-后序树遍历,c,tree,C,Tree,如果你输入这个 1111010000000 //LSON GERLWIDAKONTH //DATA 1101101000110 //RSON 它应该输出给定树的前序和后序 问题是:我的程序没有打印正确的后序,我也不知道为什么。我希望你能帮助我。前序已经是正确的,但如果您想添加任何内容来改进我的代码,我也可以。谢谢您!:D #include<stdio.h> #include<stdlib.h> #include<string.h> #defin

如果你输入这个

  1111010000000 //LSON
  GERLWIDAKONTH  //DATA
  1101101000110 //RSON
它应该输出给定树的前序和后序

问题是:我的程序没有打印正确的后序,我也不知道为什么。我希望你能帮助我。前序已经是正确的,但如果您想添加任何内容来改进我的代码,我也可以。谢谢您!:D

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 5
#define CASE 3
#define N 100

struct node{
    char data;
    struct node* llink;
    struct node* rlink;
    int tag;
};
typedef struct node Node;
typedef Node* NodePtr;

void twoPreorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o);
void threePreorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o);
void twoPostorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o);
void threePostorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o);

void printSequence(char *array[3], int size){
    int i;
    printf("LTAG:     ");
    for(i = 0; i < size; i++)
        printf("%c", array[0][i]);
    printf("\nDATA:     ");
    for(i = 0; i < size; i++)
        printf("%c", array[1][i]);
    printf("\nRTAG:     ");
    for(i = 0; i < size; i++)
        printf("%c", array[2][i]);
    printf("\n\n");
}

void onePreorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o){
    printf("%c", (*b)->data);
    *counter = *counter+1;
    int count = *counter;
    int n = *size;
    //printf("counter=%d\tsize=%d\n", count, n);
    if(count == n){
        return;
    }
    *o = (*b)->llink;
    if(*o != NULL){
        (*b)->llink = *a;
        *a = *b;
        *b = *o;
        onePreorder(counter, size, a, b, o);
    }
    //printf("o NULL\n");
    else{
        twoPreorder(counter, size, a, b, o);
    }
}

void twoPreorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o){
    *o = (*b)->rlink;
    if(*o != NULL){
        (*b)->rlink = *a;
        (*b)->tag = 1;
        *a = *b;
        *b = *o;
        onePreorder(counter, size, a, b, o);
    }
    //printf("o NULL\n");
    threePreorder(counter, size, a, b, o);
}

void threePreorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o){
    //printf("a = %c\n", (*a)->data);
    if((*a) == NULL)
        return;
    else{
        if((*a)->tag == 0){
            *o = (*a)->llink;
            (*a)->llink = *b;
            *b = *a;
            *a = *o;
            twoPreorder(counter, size, a, b, o);
        }
        else{
            *o = (*a)->rlink;
            (*a)->rlink = *b;
            (*a)->tag = 0;
            *b = *a;
            *a = *o;
            threePreorder(counter, size, a, b, o);
        }
    }
}

void preorder1(int size, NodePtr root){
    printf("Preorder:\t");
    if(root == NULL)
        return;
    NodePtr a = NULL;
    NodePtr b = malloc(sizeof(Node));
    b = root;
    NodePtr o = NULL;
    int i = 0;
    onePreorder(&i, &size, &a, &b, &o); 
    printf("\n");   
}

void onePostorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o){
    *o = (*b)->llink;
    if(*o != NULL){
        (*b)->llink = *a;
        *a = *b;
        *b = *o;
        onePostorder(counter, size, a, b, o);
    }
    else{
        twoPostorder(counter, size, a, b, o);
    }
}

void twoPostorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o){
    *o = (*b)->rlink;
    if(*o != NULL){
        printf("%c", (*b)->data);
        (*b)->rlink = *a;
        (*b)->tag = 1;
        *a = *b;
        *b = *o;
        onePostorder(counter, size, a, b, o);
    }
    else{
        printf("%c", (*b)->data);
        threePostorder(counter, size, a, b, o);
    }
}

void threePostorder(int *counter, int *size, NodePtr *a, NodePtr *b, NodePtr *o){
    //printf("a = %c\n", (*a)->data);
    if((*a) == NULL)
        return;
    else{
        if((*a)->tag == 0){
            *o = (*a)->llink;
            (*a)->llink = *b;
            *b = *a;
            printf("%c", (*b)->data);
            *counter++;
            int i = *counter;
            int n = *size;
            if(i == n)
                return;
            *a = *o;
            twoPostorder(counter, size, a, b, o);
        }
        else{
            *o = (*a)->rlink;
            (*a)->rlink = *b;
            (*a)->tag = 0;
            *b = *a;
            printf("%c", (*b)->data);
            *counter++;
            int i = *counter;
            int n = *size;
            if(i == n)
                return;
            *a = *o;
            threePostorder(counter, size, a, b, o);
        }
    }
}


void postorder1(int size, NodePtr root){
    if(root == NULL)
        return;
    printf("Postorder:\t");
    NodePtr a = NULL;
    NodePtr b = malloc(sizeof(Node));
    b = root;
    NodePtr o = NULL;
    int i = 0;
    onePostorder(&i, &size, &a, &b, &o);
    printf("%c", root->data);   
    printf("\n");
}
NodePtr createNode(char val){
    NodePtr x = malloc(sizeof(Node));
    if(x == NULL){
        printf("Overflow\n");
    }
    else{
        x->data = val;
        x->llink = NULL;
        x->rlink = NULL;
        x->tag = 0;
    }
    return x;
}

int isValidSequence(char *array[3], int size){
    int ones = 0;
    int zeroes = 0;
    int i;
    if(array[2][size-1] == '1'){
        printf("INVALID SEQUENCES!\nLast token not the last sibling\n");
        return -1;
    }
    for(i = 0; i < size; i++){
        if(array[0][i] == '1'){
            ones++;
            if(ones >= 1)   
                array[0][i] = ones + 48;
            //printf("array[0][i] = %c\n", array[0][i]);
        }
        if(array[2][i] == '0')
            zeroes++;
    }
    //printf("1: %d\t0: %d\n", ones, zeroes);
    if((zeroes - 1) != ones){
        printf("INVALID SEQUENCES!\nUnbalanced number of ones in LTAG and zeroesin RTAG sequence (excluding last token).\n");
        return -1;  
    }
    else if((zeroes - 1) == ones)
        return 1;
}

int getIndex(char value, char *array[3], int size){
    int zero = 0;
    int i;
    for(i = 0; i < size; i++){
        if(array[2][i] == '0'){
            zero++;
            //printf("index: %d\tvalue: %d\n", i, (value)-48);
            if(zero + 48 == value){
                //printf("index: %d\n", i+1);
                return (i + 1);
            }
        }
    }
    return -1;
}

int firstNotDone(int doneLson[], int doneRson[], int size){
    int i;
    for(i = 0; i < size; i++)
        if((doneLson[i] < 1) || (doneRson[i] < 1))
            return i;
    return -1;
}

void createTree(int size, char *array[3], NodePtr *root){
    int i;
    int currentIndex = 0;
    int lsonIndex;
    int isDoneLson[size];
    int isDoneRson[size];
    NodePtr node[size];
    for(i = 0; i < size; i++){
        isDoneLson[i] = 0;
        isDoneRson[i] = 0;
        node[i] = createNode(array[1][i]);
    }

    //int k = 0;
    do{
        while((array[2][currentIndex] == '1') && (isDoneRson[currentIndex] < 1)){
            //printf("has siblings\n");
            node[currentIndex]->rlink = node[currentIndex + 1];
            isDoneRson[currentIndex]++;
            currentIndex++;
        }
        isDoneRson[currentIndex]++;
        //printf("current: %c\tcurrentIndex = %d\tlson = %d\trson = %d\n", array[1][currentIndex], currentIndex, isDoneLson[currentIndex], isDoneRson[currentIndex]);
        //test siblings
        NodePtr newNode = malloc(sizeof(Node));
        newNode = node[currentIndex];

        while(newNode != NULL){
            //printf("seq: %c\n", newNode->data);
            newNode = newNode->rlink;
        }

        currentIndex = firstNotDone(isDoneLson, isDoneRson, size);
        //printf("current: %c\tcurrentIndex = %d\tlson = %d\trson = %d\n", array[1][currentIndex], currentIndex, isDoneLson[currentIndex], isDoneRson[currentIndex]);
        if(array[0][currentIndex] != '0'){
            //printf("has lson\n");
            lsonIndex = getIndex(array[0][currentIndex], array, size);
            //printf("lson of %c is %c\n", array[1][currentIndex], array[1][lsonIndex]);
            node[currentIndex]->llink = node[lsonIndex];
        }
        isDoneLson[currentIndex]++;
        //printf("current: %c\tcurrentIndex = %d\tlson = %d\trson = %d\n", array[1][currentIndex], currentIndex, isDoneLson[currentIndex], isDoneRson[currentIndex]);
        isDoneLson[currentIndex]++;
        currentIndex = firstNotDone(isDoneLson, isDoneRson, size);;
        //printf("current: %c\tcurrentIndex = %d\tlson = %d\trson = %d\n", array[1][currentIndex], currentIndex, isDoneLson[currentIndex], isDoneRson[currentIndex]);
        //k++;
    }while(currentIndex != -1);
    /*  
    for(i = 0; i < size; i++){
        printf("Node[%d]: %c\t", i, node[i]->data);
        if(node[i]->llink != NULL)
            printf("llink: %c\t", node[i]->llink->data);        
        if(node[i]->rlink != NULL)
            printf("rlink: %c\t", node[i]->rlink->data);    
        printf("\n");       
    }
**/
    root = &node[0];
    preorder1(size, node[0]);
    postorder1(size, node[0]);
    printf("\n");
}

void file(FILE *ptr, char *array[3], NodePtr *root){
    char c;
    int i = 0;
    int j = 0;
    int initSize = N;
    int k;
    int n;
    array[0] = malloc(initSize);
    do{
        c = fgetc(ptr);     
        if(c == '\n'){
            n = j;
            j = 0;
            break;
        }
        else{
            if(j == (initSize - 1)){
                initSize = initSize + N;
                array[0] = realloc(array[0], initSize);
            }
            array[0][j] = c;
            j++;
        }
    }while(1);
    array[1] = malloc(n);   
    fscanf(ptr, "%s", array[1]);
    array[2] = malloc(n);
    fscanf(ptr, "%s", array[2]);

    //print array
    printSequence(array, n);

    int isValid = isValidSequence(array, n);
    if(isValid == 1){
        createTree(n, array, root);
    }
    c = fgetc(ptr);
    if(c == EOF){
        printf("END\n");
        fclose(ptr);
    }
    else{       
        c = fgetc(ptr);
        file(ptr, array, root);
        fclose(ptr);
    }
}

void insertLson(NodePtr *father, NodePtr lson){
    NodePtr x = malloc(sizeof(Node));
    NodePtr *y;
    x = (*father)->llink;
    while(x != NULL){
        x = x->llink;
    }
    y = &x;
    x = lson;
    printf("x = %c\n", x->data);

}

void type(char *array[3], NodePtr *root){
    int lines = 0;
    char c = getchar();
    int n = 0;
    int size;
    int initSize = N;
    array[0] = malloc(initSize);
    c = getchar();

    do{

        if(c == '\n'){
            size = n;

            break;

        }

        else{
            if(n == (initSize - 1)){
                initSize = initSize + N;
                array[0] = realloc(array[0], initSize);
            }

            array[0][n] = c;
            n++;
        }

        c = getchar();

    }while(1);
    array[1] = malloc(size);
    array[2] = malloc(size);
    scanf("%s", array[1]);
    scanf("%s", array[2]);
    printSequence(array, n);
//  printf("%s\t%s\t%s\n", array[0], array[1], array[2]);
//  printf("%sdafi\n", array[0]);
    int isValid = isValidSequence(array, size);
    if(isValid == 1){
        //printSequence(array, n);
        //printf("isvalid\nsize: %d\n", n);
        createTree(n, array, root);
    }
    c = getchar();
    if(c == EOF){
        printf("END\n");
        return;
    }
    else{       
        c = getchar();
        type(array, root);
    }
}

int tree(){
    NodePtr *root = NULL;
    char *array[3];
    int *size;
    char filename[50];
    int choice; 
    printf("Choose input:\n1. Keyboard\n2. File\n");
    scanf("%d", &choice);
    switch(choice){
        case 1:
            printf("Type input\n");
            type(array, root);
            break;
        case 2:
            printf("Type filename\n");
            scanf("%s", filename);
            FILE* ptr = fopen(filename, "r");
            file(ptr, array, root);
            break;
        default:
            printf("Invalid choice\n");
            break;
    }
}

int main(void){
    tree();
    return 0;
}
#包括
#包括
#包括
#定义尺寸5
#定义案例3
#定义N 100
结构节点{
字符数据;
结构节点*llink;
结构节点*rlink;
int标签;
};
typedef结构节点;
typedef节点*NodePtr;
void two预订单(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o);
作废三个预订单(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o);
作废twoPostorder(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o);
作废三个PostOrder(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o);
无效打印序列(字符*数组[3],整数大小){
int i;
printf(“LTAG:”);
对于(i=0;i数据);
*计数器=*计数器+1;
整数计数=*计数器;
int n=*大小;
//printf(“计数器=%d\t大小=%d\n”,计数,n);
如果(计数=n){
返回;
}
*o=(*b)->llink;
如果(*o!=NULL){
(*b)->llink=*a;
*a=*b;
*b=*o;
一个预订单(计数器、大小、a、b、o);
}
//printf(“o空\n”);
否则{
两个预订单(计数器、大小、a、b、o);
}
}
void two预订单(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o){
*o=(*b)->rlink;
如果(*o!=NULL){
(*b)->rlink=*a;
(*b)->tag=1;
*a=*b;
*b=*o;
一个预订单(计数器、大小、a、b、o);
}
//printf(“o空\n”);
三个预订单(计数器、大小、a、b、o);
}
void threePreorder(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o){
//printf(“a=%c\n”,(*a)->数据);
如果((*a)==NULL)
返回;
否则{
如果((*a)->标记==0){
*o=(*a)->llink;
(*a)->llink=*b;
*b=*a;
*a=*o;
两个预订单(计数器、大小、a、b、o);
}
否则{
*o=(*a)->rlink;
(*a)->rlink=*b;
(*a)->tag=0;
*b=*a;
*a=*o;
三个预订单(计数器、大小、a、b、o);
}
}
}
void preorder1(整数大小,NodePtr根){
printf(“预订单:\t”);
if(root==NULL)
返回;
NodePtr a=NULL;
NodePtr b=malloc(sizeof(Node));
b=根;
NodePtr o=NULL;
int i=0;
一个预订单(&i,&size,&a,&b,&o);
printf(“\n”);
}
作废onePostorder(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o){
*o=(*b)->llink;
如果(*o!=NULL){
(*b)->llink=*a;
*a=*b;
*b=*o;
一个邮政订单(计数器、大小、a、b、o);
}
否则{
twoPostorder(计数器、大小、a、b、o);
}
}
void twoPostorder(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o){
*o=(*b)->rlink;
如果(*o!=NULL){
printf(“%c”,(*b)->数据);
(*b)->rlink=*a;
(*b)->tag=1;
*a=*b;
*b=*o;
一个邮政订单(计数器、大小、a、b、o);
}
否则{
printf(“%c”,(*b)->数据);
三次订购(计数器、大小、a、b、o);
}
}
void 3postOrder(int*计数器、int*大小、NodePtr*a、NodePtr*b、NodePtr*o){
//printf(“a=%c\n”,(*a)->数据);
如果((*a)==NULL)
返回;
否则{
如果((*a)->标记==0){
*o=(*a)->llink;
(*a)->llink=*b;
*b=*a;
printf(“%c”,(*b)->数据);
*计数器++;
int i=*计数器;
int n=*大小;
如果(i==n)
返回;
*a=*o;
twoPostorder(计数器、大小、a、b、o);
}
否则{
*o=(*a)->rlink;
(*a)->rlink=*b;
(*a)->tag=0;
*b=*a;
printf(“%c”,(*b)->数据);
*计数器++;
int i=*计数器;
int n=*大小;
如果(i==n)
返回;
*a=*o;
三次订购(计数器、大小、a、b、o);
}
}
}
void postorder1(整数大小,NodePtr根){
if(root==NULL)
返回;
printf(“邮购:\t”);
NodePtr a=NULL;
NodePtr b=malloc(sizeof(Node));
b=根;
NodePtr o=NULL;
int i=0;
onePostorder(&i,&size,&a,&b,&o);
printf(“%c”,根->数据);
printf(“\n”);
}
NodePtr createNode(char val){
NodePtr x=malloc(sizeof(Node));
如果(x==NULL){
printf(“溢出\n”);
}
否则{
x->data=val;
x->llink=NULL;
x->rlink=NULL;
x->tag=0;
}
返回x;
}
int isValidSequence(字符*数组[3],int大小){
整数=0;
整数零=0;
int i;
if(数组[2][size-1]='1'){
printf(“无效序列!\n最后一个令牌不是最后一个同级\n”);
返回-1;
}
对于(i=0;i=1)
数组[0][i]=1+48;
//printf(“数组[0][i]=%c\n”,数组[0][i]);
}
if(数组[2][i]=“0”)
零++;
}
//printf(“1:%d\t0:%d\n”,1,0);
如果((零-1)!=1){
printf(“无效序列!\n在LTAG和RTAG序列(不包括最后一个令牌)中的1和0的数量不平衡。\n”);
返回-1;
}
else if((零-1)=1)
返回1;
}
int getIndex(字符值,字符*数组[3],整数大小){
int零=0;
int i;