Find Preorder
int Index(char *low, char *high, char c)
{
    char *tmp = low;
    while (tmp <= high && *tmp != c)
        tmp++;
    if (tmp > high)
        return -1;
    else
        return tmp - low;
}
 
void Find_Pre(char *pl, char *ph, char *il, char *ih)
{
    if (pl <= ph && il <= ih) {
        int index = Index(il, ih, *ph);    // *ph is the root
        printf("%c", *ph);      // print the root first, since we want preorder
        Find_Pre(pl, pl+index-1, il, il+index-1);    // left subtree
        Find_Pre(pl+index, ph-1, il+index+1, ih);    // right subtree
    }
}
 
int main()
{
    char in[] = "CBEDAGFH";
    char post[] = "CEDBGHFA";
    Find_Pre(post, post+7, in, in+7);
    printf("\n");
}

Output:

ABCDEFGH

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License