当前位置:网站首页>二叉树的相关操作
二叉树的相关操作
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;
}
实验结果
建立二叉树



边栏推荐
- mysql case when
- Redis源码与设计剖析 -- 17.Redis事件处理
- 越来越成熟的Rust,都应用了哪些场景呢?
- Cloud XR面临的问题以及Cloud XR主要应用场景
- Problems faced by cloud XR and main application scenarios of cloud XR
- Basic knowledge of software testing (mind mapping)
- Mysql database common commands
- Notes on Flickr's dataset
- Tme2022 campus recruitment background development / operation development / business operation and maintenance / application development written examination (I) a little self analysis of programming q
- Creation of unity Bezier curve
猜你喜欢

11. Camera and lens

Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明

Sorting also needs to know the information and linked list

Update 3dcat real time cloud rendering V2.1.2 release

WPF implements user avatar selector

PageHelper还能结合Lambda表达式实现简洁的分页封装

为什么数字化未来取决于3D实时渲染

「数字安全」警惕 NFT的七大骗局

Hcip first day experiment

Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
随机推荐
Cet
Memory and packet buffer management of LwIP
Mock service Moco series (III) - redirection, regular expression, delay, template, event, sub module design
mongodb 集群及分片
软件测试基础知识(思维导图)
简述聚簇索引、二级索引、索引下推
越来越成熟的Rust,都应用了哪些场景呢?
Hcip first day experiment
Installation and operation instructions of SVN client (TortoiseSVN)
虚拟偶像代言产品出问题谁负责?
直击考点:PMP考试中常见敏捷知识点汇总
面试官:说说 log.Fatal 和 panic 的区别
OSPF综合实验
Mock服务moco系列(三)- 重定向、正则表达式、延迟、模板、事件、分模块设计
Which one of the electronic products has a longer service life??
Auditing related notes
Calculation date or date formatting
实时黄金交易平台哪个可靠安全?
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
Wu Enda's machine learning programming operation cannot be suspended pause problem solved