Copy Constructor for Binary TreeNodes

I have a min heap which consists of TreeNode*. I'm doing huffman coding to create a huffman binary tree from the minHeap. Everytime I pop, I need to set the root of the min heap to the TreeNode* that I'm returning to build the huffman tree. However, I realized I need a copy constructor or something of the sort to perform a deep copy so that I retain the same pointer information. So, my question is, how would I write a copy constructor that would work for TreeNode* if my TreeNode class looks as follows...

class TreeNode{
int ascii, frequency;
TreeNode *left, *right;

//member functions
}
Objects that expect to be copied in this way are given a clone method. It solves the "virtual contructor" problem too.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class TreeNode
{
    int ascii;
    TreeNode* left;
    TreeNode* right;

public:
    TreeNode() { ascii = 0; left = right = 0; }
    TreeNode* clone();
    // ...
};

TreeNode* TreeNode::clone()
{
    if (TreeNode* tmp = new TreeNode)
    {
        tmp->ascii = ascii;
        if (left) tmp->left = left->clone();
        if (right) tmp->right = right->clone();
        return tmp;
    }
    return 0;
}

Topic archived. No new replies allowed.