[英]Binary tree traversal in c
Can someone please explain what could be wrong with this code, I have been trying to look for the problem but it is still giving me these errors:有人可以解释一下这段代码可能有什么问题吗,我一直在试图寻找问题,但它仍然给我这些错误:
prog.c: In function 'inorder':
prog.c:43:20: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct node *' [-Wformat=]
printf("\n%d", t->left);
^
prog.c:44:21: warning: passing argument 1 of 'inorder' makes pointer from integer without a cast [-Wint-conversion]
inorder(t->data);
^
prog.c:39:9: note: expected 'node * {aka struct node *}' but argument is of type 'int'
int inorder(node *t)
^
prog.c: In function 'postorder':
prog.c:52:20: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct node *' [-Wformat=]
printf("\n%d", t->left);
^
prog.c:54:23: warning: passing argument 1 of 'postorder' makes pointer from integer without a cast [-Wint-conversion]
postorder(t->data);
^
prog.c:48:13: note: expected 'node * {aka struct node *}' but argument is of type 'int'
int postorder(node *t)
^
prog.c: In function 'main':
prog.c:65:1: warning: implicit declaration of function 'preorder' [-Wimplicit-function-declaration]
preorder(root);
^
prog.c:67:1: warning: implicit declaration of function 'inorder' [-Wimplicit-function-declaration]
inorder(root);
^
prog.c:69:1: warning: implicit declaration of function 'postorder' [-Wimplicit-function-declaration]
postorder(root);
^
/tmp/ccXklWoM.o: In function `main':
c4068222a0281abc5ccfae03851cee26.c:(.text+0xde): undefined reference to `preorder'
c4068222a0281abc5ccfae03851cee26.c:(.text+0xf9): undefined reference to `inorder'
c4068222a0281abc5ccfae03851cee26.c:(.text+0x114): undefined reference to `postorder'
collect2: error: ld returned 1 exit status
And here is the code below:这是下面的代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}node;
node *create()
{
node *p;
int x;
printf("Enter data (-1 for no data): ");
scanf("%d", &x);
if(x==-1)
{
return NULL;
}
else{
p=(node*)malloc(sizeof(node));
printf("Enter left child node: \n");
p->left=create();
printf("Enter right child node: \n");
p->right=create();
return p;
}
int preorder(node *t)
{
if(t!=NULL)
{
printf("\n%d", t->data);
preorder(t->left);
preorder(t->right);
}
}
int inorder(node *t)
{
if(t!=NULL)
{
printf("\n%d", t->left);
inorder(t->data);
inorder(t->right);
}
int postorder(node *t)
{
if(t!=NULL)
{
printf("\n%d", t->left);
postorder(t->right);
postorder(t->data);
}
}
}
}
int main()
{
node *root;
root=create();
printf("\nThe preorder traversal is: \n");
preorder(root);
printf("\nThe inorder traversal is: \n");
inorder(root);
printf("\nThe postorder traversal is: \n");
postorder(root);
return 0;
}
}
.}
。 You need to close them after writing your function: void foo(){//code for function}
.void foo(){//code for function}
。%d
.%d
打印指针。 If you want to print the value of your node try printf("\\n%d", t->data)
.printf("\\n%d", t->data)
。 If you want to print a pointer try using %p
.%p
。x
.x
。 Formatted code:格式化代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node *left;
struct node *right;
} node;
node *create() {
node *p;
int x;
printf("Enter data (-1 for no data): ");
scanf("%d", &x);
if (x == -1) {
return NULL;
} else {
p = (node *) malloc(sizeof(node));
//save read value
p->data=x;
printf("Enter left child node: \n");
p->left = create();
printf("Enter right child node: \n");
p->right = create();
return p;
}
}
//function is not returning anything so its return type should be void
void preorder(node *t) {
if (t != NULL) {
//key left right
printf("\n%d", t->data);
preorder(t->left);
preorder(t->right);
}
}
void inorder(node *t) {
if (t != NULL) {
//left key right
inorder(t->left);
printf("\n%d", t->data);
inorder(t->right);
}
}
void postorder(node *t) {
if (t != NULL) {
//left right key
postorder(t->right);
postorder(t->left);
printf("\n%d", t->data);
}
}
int main() {
node *root;
root = create();
printf("\nThe preorder traversal is: \n");
preorder(root);
printf("\nThe inorder traversal is: \n");
inorder(root);
printf("\nThe postorder traversal is: \n");
postorder(root);
return 0;
}
Edit: Changed function return type of preorder
, inorder
and postorder
, from int
to void
编辑:将
preorder
、 inorder
和postorder
函数返回类型从int
更改为void
In your preorder
inorder
and postoder
you make errors in the parameters:在您的
preorder
inorder
和postoder
您在参数中出错:
printf("\n%d", t->left); // you can't print a node
inorder(t->data); // you can't traverse an integer
which should be:应该是:
printf("\n%d", t->data);
inorder(t->left);
In create
you forget to assign the data:在
create
您忘记分配数据:
p->data= x;
You may be compiling as C++ instead of C. This explains the undefined references as you have the functions overloaded and the C++ compiler can't find the various overloaded functions.您可能正在编译为 C++ 而不是 C。这解释了未定义的引用,因为您有重载的函数,而 C++ 编译器找不到各种重载的函数。 Compile as C.
编译为 C。
Note: for preorder/inorder/postorder just move the print statement and recursively call the appropriate function.注意:对于前序/中序/后序,只需移动打印语句并递归调用适当的函数。
1. You have defined the function inorder, postorder and preorder in the create function. Place the closing brackets correctly and it will resolve that error.
2. In create function, you are not assigning the data to node.
3. Inorder, postorder and preorder function needs of data type node* but you are passing of data type int at several places. That is the reasons of some warnings.
4. I have written the same code based on your implementation. I complied with gcc and run a test. It seems okay. Have a look at it for your reference.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
typedef struct node node;
node *create()
{
int x;
printf("Enter data (-1 for no data): ");
scanf("%d", &x);
if(x==-1)
{
return NULL;
}
else
{
node *p;
p=(node*)malloc(sizeof(node));
p->data = x;
printf("Enter left child node: \n");
p->left=create();
printf("Enter right child node: \n");
p->right=create();
return p;
}
}
void preorder(node *t)
{
if(t!=NULL)
{
printf("\n%d", t->data);
preorder(t->left);
preorder(t->right);
}
return;
}
void inorder(node *t)
{
if(t != NULL)
{
inorder(t->left);
printf("%d\n", (t->data));
inorder(t->right);
}
return;
}
void postorder(node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\n", (t->data));
}
return;
}
int main()
{
node *root;
root=create();
printf("\nThe preorder traversal is: \n");
preorder(root);
printf("\nThe inorder traversal is: \n");
inorder(root);
printf("\nThe postorder traversal is: \n");
postorder(root);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.