I'm trying to write a tree copy function in C:
void tree_copy(treenode *source, treenode **dest)
{
treenode *newtree; //the new tree will be created using this as root
treenode **bkup = &newtree; //pointer to pointer, to keep backup of head of newtree
/*
Code to create new tree
Traverses using *newtree
And leaves *newtree pointing at last node created
But *bkup still contains the address of the head node, right?
*/
*dest = *bkup; //assign the new tree created to the parameter
}
//I'm trying to invoke it like this:
int main(void)
{
treenode *t1, *t2;
create_tree(&t1); //tested, already written function to create a tree. No bugs.
tree_copy(t1, &t2);
preorder(t2); //tested, already written function for preorder traversal.
}
The node supposed to contain the newly created tree's root (t2) still remains NULL after this operation. Why is this happening? What is wrong in my logic in backing up the starting node of new tree?
Any help will be greatly appreciated.