#include<iostream>
#include<assert.h>
using namespace std;
enum PointerTag
{
THREAD,
LINK
};
template<class T>
struct BinaryTreeNodeThd
{
T _data;
BinaryTreeNodeThd<T>* _left;
BinaryTreeNodeThd<T>* _right;
BinaryTreeNodeThd<T>* _parent;
PointerTag _leftTag;
PointerTag _rightTag;
BinaryTreeNodeThd(const T& x)
:_data(x)
,_left(NULL)
,_right(NULL)
,_parent(NULL)
,_leftTag(LINK)
,_rightTag(LINK)
{}
};
template<class T>
class BinaryTreeThd
{
public:
BinaryTreeThd()
:_root(NULL)
{}
BinaryTreeThd(const T* a,size_t size)
{
size_t index=0;
_root=_CreateTree(a,index,size);
}
void InOrderThreading()
{
BinaryTreeNodeThd<T>* prev=NULL;
_InOrderThreading(_root,prev);
}
void PrevOrderThreading()
{
BinaryTreeNodeThd<T>* prev=NULL;
_PrevOrderThreading(_root,prev);
}
void PostOrderThreading()
{
BinaryTreeNodeThd<T>* prev=NULL;
_PostOrderThreading(_root,prev);
}
void InOrderThd()
{
BinaryTreeNodeThd<T>* cur=_root;
while(cur)
{
//找最左节点
while((cur)&&(cur->_leftTag!=THREAD))
{
cur=cur->_left;
}
cout<<cur->_data<<" ";
//访后继
while(cur->_rightTag !=LINK)
{
cur=cur->_right;
cout<<cur->_data<<" ";
}
//跳转到右子树
cur=cur->_right;
}
cout<<endl;
}
void PrevOrderThd()
{
BinaryTreeNodeThd<T>* cur=_root;
while(cur)
{
while(cur&&cur->_leftTag==LINK)
{
cout<<cur->_data<<" ";
cur=cur->_left;
}
cout<<cur->_data<<" ";
cur=cur->_right;
}
cout<<endl;
}
void PostOrderThd()
{
BinaryTreeNodeThd<T>* cur=_root;
BinaryTreeNodeThd<T>* prev=NULL;
while(1)
{
while(cur&&cur->_leftTag==LINK)
{
cur=cur->_left;
}
//当子节点为THRED时,遍历左右回到根节点
while(cur&&cur->_rightTag==THREAD)
{
cout<<cur->_data<<" ";
prev=cur;
cur=cur->_right;
}
if(cur==_root)
{
cout<<_root->_data<<" ";
break;
}
//判断右节点有没有访问过
if(cur&&cur->_leftTag==LINK&&cur->_right==prev)
{
cout<<cur->_data<<" ";
cur=cur->_parent;
}
//当子节点为LINK类型,遍历右子树
if(cur&&cur->_rightTag==LINK)
{
cur=cur->_right;
}
}
cout<<endl;
}
protected:
BinaryTreeNodeThd<T>* _root;
BinaryTreeNodeThd<T>* _CreateTree(const T* _array,size_t& index,size_t size)
{
BinaryTreeNodeThd<T>* root=NULL;
if(index<size&&_array[index]!=‘#‘)
{
root=new BinaryTreeNodeThd<T>(_array[index]);
root->_left=_CreateTree(_array,++index,size);
if (root->_left)
{
root->_left->_parent = root;
}
root->_right = _CreateTree(_array, ++index, size);
if (root->_right)
{
root->_right->_parent = root;
}
}
return root;
}
void _InOrderThreading(BinaryTreeNodeThd<T>* cur,BinaryTreeNodeThd<T>*& prev)
{
if(cur==NULL)
return;
_InOrderThreading(cur->_left,prev);
if(cur->_left==NULL)
{
cur->_leftTag =THREAD;
cur->_left=prev;
}
if(prev&&prev->_right==NULL)
{
prev->_rightTag=THREAD;
prev->_right =cur;
}
prev=cur;
_InOrderThreading(cur->_right,prev);
}
void _PrevOrderThreading(BinaryTreeNodeThd<T>* cur,BinaryTreeNodeThd<T>*& prev)
{
if(cur==NULL)
return;
if(cur->_left==NULL)
{
cur->_leftTag=THREAD;
cur->_left=prev;
}
if(prev&&prev->_right==NULL)
{
prev->_rightTag=THREAD;
prev->_right=cur;
}
prev=cur;
if(cur->_leftTag == LINK)
_PrevOrderThreading(cur->_left,prev);
if(cur->_rightTag == LINK)
_PrevOrderThreading(cur->_right,prev);
}
void _PostOrderThreading(BinaryTreeNodeThd<T>* cur,BinaryTreeNodeThd<T>*& prev)
{
if(cur==NULL)
return;
_PostOrderThreading(cur->_left,prev);
_PostOrderThreading(cur->_right,prev);
if(cur->_left==NULL)
{
cur->_leftTag=THREAD;
cur->_left=prev;
}
if(prev&&prev->_right==NULL)
{
prev->_rightTag=THREAD;
prev->_right=cur;
}
prev=cur;
}
};
void Test1()
{
//char _array[10]={‘a‘,‘b‘,‘d‘,‘#‘,‘#‘,‘e‘,‘#‘,‘#‘,‘c‘,‘f‘};
int _array[10] = {1, 2, 3, ‘#‘, ‘#‘, 4, ‘#‘, ‘#‘, 5, 6};
BinaryTreeThd<int> btt1(_array,10);
btt1.InOrderThreading();
btt1.InOrderThd();
BinaryTreeThd<int> btt2(_array,10);
btt2.PrevOrderThreading();
btt2.PrevOrderThd();
BinaryTreeThd<int> btt3(_array,10);
btt3.PostOrderThreading();
btt3.PostOrderThd();
}
int main()
{
Test1();
system("pause");
return 0;
}本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1757511
原文:http://10707460.blog.51cto.com/10697460/1757511