Thread: Please help! Dynamic binary tree problem

  1. #1
    Registered User
    Join Date
    Mar 2005

    Lightbulb Please help! Dynamic binary tree problem

    Hi I am a new C programmer and have produced a program which reads in a file and prints it out. I now need to produce a dynamically built binary tree data structure to hold results.
    The program should read the data for a competitor, place this in a new structure (using memory space acquired using malloc() or calloc()) and then insert this new structure in the appropriate place within the dynamically built binary tree of structures. I am quite baffled and not sure where to start
    Any ideas would be greatly appreciated x
    Here is the code so far:

    This is the header file
    /* Field definitions for competitor structure*/
    typedef struct competitor
    char name[80];
    char address[100];
    char phone[30];
    int comp_number;
    int river_ounces;
    int sea_ounces;
    int fly_ounces;
    } data;
    #include <stdio.h>
    #include "fishing.h"
    FILE * competition;
    char oneword[79], filename[20];
    char *c;
    /* Prompt user to enter filename */
    printf("Please enter name of file \n\n");
    scanf("%s", filename);
    /* Open file */
    competition = fopen(filename, "r");
    /* If fopen() fails it returns a NULL pointer so
    this must always be checked for when opening a file */
    if (competition == NULL)
    printf("Error opening file\n");
    /* Reads one line from the file at a time */
    c=fgets(oneword, 79, competition);
    if (c != NULL)
    /* Display file */
    printf ("%s", oneword);
    while (c != NULL); /* Repeat until there are no more lines to read from */
    Here is the text file containing the competitor details and results:

    Spring Fishing.
    14th April 2005.
    Margaret Mouse
    Skirting Board House, Mosehole, Devon. DV1 2SS
    Southern 9365
    1 0 1
    2 7 13
    0 4 5
    Bert Hill
    14, Priory Lane, Birmingham, West Midlands. B19 1RU
    Central 2000
    0 12 9
    1 11 5
    1 10 4
    Donald Duck
    Village Pond, Duckington-by-Sea. S11 1QQ
    Marine 123456
    1 13 10
    4 7 15
    2 5 7

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    So how exactly are you stuck? If you don't know how a tree works, consider reading the tutorial on trees in the FAQ.

    It looks like you haven't done anything at all other than taking the framework you were given for your homework assignment and post it here. Well, walk through it step by step how you'd do it yourself, and then think of how you can break it down into codeable steps.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    You should start with defining a 'node' of your binary tree data structure. One way of doing this is something like:

    struct node 
        data value;
        struct node* left;
        struct node* right;
    Next you should try finding out how to extract and put the values inside a tree node, then I would probably start worrying about insertion nodes into a binary tree. Obviously to test if the insertion function works (aside from getting compile errors) is to create a output function, anything simple like a print tree function should do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in creating a tree
    By Lorin_sz in forum C Programming
    Replies: 1
    Last Post: 09-26-2005, 01:24 PM
  2. Binary Search tree problem
    By berryski in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2005, 12:47 PM
  3. problem in binary expression tree
    By hanij in forum C Programming
    Replies: 6
    Last Post: 04-28-2002, 08:31 AM
  4. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 08:02 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM