Thread: Member's value inside a binary tree

  1. #1
    Registered User
    Join Date
    Oct 2004

    Member's value inside a binary tree

    Hi all, I'm having this little problem right now. I know the answer must be pretty simple, but I just can't seem to find it. I create a binary tree with many nodes, each containing different members(name, number of msg, letf, right...). I'd like to modify these values but simply can't find the way to initialze them with a starting value nor to assign one to them. Anyway, here's what I'm talking about:

    Here's the tree

    typedef struct node* PtrNode;
    typedef struct node
         char* name;               /* points on the name */
         int nbMessSnt;               /* number of msg sent  */
         int nbMessTrs;               /* number of msg transmited  */
         int nbMessRec;                  /* number of msg received */
         PtrNoeud left;                    /* points the left node */
         PtrNoeud right;                   /* points the right node */
    PtrNode root = NULL;          /* root of the tree */
    What I'd like to do is asign the default value 0 to the messages. Does I have to do it when I add elements to the tree ? Right now, when I try to increase the number of messages of a member, the value I get is -842150451 which, I think, means it isn't initialized ?

    Here the function that modify these values:
    int HandleMsg()
            /* Open the file containing the messages */
         FILE* fMessages = fopen("messages.txt", "r");
         char value[30];
         PtrNode receiver = NULL;
         PtrNode sender = NULL;
         while (!feof (fMessages))
                    /* each line of the file contains 2 name; the first is the sender, the 2nd the receiver */
                    /* first we find the sender in the tree */
              ReadData(fMessages , value);
              sender= FindNode(root, value);
              if (!sender) return 0;
                    /* we find the receiver. It must be in the subtree of the sender */
              ReadDate(fMessages , value);
              receiver= FindNode(sender, value);
              if (!receiver) return 0;
                    /* If both were found, we increase the number of sent and receive msg */
              FindTransmitter(sender, receiver);
         return 1;
    I also tried initializing the values when I created the tree:

    PtrNode CreeateTree()
         FILE* fData = fopen("computers.txt", "r");
         char value[30];
         PtrNode new;
         while (!feof (fData))
              ReadData(fData, value);
              new= AddNode(root, value);
                    new->nbMessSnt = 0;
                    new->nbMessTrs = 0;
                    new->nbMessRec = 0;
              if (!racine) racine = nouveau;
         return racine;
    but it didn't worked well... Once I finished creating the tree and going throught it, the returned values were still -842150451.....

    Got an idea what I might be doing wrong ?

    Thanks, Frank

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    You're definitely working with uninitialized values, but it's hard to tell what's going wrong because you didn't supply enough of the right code. Try this: write a short test program that reads a small number of items into a tree. Make sure that the program still exhibits the problem, and post that code, including the binary tree code. But to avoid posting reams of source, make sure that it's bare bones.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Binary tree search efficiency
    By ExCoder01 in forum A Brief History of
    Replies: 8
    Last Post: 10-23-2003, 11:11 PM
  4. Binary Search Tree, Inserting node
    By cheryl_li in forum C Programming
    Replies: 1
    Last Post: 09-13-2003, 04:53 AM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
Website Security Test