0

Im working on a program in C that reads a binary tree from a file and opperates functions on it:insert elements,delete elements,search elements,display elements

With my old debugger it seems that i have 4 warnings and i dont know kow to fix them

Here is the code

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct bst
{
    double number;
    struct bst *leftChild;
    struct bst *rightChild;

} node;

node *root = NULL;


void insertNode(double value);
void preOrderPrint(node *root);
node * findMinimum(node *root);
node * deleteNode(node *root, double value);
node* search(node ** tree, double val);

int main()
{
    char line[255];
    char* endptr;
    FILE* f = fopen("tree.txt", "r");
    while (fgets(line, sizeof(line), f))
    {
        if  (*line!='*')
        {
                if(strtod (line, &endptr))
                    insertNode(strtod (line, &endptr));
                    else if (strtoimax(line,&endptr,0))
                    insertNode((strtoimax(line,&endptr,0)));
                    else
                    insertNode( strtol (line, &endptr,0));
        }

    }
    fclose(f);
    int flag = 0, key;
    int choice = 0;
    do
        {
        printf("\nEnter your choice:\n1. Display tree\n2. Insert node\n3. Delete node\n4. Exit\nChoice: ");
        scanf("%d", &choice);
            switch(choice)
            {
            case 1:
                printf("In Order Display\n");
                preOrderPrint(root);
            break;

            case 2:
            printf("Enter key to insert: ");
            scanf("%d",&key);
            flag = search(&root,key);
            if (flag)
                {
                printf("Key found in tree,cannot insert\n");
                }
                else
                {
                printf("Node %d was inserted \n",key);
                insertNode(key);
                preOrderPrint(root);
                }
            break;

            case 3:
            printf("Enter key to delete: ");
            scanf("%d", (int*)&key);
            flag = search(&root, key);
            if (!flag)
                {
                printf("Key not found in tree,cannot delete\n");
                }
                else
                {
                printf("Node %d was deleted \n",key);
                deleteNode(root,key);
                preOrderPrint(root);
                }
            break;
            case 4:
            printf("Memory Cleared\nPROGRAM TERMINATED\n");
            break;
            default: printf("Not a valid input, try again\n");
            }
        }
 while (choice != 5);
    return 0;
}


node * deleteNode(node *currentNode, double value)
{
    if(currentNode==NULL) // empty tree
        return NULL;
    else if(value < currentNode->number) // value is less than node's number. so go to left subtree
        currentNode->leftChild = deleteNode(currentNode->leftChild, value);
    else if(value > currentNode->number) // value is greater than node's number. so go to right subtree
        currentNode->rightChild = deleteNode(currentNode->rightChild, value);
    else // node found. Let's delete it!
    {
        //node has no child
        if(currentNode->leftChild == NULL && currentNode->rightChild == NULL)
        {
            currentNode = NULL;
        }
        else if(currentNode->leftChild == NULL) // node has only right child
        {
            currentNode = currentNode->rightChild;
        }
        else if(currentNode->rightChild == NULL) // node has only left child
        {
            currentNode = currentNode->leftChild;
        }
        else // node has two children
        {
            node *tempNode = findMinimum(currentNode->rightChild);
            currentNode->number = tempNode->number;
            currentNode->rightChild = deleteNode(currentNode->rightChild, tempNode->number);
        }

    }

    return currentNode;
}

node * findMinimum(node *currentNode)
{
    if(currentNode->leftChild==NULL)
        return currentNode;

    return findMinimum(currentNode->leftChild);
}

void insertNode(double value)
{


    node *tempNode;
    node *currentNode;
    node *parentNode;

    tempNode = (node *) malloc(sizeof(node));
    tempNode->number = value;
    tempNode->leftChild = NULL;
    tempNode->rightChild = NULL;

    //For the very first call
    if(root==NULL)
    {
        root = tempNode;
    }
    else
    {
        currentNode = root;
        parentNode = NULL;

        while(1)
        {
            parentNode = currentNode;

            if(value <= parentNode->number)
            {
                currentNode = currentNode->leftChild;

                if(currentNode==NULL)
                {
                    parentNode->leftChild = tempNode;
                    return;
                }
            }
            else
            {
                currentNode = currentNode->rightChild;

                if(currentNode==NULL)
                {
                    parentNode->rightChild = tempNode;
                    return;
                }
            }

        }
    }
}


void preOrderPrint(node *root)
{
    if(root==NULL)
        return;



    preOrderPrint(root->leftChild);
    printf("%g ", root->number);
    preOrderPrint(root->rightChild);
}


node* search(node ** tree, double val)
{
    if(!(*tree))
    {
        return NULL;
    }

    if(val < (*tree)->number)
    {
        search(&((*tree)->leftChild), val);
    }
    else if(val > (*tree)->number)
    {
        search(&((*tree)->rightChild), val);
    }
    else if(val == (*tree)->number)
    {
        return *tree;
    }
    //return true;
}

C:\UsersX\Desktop\39\main.c|32|warning: implicit declaration of function 'strtoimax' [-Wimplicit-function-declaration]|

C:\UsersX\Desktop\39\main.c|56|warning: assignment makes integer from pointer without a cast [-Wint-conversion]|

C:\UsersX\Desktop\39\main.c|72|warning: assignment makes integer from pointer without a cast [-Wint-conversion]|

C:\UsersX\Desktop\39\main.c|224|warning: control reaches end of non-void function [-Wreturn-type]| ||=== Build finished: 0 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|

Also the main question would be why i cannot delete the root? With my debugger it seems that the root can be deleted.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mirel
  • 19
  • 3
  • Please don't mix language tags. If you program in C then use only the C language tag. C and C++ are very different languages. – Some programmer dude Jan 29 '18 at 05:08
  • As for the warnings you get, the `strtoimax` function is not a standard function. Where did you hear about that? And if you declare a function to return a value, then it *must* return a value. Your `search` function does not always do that. As for the other two warnings, what might be the problem there is very hard to know since you don't point out which lines they are on. Please mark out line 56 and 72 somehow, for example by a comment (but I'm guess it has something to do with you assigning the *pointer* result of `search` to an `int` variable). – Some programmer dude Jan 29 '18 at 05:11
  • With if (strtoimax(line,&endptr,0)) i check if a number from file is real or hexadecimal and convert it do decimal..Do you know a better way? – Mirel Jan 29 '18 at 05:14
  • I tried to update the search function(the last else if i replaced only with else) and it still game me the same warning..Isnt compiler supposed to know the end will be reached? – Mirel Jan 29 '18 at 05:16
  • The compiler know when the end of a function is reached, that's why it could warn you that you do not return anything from all paths in the function. For example, what happens if you call `search` recursively? What will be returned then? There's no `return` statement there. – Some programmer dude Jan 29 '18 at 05:20
  • And perhaps [this `strtoimax` reference](http://en.cppreference.com/w/c/string/byte/strtoimax) might help. You seem to be missing a header `#include`. – Some programmer dude Jan 29 '18 at 05:22
  • Thanks you.But i dont know to to correct my search function.I mean that algorithm wise the solution is ok. – Mirel Jan 29 '18 at 05:26
  • On line 56 and 73,i have a int variable that stores the results of the search function.If the elements is in tree,the variable is 1 otherwise is 0.What is wrong with this? – Mirel Jan 29 '18 at 05:30
  • For a function to return a value, you must *explicitly* use the `return` statement with an expression. You don't do that in the two `else` branches. And what is `search` declared to return? Not an integer. – Some programmer dude Jan 29 '18 at 05:44
  • It seems to me that you are in over your head, and that you could need to take a few steps back and perhaps [get a couple of good beginners books](https://stackoverflow.com/a/562377/440558) to read. – Some programmer dude Jan 29 '18 at 05:44
  • Please only put one question into a question. Either address your compiler messages (not debugger warnings) or the deletion problem. – Gerhardh Jan 29 '18 at 08:36
  • Have you fixed this? – Tsakiroglou Fotis Jan 29 '18 at 15:19

0 Answers0