Hi. I am trying to implement a huffman binary tree for class. Each node has 3 pointers: left, right, and back to parent. I cannot for the life of me figure out why my code won't work for switching two nodes:
Code:
void huffmanTree::switchNodes(treeNode *& T1, treeNode *& T2)
{
treeNode * temp1 = T1->back;
treeNode * temp2 = T2->back;
if(T1 == temp1->right)
temp1->right = T2;
else
temp1->left = T2;
if(T2 == temp2->right)
temp2->right = T1;
else
temp2->left = T1;
T1->back = temp2;
T2->back = temp1;
}
I tried getting it to work with a three node tree (switching the two external nodes). If i explicitly use the root member pointer
root->left = T1;
root->right = T2;
or whatever, i can get it to work, but i don't see why the above won't.
EDIT: Delete this thread, found the problem in the above code.