gpt4 book ai didi

c - C中二叉树遍历没有输出

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:39 26 4
gpt4 key购买 nike

我是一名菜鸟程序员,我有一个项目需要使用二叉树。我所要做的就是插入一个节点,删除一个节点并添加树遍历的两种方法。问题是我无法让我的代码工作。我决定添加一些辅助函数,例如 check_element 和 create_NewNode 来帮助我更轻松地实现。运行后我的控制台上没有输出,或者它只是用运行时错误来问候我。我有一个头文件,IO.c 文件来存储我的函数和 main.c. 来测试头文件实现的函数。

Here is IO.c , used to store the functions.



#include <stdio.h>
#include <stdlib.h>
#include "Library.h"

struct node{
int data;
struct node *left;
struct node *right;

};

struct node *root;


/*-----------------------------------------------------------------------------------------*/

//function to determine if an element is already in the tree
void check_element( struct node *node, int value)
{
while( node != NULL ){
//checking if the value is here
if( value == node->data ){
printf("The element %d already exists in the tree!",value);
exit(0);
//if the value is smaller, go left
}else if( value < node->data ){
check_element( node->left, value );
//else go right
}else if( value > node->data ){
check_element( node->right, value );
//else the element was not found and we can add it to th tree
}else{
printf("Adding the element %d to the tree.",value);
exit(0);
}

}//end while

}//end check_element



/*-----------------------------------------------------------------------------------------*/


//helper function to crate a new node and set left and right pointers to NULL
struct node *create_NewNode( int value )
{
struct node *ptr;
struct node *temp = (struct node*)malloc(sizeof(struct node));
ptr = (struct node*)malloc(sizeof(struct node));

if( ptr == NULL){
printf("Memory allocation error!");
exit(-1);
}

//assigning the data to the newly created node
temp->data = value;

//setting left and right pointers to NULL
temp->left = NULL;
temp->right = NULL;

return temp;
}


/*-----------------------------------------------------------------------------------------*/


//function to add a new value to the tree;
struct node *insert_value( struct node *node, int new_value )
{

//checking if the element already exists to the tree
check_element( node, new_value );

//checking if the tree is empty
if( node == NULL ){
node = create_NewNode( new_value );
//if the value is smaller, we add it to the left
}else if( new_value < node->data ){
insert_value( node->left, new_value );
//else we add it to the right
}else{
insert_value( node->right, new_value );
}

return node;

}


/*-----------------------------------------------------------------------------------------*/


void printPostorder( struct node *node )
{
if (node == NULL){
printf("The tree is empty!");
exit(0);
}else{
//first go left
printPostorder( node->left );

//then go right
printPostorder( node->right );

//finally, print the node value
printf("%d", node->data);

}
}


/*-----------------------------------------------------------------------------------------*/


void inorder_traversal( struct node *node )
{
if( node == NULL) {
printf("The tree is empty!");
exit(0);
}else{
//first go left
inorder_traversal( node->left );

//print the node value
printf("%d ",node->data);

//then go right
inorder_traversal( node->right );

}
}


/*-----------------------------------------------------------------------------------------*/




This is the header file, called Library.h



//prototype for NewNode
struct node *create_NewNode( int value );
//prototype for insert_value
struct node *insert_value( struct node *node, int new_value );
//prototype for printPostordre
void printPostorder( struct node* node);
//prototype for printInorder
void inorder_traversal( struct node *node );
//prototype for check_element
void check_element( struct node *node, int value);



And, finally, the main.c: `enter code here:



#include <stdio.h>
#include <stdlib.h>
#include "Library.h"

int main()
{
// TEST CODE

struct node *root;
root = NULL;

insert_value(root, 1);
insert_value(root, 2);
insert_value(root, 3);


printPostorder( root );
inorder_traversal( root );







return 0;
}


PS: I did my best to write this code, but, as I said, I am a rookie and I'm pretty bad at coding.I'd also like to appologize for any grammar mistakes, i am not an englishman.

最佳答案

可能还有更多错误,但我阅读了create_node(),并希望就此提出建议:

struct node *create_NewNode(  int value )
{
struct node *ptr;
struct node *temp = malloc(sizeof(struct node));
ptr = malloc(sizeof(struct node));

if( ptr == NULL){
printf("Memory allocation error!");
exit(-1);
}

//assigning the data to the newly created node
temp->data = value;

//setting left and right pointers to NULL
temp->left = NULL;
temp->right = NULL;

return temp;
}

此处您要创建两个节点,而您打算只创建一个。您将 temp 视为新的,而您忘记了 ptr。您不需要创建另一个节点!

您需要的是树的指针,以便您在该树上添加新构造的节点(因此您可以将另一个参数传递给该函数,即树的指针)。

顺便说一句,我删除了 malloc 的转换,如 Do I cast the result of malloc? 中所述


我建议尝试根据我的建议修复您的代码。但是,我会将您链接到 treemanagement.c ,即 一棵树的代码,完整的注释,这是我了解这种数据结构的方式,以后可能会派上用场!

关于c - C中二叉树遍历没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43950914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com