我正在为我的家庭作业二叉搜索树,但它没有显示序为任何输出,序和后序。尽管我在inorder
preorder
和postorder
函数中使用cout
,但它并未给出任何输出。我觉得错误是createBst
功能但我不知道这件事......好心帮我出 预先感谢您:-)二叉搜索树:没有输出
#include <iostream>
using namespace std;
struct node
{
int info;
struct node *left;
struct node *right;
}*r;
struct node * createBst(struct node *r, int val)
{
if (r == NULL)
{
r = new node;
r->info = val;
r->left = NULL;
r->right = NULL;
}
else if (val <= r->info)
{
// cout<<r->left<<" ";
r->left = createBst(r->left, val);
}
else
{
r->right = createBst(r->right, val);
cout << r->right << " ";
}
return r;
}
void inOrder(struct node *r)
{
if (r != NULL)
{
inOrder(r->left);
cout << r->info;
inOrder(r->right);
}
}
void preOrder(struct node *r)
{
if (r != NULL)
{
cout << r->info;
preOrder(r->left);
preOrder(r->right);
}
}
void postOrder(struct node *r)
{
if (r != NULL)
{
postOrder(r->left);
postOrder(r->right);
cout << r->info;
}
}
int main()
{
r = NULL;
int n, val;
cout << "Enter the number of element" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> val;
//cout<<"check";
createBst(r, val);
}
cout << "Inorder" << endl;
//cout<<r->info<<endl;
inOrder(r);
cout << endl;
cout << "PreOrder" << endl;
preOrder(r);
cout << endl;
cout << "PostOrder" << endl;
postOrder(r);
cout << endl;
}
_不给任何输出_。这是一个非常模糊的陈述。你在编译时是否收到错误?你用什么IDE来编译这个? –