




已阅读5页,还剩37页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* 二叉树的层次遍历*/#include#include#include #include#define TREEMAX 50typedef struct nodechar data;struct node *lchild;struct node *rchild; Node;/ 递归创建二叉树Node *CreateBT()Node *t;char x;scanf(“%c“, fflush(stdin);if(x=#)t=NULL;elset=new Node;t-data=x;printf(“tt请输入%c 结点的左子结点: “,t-data);t-lchild=CreateBT();printf(“tt请输入%c 结点的右子结点: “,t-data);t-rchild=CreateBT();return t;/ 递归先序遍历二叉树void Preorder(Node *T)if(T)printf(“%3c“,T-data);Preorder(T-lchild);Preorder(T-rchild);/ 递归中序遍历二叉树void Inorder(Node *T)if(T)Inorder(T-lchild);printf(“%3c“,T-data);Inorder(T-rchild);/ 递归后序遍历二叉树void Postorder(Node *T)if(T)Postorder(T-lchild);Postorder(T-rchild);printf(“%3c“,T-data);/ 非递归先序遍历二叉树void noRecursivePreorder(Node *T)Node *StTREEMAX,*p;int top=-1;if(T!=NULL)top+;Sttop=T;while(top-1)p=Sttop;top-;printf(“%c“,p-data);if(p-rchild!=NULL)top+;Sttop=p-rchild;if(p-lchild!=NULL)top+;Sttop=p-lchild;printf(“n“);/ 非递归中序遍历二叉树void noRecursiveInorder(Node *T)Node *StTREEMAX,*p;int top=-1;if(T!=NULL)p=T;while(top-1|p!=NULL)while(p!=NULL)top+;Sttop=p;p=p-lchild;if(top-1)p=Sttop;top-;printf(“%c“,p-data);p=p-rchild;printf(“n“);/ 非递归后序遍历二叉树void noRecursivePostorder(Node *T)Node *StTREEMAX,*p;int flag,top=-1;if(T!=NULL)dowhile(T!=NULL)top+;Sttop=T;T=T-lchild;p=NULL;flag=1;while(top!=-1if(T-rchild=p)printf(“%c“,T-data);top-;p=T;elseT=T-rchild;flag=0;while(top!=-1);printf(“n“);/ 层次遍历二叉树,用 queue数组充当顺序队列void LevelOrder(Node *T)Node *queueTREEMAX, *p;int pre=-1, rear=-1;/ 树根结点先进队queue+pre=T;/ 当队列不为空时,出队一个结点的同时将其左右孩子进队while(pre-rear+TREEMAX)%TREEMAX!=0)p=queue+rear; / 出队一个结点并输出printf(“%3c“, p-data);if(p-lchild!=NULL) / 左孩子存在则进队queue+pre=p-lchild;if(p-rchild!=NULL) / 右孩子存在则进队queue+pre=p-rchild;void list()printf(“n“);printf(“ntt 二叉链表“);printf(“ntt*“);printf(“ntt* 1.建立二叉树 *“);printf(“ntt* 2.递归前序遍历 *“);printf(“ntt* 3.递归中序遍历 *“);printf(“ntt* 4.递归后序遍历 *“);printf(“ntt* 5.非递归前序遍历 *“);printf(“ntt* 6.非递归中序遍历 *“);printf(“ntt* 7.非递归后序遍历 *“);printf(“ntt* 8.层次遍历 *“);printf(“ntt* 9.清屏 *“);printf(“ntt* 0.退出程序 *“);printf(“ntt*“);printf(“ntt请输入菜单项:“);void main()Node *T=NULL;char a,b;b=m;while(b=m|b=M)list();scanf(“%c“, fflush(stdin);switch(a)case1:printf(“ntt请按先序序列输入二叉树的结点(输入#表示结点为空):n“);printf(“ntt请输入根结点:“);T=CreateBT();printf(“ntt二叉树成功建立-n“);break;case2:printf(“ntt该二叉树递归前序遍历为:“);Preorder(T);break;case3:printf(“ntt该二叉树递归中序遍历为:“);Inorder(T);break;case4:printf(“ntt该二叉树递归后序遍历为:“);Postorder(T);break;case5:printf(“ntt该二叉树非递归前序遍历为:“);noRecursivePreorder(T);break;case6:printf(“ntt该二叉树非递归中序遍历为:“);noRecursiveInorder(T);break;case7:printf(“ntt该二叉树非递归后序遍历为:“);noRecursivePostorder(T);break;case8:printf(“ntt该二叉树的层次遍历序列为:“);LevelOrder(T);break;case9:system(“cls“);break;case0:b=n;break;default:printf(“ntt*对不起,输入有误!*“);/* 多项式运算【链式结构】*/#include “stdio.h“#include “stdlib.h“typedef struct _node float coe; int index;struct _node *next; Node, *NodePtr;/ 整理多项式链表,将链表中的结点根据各项指数进行升序排列void sortPoly(NodePtr list)NodePtr p=list-next, q=list, r, ptr;/ 让 p指向链表的第二个数据结点,/ p为空则说明链表为空,不用整理了。if(p)p=p-next;else return;/ 先从链表的第一个数据结点后断开链表ptr=q-next;ptr-next=NULL;/ 再将链表的第二个直至最后一个数据结点依次插入链表while(p)q=list;ptr=q-next;while(ptr-index index)q=ptr;ptr=ptr-next;if(ptr=NULL)break;if(ptr=NULL)q-next=p;ptr=p;p=p-next;ptr-next=NULL;else if(ptr-index = p-index)ptr-coe += p-coe;r=p-next;free(p);p=r;else if(ptr-index p-index)r=p-next;p-next=ptr;q-next=p;p=r;/* 创建一个多项式链表,返回创建链表的头指针 */NodePtr createPoly() int i=1, index;float coe;NodePtr resultPtr, q;/ 开辟头结点空间resultPtr = (NodePtr)malloc(sizeof(Node);resultPtr-next=NULL;printf(“请输入多项式第%d 项的系数和指数(输入 0 0时结束):“, i+);scanf(“%f%d“, while(1)if(index else if(index=0 else / 开辟新结点空间q=(NodePtr)malloc(sizeof(Node);q-coe=coe;q-index=index;q-next=NULL;/ 将新结点插入多项式链表q-next=resultPtr-next;resultPtr-next=q;/ 输入下一项printf(“请输入多项式第%d 项的系数和指数(输入 0 0时结束):“, i+);scanf(“%f%d“, / 将输入的多项式链表整理后返回sortPoly(resultPtr);return resultPtr;/ 判断多项式链表是否已输入或是否为空int isEmpty(NodePtr list1, NodePtr list2)if(list1=NULL)return 1;else if(list2=NULL)return 2;else if(list1-next=NULL)return -1;else if(list2-next=NULL)return -2;elsereturn 0;/ 两个多项式相加NodePtr addPoly(NodePtr list1, NodePtr list2)NodePtr resultPtr, p=list1-next, q=list2-next, r;if(isEmpty(list1, list2)=1)printf(“多项式链表 L1不存在,请先选择菜单 1输入多项式 L1!n“);return NULL;else if(isEmpty(list1, list2)=2)printf(“多项式链表 L2不存在,请先选择菜单 2输入多项式 L2!n“);return NULL;/ 创建结果多项式的头结点resultPtr=(NodePtr)malloc(sizeof(Node);r
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 核酸核苷酸行业深度研究分析报告(2024-2030版)
- 2025-2030年中国瓶装氧气行业深度研究分析报告
- 2025-2030年中国五金机械塑料行业深度研究分析报告
- 餐饮协会培训课件
- 2025年中国农用金属配件行业市场发展前景及发展趋势与投资战略研究报告
- 中国蔬菜基地行业市场发展现状及前景趋势与投资分析研究报告(2024-2030)
- 2025年抖音冲锋衣行业趋势洞察报告
- 2025年 朝阳师范学院高校招聘考试笔试试题附答案
- 2025-2030年中国参茸滋补品行业市场供需态势及前景战略研判报告
- 2025年中国全自动管材生产线行业市场发展前景及发展趋势与投资战略研究报告
- JJG 646-2006移液器
- GB/T 40167-2021纸和纸板加速老化(100 ℃)
- GB/T 17626.4-2018电磁兼容试验和测量技术电快速瞬变脉冲群抗扰度试验
- GB/T 1094.12-2013电力变压器第12部分:干式电力变压器负载导则
- 活性炭改性及吸附条件研究性实验
- 小学体育教研活动记录(有内容)
- 核级阀门强度计算方法的分析
- 中国古代朝代历史知识大汉王朝科普图文PPT教学课件
- 氯氧铋光催化剂的晶体结构
- 随州市城市规划管理技术规定
- 围墙检验批质量验收记录表
评论
0/150
提交评论