本文代码为C++实现线索二叉树,文中所有代码均经过测试。
#ifndef _THREAD_BIN_TREE_H
#define _THREAD_BIN_TREE_H
#include <iostream>
#include <stack>
#include <assert.h>
using namespace::std;
typedef enum{LINK, THREAD}Tag;
template<typename _T>
class _Thread_Bin_Tree;
template<typename _T>
class _Tree_Node{
friend class _Thread_Bin_Tree<_T>;
public:
_Tree_Node() :m_data(_T()), leftChild(NULL), rightChild(NULL), ltag(LINK), rtag(LINK)
{}
_Tree_Node(_T data, _Tree_Node<_T>* left = NULL, _Tree_Node<_T>* right = NULL)
:m_data(data), leftChild(left), rightChild(left), ltag(LINK), rtag(LINK)
{}
public:
Tag ltag;
Tag rtag;
_T m_data;
_Tree_Node<_T> *leftChild;
_Tree_Node<_T> *rightChild;
};
template<typename _T>
class _Thread_Bin_Tree{
typedef _Tree_Node<_T> node_type;
public:
_Thread_Bin_Tree() = default;
_Thread_Bin_Tree(const _T& ref) :refval(ref)
{}
~_Thread_Bin_Tree() {}
public:
void init_bin_tree(const char *&);
void inorder_thread();
void inorder_traverse()const;
node_type* First()const;
node_type* Last()const;
node_type *Prev(node_type *)const;
node_type *Next(node_type *)const;
node_type *Parent(node_type *)const;
protected:
void init_bin_tree(node_type *&, const char *&);
void inorder_thread(node_type *&, node_type *&);
void inorder_traverse(node_type *)const;
node_type* First(node_type *)const;
node_type* Last(node_type *)const;
node_type *Prev(node_type *, node_type *)const;
node_type *Next(node_type*, node_type *)const;
node_type *Parent(node_type*, node_type *)const;
private:
_T refval;
node_type *root;
};
/************************************public interface******************************************/
template<typename _T>
void _Thread_Bin_Tree<_T>::init_bin_tree(const char *&str)
{
init_bin_tree(root, str);
}
template<typename _T>
void _Thread_Bin_Tree<_T>::inorder_thread()
{
node_type *pre = NULL;
inorder_thread(root, pre);
pre->rtag = THREAD;
}
template<typename _T>
void _Thread_Bin_Tree<_T>::inorder_traverse()const
{
inorder_traverse(root);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::First()const
{
return First(root);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Last()const
{
return Last(root);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Prev(node_type *cur)const
{
return Prev(root, cur);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Next(node_type *cur)const
{
return Next(root, cur);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Parent(node_type *cur)const
{
return Parent(root, cur);
}
/************************************protected interface***************************************/
template<typename _T>
void _Thread_Bin_Tree<_T>::init_bin_tree(node_type *&pt, const char *&str)
{
if(*str == refval){
pt = NULL;
}
else{
pt = new node_type(*str);
init_bin_tree(pt->leftChild, ++str);
init_bin_tree(pt->rightChild, ++str);
}
}
template<typename _T>
void _Thread_Bin_Tree<_T>::inorder_thread(node_type *&pt, node_type *&pre)
{
if(pt == NULL)
return ;
inorder_thread(pt->leftChild, pre);
if(pt->leftChild == NULL){
pt->ltag = THREAD;
pt->leftChild = pre;
}
if(pre != NULL && pre->rightChild == NULL){
pre->rtag = THREAD;
pre->rightChild = pt;
}
pre = pt;
inorder_thread(pt->rightChild, pre);
}
template<typename _T>
void _Thread_Bin_Tree<_T>::inorder_traverse(node_type *pt)const
{
if(pt != NULL){
while(pt != NULL){
pt = First(pt);
cout << pt->m_data << " ";
while(pt->ltag == THREAD && pt->rightChild != NULL){
pt = pt->rightChild;
cout << pt->m_data << " ";
}
pt = pt->rightChild;
}
}
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::First(node_type *pt)const
{
assert(pt != NULL);
while(pt->ltag == LINK)
pt = pt->leftChild;
return pt;
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Last(node_type *pt)const
{
assert(pt != NULL);
while(pt->rtag == LINK)
pt = pt->rightChild;
return pt;
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Prev(node_type *pt, node_type *cur)const
{
assert(pt != NULL && cur != NULL && pt != cur);
if(cur->ltag == THREAD)
return cur->leftChild;
else
return Last(cur->leftChild);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Next(node_type *pt, node_type *cur)const
{
assert(pt != NULL && cur != NULL);
if(cur->ltag == THREAD)
return cur->rightChild;
else
return First(cur->rightChild);
}
template<typename _T>
_Tree_Node<_T>* _Thread_Bin_Tree<_T>::Parent(node_type *pt, node_type *cur)const
{
assert(pt != NULL && cur != NULL && pt != cur);
if(cur->rtag == THREAD && cur->rightChild != NULL
&& cur->rightChild->leftChild == cur)
return cur->rightChild;
if(cur->ltag == THREAD && cur->leftChild->rightChild == cur)
return cur->leftChild;
node_type *tmp = Last(cur)->rightChild;
if(tmp != NULL && tmp->leftChild->rightChild == cur)
return tmp->leftChild;
if(tmp != NULL && tmp->leftChild == cur) //for B -> A
return tmp;
tmp = First(cur)->leftChild;
if(tmp != NULL && tmp->rightChild == cur)
return tmp;
}
#endif