在BinaryTree中,我把root设置为nullptr,但是在第一轮循环中root==nullptr并没有执行,是为什么?
# include<iostream>
# include<vector>
using namespace std;
class Node{
public:
int value;
Node *parent=nullptr;
Node *left=nullptr, *right=nullptr;
};
class BinaryTree{
private:
Node *root=nullptr;
public:
void put(int value){
if (root == nullptr){
Node *temp;
temp->value = value;
root = temp;
}
else{
_put(value, root);
}
}
void _put(int value, Node *current_node){
if (value < root->value){
if (current_node->left == nullptr){
_put(value, current_node->left);
}
else{
Node *temp;
temp->value = value;
temp->parent = current_node;
}
}
else if (value > root->value){
if (current_node->right == nullptr){
_put(value, current_node->right);
}
else{
Node *temp;
temp->value = value;
temp->parent = current_node;
}
}
}
};
int main(){
vector<int> *nums = new vector<int> {3, 1, 2, 5, 4};
BinaryTree *tree;
for(int i=0; i<nums->size(); i++){
tree->put(i);
}
return 0;
}