I'm trying to create a BST tree using C Language, more specifically for an exercise in my college. The program needs to read from an input file some integers, do some operations on the tree, and write the new numbers into another file (an output file).
However, the program only reads up to the fifth line of the file and then it crashes, causing exit code 3221226356. I don't understand much of pointers so I would like some help finding the errors.
This is the code atm (the code was poorly translated, just the function names, but you guys should get the idea behind the printf's).
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d\n", root->key);
inorder(root->right);
}
}
void increaseVector(int *vector_int, int size)
{
int *temp = realloc(vector_int, ((int)(size+1) * sizeof(int)));
if(temp == NULL){
printf("Erro ao increaser o vector.\n");
}
else{
vector_int = temp;
}
}
struct node *insert(struct node* node, int key)
{
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
struct node *lesserNodeValue(struct node* node)
{
struct node* currently = node;
while (currently->left != NULL)
currently = currently->left;
return currently;
}
struct node *deleteNode(struct node* root, int key)
{
if (root == NULL) return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = malloc(sizeof(struct node));
temp = lesserNodeValue(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
int middleVector(int *vector, int size){
if(size == 0){
return 0;
}
else if(size%2 == 0){
int middle = (int) size/2;
}
else{
int middle = (int) (size-1)/2;
}
}
void printFile(int *vector_int, int *size)
{
printf("sizeanho do size: %d\n\n",*size);
FILE *entrada;
int tempor = 0;
entrada = fopen("arquivo entrada.txt","r");
if(entrada==NULL){
printf("Erro ao abrir o arquivo");
}
else{
while (fscanf(entrada, "%d", &tempor) != EOF)
{
printf("sizeanho do size: %d\n\n",*size);
increaseVector(vector_int, *size);
printf("size antes de atribuir ao vector: %d\n\n",*size);
vector_int[*size] = tempor;
printf("tempor[%d] = %d\n",*size,tempor);
*size = (int)*size+1;
}
printf("Saiu do WHILE\n\n");
fclose (entrada);
}
}
int main()
{
struct node *root = NULL;
int *vector_int = (int *)malloc(NULL);
int size = 0;
int i;
printFile(vector_int, &size);
printf("sizeanho apos printFile: %d\n\n",size);
int posCentralVector = middleVector(vector_int,size);
printf("middle do vector: %d\n\n", posCentralVector);
root = insert(root, (int)vector_int[posCentralVector]);
for(i=0; i<size; i++)
if(vector_int[i] != root) insert(root, vector_int[i]);
/*root = insert(root, 50);
root = insert(root, 350);
root = deleteNode(root, 10);
root = deleteNode(root, 96);*/
inorder(root);
}
The input file is just a bunch of random crescent numbers.
10 20 27 34 41 44 46 53 60 68 70 73 82 87 96 101 110 113 122 124 127 133 141 145 147 154 155 157 162 163