一,简介
二,代码
中序线索二叉树的实现
#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct threadNode
{
/* data */
ElemType data;
threadNode *lchild, *rchild;
/*
tag = 0 指针指向孩子
tag = 1 线索指针
*/
int ltag, rtag;
} threadNode, *threadTree;
//中序遍历二叉树,一遍遍历一遍线索化
void MidVisTheradTree(threadTree node, threadTree &pre)
{
if (node != NULL)
{
MidVisTheradTree(node->lchild, pre); //中序遍历左子树
cout << "==============================" << endl;
cout << "\t当前结点为: " << node->data << endl;
if (node->lchild == NULL)
{ //左子树为空建立前驱线索
node->lchild = pre;
node->ltag = 1; //表示这是有左前驱线索节点
if (node->lchild)
cout << "\t当前结点: " << node->data << "左孩子为空 "
<< "前驱是: " << node->lchild->data << endl;
}
//ABD#G##E##CF###
//pre节点为node的前驱,如果pre的右孩子是空的,让pre节点指向pre的后继结点node
if (pre != NULL && pre->rchild == NULL)
{
pre->rchild = node;
pre->rtag = 1;
if (pre->rchild)
cout << "\t" << pre->data << "结点右孩子为空 "
<< "后继结点为:" << pre->rchild->data << endl;
}
cout << "==============================" << endl;
//更新pre结点
pre = node;
MidVisTheradTree(node->rchild, pre); //中序遍历右子树
}
}
//建立中序线索二叉树
void CreateMidThreadTree(threadTree node)
{
//当前节点的前驱
threadTree pre = NULL;
if (node != NULL)
{
MidVisTheradTree(node, pre);
if (pre->rchild == NULL)
{
pre->rtag = 1;
}
}
}
//利用前序创建二叉树
void built_Tree(threadTree &t)
{
char ch;
cin >> ch;
if (ch == '#')
{
t = NULL;
return;
}
else
{
t = new threadNode;
t->data = ch;
t->lchild = t->rchild = NULL;
t->ltag = t->rtag = 0;
}
built_Tree(t->lchild);
built_Tree(t->rchild);
}
int main()
{
threadTree T;
built_Tree(T);
CreateMidThreadTree(T);
system("pause");
return 0;
}
三,样例
利用前序创建二叉树
输入样例 ABD#G##E##CF###
中序遍历:#D #G# B #E# A #F# C#(#表示没有建立线索二叉树时结点左or右指针为空)
四,先序和后序
后序,先序和中序差不多,都是利用遍历的方式,只不过先序有个坑
后序
void MidVisTheradTree(threadTree node, threadTree &pre)
{
if (node != NULL)
{
MidVisTheradTree(node->lchild, pre); //中序遍历左子树
MidVisTheradTree(node->rchild, pre); //中序遍历右子树
cout << "==============================" << endl;
cout << "\t当前结点为: " << node->data << endl;
if (node->lchild == NULL)
{ //左子树为空建立前驱线索
node->lchild = pre;
node->ltag = 1; //表示这是有左前驱线索节点
if (node->lchild)
cout << "\t当前结点: " << node->data << "左孩子为空 "
<< "前驱是: " << node->lchild->data << endl;
}
//ABD#G##E##CF###
//pre节点为node的前驱,如果pre的右孩子是空的,让pre节点指向pre的后继结点node
if (pre != NULL && pre->rchild == NULL)
{
pre->rchild = node;
pre->rtag = 1;
if (pre->rchild)
cout << "\t" << pre->data << "结点右孩子为空 "
<< "后继结点为:" << pre->rchild->data << endl;
}
cout << "==============================" << endl;
//更新pre结点
pre = node;
}
}
先序要注意
void MidVisTheradTree(threadTree node, threadTree &pre)
{
if (node != NULL)
{
cout << "==============================" << endl;
cout << "\t当前结点为: " << node->data << endl;
if (node->lchild == NULL)
{ //左子树为空建立前驱线索
node->lchild = pre;
node->ltag = 1; //表示这是有左前驱线索节点
if (node->lchild)
cout << "\t当前结点: " << node->data << "左孩子为空 "
<< "前驱是: " << node->lchild->data << endl;
}
//ABD#G##E##CF###
//pre节点为node的前驱,如果pre的右孩子是空的,让pre节点指向pre的后继结点node
if (pre != NULL && pre->rchild == NULL)
{
pre->rchild = node;
pre->rtag = 1;
if (pre->rchild)
cout << "\t" << pre->data << "结点右孩子为空 "
<< "后继结点为:" << pre->rchild->data << endl;
}
cout << "==============================" << endl;
//更新pre结点
pre = node;
if(node->ltag == 0)//这是我的儿子我才处理
MidVisTheradTree(node->lchild, pre); //中序遍历左子树
MidVisTheradTree(node->rchild, pre); //中序遍历右子树
}
}
由于先序的特性是先拜访根结点,假设当前结点D,没有左孩子,前驱结点为pre,左孩子为D;
那么我们处理的D结点,由于D没有左孩子,在处理后会建立线索,左孩子的线索是指向它的前驱结点,也就是pre, 现在处理完了D结点,按照先序遍历就应该处理D的左孩子了,根据前面的描述,现在D的左孩子由原来的空节点变成了一个线索指针,指向它的前驱结点pre,D的左孩子就不为空了;
如果我们还是按照原来的做法,那么现在由于D的左孩子指向D的前驱pre,那么我们接下来就处理pre,pre的左孩子又是D这样我们就陷入了一个循环;
所以我们根据tag标识来判断这是线索指针还是他孩子的指针