#include <stdio.h> #include <malloc.h> #define OK 1 #define ERROR -1 #define FAIL 0 #define TRUE 1 #define FALSE 0 #define NodeSize sizeof(struct BiThrNode) struct BiThrNode { int num; char LTag,RTag; //为1时表示左右孩子分别指向前驱,后继。 struct BiThrNode *lchild,*rchild; }; //以先序序列方式人工输入一棵二叉树,输入0表示某结点为空 int CreatBiTree(struct BiThrNode *(*T)) { int _data; scanf("%d",&_data); if(_data==0) *T=0; else { (*T)=(struct BiThrNode *)malloc(NodeSize); if(!(*T))return ERROR; (*T)->num=_data; (*T)->LTag=0; (*T)->RTag=0; CreatBiTree(&(*T)->lchild); CreatBiTree(&(*T)->rchild); } return OK; } /***********************************************************************/ //中序线索化函数的中序遍历线索化子函数 void InThreadingDeal(struct BiThrNode *T,struct BiThrNode *(*pre)) { if(T) { InThreadingDeal(T->lchild,&(*pre)); if(!T->lchild) { T->LTag=1; T->lchild=(*pre); } if(!(*pre)->rchild) { (*pre)->RTag=1;(*pre)->rchild=T; } (*pre)=T; InThreadingDeal(T->rchild,&(*pre)); } } //中序线索化函数 int InOrderThreading(struct BiThrNode *Thrt,struct BiThrNode *T) { struct BiThrNode *pre; Thrt->LTag=1; Thrt->RTag=1; Thrt->num=0; Thrt->rchild=Thrt; if(!T)Thrt->lchild=0; else { Thrt->lchild=T; pre=Thrt; InThreadingDeal(T,&pre); pre->rchild=Thrt; pre->RTag=1; Thrt->rchild=pre; } return OK; } /***********************************************************************/ int BiTreeEmpty(struct BiThrNode Thrt) { if(Thrt.lchild==0)return TRUE; else return FALSE; } /***********************************************************************/ int ClearBiTree(struct BiThrNode *Thrt) { if(BiTreeEmpty(*Thrt))return OK; else { Thrt->lchild=0; Thrt->LTag=Thrt->RTag=1; return OK; } } /***********************************************************************/ int DestroyBiTreeDeal(struct BiThrNode *T) { if(T) { if(T->LTag==0) DestroyBiTreeDeal(T->lchild); if(T->RTag==0) DestroyBiTreeDeal(T->rchild); free(T); } return OK; } int DestroyBiTree(struct BiThrNode *Thrt) { DestroyBiTreeDeal(Thrt->lchild); free(Thrt); return OK; } /***********************************************************************/ int Root(struct BiThrNode Thrt,struct BiThrNode *(*L)) { if(BiTreeEmpty(Thrt))return FALSE; else (*L)=Thrt.lchild; return OK; } /***********************************************************************/ //用先序遍历的方法找到数据为e的结点,并返回其指针 int PreOrderFind(struct BiThrNode *T,int e,struct BiThrNode *(*L)) { if(T) { if(T->num!=e) { if(T->LTag==0) if(PreOrderFind(T->lchild,e,&(*L))) return OK; if(T->RTag==0) if(PreOrderFind(T->rchild,e,&(*L))) return OK; return FAIL; } else { (*L)=T; return OK; } } return FAIL; } //用中序遍历的方法找到最后一个节点,并用*L返回指针值 int InOrderFindLast(struct BiThrNode *T,struct BiThrNode *(*L)) { if(T) { if(T->LTag==0) if(InOrderFindLast(T->lchild,&(*L))) return OK; (*L)=T; if(T->RTag==0) if(InOrderFindLast(T->rchild,&(*L))) return OK; return FAIL; } return FAIL; } //用中序遍历的方法找到第一个节点,并用*L返回其指针值 int InOrderFindFirst(struct BiThrNode *T,struct BiThrNode *(*L),int *n) { if(T) { if(T->LTag==0) if(InOrderFindFirst(T->lchild,&(*L),n)) return OK; (*L)=T; (*n)++; if((*n)==1)return OK; if(T->RTag==0) if(InOrderFindFirst(T->rchild,&(*L),n)) return OK; return FAIL; } return FAIL; } /***********************************************************************/ //找到数据值为e的结点,并改其数据值为value int Assign(struct BiThrNode *T,int e,int value) { struct BiThrNode *L; PreOrderFind(T,e,&L); L->num=value; } /***********************************************************************/ int ShowNum(int num) { printf("%d ",num); return OK; } //先序遍历函数 int PreOrderTraverse(struct BiThrNode *T,int (*visit)(int)) { if(T) { if((*visit)(T->num)) { if(T->LTag==0) PreOrderTraverse(T->lchild,visit); if(T->RTag==0) PreOrderTraverse(T->rchild,visit); return OK; } else if(!(*visit)(T->num)) return ERROR; } return OK; } //中序遍历函数 int InOrderTraverse(struct BiThrNode *T,int (*visit)(int)) { if(T) { if(T->LTag==0) InOrderTraverse(T->lchild,visit); if(!(*visit)(T->num)) return ERROR; if(T->RTag==0) InOrderTraverse(T->rchild,visit); return OK; } return OK; } //后序遍历函数 int PostOrderTraverse(struct BiThrNode *T,int (*visit)(int)) { if(T) { if(T->LTag==0) PostOrderTraverse(T->lchild,visit); if(T->RTag==0) PostOrderTraverse(T->rchild,visit); if(!(*visit)(T->num)) return ERROR; return OK; } return OK; } /***********************************************************************/ //得到中序序列中结点数据值为e的前驱,并返回其指针 int InOrderPrior(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { if(!PreOrderFind(Thrt.lchild,e,&(*L)))return FAIL; if((*L)->LTag)(*L)=(*L)->lchild; else InOrderFindLast((*L)->lchild,&(*L)); return OK; } //得到中序序列中结点数据值为e的后继,并返回其指针 int InOrderNext(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { int n=0; if(!PreOrderFind(Thrt.lchild,e,&(*L)))return FAIL; if((*L)->RTag)(*L)=(*L)->rchild; else InOrderFindFirst((*L)->rchild,&(*L),&n); return OK; } /***********************************************************************/ //求线索二叉树深度的处理子函数 int DepthDeal( struct BiThrNode *T) { int ldepth,rdepth; if( !T ) return 0; //结点为空 else { if(T->LTag==0 && T->RTag==0) //左右孩子都非空 { ldepth = DepthDeal( T->lchild ); rdepth = DepthDeal( T->rchild ); if( ldepth >= rdepth) return ldepth+1; else return rdepth+1; } else if(T->LTag==0 && T->RTag==1) //左孩子非空,右孩子为空 { ldepth = DepthDeal( T->lchild ); return ldepth+1; } else if(T->LTag==1 && T->RTag==0) //左孩子为空,右孩子非空 { rdepth = DepthDeal( T->rchild ); return rdepth+1; } else return 1; //左右孩子都为空 } } //求线索二叉树深度,并返回深度值 int BiTreeDepth (struct BiThrNode Thrt) { int Depth; Depth= DepthDeal(Thrt.lchild); return Depth; } /***********************************************************************/ //寻找数据值为e的结点的双亲结点的迭代处理函数 int ParentDeal(struct BiThrNode *T,int e,struct BiThrNode *(*L)) { if(T==0)return FAIL; else { if((T->LTag==1&&T->RTag==1)) return FAIL; else if(T->LTag==0&&T->RTag==0) { if(T->lchild->num==e||T->rchild->num==e) { (*L)=T; return OK; } if(ParentDeal(T->lchild,e,&(*L))) return OK; if(ParentDeal(T->rchild,e,&(*L))) return OK; } else if(T->LTag==0&&T->RTag==1) { if(T->lchild->num==e) { (*L)=T; return OK; } if(ParentDeal(T->lchild,e,&(*L))) return OK; } else if(T->LTag==1&&T->RTag==0) { if(T->rchild->num==e) { (*L)=T; return OK; } if(ParentDeal(T->rchild,e,&(*L)))return OK; } return FAIL; } } //找到线索二叉树中数据值为e的结点的的双亲结点,并返回其指针 int Parent(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { if(ParentDeal(Thrt.lchild,e,&(*L))) return OK; else return FAIL; } /***********************************************************************/ //找到数据值为e的结点的左兄弟,如果有的话,返回其指针,否则返回FAIL; int LeftSibling(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { if(ParentDeal(Thrt.lchild,e,&(*L))) { if((*L)->LTag==0&&(*L)->lchild->num!=e) { (*L)=(*L)->lchild; return OK; } return FAIL; } return FAIL; } //找到数据值为e的结点的右兄弟,如果有的话,返回其指针,否则返回FAIL; int RightSibling(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { if(ParentDeal(Thrt.lchild,e,&(*L))) { if((*L)->RTag==0&&(*L)->rchild->num!=e) { (*L)=(*L)->rchild; return OK; } return FAIL; } return FAIL; } /***********************************************************************/ //找到数据值为e的结点的左孩子,并返回其指针 int LeftChild(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { if(PreOrderFind(Thrt.lchild,e,&(*L))) { if((*L)->LTag==0) { (*L)=(*L)->lchild; return OK; } return FAIL; } return FAIL; } //找到数据值为e的结点的右孩子,并返回其指针 int RightChild(struct BiThrNode Thrt,int e,struct BiThrNode *(*L)) { if(PreOrderFind(Thrt.lchild,e,&(*L))) { if((*L)->RTag==0) { (*L)=(*L)->rchild; return OK; } return FAIL; } return FAIL; } /***********************************************************************/ int main() { struct BiThrNode *T,*L; //*T为根结点 struct BiThrNode Thrt; //Thrt为头结点 int _depth; printf("Please input the data of bitree:/n"); CreatBiTree(&T); //先序输入数值,创建二叉树,0代表空树 InOrderThreading(&Thrt,T); //中序线索化二叉树 PreOrderTraverse(Thrt.lchild,ShowNum); //打印先序序列 putchar('/n'); InOrderTraverse(Thrt.lchild,ShowNum); //打印中序序列 putchar('/n'); PostOrderTraverse(Thrt.lchild,ShowNum); //打印后序序列 putchar('/n'); _depth=BiTreeDepth(Thrt); printf("The depth of the bitree is: %d/n",_depth); //显示深度值 Root(Thrt,&L); printf("%d/n",L->num); //打印根的数据值 ClearBiTree(&Thrt); PreOrderTraverse(Thrt.lchild,ShowNum); //打印先序序列 putchar('/n'); }