#include <iostream>
int sizeWhileBuilding = 0;
int sizeWhileCounting = 0;
struct Node
{
Node(int theData) :
data(theData), left(NULL), right(NULL)
{ std::cout << "Node::Node() with data " << theData << std::endl; }
int data;
Node* left;
Node* right;
};
Node* insertNode(Node* root, int data)
{
if(!root)
{
++sizeWhileBuilding;
std::cout << "Current size is " << sizeWhileBuilding << std::endl;
std::cout << "Making new node with " << data << std::endl;
root = new Node(data);
}
else if(root->data < data)
{
std::cout << "The node " << root->data << " is lesser than " << data << ". Making right node" << std::endl;
root->right = insertNode(root->right, data);
}
else if(root->data > data)
{
std::cout << "The node " << root->data << " is greater than " << data << ". Making left node" << std::endl;
root->left = insertNode(root->left, data);
}
return root;
}
int main()
{
std::cout << "Making binary tree" << std::endl;
Node* root;
if(root)
{
std::cout << "Root is NOT null after creation" << std::endl;
}
else
{
std::cout << "Root is null after creation" << std::endl;
}
std::cout << "==============================================================================" << std::endl;
root = insertNode(root, 10);
//Problem line below
std::cout << "The size of the tree is " << sizeWhileBuilding << std::endl;
}
"문제 줄"이 주석 처리 된 상태에서이 코드를 실행하면 노드 10이 실행되고 트리에 노드 10이 삽입됩니다. 문제가있는 행으로 실행할 때, 나는 그것을 몹시 괴롭힌다.전역 변수를 사용할 때 코어 덤프
문제가없는 줄을 실행하면 주 인쇄의 시작 부분에 "루트가 생성 후 null입니다"라는 내용이 나타납니다. 문제가 enter code here
인 경우 "루트가 생성 된 후 null이 아닙니다."
내가 이해하지 못하는 이유는 노드 * 루트; 초기화는 전역 변수가 사용 중인지 여부에 달려 있습니까?