二叉树2:层次遍历方式及先序、中序、后序(递归与非递归)遍历方式
来源:互联网 发布:淘宝如何购买流量 编辑:程序博客网 时间:2024/05/19 14:17
#include<stdio.h>#include<malloc.h>#include<math.h>#define MaxSize 100typedef char ElemType;typedef struct node{ ElemType data; struct node *lchild; struct node *rchild;}BTNode;// Create BinaryTree by Binary-linked-list BTNode *CreatBTNode(char *str){ BTNode *St[MaxSize],*p=NULL,*b; int top=-1,k,j=0; char ch; b=NULL; ch=str[j]; while (ch!='\0') { switch(ch) { case '(': top++; St[top]=p; k=1; break; case ')': top--; break; case',': k=2; break; default: p=(BTNode*)malloc(sizeof(BTNode)); p->data=ch; p->lchild=p->rchild=NULL; if(b==NULL) b=p; else { switch(k) { case 1: St[top]->lchild=p; break; case 2: St[top]->rchild=p; break; } } } j++; ch=str[j]; } return b;}// output Binary Tree by Tree modelvoid DispBTNode(BTNode *b){ BTNode *St[MaxSize],*p; int K[MaxSize]; int i,c,top=-1; int a=-1; int n=3; p=b; do { while(p!=NULL) { St[++top]=p; if(p!=b) ++n; K[++a]=n; p=p->rchild; } p=St[top--]; c=K[a--]; n=c; for(i=0;i<=c;i++) printf(" "); printf("----%c\n",p->data); p=p->lchild; } while (top>-1 || p!=NULL);}// traversal based on levelvoid TravLevel(BTNode *b){ BTNode *Qu[MaxSize]; // Circular-queue int front,rear; front=rear=0; if(b!=NULL) printf("%c",b->data); rear++; Qu[rear]=b; while(rear!=front) { front=(front+1)%MaxSize; b=Qu[front]; if(b->lchild!=NULL) { printf("%c",b->lchild->data); rear=(rear+1)%MaxSize; Qu[rear]=b->lchild; } if(b->rchild!=NULL) { printf("%c",b->rchild->data); rear=(rear+1)%MaxSize; Qu[rear]=b->rchild; } } printf("\n");} // traversal based on PreOrder // :recursive void PreOrder(BTNode *b){ if(b!=NULL) { printf("%c",b->data); PreOrder(b->lchild); PreOrder(b->rchild); }} // not recursive void PreOrder2(BTNode *b) { BTNode *St[MaxSize],*p; int top=-1; if(b!=NULL) { top++; St[top]=b; while(top>-1) { p=St[top]; top--; printf("%c",p->data); if(p->rchild!=NULL) { ++top; St[top]=p->rchild; } if(p->lchild!=NULL) { ++top; St[top]=p->lchild; } } } printf("\n"); } // In-Order traversal // :recursive void InOrder(BTNode *b) { if(b!=NULL) { InOrder(b->rchild); printf("%c",b->data); InOrder(b->lchild); } } // not recursive void InOrder2(BTNode *b) { BTNode *St[MaxSize],*p; int top=-1; if(b!=NULL) { p=b; while(top>-1||p!=NULL) { while(p!=NULL) { top++; St[top]=p; p=p->lchild; } if(top>-1) { p=St[top]; top--; printf("%c",p->data); p=p->rchild; } } } printf("\n"); } // traversal based on PostOrder // :recursive void PostOrder(BTNode *b) { if(b!=NULL) { PostOrder(b->lchild); PostOrder(b->rchild); printf("%c",b->data); } } // not recursive :used tags void PostOrder1(BTNode *b) { BTNode *St[MaxSize]; BTNode *p; int flag,top=-1; if(b!=NULL) { do { while(b!=NULL) { top++; St[top]=b; b=b->lchild; } p=NULL; flag=1; while(top!=-1 && flag) { b=St[top]; if(b->rchild==p) { printf("%c",b->data); top--; p=b; } else // return above to traversal left subtree of b { b=b->rchild; flag=0; } } }while(top!=-1); printf("\n"); } } // using the struct-stack to accomplish Post-Traversal void PostOrder2(BTNode *b) { BTNode *p; struct { BTNode *q; int tag; }St[MaxSize]; int top=0; p=b; do { while(p!=NULL) { top++; St[top].q=p; St[top].tag=0; p=p->lchild; } if(top>0) { p=St[top].q; if(St[top].tag==1) { printf("%c",p->data); top--; p=NULL; } else { St[top].tag=1; p=p->rchild; } } } while (p!=NULL||top!=0); } void main() { BTNode *b; b=CreatBTNode("A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); printf("Binary Tree:b"); DispBTNode(b); printf("\n\n"); printf("traversal based on level:"); TravLevel(b); printf("\n"); printf("traversal based on PreOrder:\n"); printf("recursive algorithm:"); PreOrder(b); printf("\n"); printf("not recursive algorithm:"); PreOrder2(b); printf("\n"); printf("traversal based on InOrder:\n"); printf("recursive algorithm:"); InOrder(b); printf("\n"); printf("not recursive:"); InOrder2(b); printf("\n"); printf("traversal based on PostOrder:"); printf("recursive algorithm:"); PostOrder(b); printf("\n"); printf("not recursive algorithm 1:"); PostOrder1(b); printf("not recursive algorithm 2:"); PostOrder2(b); printf("\n"); }
- 二叉树2:层次遍历方式及先序、中序、后序(递归与非递归)遍历方式
- 二叉树 非递归 先序遍历 中序遍历 后序遍历 层次遍历
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- 二叉树的先序遍历、中序遍历以及后序遍历(递归以及非递归方式)
- 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度
- 二叉树的先序、中序和后序遍历,递归与非递归方式实现。
- 二叉树(一) 先序遍历、中序遍历、后续遍历、层次遍历的递归与非递归实现
- 二叉树的递归 非递归 先序 中序 后序及层次遍历代码实现
- 二叉树的先序、中序、后序的递归及非递归实现,以及层次遍历的实现:
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
- 二叉树的先序,中序,层次遍历,递归与非递归实现
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- 分别用递归和非递归方式实现二叉树的先序、中序和后序遍历
- 递归和非递归的方式实现二叉树的先序、中序和后序遍历
- 二叉树非递归遍历之层次遍历,先序、中序遍历
- 二叉树的先序、中序、后序、层序递归及非递归遍历
- 二叉树的遍历(2)--先序遍历,中序遍历,后序遍历(非递归)
- java版的二叉树的先序遍历、中序遍历以及后序遍历(递归以及非递归方式)
- 今年暑假不AC+贪心+排序
- 通过驱动名称获取驱动路径加载驱动
- struts2 iterator标签status属性
- USACO:Agri-Net
- VC实现发送微博消息
- 二叉树2:层次遍历方式及先序、中序、后序(递归与非递归)遍历方式
- debian包管理命令大全
- data structures and application 5.3.2完整版 ( 栈的实现 )
- GDAL读写矢量文件——Java
- 获取servlet的方法
- SAP企业移动平台SAP Mobile Platform(SMP) 2.3 新特性
- Debian安装KDE图形界面
- 构造函数和构造代码块
- NEFU 654 Magic Tree(DP)