Write a program that converts a given tree to its Double tree. To create Double tree of the given tree, create a new duplicate for each node, and insert the duplicate as the left child of the original node.
Recursively convert the tree to double tree in postorder fashion. For each node, first convert the left subtree of the node, then right subtree, finally create a duplicate node of the node and fix the left child of the node and left child of left child.
Key Point: Consider other ways: pre-order, post-order or inorder.
Read full article from Double Tree | GeeksforGeeks
Recursively convert the tree to double tree in postorder fashion. For each node, first convert the left subtree of the node, then right subtree, finally create a duplicate node of the node and fix the left child of the node and left child of left child.
Key Point: Consider other ways: pre-order, post-order or inorder.
void
doubleTree(
struct
node* node)
{
struct
node* oldLeft;
if
(node==NULL)
return
;
/* do the subtrees */
doubleTree(node->left);
doubleTree(node->right);
/* duplicate this node to its left */
oldLeft = node->left;
node->left = newNode(node->data);
node->left->left = oldLeft;
}