当前位置:网站首页>顺序栈遍历二叉树
顺序栈遍历二叉树
2022-06-24 19:02:00 【悟空不买菜了】
在做这道题之前,需要用到顺序栈,那么还是老规矩,把之前的做的顺序栈做成一个动态库,这样方便使用,不会顺序栈和制作动态库的同学可以去看我另外的文章。
先去到存放库的文件夹里面看看,有没有动态库

上面很明显有这样的动态库libseqstack.so,因此可以直接来使用
先来上一个二叉树:

分析一下遍历过程:

下面我们打印像下面这棵树:

直接上代码:
这里面引入了顺序栈1.0的动态库,可以去看我的文章
non_recursion.c
#include <stdio.h>
#include <stdlib.h>
#include "seqstack1.0.h"
//定义每一个结点的数据结
typedef struct _binary_node binary_node;
struct _binary_node {
char ch;
binary_node *lchild;
binary_node *rchild;
int flag;
};
//栈递归
void stack_print(binary_node *node)
{
//先来初始化一个栈
t_seq_stack* t_stack = create_stack();
if(t_stack == NULL) {
return;
}
//直接把根结点先入栈
push_stack(t_stack,node);
//然后进行循环
while(!is_empty(t_stack)) {
//先把根结点拿出来,左右结点好入栈
//这里采用先序遍历,DLR,那么R肯定先入栈,最后打出来
binary_node* node = (binary_node*)top_stack(t_stack);
//然后出栈
pop_stack(t_stack);
//判断一下flag是否等于1,等于打印
if(node->flag == 1) {
printf("%c\n",node->ch);
continue;
}
node->flag = 1;//弹出来之后的标识,肯定要从0变成1
//否则,把右,左,和上面的根接连入栈
if(node->rchild != NULL) {
push_stack(t_stack,node->rchild);
}
if(node->lchild != NULL) {
push_stack(t_stack,node->lchild);
}
//插入弹出的结点
push_stack(t_stack,node);
}
destroy_stack(t_stack);
}
int main()
{
//这里把结点创建好
binary_node node_a = {'A',NULL,NULL,0};
binary_node node_b = {'B',NULL,NULL,0};
binary_node node_c = {'C',NULL,NULL,0};
binary_node node_d = {'D',NULL,NULL,0};
binary_node node_e = {'E',NULL,NULL,0};
binary_node node_f = {'F',NULL,NULL,0};
//把结点相互连接上
node_a.lchild = &node_b;
node_a.rchild = &node_c;
node_b.lchild = &node_d;
node_b.rchild = &node_e;
node_c.rchild = &node_f;
stack_print(&node_a);
//穿件
return 0;
}
结果:

边栏推荐
- Redis installation of CentOS system under Linux, adding, querying, deleting, and querying all keys
- Oracle create tablespaces and tables
- Obstacle avoidance sensor module (stm32f103c8t6)
- Ribbon source code analysis @loadbalanced and loadbalancerclient
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
- 16个优秀业务流程管理工具
- Kubernetes cluster deployment
- 宅男救不了元宇宙
- Five day summary of software testing
- 【CANN文档速递04期】揭秘昇腾CANN算子开发
猜你喜欢

两位湖南老乡,联手干出一个百亿IPO

Write a positive integer to the node and return a floating-point number multiplied by 0.85 when reading the node

LCD12864 (ST7565P) Chinese character display (STM32F103)

Two fellow countrymen from Hunan have jointly launched a 10 billion yuan IPO

Kubernetes cluster deployment

2022年最新四川建筑八大员(电气施工员)模拟题库及答案

Openstack actual installation and deployment tutorial, openstack installation tutorial

Nodered has no return value after successfully inserting into the database (the request cannot be ended)
![[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer](/img/14/0313857adc178ecee4c866a05e54aa.jpg)
[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer

对“宁王”边卖边买,高瓴资本“高抛低吸”已套现数十亿
随机推荐
First understand redis' data structure - string
Two solutions to the problem of 0xv0000225 unable to start the computer
CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer
【CANN文档速递04期】揭秘昇腾CANN算子开发
R for Data Science (notes) -- data transformation (used by filter)
Some ideas about chaos Engineering
gateway
Openvino2022 dev tools installation and use
Methods for comparing float types in the kernel
“拯救”直播带货,一个董宇辉还不够
Database index can improve query efficiency. Ask what will improve, what is the difference between inapplicable index and index use, and what will happen.
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
Digital twin industry case: Digital Smart port
Bytebase 加入阿里云 PolarDB 开源数据库社区
Mq-2 smoke concentration sensor (STM32F103)
Five day summary of software testing
苹果不差钱,但做内容“没底气”
Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?
Accurate calculation of task progress bar of lol mobile game