c - Preorder Postorder Tree Traversal when RSON and LSON are given -


if input this

  1111010000000 //lson   gerlwidakonth  //data   1101101000110 //rson 

it should output preorder , postorder of given tree.

the problem is: program doesn't print correct postorder , can't figure out why. hope me. preorder correct okay me if want add improve codes. thank you!: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; < size; i++)         printf("%c", array[0][i]);     printf("\ndata:     ");     for(i = 0; < size; i++)         printf("%c", array[1][i]);     printf("\nrtag:     ");     for(i = 0; < 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 = null;     nodeptr b = malloc(sizeof(node));     b = root;     nodeptr o = null;     int = 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 = *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 = *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 = null;     nodeptr b = malloc(sizeof(node));     b = root;     nodeptr o = null;     int = 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 last sibling\n");         return -1;     }     for(i = 0; < 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 , 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 0 = 0;     int i;     for(i = 0; < 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; < 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; < 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 %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; < 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 = 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; } 


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -