//线索二叉树的销毁 不知道哪一点有问题
//最后一个函数谨慎使用
#include <iostream>
using namespace std;
// 标识类型
typedef int Status;
// 真值
#define TRUE 1
// 假值
#define FALSE 0
#define OK 2
#define ERROR -1
// 溢出错误
#define OVERFLOW -2
// 动态分配空间错误
#define NEW_ERROR -3
// 二叉树定义
// 二叉树树节点数据类型
typedef int BiTreeElemType;
typedef struct BiTreeNode
{
// 二叉树树节点数据
BiTreeElemType data;
// 左右孩子指针
struct BiTreeNode *lChild, *rChild;
} BiTreeNode, *BiTree;
// 线索二叉树定义
// 线索二叉树树节点数据类型
typedef int BiThreadTreeElemType;
// 线索二叉树儿子指针类别类型
typedef enum {None, Link, Thread} PointerTag;
typedef struct BiThreadTreeNode
{
// 线索二叉树树节点数据
BiThreadTreeElemType data;
// 左右指针
struct BiThreadTreeNode *lChild, *rChild;
// 左右指针类别
PointerTag lTag, rTag;
} BiThreadTreeNode, *BiThreadTree;
// 栈定义
// 栈元素类型
typedef BiTree StackElemType;
// 栈初始分配空间大小
#define STACK_INIT_SIZE 100
// 栈大小变化增量
#define STACK_INCREMENT 10
typedef struct
{
// 栈空间首地址
StackElemType *base;
// 栈顶地址
StackElemType *top;
// 当前栈分配空间大小
int stackSize;
} SqStack;
// 初始化栈
Status InitStack(SqStack &S)
{
S.base = (StackElemType *)new StackElemType[STACK_INIT_SIZE];
// 分配失败
if(S.base == NULL)
{
cout<<"InitStack()函数出错--初始化栈分配空间出错!\n";
return NEW_ERROR;
}
S.top=S.base;
S.stackSize=STACK_INIT_SIZE;
return OK;
}
// 栈是否为空
Status StackEmpty(SqStack &S)
{
if(S.top == S.base)
{
return TRUE;
}
else
{
return FALSE;
}
}
// 入栈
Status Push(SqStack &S, StackElemType e)
{
// 栈满,追加存储空间
if(S.top-S.base >= S.stackSize)
{
StackElemType *p = S.base;
S.base=new StackElemType[S.stackSize+STACK_INCREMENT];
if(S.base==NULL)
{
cout<<"Push()函数出错--栈满重新分配空间出错!\n";
return NEW_ERROR;
}
// 复制原栈空间数据到栈新空间
for(int i=0; i<S.stackSize; i++)
{
S.base[i]=p[i];
}
// 重新设置栈顶
S.top=S.base+S.stackSize;
// 增加当前栈大小
S.stackSize+=STACK_INCREMENT;
// 销毁原栈分配空间
delete[]p;
}
*S.top++=e;
return OK;
}
// 获取栈顶元素
Status Top(SqStack &S, StackElemType &e)
{
if(S.top == S.base)
{
cout<<"Top()函数出错--获取栈顶元素,栈为空!\n";
return ERROR;
}
e=*(S.top-1);
return OK;
}
// 获取栈顶元素
Status GetTop(SqStack &S, StackElemType &e)
{
if(S.top == S.base)
{
cout<<"GetTop()函数出错--获取栈顶元素,栈为空!\n";
return ERROR;
}
e = *(S.top-1);
return OK;
}
// 获取并删除栈顶元素
Status Pop(SqStack &S, StackElemType &e)
{
if(S.top == S.base)
{
cout<<"Pop()函数出错--获取栈顶元素,栈为空!\n";
return ERROR;
}
e=*--S.top;
return OK;
}
// 清空栈元素
Status ClearStack(SqStack &S)
{
S.top = S.base;
return OK;
}
// 销毁栈空间
Status DestroyStack(SqStack &S)
{
delete[]S.base;
return OK;
}
// 队列定义
// 队列元素类型
typedef BiTree QueueElemType;
// 最大队列长度
#define MAX_QUEUE_SIZE 100
// 队列类型
// 从队列头出数据,队列尾进数据;队列头指向第一个有效元素,队列尾指向第一个可用空间
// 队列头位置等于队列尾位置,队列为空;队列头位置为队列尾下一个位置,队列为满
typedef struct
{
QueueElemType *base;
int front;
int rear;
} SqQueue;
// 初始化队列
Status InitQueue(SqQueue &Q)
{
Q.base=(QueueElemType *)new QueueElemType[MAX_QUEUE_SIZE];
if(Q.base == NULL)
{
cout<<"InitQueue()函数出错--初始化队列分配空间出错!\n";
return NEW_ERROR;
}
Q.front=Q.rear=0;
return OK;
}
// 队列是否为空
Status QueueEmpty(SqQueue &Q)
{
if (Q.front == Q.rear)
{
return TRUE;
}
else
{
return FALSE;
}
}
// 队列元素个数
int QueueLength(SqQueue &Q)
{
return (Q.rear-Q.front+MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
// 进队列
Status EnQueue(SqQueue &Q, QueueElemType &e)
{
if((Q.rear+1)%MAX_QUEUE_SIZE == Q.front)
{
cout<<"EnQueue()函数出错--队列已满!\n";
return ERROR;
}
Q.base[Q.rear] = e;
Q.rear=(Q.rear+1)%MAX_QUEUE_SIZE;
return OK;
}
// 获取队列头元素
Status GetHead(SqQueue &Q, QueueElemType &e)
{
if(Q.rear == Q.front)
{
cout<<"GetHead()函数出错--队列为空!\n";
return ERROR;
}
e = Q.base[Q.front];
return OK;
}
// 获取并删除队列头元素
Status DeQueue(SqQueue &Q, QueueElemType &e)
{
if(Q.rear == Q.front)
{
cout<<"DeQueue()函数出错--队列为空!\n";
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front+1) % MAX_QUEUE_SIZE;
return OK;
}
// 删除队列空间
Status DestroyQueue(SqQueue &Q)
{
delete[]Q.base;
return OK;
}
// 二叉树操作
// 先序递归创建二叉树
Status CreateBiTree(BiTree &T)
{
BiTreeElemType n;
cin>>n;
// 输入小于0值,二叉树设置为空
if(n<0)
{
T=NULL;
}
// 输入大于等于0值,创建节点,递归生成左右子树
else
{
T=(BiTree)new BiTreeNode;
if(T==NULL)
{
cout<<"CreateBiTree()函数出错--生成节点空间出错!\n";
return NEW_ERROR;
}
T->data=n;
CreateBiTree(T->lChild);
CreateBiTree(T->rChild);
}
return OK;
}
// 先序非递归从数组创建二叉树
Status CreateBiTreeFromArray(BiTreeElemType a[], int nFrom, int nTo, BiTree &T)
{
// 数据从下标nFrom开始,nTo结束
// 数据占据数组单元个数
int nElementCount = nTo - nFrom + 1;
BiTree upper=NULL, current, below=NULL;
int n=1;
while (n>0)
{
while (n<=nElementCount && a[n+nFrom-1]>0)
{
current=(BiTree)new BiTreeNode;
if (current == NULL)
{
cout<<"CreateBiTreeFromArray()函数出错--生成节点空间出错!\n";
return NEW_ERROR;
}
current->data = a[n + nFrom -1];
current->rChild = current;
current->lChild = upper;
upper = current;
n*=2;
}
n/=2;
below=NULL;
// 沿右链返回
while(current!=NULL && current->rChild!=current)
{
upper = current->rChild;
current->rChild = below;
below = current;
current = upper;
n/=2;
}
// 返回到根部,二叉树创建完成
if(current==NULL)
{
break;
}
// 初始化生成右子树
current->rChild = current->lChild;
current->lChild = below;
upper = current;
n = n*2 + 1;
}
T = below;
return OK;
}
// 由二叉树先序和中序递归创建二叉树
Status CreateBiTreeFromPreAndInOrderSquences(BiTreeElemType a[], int preFrom, int preTo, int inFrom, int inTo, BiTree &T)
{
if (preTo < preFrom)
{
T = NULL;
return OK;
}
// 生成节点
T = (BiTree)new BiTreeNode;
if(T==NULL)
{
cout<<"CreateBiTree()函数出错--生成节点空间出错!\n";
return NEW_ERROR;
}
T->data=a[preFrom];
int i;
for( i=inFrom; i<=inTo; i++)
{
if (a[i] == a[preFrom]) break;
}
CreateBiTreeFromPreAndInOrderSquences(a, preFrom+1, preFrom-inFrom+i, inFrom, i-1, T->lChild);
CreateBiTreeFromPreAndInOrderSquences(a, preFrom-inFrom+i+1, preTo, i+1, inTo, T->rChild);
return OK;
}
// 访问二叉树节点元素函数
Status VisitBiTreeElement(BiTreeElemType &e)
{
cout<<e<<" ";
return OK;
}
// 先序递归遍历二叉树
Status PreOrderTraverseBiTreeRec(BiTree &T, Status (* Visit)(BiTreeElemType &))
{
if (T == NULL)
{
return OK;
}
if (Visit(T->data) !=OK) return ERROR;
PreOrderTraverseBiTreeRec(T->lChild, Visit);
PreOrderTraverseBiTreeRec(T->rChild, Visit);
return OK;
}
// 中序递归遍历二叉树
Status InOrderTraverseBiTreeRec(BiTree &T)
{
if(T==NULL)
{
return OK;
}
InOrderTraverseBiTreeRec(T->lChild);
VisitBiTreeElement(T->data);
InOrderTraverseBiTreeRec(T->rChild);
return OK;
}
// 后序递归遍历二叉树
Status PostOrderTraverseBiTreeRec(BiTree &T)
{
if(T==NULL)
{
return OK;
}
PostOrderTraverseBiTreeRec(T->lChild);
PostOrderTraverseBiTreeRec(T->rChild);
VisitBiTreeElement(T->data);
return OK;
}
// 先序非递归遍历二叉树
Status PreOrderTraverseBiTreeUnrec(BiTree &T, Status (* Visit)(BiTreeElemType &))
{
SqStack S;
InitStack(S);
BiTree p;
p=T;
while(p!=NULL || (FALSE==StackEmpty(S)))
{
while(p!=NULL)
{
if (Visit(p->data) !=OK) return ERROR;
Push(S, p);
p=p->lChild;
}
if(FALSE == StackEmpty(S))
{
Pop(S, p);
p=p->rChild;
}
}
DestroyStack(S);
return OK;
}
// 中序非递归遍历二叉树
Status InOrderTraverseBiTreeUnrec(BiTree &T)
{
SqStack S;
InitStack(S);
BiTree p;
Push(S, T);
while(FALSE == StackEmpty(S))
{
Pop(S, p);
while(p!=NULL)
{
Push(S, p);
p=p->lChild;
}
if(FALSE == StackEmpty(S))
{
Pop(S, p);
VisitBiTreeElement(p->data);
Push(S, p->rChild);
}
}
DestroyStack(S);
return OK;
}
// 后序非递归遍历二叉树
Status PostOrderTraverseBiTreeUnrec(BiTree &T)
{
SqStack S;
InitStack(S);
BiTree p, q;
Push(S, T);
while(FALSE == StackEmpty(S))
{
Pop(S, p);
while(p!=NULL)
{
Push(S, p);
p=p->lChild;
}
q = NULL;
while(FALSE == StackEmpty(S))
{
Top(S, p);
if(p->rChild == q)
{
VisitBiTreeElement(p->data);
q=p;
Pop(S, p);
}
else
{
Push(S, p->rChild);
break;
}
}
}
DestroyStack(S);
return OK;
}
// 先序非递归遍历二叉树2
Status PreOrderTraverseBiTreeUnrec2(BiTree &T)
{
SqStack S;
InitStack(S);
BiTree p;
p=T;
while(1)
{
if(p!=NULL)
{
cout<<p->data<<" ";
if(p->rChild != NULL)
{
Push(S, p->rChild);
}
p=p->lChild;
}
else
{
if(FALSE == StackEmpty(S))
{
Pop(S, p);
}
else
{
break;
}
}
}
DestroyStack(S);
return OK;
}
// 层次序非递归遍历二叉树
Status LevelOrderTraverseBiTreeUnrec(BiTree &T, Status (* Visit)(BiTreeElemType &))
{
BiTree p;
SqQueue Q;
InitQueue(Q);
EnQueue(Q, T);
while(FALSE == QueueEmpty(Q))
{
DeQueue(Q, p);
if (Visit(p->data) != OK) return ERROR;
if(p->lChild != NULL)
{
EnQueue(Q, p->lChild);
}
if(p->rChild != NULL)
{
EnQueue(Q, p->rChild);
}
}
DestroyQueue(Q);
return OK;
}
// 后序递归删除二叉树节点
Status DestroyBiTreeRecInPostOrder(BiTree &T)
{
if(T==NULL)
{
return OK;
}
DestroyBiTreeRecInPostOrder(T->lChild);
DestroyBiTreeRecInPostOrder(T->rChild);
delete T;
return OK;
}
// 线索二叉树操作
// 先序非递归从数组创建线索二叉树
Status CreateBiThreadTreeFromArray(BiTreeElemType a[], int nFrom, int nTo, BiThreadTree &T)
{
// 数据从下标nFrom开始,nTo结束
// 数据占据数组单元个数
int nElementCount = nTo - nFrom + 1;
BiThreadTree upper=NULL, current, below=NULL;
int n=1;
while (n>0)
{
while (n<=nElementCount && a[n+nFrom-1]>0)
{
current=(BiThreadTree)new BiThreadTreeNode;
if (current == NULL)
{
cout<<"CreateBiTreeFromArray()函数出错--生成节点空间出错!\n";
return NEW_ERROR;
}
current->data = a[n + nFrom -1];
current->rChild = current;
current->lChild = upper;
upper = current;
n*=2;
}
n/=2;
below=NULL;
// 沿右链返回
while(current!=NULL && current->rChild!=current)
{
upper = current->rChild;
current->rChild = below;
below = current;
current = upper;
n/=2;
}
// 返回到根部,二叉树创建完成
if(current==NULL)
{
break;
}
// 初始化生成右子树
current->rChild = current->lChild;
current->lChild = below;
upper = current;
n = n*2 + 1;
}
T = below;
return OK;
}
// 中序递归线索化二叉树
Status InOrderThreadRec(BiThreadTree &T)
{
static BiThreadTree pre=NULL;
if(T==NULL)
{
return OK;
}
InOrderThreadRec(T->lChild);
if(pre!=NULL)
{
if(pre->rChild!=NULL)
{
pre->rTag = Link;
}
else
{
pre->rChild = T;
pre->rTag = Thread;
}
}
if(T->lChild!=NULL)
{
T->lTag=Link;
}
else
{
if(pre!=NULL)
{
T->lChild = pre;
T->lTag = Thread;
}
else
{
T->lTag=Link;
}
}
pre = T;
InOrderThreadRec(T->rChild);
return OK;
}
// 访问线索二叉树节点元素函数
Status VisitBiThreadTreeElement(BiThreadTreeElemType &e)
{
cout<<e<<" ";
return OK;
}
// 访问线索二叉树节点元素函数
Status VisitBiThreadTreeElementInPostOrderUnrec(BiThreadTreeElemType &e)
{
static BiThreadTreeElemType data[20];
static int nCount=0;
if (e > 0)
{
data[nCount++] = e;
}
// 倒序输出所有元素
else
{
for (int i=nCount-1; i>=0; i--)
{
cout<<data[i]<<" ";
}
}
return OK;
}
// 后序递归遍历线索二叉树
Status PostOrderTraverseBiThreadTreeRec(BiThreadTree &T, Status (* Visit)(BiThreadTreeElemType &))
{
if(T==NULL)
{
return OK;
}
PostOrderTraverseBiThreadTreeRec(T->lChild, Visit);
PostOrderTraverseBiThreadTreeRec(T->rChild, Visit);
if (Visit(T->data) != OK) return ERROR;
return OK;
}
// 使用线索非递归先序遍历线索二叉树
Status PreOrderTraverseBiThreadTreeByThreadUnrec(BiThreadTree &T, Status (* Visit)(BiThreadTreeElemType &))
{
BiThreadTree p;
p=T;
while (p != NULL)
{
if (Visit(p->data) != OK) return ERROR;
while(p->lChild!=NULL && p->lTag==Link)
{
p=p->lChild;
if (Visit(p->data) != OK) return ERROR;
}
while(p->rTag == Thread)
{
p=p->rChild;
}
p=p->rChild;
}
return OK;
}
// 使用线索非递归中序遍历线索二叉树
Status InOrderTraverseBiThreadTreeByThreadUnrec(BiThreadTree &T, Status (* Visit)(BiThreadTreeElemType &))
{
BiThreadTree p;
p=T;
while(p != NULL)
{
while(p->lChild!=NULL && p->lTag==Link)
{
p=p->lChild;
}
if (Visit(p->data) != OK) return ERROR;
while(p->rTag == Thread)
{
p=p->rChild;
if (Visit(p->data) != OK) return ERROR;
}
p=p->rChild;
}
return OK;
}
// 使用线索非递归后序遍历线索二叉树
Status PostOrderTraverseBiThreadTreeByThreadUnrec(BiThreadTree &T, Status (* Visit)(BiThreadTreeElemType &))
{
BiThreadTree p;
p=T;
while (p != NULL)
{
if (Visit(p->data) != OK) return ERROR;
while(p->rChild!=NULL && p->rTag==Link)
{
p=p->rChild;
if (Visit(p->data) != OK) return ERROR;
}
while(p->lTag == Thread)
{
p=p->lChild;
}
p=p->lChild;
}
// 输出所有元素
BiThreadTreeElemType e = -1;
if (Visit(e) != OK) return ERROR;
return OK;
}
// 后序递归删除线索二叉树节点 谨慎使用
Status DestroyBiThreadTreeRecInPostOrder(BiThreadTree &T)
{
if(T==NULL)
{
return OK;
}
else
{
DestroyBiThreadTreeRecInPostOrder(T->lChild);
DestroyBiThreadTreeRecInPostOrder(T->rChild);
delete T;
return OK;
}
return FALSE;
}
int main()
{
//1 2 3 -1 -1 -1 4 -1 -1
BiTree T;
// 先序递归创建二叉树
cout<<"输入先序创建二叉树序列:\n";
CreateBiTree(T);
cout<<"先序递归创建二叉树序列完成\n";
cout<<"先序递归遍历二叉树:\n";
PreOrderTraverseBiTreeRec(T, VisitBiTreeElement);
cout<<endl;
cout<<"中序递归遍历二叉树:\n";
InOrderTraverseBiTreeRec(T);
cout<<endl;
cout<<"后序递归遍历二叉树:\n";
PostOrderTraverseBiTreeRec(T);
cout<<endl;
cout<<"先序非递归遍历二叉树:\n";
PreOrderTraverseBiTreeUnrec(T, VisitBiTreeElement);
cout<<endl;
cout<<"中序非递归遍历二叉树:\n";
InOrderTraverseBiTreeUnrec(T);
cout<<endl;
cout<<"后序非递归遍历二叉树:\n";
PostOrderTraverseBiTreeUnrec(T);
cout<<endl;
cout<<"层次序非递归遍历二叉树:\n";
LevelOrderTraverseBiTreeUnrec(T, VisitBiTreeElement);
cout<<endl<<endl;
DestroyBiTreeRecInPostOrder(T);
BiTreeElemType a[]={1, 2, 3, 0, 4, 5};
BiTree T2;
cout<<"由二叉树顺序数组表示创建二叉树 ... ";
CreateBiTreeFromArray(a, 0, 5, T2);
cout<<"完成"<<endl;
cout<<"先序递归遍历二叉树:\n";
PreOrderTraverseBiTreeRec(T2, VisitBiTreeElement);
cout<<endl;
cout<<"中序递归遍历二叉树:\n";
InOrderTraverseBiTreeRec(T2);
cout<<endl;
cout<<"后序递归遍历二叉树:\n";
PostOrderTraverseBiTreeRec(T2);
cout<<endl;
cout<<"层次序非递归遍历二叉树:\n";
LevelOrderTraverseBiTreeUnrec(T2, VisitBiTreeElement);
cout<<endl<<endl;
DestroyBiTreeRecInPostOrder(T2);
// 存储先序遍历和后续遍历数组
BiTreeElemType a2[]={1, 2, 4, 3, 5, 2, 4, 1, 3, 5};
cout<<"二叉树先序和后序遍历数组序列:\n";
for (int i=0; i<10; i++)
{
VisitBiTreeElement(a2[i]);
}
cout<<endl;
BiTree T3;
cout<<"由二叉树先序和后序递归创建二叉树 ... ";
CreateBiTreeFromPreAndInOrderSquences(a2, 0, 4, 5, 9, T3);
cout<<"完成"<<endl;
cout<<"后序递归遍历二叉树:\n";
PostOrderTraverseBiTreeRec(T3);
cout<<endl<<endl;
DestroyBiTreeRecInPostOrder(T3);
BiThreadTreeElemType at[]={1, 2, 3, 0, 4, 5};
BiThreadTree TT;
cout<<"创建线索二叉树 ... ";
CreateBiThreadTreeFromArray(at, 0, 5, TT);
cout<<"完成"<<endl;
cout<<"后序递归遍历线索二叉树:";
PostOrderTraverseBiThreadTreeRec(TT, VisitBiThreadTreeElement);
cout<<endl;
InOrderThreadRec(TT);
cout<<"使用线索非递归先序遍历线索二叉树:\n";
PreOrderTraverseBiThreadTreeByThreadUnrec(TT, VisitBiThreadTreeElement);
cout<<endl;
cout<<"使用线索非递归中序遍历线索二叉树:\n";
InOrderTraverseBiThreadTreeByThreadUnrec(TT, VisitBiThreadTreeElement);
cout<<endl;
cout<<"使用线索非递归后序遍历线索二叉树:\n";
PostOrderTraverseBiThreadTreeByThreadUnrec(TT,VisitBiThreadTreeElementInPostOrderUnrec);
cout<<endl<<endl;
DestroyBiThreadTreeRecInPostOrder(TT);
return 0;
}