#ifndef HEAD_H__ #define HEAD_H__ #include<stdlib.h> #include<stdio.h> #include<string.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -2 typedef int status; typedef int boolean; //元素定义合集// typedef int ElementType; //typedef int SElementType;//调用conversion()函数的时候使用此类型 //typedef int QElementType; //二叉树数据结构 typedef int TElementType; typedef struct biTreeNode{ TElementType data; struct biTreeNode *lChild; struct biTreeNode *rChild; }biTreeNode,*biTree; typedef biTree QElementType; typedef biTree SElementType; /*/定义迷宫的结构体// typedef struct{ int x; int y; }PositionType; typedef struct{ int ord; PositionType seat; int di; }SElementType; ///*/ //常用函数合集 #define MAX 100 //void view(ElementType *); //void view(SElementType *); //void view(QElementType *); status visit(TElementType e); status compare(ElementType ,ElementType); void buildSet(ElementType a[],int length,int beg,int end); void printSet(ElementType a[],int length); void swap(ElementType *e1,ElementType *e2); int maxInt(int m,int n); //match.c的函数集合,用于模式匹配/// int index(char *S,char *T,int pos); int indexKMP(char *S,char *T,int pos); //快速排序 void quickSort(ElementType a[],int p,int r); void randomizedQuickSort(ElementType a[],int p,int r); //使用栈实现的数制转换/ void conversion(SElementType N,int d); #endif #ifndef QUEUE_H__ #define QUEUE_H__ #include"head.h" typedef struct QueueNode{ QElementType elem; struct QueueNode *next; }QueueNode,*QueuePtr; typedef struct{ QueuePtr front; QueuePtr rear; }linkQueue; status initQueue(linkQueue *Q); status destroyQueue(linkQueue *Q); status clearQueue(linkQueue *Q); boolean queueEmpty(linkQueue Q); int queueLength(linkQueue Q); status getHead(linkQueue Q,QElementType *e); status enQueue(linkQueue *Q,QElementType e); status deQueue(linkQueue *Q,QElementType *e); void queueTraverse(linkQueue Q,void (*view)(QElementType *)); #endif #ifndef SQSTACK_H__ #define SQSTACK_H__ #include"head.h" #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 struct stack{ SElementType *base; SElementType *top; int stackSize; }; typedef struct stack sqStack; status initStack(sqStack *S); status destroyStack(sqStack *S); status clearStack(sqStack *S); int stackLength(sqStack S); boolean stackEmpty(sqStack S); boolean push(sqStack *S,SElementType e); boolean pop(sqStack *S,SElementType *e); status getTop(sqStack S,SElementType *e); void stackTraverse(sqStack S,void (*view)(SElementType*)); #endif #ifndef BITREE_H__ #define BITREE_H__ #include"head.h" status initBiTree(biTree *T); void destroyBiTree(biTree *T); #define clearBiTree destroyBiTree void createBiTree(biTree *T); boolean biTreeEmpty(biTree T); int depth(biTree T); TElementType root(biTree T); TElementType value(biTree p); void assign(biTree p,TElementType value); #include"Queue.h" TElementType parent(biTree T,TElementType e); biTree pointer(biTree T,TElementType e); TElementType leftChild(biTree T,TElementType e); TElementType rightChild(biTree T,TElementType e); TElementType leftSibling(biTree T,TElementType e); TElementType rightSibling(biTree T,TElementType e); status insertChild(biTree p,int LR,biTree c); status deleteChild(biTree p,int LR); void preOrderTraverse(biTree T,status (*visit)(TElementType)); void inOrderTraverse(biTree T,status (*visit)(TElementType)); void postOrderTraverse(biTree T,status (*visit)(TElementType)); void levelOrderTraverse(biTree T,status (*visit)(TElementType)); #include"sqStack.h" status inOrderTraverse1(biTree T,status (*visit)(TElementType)); status inOrderTraverse2(biTree T,status (*visit)(TElementType)); #endif #include"head.h" #include<stdio.h> #include<stdlib.h> #include<time.h> void view(QElementType *e) { printf("%d\t",*e); } status visit(TElementType e) { printf("%d\t",e); return OK; } status compare(ElementType e1,ElementType e2) { if(e1==e2) return TRUE; else return FALSE; } void buildSet(ElementType a[],int length,int beg,int end) { int i; srand((unsigned)time(NULL)); for(i=0;i<length;i++) a[i]=rand()%(end-beg+1)+beg; } void printSet(ElementType a[],int length) { int i,count=1; for(i=0;i<length;i++,count++) { printf("%d\t",a[i]); if(count%10==0) printf("\n"); } } void swap(ElementType *e1,ElementType *e2) { ElementType tmp; tmp=*e1; *e1=*e2; *e2=tmp; } int maxInt(int m,int n) { return m>n?m:n; } #include"Queue.h" status initQueue(linkQueue *Q) { Q->front=(QueuePtr)malloc(sizeof(QueueNode)); if(!Q->front) exit(1); Q->rear=Q->front; Q->front->next=NULL; return OK; } status destroyQueue(linkQueue *Q)//很巧,,可以多看看 { while(Q->front) { Q->rear=Q->front->next; free(Q->front); Q->front=Q->rear; } return OK; } status clearQueue(linkQueue *Q) { while(Q->front!=Q->rear) { QueuePtr p; p=Q->front->next; free(Q->front); Q->front=p; } return OK; } boolean queueEmpty(linkQueue Q) { return (Q.front==Q.rear)?TRUE:FALSE; } int queueLength(linkQueue Q) { QueuePtr p; int i=0; p=Q.front; while(p!=Q.rear) { i++; p=p->next; } return i; } status getHead(linkQueue Q,QElementType *e) { if(queueEmpty(Q)) exit(1); *e=Q.front->next->elem; return OK; } status enQueue(linkQueue *Q,QElementType e) { QueuePtr p; p=(QueuePtr)malloc(sizeof(QueueNode)); if(!p) exit(1); p->elem=e; p->next=NULL; Q->rear->next=p; Q->rear=p; return OK; } status deQueue(linkQueue *Q,QElementType *e) { QueuePtr p; if(queueEmpty(*Q)) return ERROR; p=Q->front->next; *e=p->elem; Q->front->next=p->next; if(Q->rear==p)//如果仅有一个元素的时候,该情形发生,鲁棒性不够好 Q->rear=Q->front; free(p); return OK; } void queueTraverse(linkQueue Q,void (*view)(QElementType *)) { QueuePtr p; p=Q.front->next; while(p) { view(&p->elem); p=p->next; } } #include"sqStack.h" status initStack(sqStack *S) { S->base=(SElementType*)malloc(STACK_INIT_SIZE*sizeof(SElementType)); if(!S->base) exit(OVERFLOW); S->top=S->base; S->stackSize=STACK_INIT_SIZE; return OK; } status destroyStack(sqStack *S) { free(S->base); S->base=NULL; S->top=NULL; S->stackSize=0; return OK; } status clearStack(sqStack *S) { S->top=S->base; return OK; } int stackLength(sqStack S) { return S.top-S.base;//这个速度是O(1); /* SElementType *p; int i=0; p=S.base; for(p;p!=S.top;++p) ++i; return i; */ } boolean stackEmpty(sqStack S) { return (S.top==S.base)?1:0; } boolean push(sqStack *S,SElementType e) { if(S->top-S->base>=S->stackSize)//此处注意stackSize有可能扩展过 { S->base=(SElementType *)realloc(S->base,(S->stackSize+STACKINCREMENT)*sizeof(SElementType)); if(!S->base) exit(OVERFLOW); S->top=S->base+S->stackSize; S->stackSize+=STACKINCREMENT; } *S->top++=e; return OK; } boolean pop(sqStack *S,SElementType *e) { if(S->top==S->base) return ERROR; *e=*--S->top; return OK; } status getTop(sqStack S,SElementType *e) { if(S.base==S.top) return ERROR; *e=*(S.top-1);//此处并不是要弹出元素,只是获得元素故不能使用*--S.top。 return OK; } void StackTraverse(sqStack S,void (*view)(SElementType*)) { SElementType *p; p=S.base; while(p!=S.top) view(p++); } #include"biTree.h" #define INF 0 //以0作为空元素,也可以根据实际需要定义 TElementType NIL=0; status initBiTree(biTree *T) { *T=NULL;//构造一个空二叉树,T就是树的根,只是没有指定数据和孩子 return OK; } void destroyBiTree(biTree *T) { if(*T) { if((*T)->lChild) destroyBiTree(&(*T)->lChild); if((*T)->rChild) destroyBiTree(&(*T)->rChild); free(*T); *T=NULL; } } void createBiTree(biTree *T)//需要进一步改进 { TElementType element; scanf("%d",&element);//此处如果是使用复杂结构体的话,可以修改成一个getElement()函数 if(element==NIL)//通过输入一个无穷的数来创建一个空树,也是为了控制创建树的退出方式 *T=NULL; else { *T=(biTree)malloc(sizeof(biTreeNode)); if(!*T) exit(OVERFLOW); (*T)->data=element; createBiTree(&(*T)->lChild); createBiTree(&(*T)->rChild); } } boolean biTreeEmpty(biTree T) { return T==NULL?TRUE:FALSE; } int depth(biTree T)//二叉树的深度,递归实现 { int l,r; if(!T) return 0; if(T->lChild) l=depth(T->lChild); else l=0; if(T->rChild) r=depth(T->rChild); else r=0; return l>r?l+1:r+1; } TElementType root(biTree T) { if(biTreeEmpty(T)) return NIL; else return T->data; } TElementType value(biTree p) { return p->data; } void assign(biTree p,TElementType value) { p->data=value; } //这个是递归版本的,还未测试 /*TElementType parent(biTree T,TElementType e) { biTree p; if(T) { p=T; if(p->lChild && p->lChild->data==e || p->rChild && p->rChild->data==e) return p->data; else { parent(p->lChild,e); parent(p->rChild,e); } } return NULL; }*/ TElementType parent(biTree T,TElementType e)//使用队列来求parent { linkQueue Q; QElementType x; if(T) { initQueue(&Q); enQueue(&Q,T); while(!queueEmpty(Q)) { deQueue(&Q,&x); if(x->lChild && x->lChild->data==e || x->rChild && x->rChild->data==e) return x->data; else { if(x->lChild) enQueue(&Q,x->lChild); if(x->rChild) enQueue(&Q,x->rChild); } } } return NIL; } biTree pointer(biTree T,TElementType e) { linkQueue Q; QElementType x; if(T) { initQueue(&Q); enQueue(&Q,T); while(!queueEmpty(Q)) { deQueue(&Q,&x); if(x->data==e) return x; if(x->lChild) enQueue(&Q,x->lChild); if(x->rChild) enQueue(&Q,x->rChild); } } return NULL; } TElementType leftChild(biTree T,TElementType e)//使用了pointer函数 { biTree x; if(T) { x=pointer(T,e); if(x && x->lChild) return x->lChild->data; } return NIL; } TElementType rightChild(biTree T,TElementType e) { biTree x; if(T) { x=pointer(T,e); if(x && x->rChild) return x->rChild->data; } return NIL; } TElementType leftSibling(biTree T,TElementType e) { biTree x; TElementType e1; if(T) { e1=parent(T,e); x=pointer(T,e1); if(x->lChild && x->rChild && x->rChild->data==e) return x->lChild->data; } return NIL; } TElementType rightSibling(biTree T,TElementType e) { biTree x; TElementType e1; if(T) { e1=parent(T,e); x=pointer(T,e1); if(x->lChild && x->rChild && x->lChild->data==e) return x->rChild->data; } return NIL; } status insertChild(biTree p,int LR,biTree c) { if(p) { if(LR==0) { c->rChild=p->lChild; p->lChild=c; } else { c->rChild=p->rChild; p->rChild=c; } return OK; } return ERROR; } status deleteChild(biTree p,int LR) { if(p) { if(LR==0) clearBiTree(&p->lChild); else clearBiTree(&p->rChild); return OK; } return ERROR; } void preOrderTraverse(biTree T,status (*visit)(TElementType)) { if(T) { visit(T->data); preOrderTraverse(T->lChild,visit); preOrderTraverse(T->rChild,visit); } } void inOrderTraverse(biTree T,status (*visit)(TElementType)) { if(T) { inOrderTraverse(T->lChild,visit); visit(T->data); inOrderTraverse(T->rChild,visit); } } void postOrderTraverse(biTree T,status (*visit)(TElementType)) { if(T) { postOrderTraverse(T->lChild,visit); postOrderTraverse(T->rChild,visit); visit(T->data); } } void levelOrderTraverse(biTree T,status (*visit)(TElementType)) { linkQueue Q; QElementType x; if(T) { initQueue(&Q); enQueue(&Q,T); while(!queueEmpty(Q)) { deQueue(&Q,&x); visit(x->data); if(x->lChild) enQueue(&Q,x->lChild); if(x->rChild) enQueue(&Q,x->rChild); } putchar('\n'); } }
status inOrderTraverse1(biTree T,status (*visit)(TElementType)) { sqStack S; biTree p; initStack(&S); push(&S,T); while(!stackEmpty(S)) { while(getTop(S,&p) && p) push(&S,p->lChild); pop(&S,&p); if(!stackEmpty(S)) { pop(&S,&p); if(!visit(p->data)) return ERROR; push(&S,p->rChild); } } return OK; } status inOrderTraverse2(biTree T,status (*visit)(TElementType)) { sqStack S; initStack(&S); while(T || !stackEmpty(S)) { if(T) { push(&S,T); T=T->lChild; } else { pop(&S,&T); if(!visit(T->data)) return ERROR; T=T->rChild; } } return OK; }
#include"head.h" #include"Queue.h" #include"sqStack.h" #include"biTree.h" /* status inOrderTraverse1(biTree T,status (*visit)(TElementType)); status inOrderTraverse2(biTree T,status (*visit)(TElementType)); */ int main() { biTree T,p,ptr,T1; int deep; TElementType r,e1,e2; initBiTree(&T); printf("the address of binary tree is :%d\n",T); createBiTree(&T);//先根遍历的测试用例:986215743 printf("traverse the binary tree\n"); preOrderTraverse(T,visit); putchar('\n'); if(!biTreeEmpty(T)) printf("the binary tree is not empty\n"); deep=depth(T); printf("the deep of the binary tree is :%d\n",deep); r=root(T); printf("the root is :%d\n",r); p=T->lChild->rChild;//左孩子的右孩子 printf("the value of the current pointer is :%d\n",value(p)); assign(p,10); printf("after assign 10 to the current pointer the value is :%d\n",value(p)); e1=parent(T,4); printf("element 4's parent is :%d\n",e1);//应该是7 ptr=pointer(T,6); printf("point to 6,and the data is :%d\n",ptr->data); e1=leftChild(T,8); e2=rightChild(T,8); printf("the 8's left child is :%d,and the right child is :%d\n",e1,e2); e1=rightSibling(T,8); printf("the 8's right sibling is :%d\n",e1); e2=leftSibling(T,7); printf("the 7's left sibling is :%d\n",e2); printf("create a subtree and insert\n"); createBiTree(&T1);//使用:12,11作为创建的子树 printf("visit the new subtree\n"); levelOrderTraverse(T1,visit); putchar('\n'); p=T->lChild; insertChild(p,1,T1); printf("after insert a subtree into the tree \n"); preOrderTraverse(T,visit); putchar('\n'); p=T->rChild; deleteChild(p,0); printf("after delete a subtree using level order traverse\n"); levelOrderTraverse(T,visit); putchar('\n'); printf("after delete a subtree using in order traverse 1\n"); inOrderTraverse1(T,visit); putchar('\n'); printf("after delete a subtree using in order traverse 2\n"); inOrderTraverse2(T,visit); putchar('\n'); destroyBiTree(&T); printf("after destroy the tree the adress is :%d\n",T); return 0; }
二叉树的二叉链表实现
最新推荐文章于 2021-10-03 23:55:15 发布