C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 23rd, 2011, 04:17 PM
Darkzax Darkzax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 2 Darkzax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 17 sec
Reputation Power: 0
Classes - Inserting objects into binary tree

Hello everybody,

I am trying to add few Player object into the Binary tree (using tree template class).

Player class is derrived from GameObject class which assings new ID to every instance. (So "player[0]" have ID=1).

While I have all my Player objects declared and initialized as "player[0]", "player[1]" etc. - while I add them to binary tree - nodes are totally new objects (so first object ID = 70) - not connected with previous ones...

Please help.



here's my code:

Object Class .h file:
Code:
#pragma once
#include <iostream>


using namespace std;

class GameObject
{
protected:
		int id;
//int id;		
public:
	
	GameObject();
	//GameObject(int id);
	void Display();
	~GameObject(void);
};


Object Class .cpp file:
Code:
#include "StdAfx.h"
#include "GameObject.h"


GameObject::GameObject()
{
	static int nextID = 0;
	id=++nextID;
}

void GameObject::Display()
{
	cout << "GameObject ID: " << id << endl;
	
}

GameObject::~GameObject(void)
{
}



Tree Class template:
Code:
#pragma once
#include "Player.h"
#include "GameObject.h"


template <class T>
class Tree {
public:
     T *root; 
	 Tree();
     T *Find(const T *key); 
     void DisplayInOrder(T *localRoot);
	 void Insert(T *id);
};

template <class T>
Tree<T>::Tree(){
	root = 0;
}

template <class T>
T *Tree<T>::Find(const T *key) {
            T * current = root;
            while (current != key) {
                if (key < current)
                    current = current->leftChild;
                else
                    current = current->rightChild;
                if (current == 0)
                    return 0;
            }
            return current;
}

template <class T>
void Tree<T>::DisplayInOrder(T *localRoot) {
            if (localRoot != 0) {
                DisplayInOrder(localRoot->leftChild);
				localRoot->Display();
                DisplayInOrder(localRoot->rightChild);
            }
}

template <class T>
void Tree<T>::Insert(T *id){
	 T *newNode =  new T();
	 if (root ==0)
		 root = newNode;
	 else{
		 T *current;
		 current = root;
		 T *parent;
		 while(true){
			 parent = current;
			 if(id < current){
				 current = current->leftChild;
				 if (current == 0){
					 parent->leftChild = newNode;
					 return;
				 }
			 }
			 else{
				 current = current->rightChild;
				 if (current ==0){
					 parent->rightChild = newNode;
					 return;
				 }
			 }
		 }
	 }
 }


Player .h file:
Code:
#pragma once
#include "gameobject.h"
#include "Tree.h"
#include "LinkedList.h"
#include "Weapon.h"

class Player :
	public GameObject
{
public:
	Player();
		
	LinkedList<Weapon> *weapons;
	Player *leftChild;
	Player *rightChild;
	void Display();
	//bool operator!=(const Player &p);
//	bool operator<(const Player &p);
	~Player(void);
};


main file:
Code:
// cw2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GameObject.h"
#include "LinkedList.h"
#include "Tree.h"
#include "Player.h"
#include "Weapon.h"
#include "StackData.h"
#include "Round.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Tree<Player> *myTree = new Tree<Player>();  //Is the problem in here??

	int const playerSize = 5; // Creating const variable storing size of player array

	Player *players[playerSize]; // Creating dynamic array holding players objects

	for(int next = 0; next < playerSize; next++) //Assinging Player object (each with unique id) to array values
	{
    players[next] = new Player(); 
	}


	for(int next = 0; next < playerSize; next++) //Displaying players ID's
	{
		players[next]->Display(); 
	}


	
	myTree->Insert(players[0]);  //those ones are using NEW ID's rather than using Id's of player[n] objects
	myTree->Insert(players[1]);
	myTree->Insert(players[2]);
	myTree->Insert(players[3]);
	myTree->Insert(players[4]);

	
	cout << "\nDisplaying player ID's in tree:" << endl;

	myTree->DisplayInOrder(myTree->root);  //that shows totally new ID's

	Player *x = myTree->Find(players[0]); //so this do not work properly
	if (x !=0)
	{
	cout << " Found Link with data value : " << endl;
	x->Display();
	}
	else
	{
		cout << "Player not found" << endl;
	}

	
		
return 0
}

Reply With Quote
  #2  
Old April 27th, 2012, 09:13 PM
pubudu1199 pubudu1199 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 1 pubudu1199 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 15 sec
Reputation Power: 0
new to c++

hi,
i'm new to c++ and i'm trying to create a binary tree and i have studied you code but i couldn't get it to work could you please share the full code or give me good reference how to create simple game code like you have done

thanks

Reply With Quote
  #3  
Old April 4th, 2015, 09:46 AM
talC++ talC++ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2015
Posts: 1 talC++ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 7 sec
Reputation Power: 0
Need Help!

Quote:
Originally Posted by Darkzax
Hello everybody,

I am trying to add few Player object into the Binary tree (using tree template class).

Player class is derrived from GameObject class which assings new ID to every instance. (So "player[0]" have ID=1).

While I have all my Player objects declared and initialized as "player[0]", "player[1]" etc. - while I add them to binary tree - nodes are totally new objects (so first object ID = 70) - not connected with previous ones...

Please help.



here's my code:

Object Class .h file:
Code:
#pragma once
#include <iostream>


using namespace std;

class GameObject
{
protected:
		int id;
//int id;		
public:
	
	GameObject();
	//GameObject(int id);
	void Display();
	~GameObject(void);
};


Object Class .cpp file:
Code:
#include "StdAfx.h"
#include "GameObject.h"


GameObject::GameObject()
{
	static int nextID = 0;
	id=++nextID;
}

void GameObject::Display()
{
	cout << "GameObject ID: " << id << endl;
	
}

GameObject::~GameObject(void)
{
}



Tree Class template:
Code:
#pragma once
#include "Player.h"
#include "GameObject.h"


template <class T>
class Tree {
public:
     T *root; 
	 Tree();
     T *Find(const T *key); 
     void DisplayInOrder(T *localRoot);
	 void Insert(T *id);
};

template <class T>
Tree<T>::Tree(){
	root = 0;
}

template <class T>
T *Tree<T>::Find(const T *key) {
            T * current = root;
            while (current != key) {
                if (key < current)
                    current = current->leftChild;
                else
                    current = current->rightChild;
                if (current == 0)
                    return 0;
            }
            return current;
}

template <class T>
void Tree<T>::DisplayInOrder(T *localRoot) {
            if (localRoot != 0) {
                DisplayInOrder(localRoot->leftChild);
				localRoot->Display();
                DisplayInOrder(localRoot->rightChild);
            }
}

template <class T>
void Tree<T>::Insert(T *id){
	 T *newNode =  new T();
	 if (root ==0)
		 root = newNode;
	 else{
		 T *current;
		 current = root;
		 T *parent;
		 while(true){
			 parent = current;
			 if(id < current){
				 current = current->leftChild;
				 if (current == 0){
					 parent->leftChild = newNode;
					 return;
				 }
			 }
			 else{
				 current = current->rightChild;
				 if (current ==0){
					 parent->rightChild = newNode;
					 return;
				 }
			 }
		 }
	 }
 }


Player .h file:
Code:
#pragma once
#include "gameobject.h"
#include "Tree.h"
#include "LinkedList.h"
#include "Weapon.h"

class Player :
	public GameObject
{
public:
	Player();
		
	LinkedList<Weapon> *weapons;
	Player *leftChild;
	Player *rightChild;
	void Display();
	//bool operator!=(const Player &p);
//	bool operator<(const Player &p);
	~Player(void);
};


main file:
Code:
// cw2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GameObject.h"
#include "LinkedList.h"
#include "Tree.h"
#include "Player.h"
#include "Weapon.h"
#include "StackData.h"
#include "Round.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Tree<Player> *myTree = new Tree<Player>();  //Is the problem in here??

	int const playerSize = 5; // Creating const variable storing size of player array

	Player *players[playerSize]; // Creating dynamic array holding players objects

	for(int next = 0; next < playerSize; next++) //Assinging Player object (each with unique id) to array values
	{
    players[next] = new Player(); 
	}


	for(int next = 0; next < playerSize; next++) //Displaying players ID's
	{
		players[next]->Display(); 
	}


	
	myTree->Insert(players[0]);  //those ones are using NEW ID's rather than using Id's of player[n] objects
	myTree->Insert(players[1]);
	myTree->Insert(players[2]);
	myTree->Insert(players[3]);
	myTree->Insert(players[4]);

	
	cout << "\nDisplaying player ID's in tree:" << endl;

	myTree->DisplayInOrder(myTree->root);  //that shows totally new ID's

	Player *x = myTree->Find(players[0]); //so this do not work properly
	if (x !=0)
	{
	cout << " Found Link with data value : " << endl;
	x->Display();
	}
	else
	{
		cout << "Player not found" << endl;
	}

	
		
return 0
}


Hello Darkzax The programme that you were working on here is something I need help with at the moment. Do you have the full codes to all the classes and subclasses for both header and cpp files. If possible please send me

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Classes - Inserting objects into binary tree


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap