struct Tree{
int Data;
Tree* Left;
Tree* Right;
Tree(){
Data = 0;
Left = NULL;
Right = NULL;
}
};
Tree* Temp;
Tree* Root;
void Add(Tree* Dummy){
if(Dummy == NULL)
Dummy = Temp;
else if(Dummy -> Data <= Temp -> Data)
Add(Dummy -> Left);
else
Add(Dummy -> Right);
cout << endl << Dummy -> Data;
}
void Create(){
Temp = new Tree;
cin >> Temp -> Data;
Temp -> Left = NULL;
Temp -> Right = NULL;
if(Root == NULL)
Root = Temp;
else
Add(Root);
}
void Display(Tree* Dis){
cout << endl << Root -> Data;
cout << endl << Root -> Left -> Data;
cout << endl << Root -> Right -> Data;
}
每次输入输入的数据时,任何人都可以在这里帮助我,但是当我要显示它时,它崩溃了,我知道会有一个小学生错误,但这就是我的目的。
答案 0 :(得分:0)
我不确定您的问题是什么,但可以通过二进制搜索树示例进行检查:
#include <iostream>
using namespace std;
struct Tree{
int Data;
Tree* Left;
Tree* Right;
Tree(int data){
Data = data;
Left = NULL;
Right = NULL;
}
};
Tree* Root;
int Add(Tree* node, Tree* root){ // return int so we can know it successful
if(node == NULL || node->Data == root->Data)
return 0;
if(node->Data <= root->Data) {
if (root->Left)
Add(node, root->Left);
else
root->Left = node;
}
else {
if (root->Right)
Add(node, root->Right);
else
root->Right = node;
}
return 1;
}
int Create(int data){ // return int so we can know it successful
if (Root != NULL)
return 0;
Root = new Tree(data);
return 1;
}
void DisplayInOrder(Tree* node){
if (node == NULL)
return;
DisplayInOrder(node->Left);
cout << node->Data << endl;
DisplayInOrder(node->Right);
}
int main()
{
Create(5);
for (int i = 0; i < 4; i++) {
Tree * t = new Tree(i);
Add(t, Root);
}
DisplayInOrder(Root);
return 0;
}
我不是C++
专家,如果遇到错误,抱歉...