二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历。用二叉树作为存储结构时,取到一个节点,只 能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继。
为了保存这种在遍历中需要的信息,我们利用二叉树中指向左右子树的空指针来存放节点的前驱和后继信息。
线索化实现如下:
#pragma once
using namespace std;
enum PointerType
{
THREAD,
LINK,
};
template <class T>
struct BinaryTreeNodeThd
{
BinaryTreeNodeThd<T>* _left;
BinaryTreeNodeThd<T>* _right;
T _data;
PointerType _leftType;
PointerType _rightType;
BinaryTreeNodeThd(const T& x)
: _data(x)
, _left(NULL)
, _right(NULL)
, _leftType(LINK)
, _rightType(LINK)
{}
};
template <class T,class Ref,class Ptr>
struct BinaryTreeIterator
{
typedef BinaryTreeNodeThd<T> Node;
typedef BinaryTreeIterator<T, Ref, Ptr> Self;
BinaryTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(operator*());
}
Self operator++(int)
{
Self tem(*this);
++*this;
return tem;
}
Self& operator++()
{
if (_node->_rightType == THREAD)
{
_node = _node->_right;
}
else
{
Node* left = _node->_right;
while (left->_leftType == LINK)
{
left = left->_left;
}
_node = left;
}
return *this;
}
Self operator--(int)
{
Self tem(*this);
--*this;
return tem;
}
Self& operator--()
{
if (_node->_leftType == LINK)
{
_node = _node->_left;
}
else
{
Node* right = _node->right;
while (right->_rightType == THREAD)
{
right = right->_right;
}
_node = right;
}
reurn *this;
}
bool operator!=(const Self &s)const
{
return _node != s._node;
}
Node* _node;
};
template <class T>
class BinaryTreeThd
{
typedef BinaryTreeNodeThd<T> Node;
public:
typedef BinaryTreeIterator<T, T&, T*> Iterator;
//typedef BinaryTreeIterator<T, const T&, const T*> ConstIterator;
BinaryTreeThd(T* a, size_t n, const T& invalid)
{
size_t index = 0;
_root = _CreateBinaryTree(a, n, invalid, index);
}
//中序线索化
void InOrderThreading()
{
Node* prev = _root;
_InOrderThreading(_root, prev);
if (prev)
prev->_rightType = THREAD;
}
//中序线索化遍历
void InOrderThd()
{
Node* cur = _root;
while (cur)
{
while (cur->_leftType == LINK)
{
cur = cur->_left;
}
cout << cur->_data << " ";
if (cur->_rightType == LINK)
{
cur = cur->_right;
}
else
{
while (cur->_rightType == THREAD)
{
cur = cur->_right;
if (cur == NULL)
{
cout << endl;
return;
}
cout << cur->_data << " ";
}
cur = cur->_right;
}
}
cout << endl;
}
//前序线索化
void PrevOrderThreading()
{
Node* prev = NULL;
_PrevOrderThreading(_root, prev);
}
//前序线索化遍历
void PrevOrderThd()
{
Node* cur = _root;
while (cur)
{
while (cur->_leftType == LINK)
{
cout << cur->_data << " ";
cur = cur->_left;
}
cout << cur->_data << " ";
cur = cur->_right;
}
cout << endl;
}
void PrevOrder()
{
_PrevOrder(_root);
cout << endl;
}
Iterator Begin()
{
Node* cur = _root;
while (cur->_leftType == LINK)
{
cur = cur->_left;
}
return Iterator(cur);
}
Iterator End()
{
return NULL;
}
protected:
Node* _CreateBinaryTree(T* a,size_t n,const T& invalid,size_t& index)
{
Node* root = NULL;
if (index < n && a[index] != invalid)
{
root = new Node(a[index]);
root->_left = _CreateBinaryTree(a, n, invalid, ++index);
root->_right = _CreateBinaryTree(a, n, invalid, ++index);
}
return root;
}
//中序线索化
void _InOrderThreading(Node* cur,Node* &prev)
{
if (cur == NULL)
return;
_InOrderThreading(cur->_left, prev);
if (cur->_left == NULL)
{
cur->_left = prev;
cur->_leftType = THREAD;
}
if (prev && prev->_right == NULL)
{
prev->_right = cur;
prev->_rightType = THREAD;
}
prev = cur;
_InOrderThreading(cur->_right, prev);
}
//前序线索化
void _PrevOrderThreading(Node* cur, Node* & prev)
{
if (cur == NULL)
return;
if (cur->_left == NULL)
{
cur->_left = cur;
cur->_leftType = THREAD;
}
if (prev && prev->_right == NULL)
{
prev->_right = cur;
prev->_rightType = THREAD;
}
prev = cur;
if (cur->_leftType == LINK)
{
_PrevOrderThreading(cur->_left, prev);
}
if (cur->_rightType == LINK)
{
_PrevOrderThreading(cur->_right, prev);
}
}
void _PrevOrder(Node* root)
{
if (root == NULL)
return;
cout << root->_data << " ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
protected:
Node* _root;
};
void TestBinaryTreeThd()
{
int array[] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
BinaryTreeThd<int> t1(array,sizeof(array)/sizeof(array[0]),'#');
t1.InOrderThreading();
t1.InOrderThd();
BinaryTreeThd<int>::Iterator it = t1.Begin();
while (it != t1.End())
{
cout << *it << " ";
++it;
}
cout << endl;
BinaryTreeThd<int> t2(array, sizeof(array) / sizeof(array[0]), '#');
t2.PrevOrderThreading();
t2.PrevOrderThd();
}