I have a Tree class that holds pointers to Player objects.
Here is the code:
#pragma once
#include "Player.h"
class Tree
{
private:
public:
Player* root;
Tree(void);
~Tree(void);
void DisplayInOrder(Player* localRoot)
{
if (localRoot != 0) {
DisplayInOrder(localRoot->leftChild);
localRoot->Display();
DisplayInOrder(localRoot->rightChild);
}
}
Player* Find(const Player* key)
{
Player * current = root;
while (current != key) {
if (key < current)
current = current->leftChild;
else
current = current->rightChild;
if (current == 0)
{
cout << endl << "Player Not Found!" << endl << endl;
return 0;
}
}
cout << endl << endl << "Found Player! ";
current->Display();
cout << endl << endl;
return current;
}
void Insert(Player* insertedPlayer)
{
if(root == 0)
{
root = insertedPlayer;
}
else
{
Player * current = root;
Player * parent;
while(true)
{
parent = current;
if(insertedPlayer < current)
{
current = current->leftChild;
if(current == 0)
{
parent->leftChild = insertedPlayer;
return;
}
}
else
{
current = current->rightChild;
if(current == 0)
{
parent->rightChild = insertedPlayer;
return;
}
}
}
}
}
};
The problem is, if I insert 5 pointers to Player objects using the Insert function, then call the displayInOrder function, passing it the root as the argument, it outputs the objects in the same order I put them in, rather than in order of their "id" data member that the Insert function takes into account using overloaded operators < and != for the Player class.
I have a working example of a Tree structure that I have literally copied and pasted (with the necessary changes) in, but it still doesn't seem to work properly...
I'm at a loss ti fugure out why!
Thanks