<二叉树的基本操作>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define num 100
- #define OK 1
- typedef int Status;
- typedef char DataType;
- typedef struct node
- {
- DataType data;
- struct node *lchild,*rchild;
- }BinTNode,*BinTree;
- Status CreateBiTree(BinTree &bt)
- {//按照先序遍历次序递归建立二叉树。
- //ABC@@DE@G@@F@@
- char ch;
- scanf("%c",&ch);
- if(ch == '@') bt = NULL;
- else
- {
- bt = (BinTNode*)malloc(sizeof(BinTNode));
- bt->data = ch; //生成根结点
- CreateBiTree(bt->lchild); //构造左子树
- CreateBiTree(bt->rchild); //构造右子树
- }
- return OK;
- }
- Status Inorder(BinTree bt)
- {//二叉树中序遍历非递归算法
- BinTNode *stack[num]; //定义栈数组
- int top = ; //初始化栈
- stack[top] = bt;
- do
- {
- while(NULL!=stack[top])
- {//扫描根结点及其所有的左结点并入栈
- top = top+;
- stack[top] = stack[top-]->lchild;
- }
- top = top-; //退栈
- if(top>=) //判断栈是否为空
- {
- printf("%c",stack[top]->data); //访问结点
- stack[top] = stack[top]->rchild; //扫描右子树
- }
- }while(top>=);
- return OK;
- }
- /*
- void Vist(char ch)
- {
- printf("%c",ch);
- }
- */
- void PostOrder(BinTree bt)
- {//二叉树后序遍历递归算法
- if(bt)
- {
- PostOrder(bt->lchild);
- PostOrder(bt->rchild);
- printf("%c",bt->data);
- }
- }
- int Size(BinTree bt)
- {//统计二叉树中所有结点的个数
- int num1,num2;
- if(bt==NULL)
- return ;
- else if(bt->lchild==NULL && bt->rchild==NULL)
- return ;
- else
- {
- num1 = Size(bt->lchild);
- num2 = Size(bt->rchild);
- return (num1+num2+);
- }
- }
- int LeafCount(BinTree bt)
- {//叶子结点总数为
- int LeafNum;
- if(bt==NULL)
- LeafNum = ;
- else if((bt->lchild==NULL) && (bt->rchild==NULL)) LeafNum = ;
- else LeafNum = LeafCount(bt->lchild)+LeafCount(bt->rchild);
- //叶子数为左右子树叶子数目之和
- return LeafNum;
- }
- int Depth(BinTree bt)
- {//统计二叉树深度
- int hl,hr,max;
- if(bt!=NULL)
- {
- hl = Depth(bt->lchild); //求左子树的深度
- hr = Depth(bt->rchild); //求右子树的深度
- max = hl>hr?hl:hr;
- return (max+); //返回树的深度
- }
- else
- return ;
- }
- void Exchange(BinTree bt)
- {//交换左右二叉树
- if(bt == NULL)
- return;
- BinTNode *temp;
- temp = bt->lchild;
- bt->lchild = bt->rchild;
- bt->rchild = temp;
- Exchange(bt->lchild);
- Exchange(bt->rchild);
- }
- void main()
- {
- BinTree bt;
- int xz = ;
- int yz,sd;
- while(xz)
- {
- printf("二叉树的建立及其基本操作\n");
- printf("===========================\n");
- printf("1,建立二叉树的存储结构\n");
- printf("2,二叉树的基本操作\n");
- printf("3,交换二叉树的左右\n");
- printf("0退出系统\n");
- printf("==========================\n");
- printf("请选择:(0~3)\n");
- scanf("%d",&xz);
- getchar();
- switch(xz)
- {//输入:ABC@@DE@G@@F@@@输出:CBEGDFA
- case :
- printf("输入二叉树的先序序列结点值:\n");
- CreateBiTree(bt);
- printf("二叉树的链式存储结构建立完成\n");
- printf("\n");
- break;
- case :
- printf("该二叉树的后序遍历序列是:");
- PostOrder(bt);
- printf("\n"); //输出CGEFDBA
- printf("该二叉树的中序遍历序列是:");
- Inorder(bt);
- printf("\n"); //输出CBEGDFA
- printf("该二叉树的结点的个树是:%d\n",Size(bt));
- yz = LeafCount(bt);
- printf("叶子结点个数是:%d\n",yz);
- sd = Depth(bt);
- printf("该二叉树的深度是:%d\n",sd);
- printf("\n");
- break;
- case :
- Exchange(bt);
- printf("该二叉树已交换左右子树!\n");
- printf("\n");
- break;
- case :
- break;
- }
- }
- }
<二叉树的基本操作>的更多相关文章
- 简单物联网:外网访问内网路由器下树莓派Flask服务器
最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...
- 利用ssh反向代理以及autossh实现从外网连接内网服务器
前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...
- 外网访问内网Docker容器
外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...
- 外网访问内网SpringBoot
外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...
- 外网访问内网Elasticsearch WEB
外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...
- 怎样从外网访问内网Rails
外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...
- 怎样从外网访问内网Memcached数据库
外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...
- 怎样从外网访问内网CouchDB数据库
外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...
- 怎样从外网访问内网DB2数据库
外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...
- 怎样从外网访问内网OpenLDAP数据库
外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...
随机推荐
- Unity 之 Time
Time.deltaTime 指完成每一帧的时间,根据得到实际的测试,可以看到每一帧的所用时间不一致,差距很微小. Time.deltaTime在Update和FixedUpdate中显示的是不一样 ...
- Ambari安装指南
一.准备工作 l 基本工具 1) 安装epel,epel是一个提供高质量软件包的项目.先检查主机上是否安装: rpm -q epel-release 2) 如果没有安装,使用rpm命令安装: rpm ...
- FAILED Selenium2Library
FAILED Selenium2Library Importing test library 'Selenium2Library' failed: ImportError: cannot import ...
- codevs 1200 同余方程 逆元
题目描述 Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入描述 Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空 ...
- RobotFramework安装指南
Python下载 https://www.python.org/downloads Setuptools下载 https://pypi.python.org/pypi/setuptools 用原装各种 ...
- Android中如何实现EditText的自动换行
要实现EditText的自动换行需要实现如下设置: <EditText android:id="@+id/function_lifingcost_edit_txtRemark" ...
- table maker's delimma
table maker's delimma是计算机浮点数精度的一个问题. 浮点数的表示方式 计算机能表示的数字都是有理数,所有的有理数都可以归结为下面的模式:1.@@@ × 2#### 其中,@@@ ...
- English trip -- VC(情景课)10 A Get ready 预备课
Words dance 跳舞 exercise 运动:锻炼 fish 鱼 play basketball 打篮球 play cards 玩牌 swim 游泳 decorations 装饰品 ...
- C++中的构造函数,拷贝构造函数,赋值函数
C++中一般创建对象,拷贝或赋值的方式有构造函数,拷贝构造函数,赋值函数这三种方法.下面就详细比较下三者之间的区别以及它们的具体实现 1.构造函数 构造函数是一种特殊的类成员函数,是当创建一个类的对象 ...
- Java容器涉及的类(代码)
Customer: public class Customer implements Comparable{ private Integer customerId; private String cu ...