Thread: Binary Search Tree Adding Error

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    Binary Search Tree Adding Error

    Ello, I'm doing a program and I've run into an error. The program details as follows: I'm making at BST that stores the name of a person, and their favorite game. Functions are add to tree, remove from tree, print tree, etc. The command (ADD, REMOVE, etc.) and the person's name / favorite game is read in from an input file. Everything works fine, except after it stops adding and maybe wants to print or remove something from the tree, if it tries to add another node to the list it doesn't actually show up when printing it. Any help would be nice, if you need any missing functions just ask.

    Here is the sample input file:
    Code:
    ADD KAREN MONOPOLY
    ADD MARK SCRABBLE
    ADD JOHN MONOPOLY
    ADD SASHA WAR
    ADD COURTNEY SCRABBLE
    PRINT
    REMOVE SASHA
    ADD DAVID MONOPOLY
    PRINTGAME MONOPOLY
    PART2
    Here is some relevant code:

    Code:
    struct person
    {
        char name[20];
        char game[20];
    };
    
    struct gameTree
    {
        struct person people;
        struct gameTree * left;
        struct gameTree * right;
    };
    Code:
    struct gameTree * searchTree = NULL;
        struct gameTree * tempNode =  NULL;
    
        char command[20], name[20], game[20];
    
        while(1)
        {
            fscanf(ifp, "%s", command);
    
            if(strcmp(command, "PART2") == 0)
            {
                printf("Ended Part 1\n");
    
                break;
            }
    
            else if(strcmp(command, "ADD") == 0)
            {
                    printf("Entered Add\n");
    
                    fscanf(ifp, "%s", name);
                    fscanf(ifp, "%s", game);
                    tempNode = createNode(name, game);
                    searchTree = insertNode(searchTree, tempNode);
            }
    
            else if(strcmp(command, "REMOVE") == 0)
            {
                printf("Entered Remove\n");
    
                fscanf(ifp, "%s", name);
                searchTree = deleteNode(searchTree, name);
            }
    
            else if(strcmp(command, "PRINTGAME") == 0)
            {
                printf("Entered Print Game\n");
    
                fscanf(ifp, "%s", game);
                fprintf(ofp, "%s:\n", game);
                printGame(searchTree, game, ofp);
            }
    
            else if(strcmp(command, "PRINT") == 0)
            {
                printf("Entered Print\n");
    
                fprintf(ofp, "PRINT:\n");
                printTree(searchTree, ofp);
                fprintf(ofp, "\n");
            }
        }
    
        return 0;
    Code:
    struct gameTree * createNode(char name[], char game[])
    {
        struct gameTree * temp = (struct gameTree *)malloc(sizeof(struct gameTree));
    
        strcpy(temp->people.name, name);
        strcpy(temp->people.game, game);
    
        temp->left = NULL;
        temp->right = NULL;
    
        return temp;
    }
    Code:
    struct gameTree * insertNode(struct gameTree * root, struct gameTree * element)
    {
        if(root == NULL)
        {
            return element;
        }
    
        else
        {
            if(strcmp(element->people.name, root->people.name) > 0)
            {
                root->right = insertNode(root->right, element);
            }
    
            else if(strcmp(element->people.name, root->people.name) < 0)
            {
                root->left = insertNode(root->left, element);
            }
    
            return root;
        }
    }
    Here is the correct output (left) compared to mine (right)
    Code:
    PRINT:                     PRINT:
    COURTNEY SCRABBLE          COURTNEY SCRABBLE
    JOHN MONOPOLY              JOHN MONOPOLY
    KAREN MONOPOLY             KAREN MONOPOLY
    MARK SCRABBLE              MARK SCRABBLE
    SASHA WAR                  SASHA WAR
    
    MONOPOLY:                  MONOPOLY:
    DAVID                      JOHN
    JOHN                       KAREN
    KAREN

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Bah, fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Replies: 2
    Last Post: 12-01-2007, 10:44 PM
  3. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM