我正在尝试将int向量转换为二进制搜索树。如果这很重要,我将在Windows上使用Cygwin。出于某种原因,在跟踪之后,我发现循环的每次迭代中“ next”都具有相同的地址。有人可以告诉我我要去哪里了吗?
edit:用“ if(numbers.empty())return;”代替我的空检查行。
Tree::Tree(std::vector<int> numbers){
if (numbers.empty()) return;
node *current = &root;
current->num = *numbers.begin();
node *next;
for (std::vector<int>::iterator it = numbers.begin() + 1; it != numbers.end(); it++){
next = new node();
next->num = *it;
if (*it >= current->num){
current->right = next;
current = current->right;
} else {
current->left = next;
current = current->left;
}
}
}