Thread: BST Insert

  1. #1
    Registered User
    Join Date
    Nov 2011

    BST Insert

    So I've managed to properly handle and prepare all the data I want to store (in this case words from a text file) in an ADT style binary search tree and insert without any memory leaks.

    My problem now is making sure that no repeated words are added to the tree. To do this, I am using a retrieve function that was given to us from our text which returns a pointer to the data within the node which contains the desired key if found, or NULL if not found.

    If it returns NULL, then I know that the word is not yet in the tree and I allocate memory for a pointer to the struct to store it and call the insert function (also from the book).

    Otherwise, I use the return value to update the counter field in the data.

    void addWord (BST_TREE *list, char *string, int length)
        WORD *wordPtr;
        void *dataPtr;
        dataPtr = BST_Retrieve(list, (void*)string);
            wordPtr = createWord();
            wordPtr->word = (char*) calloc(length, sizeof(char));
            strncpy(wordPtr->word, string, length);
            if(BST_Insert(list, (void*)wordPtr))
                printf("%s was inserted succesfully (%d)\n", wordPtr->word, wordPtr->count);
                printf("Error! Could not insert\n");
            wordPtr = (WORD*)dataPtr;
            printf("%s was found in the list (%d)\n", wordPtr->word, wordPtr->count);
    My program continues to crash after the first iteration of insertion and it is driving me crazy. To my knowledge, the logic I have implemented should work, but obviously I am missing something.

    Here is how I am calling it
    void getData (BST_TREE *wordBST)
        FILE *fp1;
        char buf[BUFSIZ], *pBuf;
        int length;
        //Open file
        fp1 = fopen(FILE1, "r");
            printf("Error opening file!\n");
        //Read text from file
        while(fscanf(fp1, "%s", &buf) != EOF){
            length = strlen(buf) + 1;
            if(!isalpha(buf[length - 2])){//Get rid of punctuation
                buf[length - 2] = '\0';
            for(pBuf = buf; pBuf < buf + length - 1; pBuf++)//Get all lowercase
                *pBuf = tolower(*pBuf);
            addWord(wordBST, buf, length);
    Any and all feedback is appreciated

  2. #2
    Registered User
    Join Date
    May 2010
    Have you tried running your program through your debugger. The debugger should be able to pinpoint where the problem is being detected. Without the smallest possible complete program that illustrates your problem it is very hard to tell you where your problem is located.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. insert map into map?
    By zxcv in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2011, 01:20 PM
  2. insert
    By nirnir26 in forum C Programming
    Replies: 6
    Last Post: 11-26-2009, 08:14 AM
  3. std::insert
    By Coding in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2008, 02:08 PM
  4. insert another exe into exe
    By lliero in forum C Programming
    Replies: 8
    Last Post: 04-12-2002, 12:22 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM

Tags for this Thread