二叉树的遍历(递归、非递归)

来源:互联网 发布:明华堂淘宝 编辑:程序博客网 时间:2024/05/21 17:12

常见二叉树的基本操作(C语言实现):

建立树、前序、中序、后序遍历的递归与非递归方式。

/*常见二叉树的基本操作*/#include<stdio.h>#include<stdlib.h>typedef struct BiTree{char data;struct BiTree *lchild,*rchild;}BiTree;typedef struct stack{BiTree **base;BiTree **top;int stacksize;}Stack;void InitStack(Stack *s){s->base = (BiTree **)malloc(20*sizeof(BiTree *));if(!s->base)exit(0);s->top = s->base;s->stacksize = 20;}void Push(Stack *s,BiTree *t){*s->top = t;s->top++;//putchar(t->data);}BiTree *Pop(Stack *s){BiTree *temp; if(s->top == s->base){printf("The stack is empty!\n");exit(0);}s->top--;temp = *(s->top);return temp;}BiTree *GetTop(Stack *s){BiTree *temp; if(s->top == s->base){printf("The stack is empty!\n");exit(0);}//s->top--;temp = *(s->top-1);//printf("%c",temp->data);return temp;}BiTree *CreateBiTree(BiTree *t){//按照先序次序输入二叉树中结点的值(一个字符),空格字符表示空树char ch;//t = (BiTree *)malloc(sizeof(BiTree));scanf("%c",&ch);//fflush(stdin);//getchar();if(ch == ' ') {t = NULL;}else{t = (BiTree *)malloc(sizeof(BiTree));t->data = ch;//printf("%c\n",t->data);t->lchild = CreateBiTree(t->lchild);t->rchild = CreateBiTree(t->rchild);}return t;}void InOrderTraverse2(BiTree *t){if(t != NULL){InOrderTraverse2(t->lchild);putchar(t->data);InOrderTraverse2(t->rchild);}}void PostOrderTraverse(BiTree *t){if(t){PostOrderTraverse(t->lchild);PostOrderTraverse(t->rchild);putchar(t->data);}}void InOrderTraverse(BiTree *t){//非递归方式一 :中序遍历,输出对应的节点Stack s;BiTree *p,*temp;InitStack(&s);Push(&s,t);p = GetTop(&s);printf("中序遍历:");while(s.top != s.base){while(p){Push(&s,p->lchild);p = GetTop(&s);}p = Pop(&s);if(s.top != s.base){p = Pop(&s);printf("%c",p->data);Push(&s,p->rchild);p = GetTop(&s);//按照书本上写的,这个地方不一样,漏掉了。}}printf("\n");}void InOrderTraverse3(BiTree *t){Stack s;BiTree *p;InitStack(&s);p = t;while(p || s.top != s.base){if(p){Push(&s,p);p = p->lchild;}else{p = Pop(&s);putchar(p->data);p = p->rchild;}}}int PreOrderTraverse(BiTree *t){//先序遍历二叉树,递归方式,输出对应结点的值//int i=1;if( t!= NULL){printf("%c",t->data);PreOrderTraverse(t->lchild);//printf("%d\n",t->data);PreOrderTraverse(t->rchild);}return 0;}void PreOrderTraverse1(BiTree *t){//先序遍历二叉树,非递归方式Stack s;InitStack(&s);printf("非递归先序遍历:");while(t || s.base != s.top){while(t){putchar(t->data);Push(&s,t);t = t->lchild;}t = Pop(&s);t = t->rchild;}}void InOrderTraverse4(BiTree *t){//中序遍历二叉树,非递归方式Stack s;InitStack(&s);printf("\n非递归重新写中序遍历:");while(t || s.base != s.top){while(t){Push(&s,t);t = t->lchild;}t = Pop(&s);putchar(t->data);t = t->rchild;}}void PostOrderTraverse2(BiTree *t){//后序遍历二叉树,非递归方式Stack s;int flag[10];InitStack(&s);printf("\n非递归后序遍历:");while(t){Push(&s,t);flag[s.top - s.base] = 0;t = t->lchild;}while(s.base != s.top){t = GetTop(&s);if(t->rchild && flag[s.top - s.base] == 0){t = t->rchild;flag[s.top - s.base] = 1;while(t){Push(&s,t);flag[s.top - s.base] = 0;t = t->lchild;}t = GetTop(&s);}//flag[s.top - s.base] = 0;t = Pop(&s);putchar(t->data);}}void main(){BiTree *t;//t = (BiTree *)malloc(sizeof(BiTree));t= CreateBiTree(t);printf("递归先序遍历:");PreOrderTraverse(t);printf("\n");PreOrderTraverse1(t);printf("\n");InOrderTraverse(t);printf("非递归中序遍历(一):");InOrderTraverse2(t);printf("\n非递归中序遍历(二):");InOrderTraverse3(t);InOrderTraverse4(t);printf("\n递归法 后序遍历:");PostOrderTraverse(t);printf("\n");PostOrderTraverse2(t);//printf("%d",t->data);printf("\n");}


0 0
原创粉丝点击