This method is better ! It would take only O ( log(n) ) time.

-Dhyanesh

On 5/8/06, W Karas <[EMAIL PROTECTED]> wrote:
>
>
> shishir wrote:
> > Hi,
> > I am looking for a method for left to right insertion in a binary tree(
> > mind it, its not BST am talking about). A simple binary tree where
> > every root has not more than two childs and the insertion is always
> > done starting from the leftmost side of any given node.
> >
> >                 13
> >               /     \
> >             27     54
> >            /  \     /  \
> >          23  42
> >
> > For eg in the above case the next node should be inserted as the left
> > most child of 54.
> > Let me know if there's any doubt regarding the question.
> >
> > ~Shishir
>
> void add(elem *&root, unsigned &num_elems, elem *new_elem)
>   {
>     num_elems++;
>
>     if (num_elems == 1)
>       {
>         root = nuw_elem;
>         return;
>       }
>
>     elem *p = root;
>     unsigned mask = 1;
>
>     while (mask <= num_elems)
>       mask <<= 1;
>
>     for (mask >>= 2; mask > 1; mask >>= 1)
>       if (mask & num_elems)
>         p = p->right;
>       else
>         p = p->left;
>
>     if (num_elems & 1)
>       p->right = new_elem;
>     else
>       p->left = new_elem;
>
>     return;
>   }
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to