# doubling tree

• 03-16-2008
gunner4life
doubling tree
Hi,
I would really appreciate any help on this.

Doubling tree is a problem where a duplicate node is inserted as a left child for each node in a binary search tree.

This code works:
Code:

```void doubleTree(root) { if(root == NULL) return; else { doubleTree(root->LeftChild); doubleTree(root->RightChild); ... ..... <create a new node and insert as the left child with data field of the node same as node> } }```
but this doesnt: ????????
Code:

```void doubleTree(root) { if(root == NULL) return; else     {     ...     .....     <create a new node and insert as the left child with data field of the node          same as node>     doubleTree(root->LeftChild);     doubleTree(root->RightChild);     } }```
Shouldnt it work??
• 03-17-2008
iMalc
Nope. You'd get stack overflow because root->LeftChild is the node you just inserted, so you'd keep infinitely recursing to the node you just inserted.
• 03-17-2008
gunner4life
thnks a lot for the explanation.

one more doubt...does recursion in a binary search tree lead to top-bottom or bottom - top traversal, or can it be both ??
• 03-17-2008
arpsmack
That's kind of an odd follow-up question. Is this for homework?

The fact that you don't know the answer shows that you don't understand recursion, or traversal of binary trees... I'm not sure that just giving you answers is going to help you any.
• 03-17-2008
gunner4life
Quote:

Originally Posted by gunner4life
Hi,
I would really appreciate any help on this.

Doubling tree is a problem where a duplicate node is inserted as a left child for each node in a binary search tree.

This code works:
Code:

```void doubleTree(root) { if(root == NULL) return; else { doubleTree(root->LeftChild); doubleTree(root->RightChild); ... ..... <create a new node and insert as the left child with data field of the node same as node> } }```
but this doesnt: ????????
Code:

```void doubleTree(root) { if(root == NULL) return; else     {     ...     .....     <create a new node and insert as the left child with data field of the node          same as node>     doubleTree(root->LeftChild);     doubleTree(root->RightChild);     } }```
Shouldnt it work??

sry consider me noob....but i wanted to ask

Is the order of insertion in the above case top-bottom?? its evident that the tree traversal is top-bottom...
• 03-17-2008
gunner4life
bounce!!!!
• 03-17-2008
cboard_member
I think the phrase you're looking for is "bump" and hey, don't do it.