1

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

Output

SecretX
  • 61
  • 6
  • Could we have the input data as well? It'll make it easier to trace back the error. – Dillon Davis Jun 29 '18 at 22:52
  • Also, one problem I see- in `deleteNode`, you `free(root)`, and the try to dereference it to pass its right child to `lesserNodeValue`. – Dillon Davis Jun 29 '18 at 22:54
  • You are not checking the return code for any call to `malloc`. Do that. It may be the clue you need. – klutt Jun 29 '18 at 23:03
  • Also, check all warnings. When I compile your code with `-Wall -Wextra` I get 6 warnings. – klutt Jun 29 '18 at 23:05
  • You say that you read the file to fifth line an dthen you get an error. The example file you have posted is only one line. Post a file that causes the error you're describing. – klutt Jun 29 '18 at 23:11
  • 3
    increaseVector never updates vector_int since it is passed by value. You call to realloc likely works, but the statement "vector_int = temp" only updates a local copy of the pointer on the stack. Your first parameter should be "int **vector_int", and you should call realloc with the first parameter "*vector_int". Better still, don't bother with the vector: read the numbers from the file and insert directly into the binary tree as you go. – ScottK Jun 30 '18 at 00:03
  • 1
    Note that unless you're careful, reading the numbers in sorted order leads to a woefully unbalanced BST. – Jonathan Leffler Jun 30 '18 at 00:08
  • Good point @JonathanLeffler. I had skimmed over the BST requirement, and hadn't noticed that the numbers were sorted, so it would end up being an expensive linked list! https://stackoverflow.com/a/6380314/6693299 Having said that, given this is a BST implementation, then insert should balance the tree anyway. Reading in a sorted list in an order that produces a BST tree seems like a cheat and probably not what the prof wanted. – ScottK Jun 30 '18 at 00:27
  • You can compile your program with `gcc -g`, then use `gdb` to debug. I have implemented a set by BST, which may help you. https://github.com/foreverpersist/set – Joy Allen Jun 30 '18 at 08:56

0 Answers0