Я пытаюсь понять, как вставить большое количество чисел в двоичное дерево. Программа начинается с запроса номера и вставляет 0 в любой номер, введенный пользователем. Моя программа работает, но она начинает крушить около 40 000, где я получаю ошибку: Возвращенный процесс -1073741571 (0XC00000FD)Loop insert binary tree
Я новичок в управлении C и памятью, но я считаю, что проблема там. любой совет?
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node BSTREE;
struct node
{
int data;
struct node * left;
struct node * right;
};
void insert(BSTREE ** root, int number);
int main()
{
BSTREE *root = malloc(sizeof *root);
BSTREE *tmp = malloc(sizeof *root);
root = NULL;
int x;
int input;
FILE *fp;
fp=fopen("c:\\test2.txt", "w");
printf("Please enter a number: ");
scanf("%d", &input);
for (x = 0; x < input; x++)
{
insert(&root, x);
}
printf("%d", x);
free(root);
fclose(fp);
}
void insert(BSTREE ** root, long int number)
{
BSTREE *temp = NULL;
if(!(*root))
{
temp = (BSTREE *)malloc(sizeof(BSTREE));
temp->left = temp->right = NULL;
temp->data = number;
*root = temp;
return;
}
if(number < (*root)->data)
{
insert(&(*root)->left, number);
free(root);
}
else if(number > (*root)->data)
{
insert(&(*root)->right, number);
free(root);
}
free(root);
}
'free (root);' at 'insert' ?? и может стекать переполнение. – BLUEPIXY
'long int number' ->' int number', 'BSTREE * root = malloc (sizeof * root); BSTREE * tmp = malloc (sizeof * root); root = NULL; 'утечка памяти. – BLUEPIXY