前序和中序线索二叉树的实现:
#include
using namespace std;
struct node
{
int data;//数据域
node*left;//左子树
node*right;//右指针
bool ltag;//左标志 1: youxiansuo 0:muyou
bool rtag;//右标志
};
class binaryTree
{
public:
node *root;
binaryTree()
{
root=NULL;
}
void creatTree(node *&r);
void inThread(node *&r,node *&pre);
void in();
void showIn();
void preThread(node *&r,node *&pre);
void pre();
void showPre();
};
void binaryTree::creatTree(node *&r)
{
int temp;
cin>>temp;
if(temp==0)
r=NULL;
else
{
node *p=new node;
p->data=temp;
r=p;
r->ltag=0;
r->rtag=0;
r->left=NULL;
r->right=NULL;
creatTree(r->left);
creatTree(r->right);
}
}
void binaryTree::inThread(node *&r,node *&pre)
{
if(r==NULL)
return;
else
{
if(!r->ltag)
inThread(r->left,pre);
if(r->left==NULL)
{
r->left=pre;
r->ltag=1;
}
if(pre&&!pre->right)
{
pre->rtag=1;
pre->right=r;
}
pre=r;
if(!r->rtag)
inThread(r->right,pre);
}
}
void binaryTree::in()
{
node *pre=NULL;
inThread(root,pre);
if(pre->right==NULL)
pre->rtag=1;
}
void binaryTree::showIn()
{
node*r=root;
while(r->left)
r=r->left;
while(r->right)
{
cout<data<
if(!r->rtag)
{
node*p=r->right;
while(!p->ltag)
p=p->left;
r=p;
}
else
r=r->right;
}
cout<data;
}
void binaryTree::preThread(node *&r,node *&pre)
{
if(r==NULL)return;
else
{
if(!r->left)
{
r->left=pre;
r->ltag=1;
}
if(pre&&!pre->right)
{
pre->right=r;
pre->rtag=1;
}
pre=r;
if(!r->ltag)
preThread(r->left,pre);
if(!r->rtag)
preThread(r->right,pre);
}
}
void binaryTree::pre()
{
node *pre=NULL;
preThread(root,pre);
if(pre->right==NULL)
pre->rtag=1;
}
void binaryTree::showPre()
{
node *r=root;
while(r!=NULL)
{
cout<data<
if(r->rtag==1)
{
r=r->right;
}
else
{
if(!r->ltag)
r=r->left;
else
r=r->right;
}
}
}
int main()
{
binaryTree hehe;
//输入时候要注意先序输入并且最后要补全0 如 1 2 3 则输入为1 2 0 0 3 0 0
hehe.creatTree(hehe.root);
// hehe.in();
// hehe.showIn();
hehe.pre();
hehe.showPre();
return 0;
}
后序线索二叉树比前两者多一个parent指针域,在遍历时分情况讨论即可。
后序线索二叉树的实现:
#include
using namespace std;
struct thrNode
{
int data;//数据域
thrNode*left;//左子树
thrNode*right;//右指针
thrNode*parent;//父节点指针
bool ltag;//左标志 1: youxiansuo 0:muyou
bool rtag;//右标志
};
class thrThreadTree
{
public:
thrNode *root;
thrThreadTree()
{
root=NULL;
}
void creatTree(thrNode *&r);
void postThread(thrNode *&r,thrNode *&pre);
void post();
void showPost();
};
void thrThreadTree::creatTree(thrNode *&r)
{
int temp;
cin>>temp;
if(temp==0)
r=NULL;
else
{
thrNode *p=new thrNode;
p->data=temp;
r=p;
r->ltag=0;
r->rtag=0;
r->left=NULL;
r->right=NULL;
r->parent=NULL;
creatTree(r->left);
creatTree(r->right);
if (r->left!=NULL)
{
r->left->parent=r;
}
if (r->right!=NULL)
{
r->right->parent=r;
}
}
}
void thrThreadTree::postThread(thrNode *&r,thrNode *&pre)
{
if(r==NULL)
return;
else
{
if(!r->ltag)
postThread(r->left,pre);
if(!r->rtag)
postThread(r->right,pre);
if(r->left==NULL)
{
r->left=pre;
r->ltag=1;
}
if(pre&&!pre->right)
{
pre->rtag=1;
pre->right=r;
}
pre=r;
}
}
void thrThreadTree::post()
{
thrNode *pre=NULL;
postThread(root,pre);
if(pre->right==NULL)
pre->rtag=1;
}
void thrThreadTree::showPost()
{
if(root!=NULL)
{
thrNode *cur=root;
while(!cur->ltag || !cur->rtag)
{
if(!cur->ltag)
cur=cur->left;
else
cur=cur->right;
}
while(cur!=NULL)
{
cout<data<
thrNode *pt=cur->parent;
if(cur->rtag)
{
cur=cur->right;
}
else if(cur==root)
{
cur=NULL;
}
else if(pt->right==cur || (pt->left==cur && pt->rtag))
{
cur=pt;
}
else
{
cur=pt->right;
while(!cur->ltag || !cur->rtag)
{
if(!cur->ltag)
cur=cur->left;
else
cur=cur->right;
}
}
}
}
}
//for test
int main()
{
thrThreadTree hehe;
//输入时候要注意先序输入并且最后要补全0 如 1 2 3 则输入为1 2 0 0 3 0 0
//example 1 0 2 3 5 0 0 0 4 0 0
hehe.creatTree(hehe.root);
hehe.post();
hehe.showPost();
return 0;
}