Why the double return value is -1.#IND?
Jun 6, 2016 at 9:53pm UTC
I can't find the error in my code.I got this problem from the remove() function in Binary Search Tree.
Write your question here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
void remove(const T& x, BinaryNode* &t)
{
if (t == nullptr )
return ;
if (x < t->element)
remove(x, t->left);
else if (t->element < x)
remove(x, t->right);
else if (t->left != nullptr &&t->right != nullptr )
{
t->element = removeMin(t->right);
cout << "the value of t->element: " << t->element << endl;
}
else
{
BinaryNode* oldNode = t;
t = (t->left != nullptr ) ? t->left : t->right;
delete oldNode;
}
}
T removeMin(BinaryNode* &t)
{
if (t->left == nullptr )
{
T s=t->element;
cout << "the value of s: " << s << endl;
remove(t->element, t);
return s;
}
removeMin(t->left);
}
int main()
{
BinarySearchTree<double > t;
t.insert(4.5);
t.insert(2);
t.insert(1);
t.insert(3);
t.insert(6);
t.insert(5.5);
double a = 4.5;
t.remove(a);
t.postorder();
system("pause" );
}
the output is
1
3
2
6
-1.#IND
Last edited on Jun 6, 2016 at 9:57pm UTC
Jun 6, 2016 at 10:05pm UTC
Oh suddenly I figure out
1 2 3 4 5 6 7 8 9 10 11
T removeMin(BinaryNode* &t)
{
while (t->left != nullptr )
t = t->left;
T s=t->element;
cout << "the value of s: " << s << endl;
remove(t->element, t);
return s;
}
It works now.
Topic archived. No new replies allowed.