当前位置:网站首页>Sequential stack traversal binary tree
Sequential stack traversal binary tree
2022-06-24 20:28:00 【Wukong stopped buying vegetables】
Before doing this problem , You need a sequence stack , Then it's the old rule , Make the previous sequence stack into a dynamic library , It's easy to use , Students who don't know how to make sequence stacks and dynamic libraries can go to my other articles .
First go to the folder of the repository to have a look , Is there a dynamic library

It is obvious that there is such a dynamic library libseqstack.so, So it can be used directly
Let's start with a binary tree :

Analyze the traversal process :

Let's print a tree like the one below :

Go straight to the code :
This introduces the sequence stack 1.0 Dynamic library , You can read my article
non_recursion.c
#include <stdio.h>
#include <stdlib.h>
#include "seqstack1.0.h"
// Define the data structure of each node
typedef struct _binary_node binary_node;
struct _binary_node {
char ch;
binary_node *lchild;
binary_node *rchild;
int flag;
};
// Stack recursion
void stack_print(binary_node *node)
{
// First, initialize a stack
t_seq_stack* t_stack = create_stack();
if(t_stack == NULL) {
return;
}
// Directly put the root node on the stack first
push_stack(t_stack,node);
// And then cycle
while(!is_empty(t_stack)) {
// Take out the root node first , The left and right nodes are easy to stack
// Here we use preorder traversal ,DLR, that R It must be on the stack first , Finally, type it out
binary_node* node = (binary_node*)top_stack(t_stack);
// And then out of the stack
pop_stack(t_stack);
// Judge flag Is it equal to 1, Equal to printing
if(node->flag == 1) {
printf("%c\n",node->ch);
continue;
}
node->flag = 1;// The logo that pops up , Definitely from 0 become 1
// otherwise , Turn right , Left , Connect to the root on the stack
if(node->rchild != NULL) {
push_stack(t_stack,node->rchild);
}
if(node->lchild != NULL) {
push_stack(t_stack,node->lchild);
}
// Insert the pop-up node
push_stack(t_stack,node);
}
destroy_stack(t_stack);
}
int main()
{
// Here, create the node
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};
// Connect the nodes to each other
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);
// Wear
return 0;
}
result :

边栏推荐
- 16 excellent business process management tools
- Apple doesn't need money, but it has no confidence in its content
- Open programmable infrastructure (OPI) project, redefining dpu/ipu
- 顺序栈1.0版本
- The difference between the lazy man mode and the hungry man mode
- Teach you how to cancel computer hibernation
- Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial
- Openstack actual installation and deployment tutorial, openstack installation tutorial
- Bytebase 加入阿裏雲 PolarDB 開源數據庫社區
- It is said that Tencent officially announced the establishment of "XR" department to bet on yuanuniverse; Former CEO of Google: the United States is about to lose the chip competition. We should let T
猜你喜欢

宅男救不了元宇宙

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

二叉树的基本性质与遍历

Bytebase 加入阿里云 PolarDB 开源数据库社区

天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?

The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022

Test drive citus 11.0 beta (official blog)

图像PANR

"Super point" in "Meng Hua Lu", is the goose wronged?

Q1: error in JMeter filename must not be null or empty
随机推荐
Some small requirements for SQL Engine for domestic database manufacturers
Apache+PHP+MySQL环境搭建超详细!!!
unity之模糊背景(带你欣赏女人的朦胧美)
Data backup and recovery of PgSQL
Confirm whether the host is a large terminal or a small terminal
字节、腾讯也下场,这门「月赚3000万」的生意有多香?
How does the video platform import the old database into the new database?
OpenVINO2022 Dev Tools安装与使用
The difference between the lazy man mode and the hungry man mode
Jd.com: how does redis implement inventory deduction? How to prevent oversold?
"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"
The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
RF_DC系统时钟设置GEN1/GEN2
等等党们的胜利!挖矿退潮后,显卡价格全面暴跌
The largest DPU manufacturer in history (Part 1)
年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
Otaku can't save yuan universe
图的基本概念以及相关定义