二叉树创建以及遍历
来源:互联网 发布:阿迪达斯淘宝旗舰店 编辑:程序博客网 时间:2024/05/22 16:59
char *ps = “ABCDEFGH”;
char *is = “CBEDFAGH”;
char *ls = “CEFDBHGA”;
BtNode * Buynode(){ BtNode *s = (BtNode*)malloc(sizeof(BtNode)); if(s == NULL) exit(1); memset(s,0,sizeof(BtNode)); return s;}void Freenode(BtNode *p){ free(p);}BtNode * CreateTree1() //创建二叉树(输入){ BtNode *s = NULL; ElemType item; scanf("%c",&item); if(item != '#') { s = Buynode(); s->data = item; s->leftchild = CreateTree1(); s->rightchild = CreateTree1(); } return s;}BtNode * CreateTree2(char *&str) // 创建二叉树(给定字符串){ BtNode *s = NULL; if(str!= NULL && *str!='#') { s = Buynode(); s->data = *str; s->leftchild = CreateTree2(++str); s->rightchild = CreateTree2(++str); } return s;}BtNode *CreateTree3(char ** const str){ BtNode *s=NULL; if(*str!=NULL && **str!='#'&& str!=NULL) { s=Buynode(); s->data=**str; s->leftchild=CreateTree3(&++*str); s->rightchild=CreateTree3(&++*str); } return s;}// 前序中序建立二叉树int FindIs(char *is,int n,char x){ for(int i=0;i<n;i++) { if(is[i]==x) { return i; } }}BtNode * Create1(char *ps,char *is,int n){ BtNode *s=NULL; if(n>0) { s=Buynode(); s->data=ps[0]; int pos=FindIs(is,n,ps[0]); if(pos==-1)exit(1); s->leftchild =Create1(ps+1,is,pos); s->rightchild =Create1(ps+pos+1,is+pos+1,n-pos-1); } return s;}BtNode * CreatePI(char *ps,char *is,int n){ if(ps==NULL || is==NULL || n<1) { return NULL; } else { return Create1(ps,is,n); }}//中序后序建立二叉树int FindIs2(char *is,int n,char x){ for(int i=0;i<n;i++) { if(is[i]==x) { return i; } }}BtNode * Create2(char *is,char *ls,int n){ BtNode *s=NULL; if(n>0) { int pos=FindIs2(is,n,ls[n-1]); if(pos=-1) exit(1); s=Buynode(); s->data=ls[n-1]; s->leftchild =Create2(is,ls,pos); s->rightchild =Create2(is+pos+1,ls+pos,n-pos-1); } return s;}BtNode * CreateIL(char *is,char *ls,int n){ if(is==NULL || ls==NULL || n<1) { return NULL; } else { return Create2(is,ls,n); }}void PreOrder(BtNode *ptr) //递归前序遍历二叉树{ if(ptr!= NULL) { printf("%c ",ptr->data); PreOrder(ptr->leftchild); PreOrder(ptr->rightchild); }}void InOrder(BtNode *ptr) //中序遍历二叉树{ if(ptr != NULL) { InOrder(ptr->leftchild); printf("%c ",ptr->data); InOrder(ptr->rightchild); }}void PastOrder(BtNode *ptr) //后序遍历二叉树{ if(ptr != NULL) { PastOrder(ptr->leftchild); PastOrder(ptr->rightchild); printf("%c ",ptr->data); }}void NicePerOrder(BtNode *ptr) //非递归前序遍历(栈实现){ if(ptr==NULL) { return ; } stack st; init_stack(&st); push(&st,ptr); while(!is_empty(&st)) //栈非空时 { ptr=top(&st); pop(&st); printf("%c ",ptr->data); if(ptr->rightchild!=NULL) { push(&st,ptr->rightchild ); } if(ptr->leftchild!=NULL) { push(&st,ptr->leftchild) ; } }}void NiceInOrder(BtNode *ptr) //非递归中序遍历{ if(ptr==NULL) { return ; } stack st; // BtNode *; init_stack(&st);while(ptr!=NULL ||! is_empty(&st)){ while(ptr!=NULL) { push(&st,ptr); ptr=ptr->leftchild; }} ptr=top(&st); pop(&st); printf("%c",ptr->data); ptr=ptr->rightchild;}void NicePastOrder(BtNode *ptr) //非递归后序遍历{ if(ptr==NULL) { return ; } stack st; init_stack(&st); BtNode *tag=NULL; while(ptr!=NULL ||! is_empty(&st)) { while(ptr!=NULL) { push(&st,ptr); ptr=ptr->leftchild; } ptr=top(&st); pop(&st); if(ptr->rightchild ==NULL || ptr->rightchild ==tag) { printf("%c ",ptr->data); tag=ptr; ptr=NULL; //保证继续出栈;而不是将ptr push到栈里; } else { push(&st,ptr); ptr=ptr->rightchild ; } }}
阅读全文
0 0
- 二叉树创建以及遍历
- 二叉树的创建以及遍历
- C++ 二叉树的创建以及遍历
- 二叉树的创建以及遍历
- 二叉树创建以及遍历方式
- 二叉树动态链式创建以及遍历
- 二叉树的创建以及遍历方法
- 二叉树的存储、创建以及遍历
- java二叉树创建以及遍历
- java二叉树创建以及遍历
- 二叉树的创建以及各种遍历
- 二叉树的创建,递归遍历以及非递归遍历
- 二叉树的创建,以及递归前中后序以及层次遍历
- 二叉树 创建 遍历!!!
- C,C++创建二叉树,以及3种遍历
- 【转】线索二叉树的原理以及创建和遍历
- java实现二叉树的创建以及遍历
- java实现二叉树的创建以及遍历
- 线段树 (入门篇)
- JVM与GC
- 如何提高测试效率
- object常用的方法有哪些
- Spring总结
- 二叉树创建以及遍历
- 第二章 Spring MVC入门 —— 跟开涛学SpringMVC
- 设计模式
- thinkcmf
- 【Python】【matplotlib】键鼠响应事件
- 打造“国际村”工作环境,弋果美语一路坚持!
- CodeForces
- Python3常用内置函数
- 前端面试题