#include<stdio.h>
#include<stdlib.h>
#include<string.h> #define num 100
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define FALSE 0
#define TRUE 1 typedef int Status;
typedef char DataType; typedef struct node
{
DataType data;
struct node *lchild,*rchild;
}BinTNode,*BinTree; typedef BinTNode* ElemType; #define QueueSize 100
//循环队列的存储结构
typedef struct
{
ElemType *base;
int front,rear;
}SeQueue; 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 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);
} //构造一个循环队列
Status InitQueue(SeQueue &Q)
{
Q.base = (ElemType *)malloc(QueueSize *sizeof(ElemType));
if(!Q.base) exit();
Q.front = Q.rear = ;
return OK;
}
//插入新的元素为队尾元素
Status EnQueue(SeQueue &Q,ElemType e)
{
if((Q.rear+)%QueueSize==Q.front)
{
printf("Queue overflow");
return ;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+)%QueueSize;
return ;
} //删除队头元素
Status DeleteQ(SeQueue &Q,ElemType &e)
{
if(Q.front == Q.rear)
{
printf("Queue enpty");
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front+)%QueueSize;
return OK;
} //判空循环队列
Status IsEmptyQ(SeQueue Q)
{
if(Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
//层次遍历二叉树
void LevelOrderTraversal(BinTree bt)
{
SeQueue Q;
ElemType e;
//若是空树,则直接返回
InitQueue(Q); //创建并初始化队列
if(bt) EnQueue(Q,bt);
while(!IsEmptyQ(Q))
{
DeleteQ(Q,e);
printf("%4c",e->data);
if(e->lchild) EnQueue(Q,e->lchild);
if(e->rchild) EnQueue(Q,e->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("4,二叉树的层次遍历\n");
printf("0退出系统\n");
printf("==========================\n");
printf("请选择:(0~4)\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 :
printf("该二叉树的层序遍历序列是:");
LevelOrderTraversal(bt);
printf("\n"); //输出ABCDEFG
case :
break;
//default:printf("请输入正确的选项:(0~4):\n");
// break; }
}
}

<youcengcibianli>的更多相关文章

  1. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  2. 利用ssh反向代理以及autossh实现从外网连接内网服务器

    前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...

  3. 外网访问内网Docker容器

    外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...

  4. 外网访问内网SpringBoot

    外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...

  5. 外网访问内网Elasticsearch WEB

    外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...

  6. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. 怎样从外网访问内网CouchDB数据库

    外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...

  9. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

  10. 怎样从外网访问内网OpenLDAP数据库

    外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...

随机推荐

  1. Ubuntu 14.04 下解决maven访问速度慢问题

    参考: maven国内镜像(maven下载慢的解决方法) maven中央仓库访问速度太慢的解决办法 Ubuntu 14.04 下解决maven访问速度慢问题 在启动OVX的时候,由于sh脚本中需要使用 ...

  2. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...

  3. shell case语句

    case 格式 case 值 in 模式1) command1 command2 ... commandN ;; 模式2) command1 command2 ... commandN ;; esac ...

  4. EsayUI + MVC + ADO.NET(工作单元)

    关联的设计 关联本身不是一个模式,但它在领域建模的过程中非常重要,所以需要在探讨各种模式之前,先讨论一下对象之间的关联该如何设计.我觉得对象的关联的设计可以遵循如下的一些原则: 关联尽量少,对象之间的 ...

  5. js插件---Bootstrap 树控件

    js插件---Bootstrap 树控件 一.总结 一句话总结:可以直接用gojs,或者搜索js,jquery的树控件,或者bootstrap树控件,一大堆 gojs 二.JS组件系列——Bootst ...

  6. HTML提交方式post和get区别(实验)

    HTML提交方式post和get区别(实验) 一.post和get区别 get提交,提交的信息都显示在地址栏中. post提交,提交的信息不显示地址栏中,显示在消息体中. 二.客户端代码 <!D ...

  7. WPF 的 数据源属性 和 数据源

    (一)数据源(数据对象)属性 :path 或  path的值(path=VM.Property或M.Property),通常具有通知功能(特例除外). (二)path不能孤立而存在,它一定具有所归属的 ...

  8. pip 源 替换国内源

    网上收集来的pip源地址: 阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simp ...

  9. UVA-1663 Purifying Machine (最大匹配数)

    题目大意:每一个01串中最多含有一个‘*’,‘*’既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串.如:000.010.0*1表示000.010.001.011, ...

  10. java.lang.Exception: Socket bind failed: [730048]

    严重: Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/ ...