【发布时间】:2020-04-26 08:32:35
【问题描述】:
我为二叉树创建了一个程序。由于某种原因,我无法正确显示它。请帮我以正确的格式写出来。这是代码,如果有其他错误请告诉我。
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left, *right;
node(int x)
{
data = x;
left = right = NULL;
}
};
class tree
{
public:
node *p;
node *create()
{
int x;
cout << "Enter data(-1 for NULL): ";
cin >> x;
if(x == -1)
return NULL;
p = new node(x);
cout << "Enter left child: ";
p -> left = create();
cout << "Enter right child: ";
p -> right = create();
return p;
}
void display1()
{
display(p);
}
void display(node *root)
{
if(root != NULL)
{
display(root -> left);
cout << " " << root -> data << " ";
display(root -> right);
}
}
};
int main()
{
tree t;
t.create();
t.display1();
return 0;
}
【问题讨论】:
-
请详细描述您的问题。