j essaie de coder un arbre n-aire pour un moteur2D, le principe fonctionne bien mais j ai un probleme au niveau du delete (dans le destructeur de CObjectManager et vu que l arbre doit etre creer et effacer a chaque frame je ne peu pas laisser de fuite...
le code est composer de la classe CObject qui represente l element et de CObjectManager qui permet de gerer les elements (CObjectManager contient l element primaire ainsi qu un map de * pour acceder au element plus rapidement)


puis je n ai jamais coder ce genre de truc avant donc y a surement pas mal d erreur de debutant
toute idee d optimisation est la bienvenue!



Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
#ifndef __COBJECT
#define __COBJECT
 
#include <map>
#include <vector>
#include <iostream>
#include <string>
 
class CObject
{
public:
	CObject();
	~CObject();
	CObject &operator=(const CObject &);
 
	std::string ID;
	std::vector<CObject*>	fils;
	CObject*				parent;
	void					Render();
};
 
class CObjectManager
{
public:
	CObjectManager();
	~CObjectManager();
 
	int		AddChild(std::string IDParent, std::string ID);
	int		RemoveChild(std::string ID);
	void	RenderTree();
	void	Reset();
 
	CObject	primaire;
 
private:
	std::vector<CObject*> element_restant;
	std::map<std::string, CObject*> element;
};
#endif
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
#include "CObject.h"
 
CObject::CObject(){
	this->parent = NULL;
}
 
CObject::~CObject(){
}
 
CObject &CObject::operator=(const CObject &source){
	this->ID = source.ID;
	this->fils = source.fils;
	this->parent = source.parent;
    return *this;
}
 
void CObject::Render(){
	std::cout << " < " << this->ID << " (" << this->fils.size() << ") > "; 
}
 
CObjectManager::CObjectManager()
{
	this->primaire.ID = "A";
	this->primaire.parent = NULL;
 
	this->element_restant.push_back(&this->primaire);
	this->element["A"] = &this->primaire;
}
 
CObjectManager::~CObjectManager()
{
	std::map<std::string, CObject*>::iterator it;
	for (it = this->element.begin(); it != this->element.end(); it++)
	{
		if (it->second != NULL)	
		{
			it->second->ID = "Z";
			it->second->fils.erase(it->second->fils.begin(), it->second->fils.end());
			it->second->parent = NULL;
			delete (it->second);
		}
	}
}
//============================
//	Ajoute un child
//============================
int CObjectManager::AddChild(std::string IDParent, std::string ID)
{
	std::map<std::string, CObject*>::iterator it = this->element.end();
 
	it = this->element.find(IDParent);
	if (it != this->element.end())
	{
		std::cout << "\n ID detecte: " << IDParent << " ajout de " << ID << std::endl;
 
		CObject* child = new CObject;
		child->ID = ID;
		child->parent = it->second;
		it->second->fils.push_back(child);
		this->element[ID] = child;
		return 1;
	}
	else return 0;
}
//============================
//	Supprime un child
//============================
int CObjectManager::RemoveChild(std::string ID)
{
	std::map<std::string, CObject*>::iterator it = this->element.end();
	std::vector<CObject*>::iterator vec_it;
 
	it = this->element.find(ID);
	if (it != this->element.end())
	{
		//enlever le fils ?
		//on itere sur le vector du parent
		for ( vec_it = it->second->parent->fils.begin(); vec_it != it->second->parent->fils.end(); vec_it++)
		{
			if ( (*vec_it)->ID == ID)break;
		}
		it->second->parent->fils.erase(vec_it);
 
		this->element.erase(it);
		delete it->second;
 
		return 1;
	}
	else return 0;
}
//====================================
//	Parcour l arbre
//====================================
void CObjectManager::RenderTree()
{
	std::vector<CObject*>::iterator it;
	CObject* current ;
	current = &this->primaire; //commence par le 1 noeud
do
{
	//ajoute les fils de l element a la liste a faire
	this->element_restant.insert(this->element_restant.end(), current->fils.begin(),current->fils.end());
 
	//enleve le 1 elements 
	it = this->element_restant.begin();
	this->element_restant.erase(it);
 
	//effectue le traitement
	current->Render();
 
	//passe a l element suivant
	current = this->element_restant.front();
 
}while(this->element_restant.size() != 0);
}
 
//===================================
//Remet l arbre a 0
//===================================
void CObjectManager::Reset(){
}