Computer Science Canada

2 Words.. BINARY TREES

Author:  Dauntless [ Sun Jan 04, 2004 12:42 am ]
Post subject:  2 Words.. BINARY TREES

OK, here at Massey, Gr 12 compsci classes got a christmas gift of a two-week extension on our binary tree assignment which only about half the students understand how to implement it (a couple cocky kids like Andy who understand it and are working on extra stuff still advertise that they need help.. DAMN YOU ANDY!).

So what I'm asking you formally is if you can give me some help understanding the utilization of binary trees in the code. Make sure it's not cheating or anything...but I would appreciate some real code so I can demystify this pointer stuff. I understand the concept, with the organization of the trees, but the coding is too abstract.

For example.

code:
void main()
{
        loveTree boys; //Tree with all the clients that are male
        loveTree girls;
        loveNode *person; //If you get it, you'll get it ;)
        int numDaters; // How many clients there are, input from text file
        string tempNum;
       
        ifstream fin("daters.txt");
//Input number of clients from text
        getline (fin,tempNum,'\n');
        numDaters=atoi(tempNum.c_str());
//
        for (int i=0;i<numDaters;i++)
        {
                person = new loveNode;
                person->fileIn(fin);
               
                if (person->genShow()=="m" || person->genShow()=="M")
                        boys.add(person);
                else if (person->genShow()=="f" || person->genShow()=="F")
                        girls.add(person);
               
//          HERE
        }
       
}


Okay, so Mr. Mickey says that I should add a display function call where I indicated it I want to show all the clients in the tree. Now the sticky problem I have is, after I've allocated all the info to memory, how can I access it again without making a public function to access the private value (the root)? Seeing as I create a new node every time it begins a new loop, how am I supposed to separate one "person" from another? I considered reading from file repeatedly every time I try to add someone to the tree, or show certain people, for example.

Also, I created a function and put the call where I indicated. Starting from person A through Z, each person was added, and then their name and the people they were compatible with were displayed using the function. The problem is, since it displays as each person is added, the first person has nobody to look at, so it displays a negative message. If the second person added is compatible with the first, it will display a positive message. Often what will happen is the first person will be compatible with say the second, third, or even last person, but after the first person is added a negative message is displayed. So what happens is that if person A and person C are cross-compatible, after person A's name is displayed, a message that they are not compatible with anyone is shown. But when person C is added, person A is in the tree, whereas vice versa the same is not true. This results in the message after person C's name saying that A is compatible with C.

If this confused you at all, ask for clarification, but I need quick help! It's due monday! I'll improvise and do it the stupid way if I have to, but if this all makes sense to you I would appreciate the help alot. Thanks in advance.


: