Reputation: 383
this isn't working, does this look right to you guys? I think the logic is right, but I could be totally wrong
anyone have any ideas?
This is just the insert function, it's only supposed to work for ints
void BST::Insert(int valueToInsert) {
if (root == NULL) {
root = new Node();
root->val = valueToInsert;
root->parent = NULL;
root->left = NULL;
root->right = NULL;
} else {
Node* tmp = new Node();
tmp->val=valueToInsert;
Node* trav = root;
tmp->left=NULL;
tmp->right=NULL;
while (true) {
if((trav->val)>(trav->val)) {
if (trav->right == NULL) {
trav->right = tmp;
tmp->parent = trav;
tmp->right = NULL;
tmp->left = NULL;
break;
} else {
trav = trav->right;
continue;
}
}
if ((tmp->val)<(trav->val)) {
if (trav->left == NULL) {
trav->left = tmp;
tmp->parent = trav;
break;
}else {
trav = trav->left;
continue;
}
}
}
}
Upvotes: 0
Views: 293
Reputation: 7326
while (true) {
if((trav->val)>(trav->val)) {
Right below the while condition trav->val is being compared to trav->val. This is not the intent, I guess.
Upvotes: 0
Reputation: 500227
The following looks suspicious:
if((trav->val)>(trav->val)) {
^^^^ ^^^^
Was the first trav
meant to be tmp
?
Upvotes: 1