Binary Tree doesn't print it's elements

This is a discussion on Binary Tree doesn't print it's elements within the C Programming forums, part of the General Programming Boards category; Hi guys, I want to read a file full of names and put every name in a sorted binary tree. ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Binary Tree doesn't print it's elements

    Hi guys,

    I want to read a file full of names and put every name in a sorted binary tree.

    I managed to read every name and "store" it in the tree, but when I want to print all
    names, it doesn't print any of them.

    Here's the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXLEN 32
    
    static FILE *file;
    
    struct tnode {
        char *name;
        int count;
        struct tnode *left;
        struct tnode *right;
    };
    
    struct tnode *addtree(struct tnode *, char *);
    void treeprint(struct tnode *);
    int getname(char *, int);
    
    int main()
    {
        struct tnode *root;
        char name[MAXLEN];
    
        file = fopen("names.txt", "r");
        root = NULL;
        while(getname(name, MAXLEN) != EOF)
            if(isalpha(name[0]))
                addtree(root, name);
    
        treeprint(root);
        return 0;
    }
    
    struct tnode *addtree(struct tnode *p, char *w)
    {
        int cond;
    
        if(p == NULL) {
            p = (struct tnode *) malloc(sizeof(struct tnode));
            p -> name = strdup(w);
            //printf("%s", p -> name);
            p -> count = 1;
            p -> left = p -> right = NULL;
        } else if((cond = strcmp(w, p -> name)))
            p -> count++;
        else if(cond < 0)
            p -> left = addtree(p -> left, w);
        else
            p -> right = addtree(p -> right, w);
    }
    
    void treeprint(struct tnode *p)
    {
        if(p != NULL) {
            treeprint(p -> left);
            printf("%s\n", p -> name);
            treeprint(p -> right);
        }
    }
    
    int getname(char *name, int lim)
    {
        int c;
        char *w = name;
    
        while(--lim > 0 && (c = getc(file)) != EOF && isalpha(c))
            *w++ = c;
    
        if(c == EOF) {
            ungetc(c, file);
            return EOF;
        }
    
        *w = '\0';
        return name[0];
    }
    The file is in the attachment.

    I hope someone can point out my mistake(s).

    greetz
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    First, fix this:
    Code:
    $ gcc -Wall main.c
    main.c: In function ‘addtree’:
    main.c:40: warning: implicit declaration of function ‘malloc’
    main.c:40: warning: incompatible implicit declaration of built-in function ‘malloc’
    main.c:51: warning: control reaches end of non-void function
    You need to #include stdlib.h for malloc and return p from your add function.

    Then:
    Code:
        while(getname(name, MAXLEN) != EOF)
            if(isalpha(name[0])) {
                root = addtree(root, name);
    You need to assign the result of addtree to root to pass back the tree. You don't pass in the address of root, so you can't change what it points at from inside addtree.


    Code:
        } else if((cond = strcmp(w, p -> name)))
            p -> count++;
        else if(cond < 0)
            p -> left = addtree(p -> left, w);
        else
            p -> right = addtree(p -> right, w);
    Read your strcmp docs.
    This checks if the strings are NOT equal (any non-zero value).
    This checks if the first string is less than the second, but it never happens because of that.
    This last case, which should be when the new name is larger than the current node, actually only executes if the names are the same. Specifically, if cond is non-zero (implied by that), and not less than zero (implied by that).

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Thank you very much anduril462!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print all nodes of the same level from binary tree
    By budala in forum C Programming
    Replies: 3
    Last Post: 09-08-2010, 07:31 AM
  2. how to print a binary tree..
    By transgalactic2 in forum C Programming
    Replies: 6
    Last Post: 11-19-2008, 01:53 PM
  3. Insert elements in binary tree
    By lastrial in forum C Programming
    Replies: 3
    Last Post: 05-23-2007, 11:22 AM
  4. print a binary tree!
    By basilis in forum C Programming
    Replies: 1
    Last Post: 08-26-2001, 11:36 AM
  5. print a binary tree!
    By basilis in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-26-2001, 08:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21