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

边栏推荐
- 1、 Downloading and installing appium
- Bytebase 加入阿里云 PolarDB 开源数据库社区
- 【Go語言刷題篇】Go從0到入門4:切片的高級用法、初級複習與Map入門學習
- Bytebase joins Alibaba cloud polardb open source database community
- Confirm whether the host is a large terminal or a small terminal
- Oracle create tablespaces and tables
- Coinbase will launch the first encryption derivative for individual investors
- 华为云ModelArts第四次蝉联中国机器学习公有云服务市场第一!
- The agile way? Is agile development really out of date?
- VXLAN 与 MPLS:从数据中心到城域以太网
猜你喜欢

宅男救不了元宇宙

Nodered has no return value after successfully inserting into the database (the request cannot be ended)

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

Vs2017 setting function Chinese Notes

Download steps of STM32 firmware library

Bytebase joins Alibaba cloud polardb open source database community

Working for 6 years with a monthly salary of 3W and a history of striving for one PM

Full link service tracking implementation scheme

16个优秀业务流程管理工具

Data backup and recovery of PgSQL
随机推荐
unity之模糊背景(带你欣赏女人的朦胧美)
Maps are grouped according to the values of the passed in parameters (similar to database groupby)
Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial
【CANN文档速递06期】初识TBE DSL算子开发
Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function
Openstack actual installation and deployment tutorial, openstack installation tutorial
"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"
VXLAN 与 MPLS:从数据中心到城域以太网
What other data besides SHP data
Five day summary of software testing
Two solutions to the problem of 0xv0000225 unable to start the computer
字节、腾讯也下场,这门「月赚3000万」的生意有多香?
Making startup U disk -- Chinese cabbage U disk startup disk making tool V5.1
Apache+PHP+MySQL环境搭建超详细!!!
Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month
Win7 10 tips for installing Office2010 five solutions for installing MSXML components
华为云ModelArts第四次蝉联中国机器学习公有云服务市场第一!
SQL export CSV data, unlimited number of entries
Full link service tracking implementation scheme
1、 Downloading and installing appium