




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1 请根据用户输入的“扩展的先序遍历序列 (用小圆点表示空子树),建立以二叉链表方式存储的二叉树,然后写出后序遍历该二叉树的非递归算法。方法一:#include <stdlib.h>#include <stdio.h>#define MAX_TREE_SIZE 100typedef struct BiTNode char data;struct BiTNode * lchild;struct BiTNode * rchild;BiTNode, *BiTree;/函数声明void Print(BiTree *root);void Selection(int sel,BiT
2、ree *root);void ReadExPreOrder(BiTree *root);void PrintExPreOrder(BiTree root);void PostOrderTraverse(BiTree T);/主函数void main()BiTree root=NULL; /初始化根结点 Print(&root);while (true)printf("nPress enter to continue.");getchar();getchar();system("cls");Print(&root);void Print(
3、BiTree *root) /提示int sel;printf("使用说明:本程序可实现二叉链表方式存储的二叉树,输入为扩展先序遍历序列.n");printf("-n");printf("1.输入扩展先序遍历序列并建立对应的二叉树.n");printf("2.打印当前的二叉树的扩展先序遍历序列.n");printf("3.后序遍历当前的二叉树并打印遍历序列.n");printf("4.按其它任意键退出.n");printf("-n");printf(&qu
4、ot;请选择你要的操作:");scanf("%d", &sel);getchar();Selection(sel, root);void Selection(int sel, BiTree *root) /根据用户输入决定下一步骤switch (sel) case 1: ReadExPreOrder(root); break;case 2: PrintExPreOrder(*root); break;case 3: PostOrderTraverse(*root); break;default: exit(0);void ReadExPreOrder(Bi
5、Tree *root) /先序遍历二叉树,root为指向二叉树或某一子树根结点的指针char ch;ch = getchar();if (ch = '.')*root = NULL;else (*root) = (BiTree)malloc(sizeof(BiTNode);(*root)->data = ch;ReadExPreOrder(&(*root)->lchild);ReadExPreOrder(&(*root)->rchild);void PrintExPreOrder(BiTree root)char ch;if (root!=NU
6、LL)ch = root->data;printf("%c", ch);PrintExPreOrder(root->lchild);PrintExPreOrder(root->rchild);elseprintf(".");void PostOrderTraverse(BiTree T) BiTree stackMAX_TREE_SIZE, p;int tagMAX_TREE_SIZE,top=0;p = T;while(p | top != 0)while(p) top+;stacktop=p;tagtop=0;p=p->lch
7、ild;/从根开始,左结点依次入栈if(top> 0) if(tagtop=1)/表示是从该结点的右子树返回,那么访问该结/点p=stacktop;printf("%c", p->data);top-;p=NULL;/将p指向NULL,那么下次进入while循环时,不做左子/树入栈操作else /说明是从该结点左子树返回,应继续访问其右子树p=stacktop;if(top> 0) p=p->rchild;tagtop=1; /说明该结点的右子树已访问 /end of else /end of if/end of while方法二顺序栈:#inclu
8、de<iostream>using namespace std;typedef struct node char ch;struct node *lchild;struct node *rchild;node;typedef struct binarytreenode *head;/指向根结点bt;int n=0;void ini(bt * &b)b =new bt;b->head=NULL;void precreate(node * &t)/先序递归建立二叉树cout<<"nn请输入结点数据,以'.'代表空:"
9、char c;cin>>c;if(c='.') t=NULL;elsen+;t=new node;t->ch=c;precreate(t->lchild);precreate(t->rchild);/-/非递归算法需要栈typedef struct stack node *base;node *top;int stacksize;stack;void ini(stack &s) s.base=new node *100 ;s.top=s.base;s.stacksize=100;void push(stack &s,node *el
10、em) (*s.top+)=elem;void pop(stack &s,node * &elem)elem= *(-s.top);int isempty(stack &s) if(s.base=s.top)return 1;else return 0;void gettop(stack &s,node *&t) t=*(s.top-1);void afttraverse(node *t) /后序遍历非递归算法stack s;ini(s);node * lastvist = NULL;while (NULL != t | !isempty(s) whil
11、e (NULL != t) push(s,t); t = t->lchild; gettop(s,t); if (NULL = t->rchild | lastvist = t->rchild) cout<<" "<<t->ch; pop(s,lastvist); t = NULL; else t = t->rchild;int main() bt *b;ini(b);precreate(b->head);/构造二叉树cout<<"nn后序遍历: "afttraverse(b->
12、;head);cout<<"nn"方法三链栈:#include<stdio.h>#include<stdlib.h>typedef struct BiTNode char item; struct BiTNode *lchild,*rchild;BiTNode,*Tree;typedef struct Stack Tree m; struct Stack *next;Stack,*st;Tree CreateTree()Tree B; char ch; ch=getchar(); if(ch='.') return NUL
13、L; else B=(Tree)malloc(sizeof(BiTNode); B->item=ch; B->lchild=CreateTree(); B->rchild=CreateTree(); return B; void PostOrder(Tree T)Tree p,q; st s,top; int op; p=T; top=NULL; dowhile(p!=NULL)s=(st)malloc(sizeof(Stack);/使用链表形式存储栈,即/链栈 s->m=p; s->next=top; top=s; p=p->lchild; op=1;/该
14、标志用于控制进入下面的while循环 q=NULL; while(top!=NULL&&op=1)if(top->m->rchild=q) /假设当前栈顶结点的右孩子是上次访问的结点,那么打印该结点 printf("%c",top->m->item);/第一次时打印最左下边结点 q=top->m; /q记录上次访问的结点 top=top->next; /栈指针后移,即将该结点从栈中弹出 else/假设当前栈顶结点的右孩子不是上次访问的结点,那么先访问其右孩/子 p=top->m->rchild; op=0;/o
15、p用来控制往右走一步后跳出当前while循环,进入下/一次外层的while循环 while(top!=NULL);int main()Tree T; printf("please enter the chars:n"); T=CreateTree(); PostOrder(T);getchar();getchar(); return 0;方法四:#include <stdio.h> #include <stdlib.h> struct TREE /二叉树结点结构 char data; struct TREE *lsubtree; struct TRE
16、E *Rsubtree; ; struct TREE *tree; void createtree(struct TREE *&p) /先序创立二叉树 char ch;ch = getchar(); if (ch = 'n') p = NULL; else if(ch = '.') p = NULL; else p = (struct TREE*)malloc(sizeof(struct TREE);/分配/结点空间p->data = ch; createtree(p->lsubtree); /递归创立左子树createtree(p->
17、Rsubtree); /递归创立右子树 void lastorder(struct TREE *tree)/非递归后序遍历 struct TREE *p; struct TREE *s100;/用一个指针数组来用作栈int top=-1;/下标int mack100;/标志数组p=tree; do while(p->lsubtree)!=NULL)&&(macktop+1!=2)/循环/直到让p向左滑到最左子树,并且该结点非第二次入栈 top=top+1; stop=p;/每循环一次,当前结点入栈macktop=1;/结点第一次入栈时,标志为1p=p->lsubtr
18、ee;/找最左子树 /叶子结点无须入栈printf("%cn",p->data); /输出末节点p=stop;/出栈顶元素top=top-1;/每出一个栈顶元素,下标就后退一位if (macktop+1=1)/假设结点是第一次出栈,那么再入栈 top=top+1; stop=p; macktop=2;/结点第二次入栈时,标志为2p=p->Rsubtree;/访问右子树 while (top!=-1); /top=-1时,说明根结点刚访问完,跳出循环后/打印printf("%cn",s0->data);/最后把根输出int main()p
19、rintf("请输入二叉树数据n");createtree(tree);printf("后序遍历结果:n");lastorder(tree);getchar();getchar(); 方法五:#include<stdio.h> #include <stdlib.h>#define maxsize 50 #define OK 1typedef struct Bnode /声明二叉树的数据结构 char ch; struct Bnode *lchild,*rchild; bnode,*btree; typedef struct typ
20、e btree node; int flag; datatype,*pdatatype; typedef struct /声明栈的数据结构datatype datamaxsize;int top; seqstack,*pseqstack; btree creatbtree() /创立二叉树 int i=0; btree t;char c;c=getchar();if(c='#') t=NULL;i+; else t=(btree)malloc(sizeof(bnode); t->data=c;i+; t->lchild=creatbtree(); t->rchild=creatbtree(); return t;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版10kv配电站施工设备租赁合同
- 2025年度美容院商铺租赁合同:美容院会员卡销售及积分系统合作协议
- 二零二五年度三子女债务分割与离婚协议模板
- 2025年中国三杆式闸机行业市场供需格局及投资规划建议报告
- 二零二五年度4S店洗车美容项目承包合作协议
- 2025年度9A文离婚协议范本制作与案例分析
- 2025年全新4S店店面租赁及品牌推广合同
- 美容院商铺租赁合同2025年规范文本-@-1
- 2025年食堂承包合同(大路食堂智能化升级)
- 今致人力(2025版)-电子商务行业人力资源服务合同
- 《教育系统重大事故隐患判定指南》知识培训
- 流转卡管理制度
- 燃气管线保护施工专项方案
- T-CALC 007-2025 重症监护病房成人患者人文关怀规范
- 肾脓肿的护理讲课
- 储能站施工组织设计施工技术方案(技术标)
- 小学数学二年级奥数应用题100道及答案
- DB32∕T 3148-2016 矿渣粉单位产品能源消耗限额
- 虚拟化资源调度策略-洞察分析
- 2025年江苏省环保集团招聘笔试参考题库含答案解析
- 公共实训基地建设可行性报告
评论
0/150
提交评论