1 Replies - 219 Views - Last Post: 09 January 2013 - 05:53 AM Rate Topic: -----

#1 j31594  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-January 13

Loading customers from file to a binary tree

Posted 09 January 2013 - 02:16 AM

Basically, I need to load all the customers from customers.dat and put them in a binary tree. Also whenever a user adds a customer it should also be added to the binary tree. I managed to code the essential functions for the binary tree but now I can't figure out how to insert the customers in the tree and later search for a particular customer, according to the customer ID entered by the user.

These are my functions:

struct node
{
  int key_value;
  struct node *left;
  struct node *right;
};

struct node *root = 0;

void destroy_tree(struct node *leaf)
{
  if( leaf != 0 )
  {
      destroy_tree(leaf->left);
      destroy_tree(leaf->right);
      free( leaf );
  }
}

insert(int key, struct node **leaf)
{
    if( *leaf == 0 )
    {
        *leaf = (struct node*) malloc( sizeof( struct node ) );
        (*leaf)->key_value = key;
        /* initialize the children to null */
        (*leaf)->left = 0;
        (*leaf)->right = 0;
    }
    else if(key < (*leaf)->key_value)
    {
        insert( key, &(*leaf)->left );
    }
    else if(key > (*leaf)->key_value)
    {
        insert( key, &(*leaf)->right );
    }
}

struct node *search(int key, struct node *leaf)
{
  if( leaf != 0 )
  {
      if(key==leaf->key_value)
      {
          return leaf;
      }
      else if(key<leaf->key_value)
      {
          return search(key, leaf->left);
      }
      else
      {
          return search(key, leaf->right);
      }
  }
  else return 0;
}



Should I call the insert function from the AddCustomer() method and then call the search function from the SearchCustomer() method ?
Please I really need help, I am only a beginner with C.

This post has been edited by baavgai: 09 January 2013 - 05:47 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: Loading customers from file to a binary tree

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Loading customers from file to a binary tree

Posted 09 January 2013 - 05:53 AM

No, those are this guy's functions: http://www.cprogramm...c/lesson18.html

This is not the best implementation you'll find, btw. I'd start by copying better code. :P

Ultimately, I'm afraid you're actually going to have to write some code yourself. What data does customer contain? It should probably be a struct. How would you use a struct instead of an int in that tree?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1