GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Mar-2005, 09:33
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Unhappy

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:

CPP / C++ / C Code:
/*
   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;[/quote]

CPP / C++ / C Code:
#include <stdio.h>
#include "fishing.h"

main()
{

    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");
       exit(1);
    }

	else
		{
			do
			{
            /* 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 */
		}


		fclose(competition);
}

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
Last edited by LuciWiz : 15-Mar-2005 at 09:43. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 15-Mar-2005, 11:56
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello robsmith. Do you remember the band the cure? You probably get that alot though.

Anyway, you are probably familiar with a tree structure, but it helps to review it in order to create it.

You should start with a data structure that has pointers to left and right branches. If you want to traverse backwards through a tree, you could add a root node as well, but this is not really necessary.

CPP / C++ / C Code:
struct node{
  //data as needed
  struct node* left;
  struct node* right;
};

You probably want to define a local variable for your root node and also set it to 0 so you know if your tree contains any data or not.

CPP / C++ / C Code:
  struct node* root;
  root = 0;

When you add data, you should allocate the memory for it as well as copy in the data that it will contain

CPP / C++ / C Code:
struct node* new_node = (struct node*) malloc(sizeof(struct  node) );
strcpy(new_node->name,word);
// and so on...
new_node->left = 0;    //Set left and right branches as null on all new nodes.
new_node->right = 0;
root = insert_node(root, new_node);  //Return the root just in case it changes.

Then your insert node function should parse to the proper location and insert the node.

CPP / C++ / C Code:
struct node* insert_node(struct node* root, struct node* new_node)
{
  struct node* current;
  int compare;

  if(!root)
    root = new_node;   //Tree was empty this will be the start.
  else{
    current = root;
    while(1){
       compare = strcmp(current->name,new_node->name);
       if(compare < 0 ){
          if(current->left)
             current = current->left;
          else{
             current->left = new_node;
             break;
          }
      }
      if(compare > 0 ){
            //Similar to left, just use right
      }
    }
  }
  return root;
}

That should get you started. That is 100% untested off of the top of my head, but hopefully it can point you in the right direction.

Also, if you know how to do recursion, it works really well for a tree like structure.

Good luck!
  #3  
Old 15-Mar-2005, 14:40
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Thumbs up

Thanks


Hi, thanks for all your help, it was extremely kind of you :-D

I'll try it out ASAP.

No i dont remember a band called the cure sorry, was there a rob smith?
  #4  
Old 15-Mar-2005, 22:20
jheron jheron is offline
New Member
 
Join Date: Feb 2005
Posts: 23
jheron is on a distinguished road
Talking

Quote:
Originally Posted by robsmith
Hi, thanks for all your help, it was extremely kind of you :-D

I'll try it out ASAP.

No i dont remember a band called the cure sorry, was there a rob smith?

HAHa lol ... you must be joking??
or I am feeling old lol
The cure was an 80's band that sucked and their singers name was Rob Smith :-P
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying node attributes in an XML tree display njp01u MS Visual C++ / MFC Forum 2 07-Feb-2005 18:42
problem reading to a dynamic array noamfrie C Programming Language 9 02-Jan-2005 19:35
Binary Tree Trouble neufunk C Programming Language 4 06-Dec-2004 10:52
tree problem zuzupus MySQL / PHP Forum 0 01-Aug-2003 09:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:03.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.