gpt4 book ai didi

c - 插入二叉树时出现错误?

转载 作者:行者123 更新时间:2023-11-30 21:25:35 25 4
gpt4 key购买 nike

我有这段代码,但无法找出其中的错误,或者我缺少一些边缘情况。它是使用字典插入二叉树。我不是指针专家,可能存在内存问题或其他问题。

/* just.h 
typedef struct node *tree_ptr;
typedef struct table *Table;
*/

#include <stdio.h>
#include <stdlib.h>
#include "just.h"
#include <string.h>

int comp(int x, int y ){
if (x>y) return 1;
else if (x<y) return -1;
return 0;
}
struct node {
int age;
tree_ptr left;
tree_ptr right;
tree_ptr next;
};

struct table {
tree_ptr head;
};

tree_ptr create(int age ) {
tree_ptr t = malloc(sizeof(*t));
t->age = age;
t->left = t->right = NULL;
return t;
}

Table init() {
Table tt = malloc(sizeof(tree_ptr));
tt->head = NULL;
return tt;
}
Table insert(Table temp ,int age) {
// Table node = init();
// node->head = create(age);
// node->head->next = temp->head;
// return node;
Table node = temp;
if(node == NULL || temp==NULL || temp->head == NULL || node- >head==NULL) {
//node = init();
tree_ptr t = create(age);
node->head = t;
temp->head = t ;
printf("wow %d \n",node->head->age);
return node;
}
else {
// if(node!=NULL) {
int cmp = comp(node->head->age,age);
printf("%d \n ", cmp);
if(cmp < 0) {
if(node->head->left==NULL) {
tree_ptr t = create(age);
//node->head = malloc(sizeof(tree_ptr));
node->head->left = t;
printf(" left null %d \n",node->head->left->age);
return node;
}
else {
printf("wow added to left branch %d \n",node->head->left->age);
return insert((Table)node->head->left,age);
}
}
else if(cmp > 0) {
if(node->head->right==NULL) {
tree_ptr t = create(age);
//node = malloc(sizeof(tree_ptr));
node->head->right = t;
printf(" right null %d \n",node->head->right->age);
return node;
}
else {
printf("wow added to right branch %d \n",node->head->right->age);
return insert((Table)node->head->right,age);
}
}
else return NULL;
}
}

void print_table(tree_ptr temp ) {
if(temp==NULL)
return;
print_table(temp->left);
printf("%d \n ", temp->age);
print_table(temp->right);
}

int main(int argc, char * argv[]) {
Table tb;
tb = init();
//tb->head = malloc(sizeof(tree_ptr));
//if(tb->head == NULL) {
// printf("Why you are null??");
// tb->head = create(5);
//}
for(int i=0;i<6;i++)
tb = insert(tb,i);
// print_table(tb->head);
// while(tb->head!=NULL) {
// printf("%d \n", tb->head->age);
// tb->head = tb->head->next;
// }
return 0;
}

最佳答案

去掉ptr的隐藏并修复insert

#include <stdio.h>                                                             
#include <stdlib.h>
#include <string.h>

int comp(int x, int y ){
if (x>y) return 1;
else if (x<y) return -1;
return 0;
}
typedef struct node node;

struct node {
int age;
node *left;
node *right;
};

typedef struct table {
node *head;
} table;

node *create(int age) {
node *t = malloc(sizeof(*t));
t->age = age;
t->left = t->right = NULL;
return t;
}

table *init(table *tt) {
tt->head = NULL;
return tt;
}
node *insert(node **_node ,int age) {
// Table node = init();
// node->head = create(age);
// node->head->next = temp->head;
// return node;
node *node = *_node;
if(!node) {
//node = init();
node = create(age);
printf("wow %d \n",node->age);
*_node = node;
return node;
}
else {
// if(node!=NULL) {
int cmp = comp(node->age,age);
printf("%d \n ", cmp);
if(cmp < 0) {
if(node->left==NULL) {
node->left = create(age);
//node->head = malloc(sizeof(tree_ptr));
printf(" left null %d \n",node->left->age);
return node;
}
else {
printf("wow added to left branch %d \n",node->left->age);
return insert(&node->left,age);
}
}
else if(cmp > 0) {
if(node->right==NULL) {
node->right = create(age);
printf(" right null %d \n",node->right->age);
return node;
}
else {
printf("wow added to right branch %d \n",node->right->age);
return insert(&node->right,age);
}
}
else return NULL;
}
}

void print_table(node *temp) {
if(temp==NULL)
return;
print_table(temp->left);
printf("%d \n ", temp->age);
print_table(temp->right);
}
int main(int argc, char * argv[]) {
table tb;
init(&tb);
//tb->head = malloc(sizeof(tree_ptr));
//if(tb->head == NULL) {
// printf("Why you are null??");
// tb->head = create(5);
//}
for(int i=0;i<6;i++) {
insert(&tb.head,i * 7);
}
for(int i=0;i<6;i++) {
insert(&tb.head,i * 3);
}
print_table(tb.head);
// while(tb->head!=NULL) {
// printf("%d \n", tb->head->age);
// tb->head = tb->head->next;
// }
return 0;
}

关于c - 插入二叉树时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28801444/

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