当前位置:网站首页>顺序栈遍历二叉树
顺序栈遍历二叉树
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;
}
结果:

边栏推荐
- R for Data Science (note) -- data transformation (select basic use)
- 科技抗疫: 运营商网络洞察和实践白皮书 | 云享书库NO.20推荐
- Coinbase will launch the first encryption derivative for individual investors
- The name of the button in the Siyuan notes toolbar has changed to undefined. Has anyone ever encountered it?
- Bytebase joins Alibaba cloud polardb open source database community
- 消息称腾讯正式宣布成立“XR”部门,押注元宇宙;谷歌前 CEO:美国即将输掉芯片竞争,要让台积电、三星建更多工厂...
- 苹果不差钱,但做内容“没底气”
- IP address to integer
- You can capture fingerprints with a mobile camera?! Accuracy comparable to signature and monogram, expert: you are aggravating discrimination
- RF_ DC system clock setting gen1/gen2
猜你喜欢

Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function

Download steps of STM32 firmware library

Zadig + cave Iast: let safety dissolve in continuous delivery

Behind Tiantian Jianbao storm: tens of millions in arrears, APP shutdown, and the founder's premeditated plan to run away?

What other data besides SHP data

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

What about the Golden Angel of thunder one? Golden Angel mission details

Five day summary of software testing

Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial

Uninstall tool v3.5.10.5670 single file portable official version
随机推荐
[go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning
Wait for the victory of the party! After mining ebb tide, graphics card prices plummeted across the board
Geoscience remote sensing data collection online
【CANN文档速递04期】揭秘昇腾CANN算子开发
Redis installation of CentOS system under Linux, adding, querying, deleting, and querying all keys
年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial
Jd.com: how does redis implement inventory deduction? How to prevent oversold?
等等党们的胜利!挖矿退潮后,显卡价格全面暴跌
Openstack actual installation and deployment tutorial, openstack installation tutorial
Behind Tiantian Jianbao storm: tens of millions in arrears, APP shutdown, and the founder's premeditated plan to run away?
两位湖南老乡,联手干出一个百亿IPO
Saltstack state state file configuration instance
视频平台如何将旧数据库导入到新数据库?
Get to know the data structure of redis - hash
Anti epidemic through science and technology: white paper on network insight and practice of operators | cloud sharing library No.20 recommendation
Openvino2022 dev tools installation and use
Unit actual combat lol skill release range
What about the Golden Angel of thunder one? Golden Angel mission details
Apache+php+mysql environment construction is super detailed!!!