Thread: Insertion of tree nodes embedded in a list

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Insertion of tree nodes embedded in a list

    Code:
    struct t_node
    {
        char word[20];
        int wordAppears;
        
        struct t_node *left;
        struct t_node *right;
    };
    typedef struct t_node TREE_NODE;
    
    struct t_tree
    {
        TREE_NODE *root;
    };
    typedef struct t_tree TREE;
    
    struct node
    {
        char title[50];
        int height, wordsInTree;
        TREE *tree;
        
        struct node *next;
    };
    typedef struct node LIST_NODE;
    
    struct list
    {
        LIST_NODE *head;
    };
    typedef struct list LIST;
    
    TREE_NODE *addTreeNode(char *string, TREE_NODE *pCurrent)
    {
        if(pCurrent == NULL) //if the root doesn't exist yet
        {
            pCurrent = (TREE_NODE *)(malloc(sizeof(TREE_NODE))); //create new node
            pCurrent->wordAppears = 0;
            pCurrent->left = NULL;
            pCurrent->right = NULL;
            strcpy(pCurrent->word, string);
        }
        else if(strcmp(string, pCurrent->word) < 0)
            //word is smaller, send left
            addTreeNode(string, pCurrent->left);
        else if(strcmp(string, pCurrent->word) > 0)
            addTreeNode(string, pCurrent->right);
        else
            //we have a match and we should increment the word counter
            pCurrent->wordAppears++;
       
       return pCurrent;
    }
    
    void loadData(FILE *file, LIST *chapters)
    {
        LIST_NODE *currentChapter;
        int totalChapters = 0;
        int i = 0;
        int wordsInTree = 0;
        char word[20];
        
        fscanf(file, "%d\n", &totalChapters);
        
        currentChapter = chapters->head; //initialize outside loop
        while(i < totalChapters)
        {
            if(currentChapter == NULL) //this is the first chapter
            {
                currentChapter = (LIST_NODE *)(malloc(sizeof(LIST_NODE)));
                currentChapter->tree = NULL;
                currentChapter->wordsInTree = 0;
                currentChapter->height = 0;
            }
            //create the tree
            currentChapter->tree = (TREE *)(malloc(sizeof(TREE)));
            currentChapter->tree->root = NULL;
    
            //read title and the number of words in the file
            fscanf(file, "%s %d", &currentChapter->title, &currentChapter->wordsInTree);
            wordsInTree = currentChapter->wordsInTree;
            
            //currentChapter->height = heightOfTree(currentChapter->wordsInTree);
            
            //fill the tree
            while(wordsInTree > 0)
            {
                fscanf(file, "%s", &word);
                //printf("%s\n", word);
                addTreeNode(word, currentChapter->tree->root);
                wordsInTree--;
            }
            
            if(i == 0)
            	chapters->head = currentChapter; //fill head node
            
            currentChapter->next = (LIST_NODE *)(malloc(sizeof(LIST_NODE)));
            currentChapter = currentChapter->next; //move to next node
            i++;
        }
        
        //printf("%s\n", chapters->head->height);
        /*printf("%s\n", chapters->head->next->title);
        printf("%s\n", chapters->head->next->next->title);
        printf("%s\n", chapters->head->next->next->next->title);*/
    }
    Everything loads in the list fine but inside the loop to add the words to the tree inside the list the root is always returned as null when trying to print. I'm using a standard insert and I don't see any pointing problems even though from the output that is most likely what it is. Any help at "pointing" out my blind spots would be appreciated. The pun is stupid, I'm drunk on programming.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try say
    pCurrent->left = addTreeNode(string, pCurrent->left);

    You're ignoring the all important return result when you actually create a new node.
    There's a lot of ignored return results.

    And remove all those malloc casts, this is C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource manager tree
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 09-07-2007, 10:27 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM