惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
IT之家
IT之家
V
Visual Studio Blog
The Cloudflare Blog
博客园 - 司徒正美
Jina AI
Jina AI
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
S
SegmentFault 最新的问题
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - Franky
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 不再专注于.net技术

嵌入式c/C++ 多线程专题3 - 不再专注于.net技术 - 博客园 多线程调度专题2 - 不再专注于.net技术 - 博客园 多线程同步问题专题(1) TCP/IP卷一(4) TCP/IP卷一(3) TCP/IP卷一(2) TCP/IP卷一(1) Net Programming 设备坐标 投资收益 The Strategy Pattern Com+ ?? Learning Bridge pattern. debian linux 树结构的遍历与建立树 初始化COM GNU Debian Linux学习 vc调用vb DLL方法
删除内存树结构的时候,千万注意内存泄漏问题,采用递归比较...
不再专注于.net技术 · 2006-01-24 · via 博客园 - 不再专注于.net技术

测试代码:
void CTestTreeDlg::OnBuild()
{
   TreeStruct * pTemp;
   pTemp = new TreeStruct();
   pTemp->m_strProperty = "Root";
   pTemp->AddSon("A")->DelSon()->AddBrother("B");
   pTemp->AddSon("CCC");
   pTemp->AddBrother("DDD");
   //pTemp->DelSon();

   buildTree(pTemp,NULL);
}
void CTestTreeDlg::buildTree(TreeStruct *pBase,HTREEITEM hParent1)
{
   HTREEITEM hParent;
   hParent= m_Tree.InsertItem(pBase->m_strProperty,0,0,hParent1);

   if(pBase->m_pSon)
   {
    buildTree(pBase->m_pSon,hParent);
   }
   if(pBase->m_pNextBrother  )
   {
    buildTree(pBase->m_pNextBrother,hParent1);
   }
}
数据结构的头文件:

class TreeStruct 
{
public:
 TreeStruct();
 virtual ~TreeStruct();
public:
 CString   m_strProperty;
 TreeStruct *m_pNextBrother;
 TreeStruct *m_pSon;
 TreeStruct * m_pParent;
public:
 TreeStruct * DelSon();
 TreeStruct * AddSon(CString strName);
 TreeStruct * AddBrother(CString strName);
 TreeStruct * GetRoot();

};

数据结构的实现文件:

TreeStruct * TreeStruct::DelSon()
{
  TreeStruct * pTemp = NULL;
  pTemp = m_pSon;
  TreeStruct *pTemp1 = NULL;
  TreeStruct *pTemp2 = NULL;
  if(pTemp)
  {
      pTemp1 = pTemp->m_pSon;
   if(pTemp1)
    return pTemp1->DelSon();
   pTemp2 = pTemp->m_pNextBrother;
   if(pTemp2)
    pTemp2->DelSon();
       delete pTemp;
    pTemp = NULL;
    m_pSon = NULL;
  }
  return this;
}

TreeStruct * TreeStruct::AddSon(CString strName)
{
 if(m_pSon)
 {
  return m_pSon->AddBrother(strName);
 }
 else
 {
  TreeStruct * pTemp = new TreeStruct();
  pTemp->m_strProperty = strName;
  pTemp->m_pParent = this;
  m_pSon = pTemp;
  return m_pSon;
 }
}
TreeStruct * TreeStruct::AddBrother(CString strName)
{
 if(m_pNextBrother == NULL)
 { 
  m_pNextBrother =  new TreeStruct();
  m_pNextBrother->m_strProperty = strName;
  m_pNextBrother->m_pParent = this;
  return m_pNextBrother;
 }
 else
 { 
  return  m_pNextBrother->AddBrother(strName);
 }
}

TreeStruct *TreeStruct::GetRoot()
{
  TreeStruct *pNowData = NULL;
  TreeStruct *pResult  = NULL;
  pNowData = this;
  while(pNowData)
  {
   pResult = pNowData->m_pParent;
   pNowData = pNowData->m_pParent;
  }
  return pResult;
}