[英]Implementing 'insert' function for binary tree in C
我正在尝试在 C 中为二叉树实现通常的“插入”函数,但我无法弄清楚我的代码在哪里出错了(当然,这是错误的,因为它不起作用......) .
有人可以帮我弄清楚我做错了什么吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node *left;
struct node *right;
int data;
} node;
void insert (node *root, int n);
void print (node *root);
int main(void){
node *root = malloc(sizeof(node));
root->left = root->right = NULL;
root->data = 50;
insert (root, 1);
}
void insert (node *root, int n){
node *cursor = root;
while (cursor != NULL){
if (n <= cursor->data){
cursor = cursor->left;
printf("left\n");
}
else if (cursor->data < n){
cursor = cursor->right;
printf("right\n");
}
else {
printf("Invalid data in the tree.\n");
return;
}
}
cursor = malloc(sizeof(node));
printf("%p\n", root->left);
cursor->data = n;
cursor->left = NULL;
cursor->right = NULL;
}
void print (node* root){
if (root == NULL){
return;
}
print(root->left);
printf("%i ", root->data);
print(root->right);
}
您正在分配给cursor
并且cursor->left
或cursor->right
未分配。 确保新分配的节点由cursor->left
或cursor->right
指向。
void insert (node *root, int n){
node *cursor = root;
while (cursor != NULL){
if (n <= cursor->data){ /* Change 1 follows */
printf("left\n");
if(cursor->left == NULL) {
cursor->left = malloc(sizeof(node));
cursor = cursor->left;
break;
}
}
else if (cursor->data < n){
printf("right\n");
if(cursor->right == NULL) { /* Change 2 follows */
cursor->right = malloc(sizeof(node));
cursor = cursor->right;
break;
}
}
else {
printf("Invalid data in the tree.\n");
return;
}
}
/* Change 3 : 1 line deleted */
printf("%p\n", root->left); /* Why? */
cursor->data = n;
cursor->left = NULL;
cursor->right = NULL;
}
您需要保留对前一个cursor
的引用。 需要找到cursor->data
为空的地方; 但是,如果您要查找cursor
本身为空的位置,它不会为您做任何事情,因为cursor
不是对前一个cursor->data
的引用,而是一个副本:当您更改cursor
,您的结构中没有任何变化.
您的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node *left;
struct node *right;
int data;
} node;
void insert (node *root, int n);
void print (node *root);
int main(void){
node *root = malloc(sizeof(node));
root->left = root->right = NULL;
root->data = 50;
insert (root, 1);
}
void insert (node *root, int n){
node *cursor = root;
while (cursor != NULL){
if (n <= cursor->data){
cursor = cursor->left;
printf("left\n");
}else if (cursor->data < n){
cursor = cursor->right;
printf("right\n");
}else {
printf("Invalid data in the tree.\n");
return;
}
}
cursor = malloc(sizeof(node));
printf("%p\n", root->left);
cursor->data = n;
cursor->left = NULL;
cursor->right = NULL;
}
void print (node* root){
if (root == NULL){
return;
}
print(root->left);
printf("%i ", root->data);
print(root->right);
}
假设您想在下面的树中添加 1
5
/ \
2 8
您的代码有一个游标指针。
该游标指针指向 5,然后是 2,然后是 NULL。
当它达到 NULL 时,您分配新内存,并且光标指向该新内存。
就是这样。 你还没有做出你想做的。
光标指针不是树的一部分。 它只是一个用于访问树节点的变量。
现在节点 *left 和节点 *right 是树的一部分,这些是必须分配内存的节点。
您的问题的一种解决方案是以下代码:
void insert (node **root, int n){
node *cursor = *root;
while (cursor != NULL){
if (n <= cursor->data){
cursor = *(root=&cursor->left);
printf("left\n");
}else if (cursor->data < n){
cursor = *(root=&cursor->right);
printf("right\n");
}else {
printf("Invalid data in the tree.\n");
return;
}
}
*root = malloc(sizeof(node));
cursor=*root;
cursor->data = n;
cursor->left = NULL;
cursor->right = NULL;
}
int main(void){
node *root = NULL;
insert (&root, 1);
}
进一步改进:
void insert (node **root, int n){
node *cursor = *root;
while (cursor != NULL) cursor=*(root=(n<cursor->data)?&cursor->left:&cursor->right);
cursor=*root=malloc(sizeof(node));
cursor->data = n;
cursor->left = NULL;
cursor->right = NULL;
}
int main(void){
node *root = NULL;
insert (&root, 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.