structfun.h
//数据结构函数头文件
#include <stdio.h>
#include <iostream>
#include<string>
using std::cout;
using std::cin;
using std::string;
#define MAXSIZE 200
#define OK 1
#define ERROR 0
typedef string ElemType;
//5->线索二叉树存储结构
typedef enum {Link,Thread} Tag;//Link链接 Thread线索
typedef struct BinaryThreadNode
{
ElemType data;
struct BinaryThreadNode *lchild,*rchild,*parent;//加上parent指针是为后序线索二叉树准备(中序前序用不到)
Tag LTag,Rtag;
}BinaryThreadNode,*BinaryThreadTree;
//线索二叉树创建(前序法)
int CreatBinaryThread(BinaryThreadTree &btt);
//线索二叉树线索化(中序遍历)
int ThreadBinary(BinaryThreadTree &btt);
//添加线索二叉树的头节点
int ThreadBinaryLoopLink(BinaryThreadTree &btt);
//线索二叉树遍历
int ThreadBinarySearch(BinaryThreadTree &btt);
//前序线索化二叉树
int BeforeThreadBinary(BinaryThreadTree &btt);
//前序遍历线索二叉树
int BeforeThreadBinarySearch(BinaryThreadTree &btt);
//后序线索化二叉树
int AfterThreadBinary(BinaryThreadTree &btt);
//创建线索二叉树(三叉链表带根节点)
int CreatBinaryThreadB(BinaryThreadTree &btt,BinaryThreadTree &pt);
//后序线索化二叉树遍历
int AfterThreadBinarySearch(BinaryThreadTree &btt);
structfun.cpp 功能实现
#include<iostream>
#include<stdio.h>
#include<string>
#include<sstream>
#include"structfun.h"
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::ostringstream;
//线索二叉树创建(前序法)
int CreatBinaryThread(BinaryThreadTree &btt)
{
ElemType e;
printf("请输入线索二叉树节(空节点用“#”)表示:");
cin>>e;
if(e=="#")
{
btt->data="";
return 1;
}
btt->data=e;
//btt->LTag=Link;
//btt->Rtag=Link;
btt->lchild=new BinaryThreadNode();
btt->rchild=new BinaryThreadNode();
CreatBinaryThread(btt->lchild);
CreatBinaryThread(btt->rchild);
return OK;
}
//线索二叉树线索化(中序遍历)
static BinaryThreadTree pre=new BinaryThreadNode();//全局变量static引用定义同时进行,作用域当前模块,extern在头文件中引用,在模块中定义,作用域整个工程。
int ThreadBinary(BinaryThreadTree &btt)
{
if(btt->data!="")
{
ThreadBinary(btt->lchild);
if(btt->lchild->data=="") //lchild为空指针指向后继
{
btt->LTag=Thread;
btt->lchild=pre;
}
else
btt->LTag=Link;
if(pre->data!=""&&pre->rchild->data=="")//rchild为空指向前驱
{
pre->Rtag=Thread;
pre->rchild=btt;
}
else
pre->Rtag=Link;
pre=btt;
ThreadBinary(btt->rchild);
}
return OK;
}
//添加线索二叉树的头节点函数
int ThreadBinaryLoopLink(BinaryThreadTree &btt)//添加线索二叉树的头节点
{
ThreadBinary(btt);//线索化二叉树
BinaryThreadTree head,temp;
head = new BinaryThreadNode();
temp = new BinaryThreadNode();
head->data="head";
head->lchild=btt;
temp=btt;
while(temp->lchild->data!="")//查找最左节点lchild指向head
temp=temp->lchild;
temp->lchild=head;
temp=btt;
while(temp->rchild->data!="")//查找最右节点rchild指向head,head指向rchild
temp=temp->rchild;
temp->rchild=head;
head->rchild=temp;
head->LTag=Link;
head->Rtag=Thread;
btt=head;//btt指针移到头结点
return OK;
}
//线索二叉树遍历
int ThreadBinarySearch(BinaryThreadTree &btt)
{
BinaryThreadTree temp;
temp=btt->lchild;
while(temp!=btt)
{
while(temp->LTag==Link)
temp=temp->lchild;//从根目录开始找到最后一个左孩子
printf("%s ",temp->data.c_str());
while(temp->Rtag!=Link&&temp->rchild!=btt)//顺着右边开始遍历
{
temp=temp->rchild;
printf("%s ",temp->data.c_str());
}
temp=temp->rchild;
}
return OK;
}
//前序线索化二叉树
int BeforeThreadBinary(BinaryThreadTree &btt)
{
if(btt->data!="")
{
if(btt->lchild->data=="") //lchild为空指针指向后继
{
btt->LTag=Thread;
btt->lchild=pre;
}
else
btt->LTag=Link;
if(pre->data!=""&&pre->rchild->data=="")//rchild为空指向前驱
{
pre->Rtag=Thread;
pre->rchild=btt;
}
else
pre->Rtag=Link;
pre=btt;
if(btt->LTag==Link)//这个if是确保btt不是末尾节点
BeforeThreadBinary(btt->lchild);
if(btt->Rtag==Link)
BeforeThreadBinary(btt->rchild);
}
return OK;
}
//前序遍历线索二叉树
int BeforeThreadBinarySearch(BinaryThreadTree &btt)
{
BinaryThreadTree temp;
temp=btt;
while(temp->LTag!=Thread)
{
printf("%s ",temp->data.c_str());
temp=temp->lchild;
}
while(temp->data!="")
{
if(temp->LTag==Link)
{
printf("%s ",temp->data.c_str());
temp=temp->lchild;
}else
{
printf("%s ",temp->data.c_str());
temp=temp->rchild;
}
}
return OK;
}
//线索二叉树创建(前序法)三叉链表包含parent指针
int CreatBinaryThreadB(BinaryThreadTree &btt,BinaryThreadTree &pt)
{
ElemType e;
printf("请输入线索二叉树节点(空节点用“#”表示:");
cin>>e;
if(e=="#")
{
btt->data="";
return 1;
}
if(pt->data!="")
btt->parent=pt;//增加一个父节点指针
btt->data=e;
btt->lchild=new BinaryThreadNode();
btt->rchild=new BinaryThreadNode();
CreatBinaryThreadB(btt->lchild,btt);//btt就是btt->lchild或rchild的父节点
CreatBinaryThreadB(btt->rchild,btt);
return OK;
}
//后序线索化二叉树
int AfterThreadBinary(BinaryThreadTree &btt)
{
if(btt->data=="")
return OK;
AfterThreadBinary(btt->lchild);
AfterThreadBinary(btt->rchild);
//printf("%s ",btt->data.c_str());
//处理左节点线索化(后缀)
if(btt->rchild->data=="")
{
btt->rchild=pre;
btt->Rtag=Thread;
}
else btt->Rtag=Link;
pre=btt;
//处理右节点线索化(前缀)
if(btt->lchild->data=="")
{
if(btt->parent->rchild->data!=""&&btt->parent->rchild->data!=btt->data)
{
//如果当前节点是左节点而且当前节点的父节点有右节点,那么线索链接到此右节点
btt->lchild=btt->parent->rchild;
btt->LTag=Thread;
}
else
{
//否则线索链接到父节点
btt->lchild=btt->parent;
btt->LTag=Thread;
}
}else btt->LTag=Link;
return OK;
}
//后序线索化二叉树遍历(采用前缀遍历需要一个小树一个小树遍历)
int AfterThreadBinarySearch(BinaryThreadTree &btt)
{
BinaryThreadTree temp;
temp=btt;
printf("后序线索化(后缀)遍历:");
while(temp->data!="")
{
printf("%s ",temp->data.c_str());
temp=temp->rchild;
}
return OK;
}
main.cpp
#include<iostream>
#include<stdio.h>
#include <stdlib.h>
#include<string>
#include"structfun.h"
using std::string;
using std::printf;
using std::scanf;
using std::endl;
using std::to_string;
void main()
{
//二叉树线索化
BinaryThreadTree btt;
btt=new BinaryThreadNode();
//CreatBinaryThread(btt);//创建线索链表结构二叉树(二叉链表)
CreatBinaryThreadB(btt,btt);//创建线索链表结构二叉树(三叉链表包含parent指针)
//ThreadBinary(btt);//二叉树线索化
//ThreadBinaryLoopLink(btt);//二叉树线索化并根目录加头节点
//中序遍历线索化二叉树
//printf("中序遍历线索化二叉树:\n");
//ThreadBinarySearch(btt);
前序线索化二叉树
//BeforeThreadBinary(btt);
printf("前序线索化二叉树:");
//BeforeThreadBinarySearch(btt);
//后序线索化二叉树
//后序线索化二叉树需要采用三叉链表的存储结构,既需要知道当前节点的双亲
//lchild-ltag-data-parent-rtag-rchild
AfterThreadBinary(btt);
AfterThreadBinarySearch(btt);
system("pause");
}