이전에 이런 종류의 오류가 발생하지 않았습니다. CodeBlock으로 컴파일 할 때 코드는 정상적으로 작동하지만 코드를 VS (2015 커뮤니티)에 복사하면 두 개의 숫자 만 표시됩니다.Visual Studio에서 잘못된 결과가 발생했습니다.
을 (나는 이진 트리를 인쇄하는) 이것은 이진 검색 트리의 코드이며, Visual Studio에서 CodeBlocks에서 잘 작동하지만
#include<iostream>
using namespace std;
struct node
{
int data;
int balance_factor;
node* left_linker;
node* right_linker;
};
struct binary_tree
{
node* root;
};
void init_tree(binary_tree& in_tree);
node* create_new_node(int data);
void insert_node(int in_data,binary_tree& in_tree);
node* insert_recursively(int in_data,node* in_root);
bool tree_empty(binary_tree& in_tree);
void print_tree_from_root(node* in_root);
int main(int argc,char* argv[])
{
binary_tree tree_1;
init_tree(tree_1);
insert_node(10,tree_1);
insert_node(30,tree_1);
insert_node(40,tree_1);
insert_node(20,tree_1);
insert_node(50,tree_1);
print_tree_from_root(tree_1.root);
}
void init_tree(binary_tree& in_tree)
{
in_tree.root = NULL;
}
node* create_new_node(int data)
{
node* temp = new node;
temp->data = data;
temp->left_linker = NULL;
temp->right_linker = NULL;
return temp;
}
void insert_node(int in_data,binary_tree& in_tree)
{
if(tree_empty(in_tree))
{
in_tree.root = create_new_node(in_data);
}
else
{
insert_recursively(in_data,in_tree.root);
}
}
node* insert_recursively(int in_data,node* in_root)
{
if(in_root == 0)
{
in_root = create_new_node(in_data);
return in_root;
}
else
{
if(in_data > in_root->data)
{
in_root->right_linker = insert_recursively(in_data,in_root->right_linker);
}
else if(in_data < in_root->data)
{
in_root->left_linker = insert_recursively(in_data,in_root->left_linker);
}
}
}
bool tree_empty(binary_tree& in_tree)
{
if(in_tree.root == 0)
return true;
return false;
}
void print_tree_from_root(node* in_root)
{
if(in_root == NULL)
{
return;
}
else
{
cout<<in_root->data<<endl;
print_tree_from_root(in_root->left_linker);
print_tree_from_root(in_root->right_linker);
}
}
게시하지 마십시오 코드를 반환합니다. [mcve]를 작성하여 텍스트로 게시하십시오. – nvoigt
언어로 태그를 지정하는 것을 잊지 마십시오 (C++이라고 생각하십니까?) 실제로 할 일이있을 때 * 질문을 던집니다. – nvoigt
코드에 대한 전부는 아니며, 문제는 편집기입니다. VisualStudio에서 위의 결과가 올바르지 않습니다. –