Search code examples
binary-treetraversal

Why does it say I don't declare a varibale but actually I declared?


It's about Bitree. It should output Postorder traversal after input Preorder traversal and Inorder traversal.

#include <stdio.h>
#include <string.h>

void traverBitree(char *first, char *middle);
int index = 0;

int main(void)
{
    char first[27], middle[27]; 
    gets(first);
    gets(middle);
    traverBitree(first, middle);
}

void traverBitree(char *first, char *middle)
{
    if (strlen(middle) == 0)
        return;                 
    char ch = first[index++];   
    int pos = 0;                
    while (middle[pos++] != ch) 
    char left[26], right[26];   
    strncpy(left, middle, pos); 
    strcpy(right, middle+pos);  
    right[pos+1] = '\0';        
    traverBitree(first, left);  
    traverBitree(first, right);
    printf("%c", ch);     
}

I think I have declared the "left" and "right", why actually not? There must be something wrong but I don't know.


Solution

  • Your while statement while (middle[pos++] != ch) lacks braces, so the compiler is looking for them and the code below won't compile well.

    You have to wrap the statements you want in {}