B
Her mein Code (noch in den Kinderschuhen) würde mich freuen wenn ihr VErbesserungsvorschläge, Optimierung hinsichtlich Performance, und Kritik geben könntet..
#pragma once
// CAllocation command target
class CAllocation : public CObject{
//Notwendige Pointer
CAllocation *m_pFather, *m_pBrother, *m_pFirst_Son;
//DEbug Name
CString m_strName;
public:
class iterator{
CAllocation *m_pIterPos;
public:
//Konstruktor
iterator(CAllocation *pPos): m_pIterPos(pPos){}
//Iteration ein Knoten nach vorne
void operator++(){
//Baum nach unten klettern
if(m_pIterPos->m_pFirst_Son!=NULL)
m_pIterPos= m_pIterPos->m_pFirst_Son;
else if(m_pIterPos->m_pBrother!=NULL)
m_pIterPos= m_pIterPos->m_pBrother;
else{
//Baum hochklettern
CAllocation *p= m_pIterPos->m_pFather->m_pBrother;
while(m_pIterPos->m_pFather->m_pBrother==NULL && m_pIterPos!=NULL)
m_pIterPos=m_pIterPos->m_pFather;
if(m_pIterPos==NULL)
return;
m_pIterPos=m_pIterPos->m_pFather->m_pBrother;
}
}
//Aktueller Knoten an Iteration
CAllocation* operator->() const {
return m_pIterPos;
}
CAllocation* operator*() const {
return m_pIterPos;
}
bool operator!=(const CAllocation *pOpp) const {
return m_pIterPos!=pOpp;
}
};
//Default Konstruktor
CAllocation(CString strName) : m_pFather(NULL),
m_pBrother(NULL),
m_pFirst_Son(NULL),
m_strName(strName){
}
//Konstruktor
CAllocation(CAllocation *pFather,CString strName): m_pFather(pFather),
m_pBrother(NULL),
m_pFirst_Son(NULL),
m_strName(strName){
ASSERT(m_pFather!= NULL);
//AlleBrüder des Vaters druchlaufen und diesen Knoten als leter Bruder anhängen!
CAllocation *pChilds= m_pFather->m_pFirst_Son;
//Wenn Vater keine Kinder hat dann diese Knoten als Erster Sohn definieren, sonst als letzter Bruder.
if(pChilds==NULL)
m_pFather->m_pFirst_Son= this;
else{
for(; pChilds->m_pBrother!= NULL; pChilds= pChilds->m_pBrother);
pChilds->m_pBrother= this;
}
}
//Destruktor
virtual ~CAllocation(){
//Zuerst Knoten aus Baum struktur ausgliedern
CAllocation *pFather= this->m_pFather;
if(pFather!=NULL){
CAllocation *pChilds= pFather->m_pFirst_Son;
//Dieser Knoten ist das erste Kind, also daszweite Kind als erstes Kind des Vater setzen
if(pChilds==this)
pFather->m_pFirst_Son= pChilds->m_pBrother;
else{
while(pChilds->m_pBrother!= this)
pChilds= pChilds->m_pBrother;
//Knoten in Childliste ausketten
pChilds->m_pBrother= pChilds->m_pBrother->m_pBrother;
}
}
//Ab hier steht der Knoten alleine, und kann samt unterknoten gellöscht werden
CAllocation::iterator it= this;
while(this != this->end()){
CAllocation *p = (*it);
++it;
delete p;
}
// Iterator verwenden!!
}
void TRACE_Debug(){
CAllocation::iterator it= this;
UINT iCounter=0;
while(it != this->end()){
printf("Nr %2i \t NodeName %s \n",iCounter,(*it)->m_strName);
++it;
iCounter++;
}
printf("Nr %2i \t NodeName %s \n",iCounter,(*it)->m_strName);
//for(CAllocation::iterator it= this,int i=0; it != this->end() ; ++it,++i)
// printf("Nr %2i \t NodeName %s \n",i,(*it)->m_strName);
}
UINT GetChildsCount(){
UINT iCounter=0;
for(CAllocation *pChilds= m_pFirst_Son; pChilds->m_pBrother!= NULL; pChilds= pChilds->m_pBrother, iCounter++);
return iCounter;
}
CAllocation* begin(){
//Von jedem Kind Knoten aus die Wurzez bzw. oberste Vater ermitteln
CAllocation *pGrandfather= m_pFather;
for(; pGrandfather!= NULL; pGrandfather= pGrandfather->m_pFather);
return pGrandfather;
}
CAllocation* end(){
CAllocation *pChilds= this;
while(1){
//Brüder für diesen Knoten durchlaufen
for(; pChilds->m_pBrother!= NULL; pChilds= pChilds->m_pBrother);
//Eine ebene des letzen Kindes (Bruders) nach unten steigen
if(pChilds->m_pFirst_Son==NULL)
break;
pChilds=pChilds->m_pFirst_Son;
}
return pChilds;
}
};
Danke im Vorraus