※数据结构※→☆非线性结构(tree)☆============树结点 链式存储结构(tree node list)(十六)

来源:互联网 发布:中学生题库软件 编辑:程序博客网 时间:2024/06/07 22:50

结点:

        包括一个数据元素及若干个指向其它子树的分支;例如,A,B,C,D等。

在数据结构的图形表示中,对于数据集合中的每一个数据元素用中间标有元素值的方框表示,一般称之为数据结点,简称结点。


        在C语言中,链表中每一个元素称为“结点”,每个结点都应包括两个部分:一为用户需要用的实际数据;二为下一个结点的地址,即指针域和数据域。


        数据结构中的每一个数据结点对应于一个储存单元,这种储存单元称为储存结点,也可简称结点


树结点(树节点):

        


树节点相关术语:

  • 节点的度:一个节点含有的子树的个数称为该节点的度;
  • 叶节点或终端节点:度为0的节点称为叶节点;
  • 非终端节点或分支节点:度不为0的节点;
  • 双亲节点或父节点:若一个结点含有子节点,则这个节点称为其子节点的父节点;
  • 孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点;
  • 兄弟节点:具有相同父节点的节点互称为兄弟节点;
  • 节点的层次:从根开始定义起,根为第1层,根的子结点为第2层,以此类推;
  • 堂兄弟节点:双亲在同一层的节点互为堂兄弟;
  • 节点的祖先:从根到该节点所经分支上的所有节点;
  • 子孙:以某节点为根的子树中任一节点都称为该节点的子孙。

        根据树结点的相关定义,采用“双亲孩子表示法”。其属性如下:

DWORDm_dwLevel;//Node levels: starting from the root to start defining the root of the first layer, the root node is a sub-layer 2, and so on;Tm_data;//the friend class can use it directlyAL_TreeNodeList<T>*m_pParent;//Parent tree nodeAL_ListSingle<AL_TreeNodeList<T>*>m_listChild;//All Child tree node

树的几种表示法

        在实际中,可使用多种形式的存储结构来表示树,既可以采用顺序存储结构,也可以采用链式存储结构,但无论采用何种存储方式,都要求存储结构不但能存储各结点本身的数据信息,还要能唯一地反映树中各结点之间的逻辑关系。


        1.双亲表示法

                由于树中的每个结点都有唯一的一个双亲结点,所以可用一组连续的存储空间(一维数组)存储树中的各个结点,数组中的一个元素表示树中的一个结点,每个结点含两个域,数据域存放结点本身信息,双亲域指示本结点的双亲结点在数组中位置。

                


        2.孩子表示法

                1.多重链表:每个结点有多个指针域,分别指向其子树的根
                        1)结点同构:结点的指针个数相等,为树的度k,这样n个结点度为k的树必有n(k-1)+1个空链域.
                                                
                        2)结点不同构:结点指针个数不等,为该结点的度d
                                                

                2.孩子链表:每个结点的孩子结点用单链表存储,再用含n个元素的结构数组指向每个孩子链表

                


        3.双亲孩子表示法

                1.双亲表示法,PARENT(T,x)可以在常量时间内完成,但是求结点的孩子时需要遍历整个结构。
                2.孩子链表表示法,适于那些涉及孩子的操作,却不适于PARENT(T,x)操作。
                3.将双亲表示法和孩子链表表示法合在一起,可以发挥以上两种存储结构的优势,称为带双亲的孩子链表表示法

                


        4.双亲孩子兄弟表示法 (二叉树专用)

                又称为二叉树表示法,以二叉链表作为树的存储结构。

                                

                


链式存储结构
        在计算机中用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的).
        它不要求逻辑上相邻的元素在物理位置上也相邻.因此它没有顺序存储结构所具有的弱点,但也同时失去了顺序表可随机存取的优点.


        链式存储结构特点:
                1、比顺序存储结构的存储密度小 (每个节点都由数据域和指针域组成,所以相同空间内假设全存满的话顺序比链式存储更多)。
                2、逻辑上相邻的节点物理上不必相邻。
                3、插入、删除灵活 (不必移动节点,只要改变节点中的指针)。
                4、查找结点时链式存储要比顺序存储慢。
                5、每个结点是由数据域和指针域组成。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注我的博客。
本节笔记到这里就结束了。


潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。
编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。
如果文章中有什么疏漏的地方,也请大家指正。也希望大家可以多留言来和我探讨编程相关的问题。
最后,谢谢你们一直的支持~~~


       C++完整个代码示例(代码在VS2005下测试可运行)

        


AL_TreeNodeList.h

/**  @(#)$Id: AL_TreeNodeList.h 52 2013-09-23 10:38:22Z xiaoting $  @briefEach of the data structure corresponds to a data node storage unit, this storage unit is called storage node, the node can   also be referred to.  The related concepts of tree node   1.degreedegree node: A node of the subtree containing the number is called the node degree;  2.leafleaf nodes or terminal nodes: degree 0 are called leaf nodes;  3.branchnon-terminal node or branch node: node degree is not 0;  4.parentparent node or the parent node: If a node contains a child node, this node is called its child node's parent;  5.childchild node or child node: A node subtree containing the root node is called the node's children;  6.slibingsibling nodes: nodes with the same parent node is called mutual sibling;  7.ancestorancestor node: from the root to the node through all the nodes on the branch;  8.descendantdescendant nodes: a node in the subtree rooted at any node is called the node's descendants.  ////////////////////////////////Chain storage structure//////////////////////////////////////////  The computer using a set of arbitrary linear table storage unit stores data elements (which may be a continuous plurality of memory   cells, it can be discontinuous).  It does not require the logic elements of adjacent physical location is adjacent to and therefore it is not sequential storage   structure has a weakness, but also lost the sequence table can be accessed randomly advantages.  Chain store structural features:  1, compared with sequential storage density storage structure (each node consists of data fields and pointers domains, so the same   space is full, then assume full order of more than chain stores).  2, the logic is not required on the adjacent node is physically adjacent.  3, insert, delete and flexible (not the mobile node, change the node as long as the pointer).  4, find the node when stored sequentially slower than the chain stores.  5, each node is a pointer to the data fields and domains.  @Author $Author: xiaoting $  @Date $Date: 2013-09-23 18:38:22 +0800 (周一, 23 九月 2013) $  @Revision $Revision: 52 $  @URL $URL: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_TreeNodeList.h $  @Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_TreeNodeList.h 52 2013-09-23 10:38:22Z xiaoting $ */#ifndef CXX_AL_TREENODELIST_H#define CXX_AL_TREENODELIST_H#ifndef CXX_AL_LISTSINGLE_H#include "AL_ListSingle.h"#endif#ifndef CXX_AL_QUEUELIST_H#include "AL_QueueList.h"#endif/////////////////////////////////////////////////////////////////////////////AL_TreeNodeList///////////////////////////////////////////////////////////////////////////template<typename T> class AL_TreeNodeList{public:/*** Destruction** @param* @return* @note* @attention */~AL_TreeNodeList();/*** GetLevel** @param* @returnDWORD* @note Node levels: starting from the root to start defining the root of the first layer, the root node is a sub-layer 2, and so on;* @attention */DWORD GetLevel() const;/*** SetLevel** @paramDWORD dwLevel <IN>* @return* @note Node levels: starting from the root to start defining the root of the first layer, the root node is a sub-layer 2, and so on;* @attention */VOID SetLevel(DWORD dwLevel);/*** GetData** @param* @returnT* @note * @attention */T GetData() const;/*** SetData** @paramconst T& tTemplate <IN>* @return* @note * @attention */VOID SetData(const T& tTemplate);/*** GetParent** @param* @returnAL_TreeNodeList<T>** @note parent node pointer, not to manager memory* @attention */AL_TreeNodeList<T>*GetParent() const;/*** SetParent** @paramDWORD dwIndex <IN>* @paramAL_TreeNodeList<T>* pParent <IN>* @returnBOOL* @note parent node pointer, not to manager memory* @attention as the child to set the parent at the index (from the left of parent's child )*/BOOL SetParent(DWORD dwIndex, AL_TreeNodeList<T>* pParent);/*** SetParentLeft** @paramAL_TreeNodeList<T>* pParent <IN>* @returnBOOL* @note parent node pointer, not to manager memory* @attention as the child to set the parent at the left (from the left of parent's child )*/BOOL SetParentLeft(AL_TreeNodeList<T>* pParent);/*** SetParentRight** @paramAL_TreeNodeList<T>* pParent <IN>* @returnBOOL* @note parent node pointer, not to manager memory* @attention as the child to set the parent at the right (from the right of parent's child )*/BOOL SetParentRight(AL_TreeNodeList<T>* pParent);/*** Insert** @paramDWORD dwIndex <IN>* @paramAL_TreeNodeList<T>* pInsertChild <IN> * @returnBOOL* @note inset the const AL_TreeNodeList<T>*  into the child notes at the position* @attention*/BOOL Insert(DWORD dwIndex, AL_TreeNodeList<T>* pInsertChild);/*** InsertLeft** @paramAL_TreeNodeList<T>* pInsertChild <IN> * @returnBOOL* @note inset the const AL_TreeNodeList<T>*  into the child notes at the left* @attention*/BOOL InsertLeft(AL_TreeNodeList<T>* pInsertChild);/*** InsertRight** @paramAL_TreeNodeList<T>* pInsertChild <IN> * @returnBOOL* @note inset the const AL_TreeNodeList<T>*  into the child notes at the right* @attention*/BOOL InsertRight(AL_TreeNodeList<T>* pInsertChild);/*** GetChild** @paramDWORD dwIndex <IN>* @returnAL_TreeNodeList<T>** @note the dwIndex must is little than the node degree* @attention*/AL_TreeNodeList<T>* GetChild(DWORD dwIndex) const;/*** GetChildLeft** @param* @returnAL_TreeNodeList<T>** @note * @attention*/AL_TreeNodeList<T>* GetChildLeft() const;/*** GetChildRight** @param* @returnAL_TreeNodeList<T>** @note * @attention*/AL_TreeNodeList<T>* GetChildRight() const;/*** GetDegree** @param* @returnDWORD* @note degree node: A node of the subtree containing the number is called the node degree;* @attention */DWORD GetDegree() const;/*** IsLeaf** @param* @returnBOOL* @note leaf nodes or terminal nodes: degree 0 are called leaf nodes;* @attention */BOOL IsLeaf() const;/*** IsBranch** @param* @returnBOOL* @note non-terminal node or branch node: node degree is not 0;* @attention */BOOL IsBranch() const;/*** IsParent** @paramconst AL_TreeNodeList<T>* pChild <IN>* @returnBOOL* @note parent node or the parent node: If a node contains a child node, this node is called its child * @attention */BOOL IsParent(const AL_TreeNodeList<T>* pChild) const;/*** GetSibling** @paramAL_ListSingle<AL_TreeNodeList<T>*>& listSibling <OUT>* @returnBOOL* @note sibling nodes: nodes with the same parent node is called mutual sibling;* @attention */BOOL GetSibling(AL_ListSingle<AL_TreeNodeList<T>*>& listSibling) const;/*** GetAncestor** @paramAL_ListSingle<AL_TreeNodeList<T>*>& listAncestor <OUT>* @returnBOOL* @note ancestor node: from the root to the node through all the nodes on the branch;* @attention */BOOL GetAncestor(AL_ListSingle<AL_TreeNodeList<T>*>& listAncestor) const;/*** GetDescendant** @paramAL_ListSingle<AL_TreeNodeList<T>*>& listDescendant <OUT>* @returnBOOL* @note ancestor node: from the root to the node through all the nodes on the branch;* @attention */BOOL GetDescendant(AL_ListSingle<AL_TreeNodeList<T>*>& listDescendant) const;protected:public:/*** Construction** @param* @return* @note private the Construction, avoid the others use it* @attention*/AL_TreeNodeList();/*** Construction** @paramconst T& tTemplate <IN>* @return* @note* @attention private the Construction, avoid the others use it*/AL_TreeNodeList(const T& tTemplate);/***Copy Construct** @paramconst AL_TreeNodeList<T>& cAL_TreeNodeList* @return*/AL_TreeNodeList(const AL_TreeNodeList<T>& cAL_TreeNodeList);/***Assignment** @paramconst AL_TreeNodeList<T>& cAL_TreeNodeList* @returnAL_TreeNodeList<T>&*/AL_TreeNodeList<T>& operator = (const AL_TreeNodeList<T>& cAL_TreeNodeList);public:protected:private:DWORDm_dwLevel;//Node levels: starting from the root to start defining the root of the first layer, the root node is a sub-layer 2, and so on;Tm_data;//the friend class can use it directlyAL_TreeNodeList<T>*m_pParent;//Parent tree nodeAL_ListSingle<AL_TreeNodeList<T>*>m_listChild;//All Child tree node};/////////////////////////////////////////////////////////////////////////////AL_TreeNodeList////////////////////////////////////////////////////////////////////////////*** Construction** @param* @return* @note private the Construction, avoid the others use it* @attention*/template<typename T>AL_TreeNodeList<T>::AL_TreeNodeList():m_dwLevel(0x00),m_pParent(NULL){//memset(&m_data, 0x00, sizeof(T));//can not use memset, as to pointer or virtural pointer of classm_listChild.Clear();}/*** Construction** @paramconst T& tTemplate <IN>* @return* @note* @attention private the Construction, avoid the others use it*/template<typename T>AL_TreeNodeList<T>::AL_TreeNodeList(const T& tTemplate):m_dwLevel(0x00),m_data(tTemplate),m_pParent(NULL){m_listChild.Clear();}/*** Destruction** @param* @return* @note* @attention */template<typename T>AL_TreeNodeList<T>::~AL_TreeNodeList(){//it doesn't matter to clear the pointer or not.m_dwLevel = 0x00;m_pParent = NULL;//not to manager the memorym_listChild.Clear();}/*** GetLevel** @param* @returnDWORD* @note Node levels: starting from the root to start defining the root of the first layer, the root node is a sub-layer 2, and so on;* @attention */template<typename T> DWORD AL_TreeNodeList<T>::GetLevel() const{return m_dwLevel;}/*** SetLevel** @paramDWORD dwLevel <IN>* @return* @note Node levels: starting from the root to start defining the root of the first layer, the root node is a sub-layer 2, and so on;* @attention */template<typename T> VOID AL_TreeNodeList<T>::SetLevel(DWORD dwLevel){m_dwLevel = dwLevel;}/*** GetData** @param* @returnT* @note * @attention */template<typename T> T AL_TreeNodeList<T>::GetData() const{return m_data;}/*** SetData** @paramconst T& tTemplate <IN>* @return* @note * @attention */template<typename T> VOID AL_TreeNodeList<T>::SetData(const T& tTemplate){m_data = tTemplate;}/*** GetParent** @param* @returnAL_TreeNodeList<T>** @note parent node pointer, not to manager memory* @attention */template<typename T> AL_TreeNodeList<T>* AL_TreeNodeList<T>::GetParent() const{return m_pParent;}/*** SetParent** @paramDWORD dwIndex <IN>* @paramAL_TreeNodeList<T>* pParent <IN>* @returnBOOL* @note parent node pointer, not to manager memory* @attention as the child to set the parent at the index (from the left of parent's child )*/template<typename T> BOOL AL_TreeNodeList<T>::SetParent(DWORD dwIndex, AL_TreeNodeList<T>* pParent){BOOL  bSetParent = FALSE;bSetParent = pParent->Insert(dwIndex, this);if (TRUE == bSetParent) {//current node insert to the parent successfullym_pParent = pParent;}return bSetParent;}/*** SetParentLeft** @paramAL_TreeNodeList<T>* pParent <IN>* @return* @note parent node pointer, not to manager memory* @attention as the child to set the parent at the left (from the left of parent's child )*/template<typename T> BOOL AL_TreeNodeList<T>::SetParentLeft(AL_TreeNodeList<T>* pParent){return SetParent(0x00, pParent);}/*** SetParentRight** @paramAL_TreeNodeList<T>* pParent <IN>* @return* @note parent node pointer, not to manager memory* @attention as the child to set the parent at the right (from the right of parent's child )*/template<typename T> BOOL AL_TreeNodeList<T>::SetParentRight(AL_TreeNodeList<T>* pParent){return SetParent(pParent->GetDegree(), pParent);}/*** Insert** @paramDWORD dwIndex <IN>* @paramAL_TreeNodeList<T>* pInsertChild <IN> * @returnBOOL* @note inset the const AL_TreeNodeList<T>*  into the child notes at the position* @attention*/template<typename T> BOOL AL_TreeNodeList<T>::Insert(DWORD dwIndex, AL_TreeNodeList<T>* pInsertChild){BOOL  bInsert = FALSE;bInsert = m_listChild.Insert(dwIndex, pInsertChild);if (TRUE == bInsert) {if (GetLevel()+1 != pInsertChild->GetLevel()) {//deal with the child levelINT iLevelDiff = pInsertChild->GetLevel() - GetLevel();pInsertChild->SetLevel(GetLevel()+1);AL_ListSingle<AL_TreeNodeList<T>*> listDescendant;if (FALSE == GetDescendant(listDescendant)) {return FALSE;}AL_TreeNodeList<T>* pDescendant = NULL;for (DWORD dwCnt=0; dwCnt<listDescendant.Length(); dwCnt++) {if (TRUE == listDescendant.Get(pDescendant, dwCnt)) {if (NULL != pDescendant) {//set child levelpDescendant->SetLevel(pDescendant->GetLevel()-iLevelDiff+1);}else {//errorreturn FALSE;}}else {//errorreturn FALSE;}}}//child node insert to the current successfullypInsertChild->m_pParent = this;}return bInsert;}/*** InsertLeft** @paramAL_TreeNodeList<T>* pInsertChild <IN> * @returnBOOL* @note inset the const AL_TreeNodeList<T>*  into the child notes at the left* @attention*/template<typename T> BOOL AL_TreeNodeList<T>::InsertLeft(AL_TreeNodeList<T>* pInsertChild){return Insert(0x00, pInsertChild);}/*** InsertRight** @paramAL_TreeNodeList<T>* pInsertChild <IN> * @returnBOOL* @note inset the const AL_TreeNodeList<T>*  into the child notes at the right* @attention*/template<typename T> BOOL AL_TreeNodeList<T>::InsertRight(AL_TreeNodeList<T>* pInsertChild){return Insert(GetDegree(), pInsertChild);}/*** GetChild** @paramDWORD dwIndex <IN>* @returnAL_TreeNodeList<T>** @note the dwIndex must is little than the node degree* @attention*/template<typename T> AL_TreeNodeList<T>* AL_TreeNodeList<T>::GetChild(DWORD dwIndex) const{AL_TreeNodeList<T>* pChild = NULL;if (TRUE == m_listChild.Get(pChild, dwIndex)) {return pChild;}return NULL;}/*** GetChildLeft** @param* @returnAL_TreeNodeList<T>** @note * @attention*/template<typename T> AL_TreeNodeList<T>* AL_TreeNodeList<T>::GetChildLeft() const{return GetChild(0x00);}/*** GetChildRight** @param* @returnAL_TreeNodeList<T>** @note * @attention*/template<typename T> AL_TreeNodeList<T>* AL_TreeNodeList<T>::GetChildRight() const{return GetChild(GetDegree());}/*** GetDegree** @param* @returnDWORD* @note degree node: A node of the subtree containing the number is called the node degree;* @attention */template<typename T> DWORD AL_TreeNodeList<T>::GetDegree() const{return m_listChild.Length();}/*** IsLeaf** @param* @returnBOOL* @note leaf nodes or terminal nodes: degree 0 are called leaf nodes;* @attention */template<typename T> BOOL AL_TreeNodeList<T>::IsLeaf() const{//return (TRUE == m_listChild.IsEmpty()) ? TRUE:FALSE;return (0x00 == GetDegree()) ? TRUE:FALSE;}/*** IsBranch** @param* @returnBOOL* @note non-terminal node or branch node: node degree is not 0;* @attention */template<typename T> BOOL AL_TreeNodeList<T>::IsBranch() const{//return (FALSE == m_listChild.IsEmpty()) ? TRUE:FALSE;return (0x00 != GetDegree()) ? TRUE:FALSE;}/*** IsParent** @paramconst AL_TreeNodeList<T>* pChild <IN>* @returnBOOL* @note parent node or the parent node: If a node contains a child node, this node is called its child * @attention */template<typename T> BOOL AL_TreeNodeList<T>::IsParent(const AL_TreeNodeList<T>* pChild) const{// AL_TreeNodeList<T>* pCompare = NULL;// for (DWORD dwCnt=0; dwCnt<GetDegree(); dwCnt++) {// if (TRUE == m_listChild.Get(pCompare, dwCnt)) {// if (pCompare == pChild) {// //find the child// return TRUE;// }// }// }// return FALSE;if (this == pChild->m_pParent) {return TRUE;}return FALSE;}/*** GetSibling** @paramAL_ListSingle<AL_TreeNodeList<T>*>& listSibling <OUT>* @returnBOOL* @note sibling nodes: nodes with the same parent node is called mutual sibling;* @attention */template<typename T> BOOLAL_TreeNodeList<T>::GetSibling(AL_ListSingle<AL_TreeNodeList<T>*>& listSibling) const{BOOL bSibling = FALSE;if (NULL == m_pParent) {//not parent nodereturn bSibling;}AL_ListSingle<AL_TreeNodeList<T>*>&listParentChild = m_pParent->m_listChild;AL_TreeNodeList<T>* pParentChild = NULL;for (DWORD dwCnt=0; dwCnt<m_pParent->GetDegree(); dwCnt++) {pParentChild = m_pParent->GetChild(dwCnt);if (NULL != pParentChild) {//get the childif (pParentChild == this) {//itselfcontinue;}listSibling.InsertEnd(pParentChild);bSibling = TRUE;}else {//error can not get the childreturn FALSE;}}return bSibling;}/*** GetAncestor** @paramAL_ListSingle<AL_TreeNodeList<T>*>& listAncestor <OUT>* @returnBOOL* @note ancestor node: from the root to the node through all the nodes on the branch;* @attention */template<typename T> BOOLAL_TreeNodeList<T>::GetAncestor(AL_ListSingle<AL_TreeNodeList<T>*>& listAncestor) const{if (NULL == m_pParent) {//not parent nodereturn FALSE;}AL_TreeNodeList<T>* pParent = m_pParent;while (NULL != pParent) {listAncestor.InsertEnd(pParent);pParent = pParent->m_pParent;}return TRUE;}/*** GetDescendant** @paramAL_ListSingle<AL_TreeNodeList<T>*>& listDescendant <OUT>* @returnBOOL* @note ancestor node: from the root to the node through all the nodes on the branch;* @attention */template<typename T> BOOL AL_TreeNodeList<T>::GetDescendant(AL_ListSingle<AL_TreeNodeList<T>*>& listDescendant) const{if (TRUE == IsLeaf()) {//child nodereturn FALSE;}AL_TreeNodeList<T>* pDescendant = NULL;for (DWORD dwCnt=0; dwCnt<GetDegree(); dwCnt++) {pDescendant = GetChild(dwCnt);if (NULL != pDescendant) {//get the childlistDescendant.InsertEnd(pDescendant);}else {//error can not get the childreturn FALSE;}}//loop the all node in listDescendantDWORD dwDescendantLoop = 0x00;AL_TreeNodeList<T>* pDescendantLoop = NULL;while (TRUE == listDescendant.Get(pDescendant, dwDescendantLoop)) {dwDescendantLoop++;if (NULL != pDescendant) {for (DWORD dwCnt=0; dwCnt<pDescendant->GetDegree(); dwCnt++) {pDescendantLoop = pDescendant->GetChild(dwCnt);if (NULL != pDescendantLoop) {//get the descendantlistDescendant.InsertEnd(pDescendantLoop);}else {//error can not get the descendantreturn FALSE;}}}else {//errorreturn FALSE;}}return TRUE;/*AL_TreeNodeList<T>* pDescendant = NULL;AL_QueueList<AL_TreeNodeList<T>*> queueDescendant;for (DWORD dwCnt=0; dwCnt<GetDegree(); dwCnt++) {pDescendant = GetChild(dwCnt);if (NULL != pDescendant) {//get the childqueueDescendant.Push(pDescendant);}else {//error can not get the childreturn FALSE;}}AL_TreeNodeList<T>* pDescendantLoop = NULL;AL_TreeNodeList<T>* pDescendantChild = NULL;while (FALSE == queueDescendant.IsEmpty()) {if (TRUE == queueDescendant.Pop(pDescendantLoop)) {if (NULL != pDescendantLoop) {listDescendant.InsertEnd(pDescendantLoop); for (DWORD dwCnt=0; dwCnt<pDescendantLoop->GetDegree(); dwCnt++) {pDescendantChild = pDescendantLoop->GetChild(dwCnt);if (NULL != pDescendantChild) {queueDescendant.Push(pDescendantChild);}}}else {return FALSE;}}else {return FALSE;}}return TRUE;*/}/***Assignment** @paramconst AL_TreeNodeList<T>& cAL_TreeNodeList* @returnAL_TreeNodeList<T>&*/template<typename T> AL_TreeNodeList<T>& AL_TreeNodeList<T>::operator = (const AL_TreeNodeList<T>& cAL_TreeNodeList){m_dwLevel = cAL_TreeNodeList.m_dwLevel;m_data = cAL_TreeNodeList.m_data;m_pParent = cAL_TreeNodeList.m_pParent;m_listChild = cAL_TreeNodeList.m_listChild;return *this;}#endif // CXX_AL_TREENODELIST_H/* EOF */


测试代码

#ifdef TEST_AL_TREENODELISTAL_TreeNodeList<DWORD> cTreeNodeList;cTreeNodeList.SetLevel(1);DWORD dwLevel = cTreeNodeList.GetLevel();std::cout<<dwLevel<<std::endl;cTreeNodeList.SetData(10);DWORD dwData = cTreeNodeList.GetData();std::cout<<dwData<<std::endl;AL_TreeNodeList<DWORD> cTreeNodeListParent;cTreeNodeListParent.SetLevel(0);cTreeNodeListParent.SetData(0);cTreeNodeList.SetParent(0x00, &cTreeNodeListParent);AL_TreeNodeList<DWORD> cTreeNodeListChild20(20);cTreeNodeListChild20.SetLevel(2);AL_TreeNodeList<DWORD> cTreeNodeListChild21(21);cTreeNodeListChild21.SetLevel(2);AL_TreeNodeList<DWORD> cTreeNodeListChild22(22);cTreeNodeListChild22.SetLevel(2);AL_TreeNodeList<DWORD> cTreeNodeListChild23(23);cTreeNodeListChild23.SetLevel(2);cTreeNodeList.Insert(0x00, &cTreeNodeListChild21);cTreeNodeList.InsertLeft(&cTreeNodeListChild20);cTreeNodeList.InsertRight(&cTreeNodeListChild22);cTreeNodeList.Insert(0x03, &cTreeNodeListChild23);AL_TreeNodeList<DWORD> cTreeNodeListChild30(30);cTreeNodeListChild30.SetLevel(3);AL_TreeNodeList<DWORD> cTreeNodeListChild31(31);cTreeNodeListChild31.SetLevel(3);AL_TreeNodeList<DWORD> cTreeNodeListChild32(32);cTreeNodeListChild32.SetLevel(3);AL_TreeNodeList<DWORD> cTreeNodeListChild33(33);cTreeNodeListChild33.SetLevel(3);cTreeNodeListChild31.SetParent(0x00, &cTreeNodeListChild20);cTreeNodeListChild30.SetParentLeft(&cTreeNodeListChild20);cTreeNodeListChild32.SetParentRight(&cTreeNodeListChild20);cTreeNodeListChild33.SetParent(0x03, &cTreeNodeListChild20 );AL_TreeNodeList<DWORD>*pParent = NULL;pParent = cTreeNodeList.GetParent();if (NULL != pParent) {std::cout<<pParent->GetLevel()<<"    "<<pParent->GetData()<<"    "<<pParent->GetDegree()<<std::endl;std::cout<<pParent->IsLeaf()<<"    "<<pParent->IsBranch()<<"    "<<pParent->IsParent(&cTreeNodeList)<<"    "<<pParent->IsParent(&cTreeNodeListChild33)<<std::endl;}AL_TreeNodeList<DWORD>*pChild = NULL;pChild = cTreeNodeList.GetChild(0x01);if (NULL != pChild) {std::cout<<pChild->GetLevel()<<"    "<<pChild->GetData()<<"    "<<pChild->GetDegree()<<std::endl;std::cout<<pChild->IsLeaf()<<"    "<<pChild->IsBranch()<<"    "<<pChild->IsParent(&cTreeNodeList)<<"    "<<pChild->IsParent(&cTreeNodeListChild33)<<std::endl;}pChild = cTreeNodeList.GetChild(0x00);if (NULL != pChild) {std::cout<<pChild->GetLevel()<<"    "<<pChild->GetData()<<"    "<<pChild->GetDegree()<<std::endl;std::cout<<pChild->IsLeaf()<<"    "<<pChild->IsBranch()<<"    "<<pChild->IsParent(&cTreeNodeList)<<"    "<<pChild->IsParent(&cTreeNodeListChild33)<<std::endl;}pChild = cTreeNodeList.GetChildLeft();if (NULL != pChild) {std::cout<<pChild->GetLevel()<<"    "<<pChild->GetData()<<"    "<<pChild->GetDegree()<<std::endl;std::cout<<pChild->IsLeaf()<<"    "<<pChild->IsBranch()<<"    "<<pChild->IsParent(&cTreeNodeList)<<"    "<<pChild->IsParent(&cTreeNodeListChild33)<<std::endl;}pChild = cTreeNodeList.GetChild(0x03);if (NULL != pChild) {std::cout<<pChild->GetLevel()<<"    "<<pChild->GetData()<<"    "<<pChild->GetDegree()<<std::endl;std::cout<<pChild->IsLeaf()<<"    "<<pChild->IsBranch()<<"    "<<pChild->IsParent(&cTreeNodeList)<<"    "<<pChild->IsParent(&cTreeNodeListChild33)<<std::endl;}pChild = cTreeNodeList.GetChildRight();if (NULL != pChild) {std::cout<<pChild->GetLevel()<<"    "<<pChild->GetData()<<"    "<<pChild->GetDegree()<<std::endl;std::cout<<pChild->IsLeaf()<<"    "<<pChild->IsBranch()<<"    "<<pChild->IsParent(&cTreeNodeList)<<"    "<<pChild->IsParent(&cTreeNodeListChild33)<<std::endl;}AL_ListSingle<AL_TreeNodeList<DWORD>*> cListSingle;AL_TreeNodeList<DWORD>*pList = NULL;BOOL bSibling = cTreeNodeList.GetSibling(cListSingle);if (TRUE == bSibling) {for (DWORD dwCnt=0; dwCnt<cListSingle.Length(); dwCnt++) {if (TRUE == cListSingle.Get(pList, dwCnt)) {if (NULL != pList) {std::cout<<pList->GetLevel()<<"    "<<pList->GetData()<<"    "<<pList->GetDegree()<<std::endl;std::cout<<pList->IsLeaf()<<"    "<<pList->IsBranch()<<"    "<<pList->IsParent(&cTreeNodeList)<<"    "<<pList->IsParent(&cTreeNodeListChild33)<<std::endl;}}}}cListSingle.Clear();bSibling = cTreeNodeListChild20.GetSibling(cListSingle);if (TRUE == bSibling) {for (DWORD dwCnt=0; dwCnt<cListSingle.Length(); dwCnt++) {if (TRUE == cListSingle.Get(pList, dwCnt)) {if (NULL != pList) {std::cout<<pList->GetLevel()<<"    "<<pList->GetData()<<"    "<<pList->GetDegree()<<std::endl;std::cout<<pList->IsLeaf()<<"    "<<pList->IsBranch()<<"    "<<pList->IsParent(&cTreeNodeList)<<"    "<<pList->IsParent(&cTreeNodeListChild33)<<std::endl;}}}}cListSingle.Clear();BOOL bAncestor = cTreeNodeListParent.GetAncestor(cListSingle);if (TRUE == bAncestor) {for (DWORD dwCnt=0; dwCnt<cListSingle.Length(); dwCnt++) {if (TRUE == cListSingle.Get(pList, dwCnt)) {if (NULL != pList) {std::cout<<pList->GetLevel()<<"    "<<pList->GetData()<<"    "<<pList->GetDegree()<<std::endl;std::cout<<pList->IsLeaf()<<"    "<<pList->IsBranch()<<"    "<<pList->IsParent(&cTreeNodeList)<<"    "<<pList->IsParent(&cTreeNodeListChild33)<<std::endl;}}}}bAncestor = cTreeNodeListChild33.GetAncestor(cListSingle);if (TRUE == bAncestor) {for (DWORD dwCnt=0; dwCnt<cListSingle.Length(); dwCnt++) {if (TRUE == cListSingle.Get(pList, dwCnt)) {if (NULL != pList) {std::cout<<pList->GetLevel()<<"    "<<pList->GetData()<<"    "<<pList->GetDegree()<<std::endl;std::cout<<pList->IsLeaf()<<"    "<<pList->IsBranch()<<"    "<<pList->IsParent(&cTreeNodeList)<<"    "<<pList->IsParent(&cTreeNodeListChild33)<<std::endl;}}}}cListSingle.Clear();BOOL bDescendant = cTreeNodeListParent.GetDescendant(cListSingle);if (TRUE == bDescendant) {for (DWORD dwCnt=0; dwCnt<cListSingle.Length(); dwCnt++) {if (TRUE == cListSingle.Get(pList, dwCnt)) {if (NULL != pList) {std::cout<<pList->GetLevel()<<"    "<<pList->GetData()<<"    "<<pList->GetDegree()<<std::endl;std::cout<<pList->IsLeaf()<<"    "<<pList->IsBranch()<<"    "<<pList->IsParent(&cTreeNodeList)<<"    "<<pList->IsParent(&cTreeNodeListChild33)<<std::endl;}}}}cListSingle.Clear();bDescendant = cTreeNodeListChild33.GetDescendant(cListSingle);if (TRUE == bDescendant) {for (DWORD dwCnt=0; dwCnt<cListSingle.Length(); dwCnt++) {if (TRUE == cListSingle.Get(pList, dwCnt)) {if (NULL != pList) {std::cout<<pList->GetLevel()<<"    "<<pList->GetData()<<"    "<<pList->GetDegree()<<std::endl;std::cout<<pList->IsLeaf()<<"    "<<pList->IsBranch()<<"    "<<pList->IsParent(&cTreeNodeList)<<"    "<<pList->IsParent(&cTreeNodeListChild33)<<std::endl;}}}}cListSingle.Clear();#endif


原创粉丝点击