二叉树的线索化是指,将二叉树中节点的左指针或者右指针为空时,将其的左指针指向前驱,右指针指向后继的,同时将这些左右指针标记出来的一种做法,相应的代码如下所示:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <assert.h>
enum pointer
{
THREAD,
LINK,
};
template<class T>
struct BinaryTreeNodeThd
{
T _data;
BinaryTreeNodeThd<T> *_left;
BinaryTreeNodeThd<T> *_right;
pointer _lefttog;
pointer _righttog;
BinaryTreeNodeThd(const T&x)
:_data(x)
, _left(NULL)
, _right(NULL)
, _lefttog(LINK)
, _righttog(LINK)
{}
};
template <class T>
class BinaryTreeThd
{
typedef BinaryTreeNodeThd<T> Node;
protected:
Node *_root;
Node *_creattree(const T a[], size_t size, size_t &index, T invalid)
{
Node * root = NULL;
if (index < size && a[index] != invalid)
{
root = new Node(a[index]);
root->_left = _creattree(a, size, ++index, invalid);
root->_right = _creattree(a, size, ++index, invalid);
}
return root;
}
void _InorderThreating(Node *cur, Node *&prev)
{
if (cur == NULL)
return;
_InorderThreating(cur->_left, prev);
if (cur->_left == NULL)
{
cur->_lefttog = THREAD;
cur->_left = prev;
}
if (prev&&prev->_right == NULL)
{
prev->_righttog = THREAD;
prev->_right = cur;
}
prev = cur;
_InorderThreating(cur->_right, prev);
}
void _postorderThreating(Node *cur, Node *&prev)
{
if (cur == NULL)
return;
_InorderThreating(cur->_left, prev);
_InorderThreating(cur->_right, prev);
if (cur->_left == NULL)
{
cur->_lefttog = THREAD;
cur->_left = prev;
}
if (prev&&prev->_right == NULL)
{
prev->_righttog = THREAD;
prev->_right = cur;
}
prev = cur;
}
void _preorderThreating(Node *cur, Node *&prev)
{
if (cur == NULL)
return;
if (cur->_left == NULL)
{
cur->_lefttog = THREAD;
cur->_left = prev;
}
if (prev&&prev->_right == NULL)
{
prev->_righttog = THREAD;
prev->_right = cur;
}
prev = cur;
if (cur->_lefttog==LINK)
_preorderThreating(cur->_left, prev);
if (cur->_righttog == LINK)
_preorderThreating(cur->_right, prev);
}
void _print(Node *root)
{
if (root == NULL)
return;
cout << root->_data << " ";
_print(root->_left);
_print(root->_right);
}
public:
BinaryTreeThd(const T *a="", int size=0)
{
size_t index = 0;
_root = _creattree(a, size, index, '#');
}
void preorderThreating()
{
Node *prev = NULL;
_preorderThreating(_root, prev);
}
void preorderInd()
{
Node *cur = _root;
while (cur)
{
if (cur == NULL)
return;
if (cur->_lefttog == LINK)
{
cout << cur->_data << " ";
cur = cur->_left;
}
cout << cur->_data << " ";
/*while (cur->_righttog == THREAD)
{
cur = cur->_right;
cout << cur->_data << " ";
}
if (cur->_lefttog == LINK)
{
cur = cur->_left;
}
else
{
cur = cur-> _right;
}*/
cur = cur ->_right;
}
}
void InorderThreating()
{
Node *prev = NULL;
_InorderThreating(_root, prev);
}
void InorderInd()
{
Node *cur = _root;
while (cur)
{
if (cur == NULL)
return;
while (cur->_lefttog == LINK)
{
cur = cur->_left;
}
cout << cur->_data << " ";
while (cur->_righttog == THREAD)
{
cur = cur->_right;
cout << cur->_data << " ";
}
cur = cur->_right;
}
}
void print()
{
_print(_root);
}
};
void test()
{
BinaryTreeThd<char> b("12#3##45#6#7##8", 15);
b.preorderThreating();
b.preorderInd();
}
int main()
{
test();
getchar();
return 0;
}