这不起作用,这对你们来说是正确的吗?我认为逻辑是正确的,但我可能完全错了
有人有想法么?
这只是插入功能,它只适用于整数
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;
}
}
}
}