|
|
|
|
|
#pragma once
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
|
|
* Version : 1.0
|
|
|
|
|
|
* Date : 15 Nov 2016
|
|
|
|
|
|
* Description: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
|
class TTreeNode
|
|
|
|
|
|
{
|
|
|
|
|
|
public://methods
|
|
|
|
|
|
inline TTreeNode(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
inline ~TTreeNode(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
inline void clear(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
pParent = 0;
|
|
|
|
|
|
children.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void removeChild( TTreeNode* pChild)
|
|
|
|
|
|
{
|
|
|
|
|
|
pChild->pParent= 0;
|
|
|
|
|
|
children.remove(pChild);
|
|
|
|
|
|
}
|
|
|
|
|
|
//<2F><><EFBFBD>ø<EFBFBD><C3B8>ڵ<EFBFBD>
|
|
|
|
|
|
inline void setParent( TTreeNode* pNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
//ԭ<><D4AD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ձ<EFBFBD><D5B1>ڵ<EFBFBD>
|
|
|
|
|
|
if(pParent != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
pParent->removeChild(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>¸<EFBFBD><C2B8>ڵ<EFBFBD>
|
|
|
|
|
|
pParent = pNode;
|
|
|
|
|
|
|
|
|
|
|
|
if(0 != pParent)
|
|
|
|
|
|
pParent->children.push_back(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
inline void addChild( TTreeNode* pNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
pNode->setParent(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline int childCount(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
return children.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
public: //members
|
|
|
|
|
|
TTreeNode* pParent;
|
|
|
|
|
|
T value;
|
|
|
|
|
|
list<TTreeNode*> children;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
|
class TMultiTree
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
inline TMultiTree(void):m_pRoot(0){}
|
|
|
|
|
|
inline virtual ~TMultiTree(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void SetRoot( TTreeNode<T>* pRoot) {
|
|
|
|
|
|
m_pRoot = pRoot;
|
|
|
|
|
|
if(m_pRoot)
|
|
|
|
|
|
m_pRoot->pParent = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
inline TTreeNode<T>* GetRoot(void) const {return m_pRoot; }
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD>㣬pParentΪ<74><CEAA><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>,pNewNodeΪ<65>²<EFBFBD><C2B2><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>Ϊ<EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ϵ<EFBFBD><CFB5>
|
|
|
|
|
|
// 1Ϊ pNode1 <20><><EFBFBD><EFBFBD> pNode2 -1Ϊ pNode1<65><31><EFBFBD><EFBFBD>pNode2 0Ϊƽ<CEAA><C6BD> return true if succeed
|
|
|
|
|
|
inline bool InsertNode(TTreeNode<T>* pParent, TTreeNode<T>* pNewNode,int(*Compare)(const TTreeNode<T>* , const TTreeNode<T>* ))
|
|
|
|
|
|
{
|
|
|
|
|
|
if(0 == pParent)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
if(pParent->childCount() < 1 )
|
|
|
|
|
|
{
|
|
|
|
|
|
pParent->addChild(pNewNode);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typename list<TTreeNode<T>*>::iterator iter = pParent->children.begin();
|
|
|
|
|
|
vector<TTreeNode<T>*> newChildren; //<2F><><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>Ӻ<EFBFBD><D3BA><EFBFBD>
|
|
|
|
|
|
int rst = 0;
|
|
|
|
|
|
for(; iter != pParent->children.end(); iter ++ )
|
|
|
|
|
|
{
|
|
|
|
|
|
rst = Compare(*iter,pNewNode);
|
|
|
|
|
|
if(1 == rst) //*iter<65><72>newNode<64>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
return InsertNode(*iter,pNewNode,Compare );
|
|
|
|
|
|
else if(-1 == rst) //newNode<64><65>*iter<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
{
|
|
|
|
|
|
newChildren.push_back(*iter);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><>newChildren<65><6E>Ԫ<EFBFBD>ش<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>pnewNode<64><65><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
for(int i = 0; i < (int)newChildren.size(); i ++ )
|
|
|
|
|
|
{
|
|
|
|
|
|
pParent->removeChild(newChildren[i]);
|
|
|
|
|
|
pNewNode->addChild(newChildren[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pParent->addChild(pNewNode);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
TTreeNode<T>* m_pRoot; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|