Thread: Binary Search Tree, Inserting node

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Question Binary Search Tree, Inserting node

    Hi, this is my program

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    typedef struct record
    {
            char key[30];
            int count;
    }DATA;
    typedef DATA* DATAPTR;
    
    
    typedef struct tree
    {
            DATA *word;
            struct tree *left;
            struct tree *right;
    }TREENODE, *TREE;
    
    TREE insert_node(TREE , TREE, DATA *);
    
    TREE root;
    TREE current;
    
    int main(int argc, char *argv[])
    {
               
               
               DATAPTR new_data;
               DATAPTR root_data;
               
              
               FILE * input;
               input = fopen("bst.txt", "r");
    
             new_data = (DATAPTR)malloc(sizeof(DATA));
    
              while ((fscanf(input, "%s", new_data->key))!=EOF)
              {
                       insert_node(root, current, new_data);
              }
               fclose(input);
    
             getch();
              return 0;
    }
    
    
    TREE insert_node(TREE root, TREE current, DATAPTR ins_data)
    {
        TREE temp;
    
        if(!current)
        {
           current = (TREE)malloc(sizeof(TREENODE));
           if (current)
           {
                    current->word = ins_data;
                    current->left = NULL;
                    current->right = NULL;
           }
        }
        if(!root)   //empty tree
        {
            return current;
        }
        else
        {
            temp = root;
            if ((strcmp(ins_data->key, temp->word->key))<0)
            {
                    if(temp->left == NULL)
                    {
                          temp->left = current;
                    }
                    else
                    {
                            insert_node(temp->left, current, ins_data->key);
                    }
            }
            else if((strcmp(current->word, temp -> word->key))<0)
            {
                    if (temp -> right == NULL)
                    {
                            temp->right = current;
                    }
                    else
                    {
                            insert_node(temp->right, current, ins_data->key);
                    }
            }
            else root->word->count++;      
            return root;
    
        }
    }
    But i don't know why every time when it exits from the insert_node function, the values of both TREE root and TREE current changed back to NULL.

    I've been spending the whole day on this problem. Can anyone help me please, thanks a lot
    Last edited by cheryl_li; 09-13-2003 at 03:51 AM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    SOLVED

    THANKS
    PLEASE IGNORE THIS THREAD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in binary search
    By LINUX in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2009, 08:50 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM