当前位置:网站首页>二叉树的相关操作
二叉树的相关操作
2022-07-25 17:58:00 【心を見て心をほれる】
问题描述
实现以下算法:
1.以二叉链表表示二叉树,建立一棵二叉树;
2.输出二叉树的中序遍历结果;
3.输出二叉树的前序遍历结果;
4.输出二叉树的后序遍历结果;
5. 输出二叉树的层序遍历结果。
6.交换二叉树每个结点的左孩子和右孩子。
7.利用二叉树计算表达式的值。建立表达式树,并计算表达式的值
8. 树的层序zigzag输出,树按层序遍历,奇数层从左至右输出,偶数层从右至左输出。
提示:以下是本篇文章正文内容,下面案例可供参考
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
typedef struct BTNode *Position;
typedef Position BTree;
struct BTNode
{
char data;
Position lChild, rChild;
};
//建立二叉函数
BTree CreateBTree(BTree bt)
{
char ch;
printf("节点: ");
fflush(stdin); /* 清空缓存区 */
scanf("%c", &ch);
fflush(stdin);
if (ch != '#')//出现#即为该节点没有左(右)孩子
{
bt = new BTNode;
bt->data = ch;
bt->lChild = NULL;
bt->rChild = NULL;
printf("%c的左孩子: ", bt->data);
bt->lChild = CreateBTree(bt->lChild);
printf("%c的右孩子: ", bt->data);
bt->rChild = CreateBTree(bt->rChild);
}
return bt;
}
//先序遍历
void PreOrder(BTree bt)
{
if (bt != NULL)
{
printf("%c", bt->data);
PreOrder(bt->lChild);
PreOrder(bt->rChild);
}
}
//中序遍历
void InOrder(BTree bt)
{
if (bt != NULL)
{
InOrder(bt->lChild);
printf("%c", bt->data);
InOrder(bt->rChild);
}
}
//后序遍历
void PostOrder(BTree bt)
{
if (bt != NULL)
{
PostOrder(bt->lChild);
PostOrder(bt->rChild);
printf("%c", bt->data);
}
}
//层序遍历
void CenOrder(BTree bt)
{
BTree temp[100];
int in=0,out=0;
temp[in++]=bt;
while(in>out){
if(temp[out]){
printf("%c",temp[out]->data);
temp[in++]=temp[out]->lChild;
temp[in++]=temp[out]->rChild;
}
out++;
}
}
//zigzag层序遍历
void ZigZagOrder(BTree bt)
{
BTree temp[100];
int in=0,out=0,k,i,j;
temp[in++]=bt;
while(in>out){
if(temp[out]){
temp[in++]=temp[out]->lChild;
temp[in++]=temp[out]->rChild;
}
out++;
}
k=ceil(log(in+1)/log(2));
printf("%c",temp[0]->data);
for(i=2;i<=k;i++){
if(i%2==1){
for(j=pow(2,i-1);j<=pow(2,i)-1;j++){
if(temp[j-1]!=NULL)
printf("%c",temp[j-1]->data);
}
}
else{
for(j=pow(2,i)-1;j>=pow(2,i-1);j--){
if(temp[j-1]!=NULL)
printf("%c",temp[j-1]->data);
}
}
}
}
//交换每个节点的左孩子和右孩子
BTree Change(BTree bt)
{
BTree t;
if(bt->data!='#'){
if(bt->lChild != NULL&&bt->rChild != NULL){
Change(bt->lChild);
Change(bt->rChild);
}
t=bt->lChild;
bt->lChild=bt->rChild;
bt->rChild=t;
}
return bt;
}
int main()
{
int choose=-1;
printf("1、建立二叉树\n");
printf("2、先序遍历\n");
printf("3、中序遍历\n");
printf("4、后序遍历\n");
printf("5、层序遍历\n");
printf("6、交换每个节点的左孩子和右孩子\n");
printf("7、zigzag层序遍历\n");
printf("0、退出\n\n");
while(choose!=0){
printf("请选择:");
scanf("%d", &choose);
switch (choose){
case 1:
BTree bt;
bt = CreateBTree(bt);
printf("成功建立二叉树\n");
break;
case 2:
printf("先序遍历:");
PreOrder(bt);
printf("\n");
break;
case 3:
printf("中序遍历:");
InOrder(bt);
printf("\n");
break;
case 4:
printf("后序遍历:");
PostOrder(bt);
printf("\n");
break;
case 5:
printf("层序遍历:");
CenOrder(bt);
printf("\n");
break;
case 6:
Change(bt);
printf("交换成功!\n");
break;
case 7:
printf("zigzag层序遍历:");
ZigZagOrder(bt);
printf("\n");
break;
}
}
getchar();
return 0;
}
实验结果
建立二叉树



边栏推荐
- [cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
- The new version of 3dcat v2.1.3 has been released. You can't miss these three function updates!
- 专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我
- Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation
- SVN客户端(TortoiseSVN)安装及使用说明
- OSPF --- open shortest priority path protocol
- H5 test point (mind map)
- Redis源码与设计剖析 -- 17.Redis事件处理
- 排序还需要了解的信息以及链表
- I'm also drunk. Eureka delayed registration and this pit!
猜你喜欢

绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码

CH582 BLE 5.0 使用 LE Coded 广播和连接

云流化和云桌面有什么关系

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation

Hcip first day experiment
![[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
Principle and implementation of UDP penetration NAT in P2P

Unity 贝塞尔曲线的创建

2022/7/23
随机推荐
Go defer and recover simple notes
mongodb 集群及分片
Redis源码与设计剖析 -- 15.RDB持久化机制
Basic knowledge of software testing (mind mapping)
Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number
Installation steps and usage of NVM under windows10 system
ORB_SLAM3复现——上篇
PHP解决并发问题的几种实现
[untitled]
简述Synchronized以及锁升级
itextpdf实现多PDF文件合并为一个PDF文档
Notes on Flickr's dataset
期货开户哪家最好最安全
Which real-time gold trading platform is reliable and safe?
How to fix the first row title when scrolling down in Excel table / WPS table?
Mock service Moco series (I) - introduction, first demo, get request, post request
云VR:虚拟现实专业化的下一步
Cloud VR: the next step of virtual reality specialization
Dating activity records
Tme2022 campus recruitment background development / operation development / business operation and maintenance / application development written examination (I) a little self analysis of programming q