c - Level order insertion into a binary tree? -


suppose given level order traversal output. how construct binary tree populated data @ correct positions?

please note i'm not trying sketch tree given traversal output, read off traversal data array populate binary tree actual coding in c.

eg:

let a[] = {a, b, c, d, e, f, g}; //the traversal output in array

so level-order tree this:

                       / \            b   c         / \  / \        d   e f  g 

suppose there tree node structure so:

typedef struct node {     char data;     struct node* left;     struct node* right; }tree; 

now i'm trying read a[] values , code tree looks diagram. there many examples of level-order traversal couldn't find relevant on actual coding binary tree construction. sort of "reverse of traversal".

also please note not homework, though don't have problems tagging if more people notice way. :)

one possible solution:

char a[size] = {a,b,c,d,e,f,g};      node* func(int index){         if(index < size){             node *tmp = new node();             tmp->data = a[index];             tmp->left = func(2*index + 1);             tmp->right = func(2*index + 2);         }         return tmp;     } 

stacktrace tree:

                                     a->a[0]           b->func(2*0 + 1)=[1]                              c->func(2*0 + 2)=[2] d->func(2*1 + 1)=[3]    e->func(2*1 + 2)=[4]        f->func(2*2 + 1)=[5]     g->func(2*2 + 2)=[6] 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -