For an assignment for school, I need to take a binary search tree and convert it into a double-threaded binary search tree. I have a general understanding on how the threads work, but I realize that in order to know where it exactly the threads are, I need to know the parent of the last inserted node (prev), as well as the parent of that node as well (twoBack). However, in my attempts to modify one of the functions in order to save those values, I can not quite get what I am looking for.
If someone could point out exactly what I'm doing wrong, and explain it to me so that I can learn from my mistakes, I'd greatly appreciate it. Thanks!
Both of these functions a are part of the class for the BST itself:
void insert(const Key& k, const E& e)
{
root = inserthelp(root, k, e);
nodecount++;
}
BSTNode<Key, E>* BST<Key, E>::inserthelp(BSTNode<Key, E>* root, const Key& k, const E& it)
{
if (root == NULL) // Empty tree: create node
{
return new BSTNode<Key, E>(k, it, NULL, NULL);
}
if (k < root->key())
{
if (prev != NULL)
twoBack = prev;
prev = root;
root->setLeft(inserthelp(root->left(), k, it));
}
else
{
if (prev != NULL)
twoBack = prev;
prev = root;
root->setRight(inserthelp(root->right(), k, it));
}
return root; // Return tree with node inserted
}