线索二叉树的基本操作

以下代码由C编写,VS调试:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
typedef char TElemType;
typedef enum{Link,Thread} PointerTag ;


//定义线索二叉树
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild; //左右孩子指针
PointerTag LTag,RTag;
}BiTNode,*BiTree;


//----------------------------二叉树所需栈操作--------------------------------
typedef BiTree SElemType;


//存放二叉树栈的定义
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;


//构造一个空栈S
Status InitStack(SqStack *S)
{
S->base =(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S->base ) exit(OVERFLOW); //存储分配失败
S->top =S->base ;
S->stacksize =STACK_INIT_SIZE;
return OK;
}//InitStack


//插入元素e为新的栈顶元素
Status Push(SqStack *S,SElemType e)
{
if(S->top-S->base>=S->stacksize  ) { //栈满,追加存储空间
S->base =(SElemType *)realloc (S->base ,(S->stacksize +STACKINCREMENT) * sizeof(SElemType));
if(!S->base ) exit(OVERFLOW);
S->top =S->base +S->stacksize ;
S->stacksize +=STACKINCREMENT;
}
*S->top ++=e;
return OK;
} //Push


//若栈不为空,则删除S的栈顶元素,用E返回其值,并返回OK;否则返回ERROR
Status Pop(SqStack *S,SElemType *e)
{
if(S->top==S->base ) return ERROR;
e=--S->top ;
return (*e);
} //Pop


//判断栈是否为空
Status StackEmpty(SqStack *S)
{
if(S->base==S->top) return TRUE;
else return FALSE;
} //StackEmpty


//---------------------------------------------------------------------------


//按先序次序输入二叉树中节点的值(一个字符),空格字符表示空树,构造二叉链表表示的二叉树T。
BiTree CreateBiTree(BiTree T)
{
char ch;
ch=getchar();
if(ch==' ')T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
T->data=ch; //生成根节点
T->lchild =CreateBiTree(T->lchild); //构造左子树
T->rchild =CreateBiTree(T->rchild);
}
return T;
} //CreateBiTree


//输入
Status PrintElement(TElemType e)
{
printf("%c",e);
return OK;
}


//采用二叉链表存储结构,先序遍历二叉树T的递归算法
Status PreOrderTraverse(BiTree T,Status(*Visit)(TElemType e))
{
if(T!=NULL){
if(Visit(T->data ))
if(PreOrderTraverse(T->lchild ,Visit))
if(PreOrderTraverse(T->rchild ,Visit)) return OK;
return ERROR;
}else return OK;
} //PreOrderTraverse


//采用二叉链表存储结构,中序遍历二叉树T的非递归算法
Status InOrderTraverse(BiTree T,Status(*Visit)(TElemType e))
{
BiTree p;SqStack S;
InitStack(&S);p=T;
while(p||!StackEmpty(&S)){
if(p)
{Push(&S,p);p=p->lchild;} //根指针进栈,遍历左子树
else{ //跟指针退栈,访问根结点,遍历,右子树
p=Pop(&S,p);
if(!Visit(p->data)) return ERROR;
p=p->rchild;
} //else
} //while
return OK;
} //InOrderTraverse


//统计二叉树中,叶子结点的个数
void CountLeaf(BiTree T,int *count)
{
if(T){
if((!T->lchild)&&(!T->rchild))
(*count)++;
CountLeaf(T->lchild ,count);
CountLeaf(T->rchild ,count);
}
} //CountLeaf


//求二叉树的深度
Status Depth(BiTree T)
{
int depthval,depthLeft,depthRight;
if(!T)
depthval=0;
else{depthLeft=Depth(T->lchild );
depthRight=Depth(T->rchild );
depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
}
return depthval;
}


//线索化
void InThreading(BiTree p,BiTree *pre)
{
if(p){
InThreading(p->lchild,pre);  //左子树线索化
if(!p->lchild){p->LTag=Thread;p->lchild =(*pre);} //前驱线索
if(!(*pre)->rchild){(*pre)->RTag=Thread;(*pre)->rchild=p;}//后继线索
(*pre)=p; //保持pre指向p的前驱
InThreading(p->rchild,pre);//右子树线索化
}
}


//中序遍历二叉树T,并将其中序列线索化,Thrt指向头指针
BiTree InOrderThreading(BiTree Thrt,BiTree T)
{
BiTree pre;
if(!(Thrt=(BiTree)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
Thrt->LTag =Link;Thrt->RTag =Thread; //建头结点
Thrt->rchild =Thrt;                  //右指针回针
if(!T)Thrt->lchild =Thrt;            //若二叉树空,则左指针回针
else{
Thrt->lchild =T;pre=Thrt;
InThreading(T,&pre);                  //中序遍历进行中序线索化
pre->rchild=Thrt;pre->RTag=Thread;    //最后一个结点线索化
Thrt->rchild =pre;
}
return Thrt;
} //InOrderThreading


//T指向头结点,中序遍历二叉线索树的非递归算法
Status InOrderTraverse_Thr(BiTree T,Status(*Visit)(TElemType e)){
BiTree p;
p=T->lchild ;
while(p!=T){   //空树或遍历结束时P==T
while(p->LTag !=Thread) p=p->lchild ;
if(!Visit(p->data ))return ERROR;//访问其左子树为空的结点
while(p->RTag ==Thread&&p->rchild !=T){
p=p->rchild ;Visit(p->data );
}
p=p->rchild ;
}
}


void main(){
BiTNode T;
BiTree T1,T2;
int depthval,count=0;
printf("先序序列建立二叉树\n请输入:");
T1=CreateBiTree(&T);
printf("先序遍历二叉树:\n");
PreOrderTraverse(T1,PrintElement);
printf("\n中序遍历二叉树:\n");
InOrderTraverse(T1,PrintElement);
printf("\n二叉树的叶子结点的个数:\n");
    CountLeaf(T1,&count);
printf("%d",count);
depthval=Depth(T1);
printf("\n二叉树的深度:\n");
printf("%d",depthval);
printf("\n二叉树线索化后线索树查找:\n");
T2=InOrderThreading(&T,T1);
InOrderTraverse_Thr(T2,PrintElement);
system("pause");
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值