今天,我們一起用c++實現一顆線索二叉樹,具體**如下:
threadinorderiterator.h具體內容如下:
[cpp]view plain
copy
#include "threadtree.h"
template
<
typename
type>
class
threadinorderiterator
threadnode*first();
threadnode*prior();
threadnode*next();
void
print();
void
print(threadnode*start,
intn = 0);
void
inorder();
void
insertleft(threadnode*left);
void
insertright(threadnode*right);
threadnode*getparent(threadnode*current);
private
: threadtree&m_ptree;
threadnode*m_pcurrent;
void
inthread(threadnode*current, threadnode*pre);
};
template
<
typename
type>
void
threadinorderiterator::inthread(
threadnode*current, threadnode*pre)
if(pre->m_pright == null)
pre = current;
inthread(current->m_pright, pre);
} }
template
<
typename
type> threadnode* threadinorderiterator::first()
return
m_pcurrent;
} template
<
typename
type> threadnode* threadinorderiterator::prior()
} m_pcurrent = pmove;
if(m_pcurrent == m_ptree.m_proot)
return
m_pcurrent;
} template
<
typename
type> threadnode* threadinorderiterator::next()
} m_pcurrent = pmove;
if(m_pcurrent == m_ptree.m_proot)
return
m_pcurrent;
} template
<
typename
type>
void
threadinorderiterator::inorder()
m_pcurrent = pmove;
cout <
; while
(pmove != m_ptree.m_proot&&pmove)
cout ; }
template
<
typename
type>
void
threadinorderiterator::insertleft(threadnode*left)
m_pcurrent = left;
} template
<
typename
type>
void
threadinorderiterator::insertright(threadnode*right)
m_pcurrent = right;
} template
<
typename
type> threadnode* threadinorderiterator::getparent(
threadnode*current)
pmove = pmove->m_pleft;
if(pmove == m_ptree.m_proot)
} if
(pmove->m_pright == current)
pmove = pmove->m_pright;
while
(pmove->m_pleft != current)
return
pmove;
} template
<
typename
type>
void
threadinorderiterator::print(threadnode*start,
intn)
if(n >= 0)
return
; }
if(start->m_nrightthread == 0)
for(
inti = 0; i
if(n >= 0)
if(start->m_nleftthread == 0)
} template
<
typename
type>
void
threadinorderiterator::print()
threadnode.h具體內容如下:
[cpp]view plain
copy
template
<
typename
type>
class
threadtree;
template
<
typename
type>
class
threadinorderiterator;
template
<
typename
type>
class
threadnode
threadnode(const
type item) :m_data(item), m_pleft(null), m_pright(null)
, m_nleftthread(0), m_nrightthread(0){}
private
: int
m_nleftthread, m_nrightthread;
threadnode*m_pleft, *m_pright;
type m_data;
};
threadtree.h具體內容如下:
[cpp]view plain
copy
#include "threadnode.h"
template
<
typename
type>
class
threadinorderiterator;
template
<
typename
type>
class
threadtree
private
: threadnode*m_proot;
};
main.cpp具體內容如下:
[cpp]view plain
copy
#include
using
namespace
std;
#include "threadinorderiterator.h"
intmain();
for(
inti = 0; i
threadtree.print();
cout <
threadtree.inorder();
return
0;
}
執行效果如圖1所示:
圖1 執行效果
線索二叉樹
當用二叉鍊錶作為二叉樹的儲存結構時,因為每個結點中只有指向其左 右兒子結點的指標,所以從任一結點出發只能直接找到該結點的左 右兒子。在一般情況下靠它無法直接找到該結點在某種遍歷序下的前驅和後繼結點。如果在每個結點中增加指向其前驅和後繼結點的指標,將降低儲存空間的效率。我們可以證明 在n個結點的二叉鍊...
線索二叉樹
1.線索二叉樹結構和操作定義 threadbintree.h 功能 線索標誌域所有值 typedef enumnodeflag 功能 線索二叉樹結構體 typedef struct threadtreethreadbintree 前驅節點指標 threadbintree previous null ...
線索二叉樹
原始碼 中序線索二叉樹 author 菜鳥 version 2014.7.23 include include include typedef char datatype using namespace std 定義線索二叉樹的結構體 typedef struct nodethreadbitreen...