当前位置:网站首页>C language version: traversal mode and reverse order of binary tree
C language version: traversal mode and reverse order of binary tree
2022-04-22 05:28:00 【Li Feiyu_ smile】
C Language version : Binary tree in data structure , The first sequence traversal , In the sequence traversal , After the sequence traversal , And their reverse order .
List of articles
Traversal of binary tree
The traversal of binary tree : The first sequence traversal : Around the head , In the sequence traversal : Left head right , After the sequence traversal : Left and right The rules of , The following is a diagram of the binary tree of this article .

Tips : The following is the main body of this article , The following cases can be used for reference
One 、 The use of stacks
Stack operation rules : First in, then out , For example, binary conversion is also an application of stack , The previous article introduced the conversion of binary system , The reverse order of binary tree can use stack .
Two 、 Traverse the way
1. The first sequence traversal
The code is as follows ( Example ):
// positive sequence
void perorder(RTreeNode* p)
{
if (p != NULL)
{
printf("->%c", p->data);
perorder(p->lchild);
perorder(p->rchild);
}// The first sequence traversal
}
// The reverse
void sperorder(RStack* S, RTreeNode* p)
{
if (p != NULL)
{
push(S, p->data);// Into the stack
sperorder(S, p->lchild);
sperorder(S, p->rchild);
}
}// Tree traversal requires recursion , The condition for ending recursion is p Point to empty time
// Print the results in reverse order
void treeout(RStack p)
{
while (!empty(&p))
{
printf("->%c", pop(&p));
}
}
2. In the sequence traversal
The code is as follows ( Example ):
// positive sequence
void perorder(RTreeNode* p)
{
if (p != NULL)
{
perorder(p->lchild);
printf("->%c", p->data);
perorder(p->rchild);
}// The first sequence traversal
}
// The reverse
void sperorder(RStack* S, RTreeNode* p)
{
if (p != NULL)
{
sperorder(S, p->lchild);
push(S, p->data);// Into the stack
sperorder(S, p->rchild);
}
}// Tree traversal requires recursion , The condition for ending recursion is p Point to empty time
// Print the results in reverse order
void treeout(RStack p)
{
while (!empty(&p))
{
printf("->%c", pop(&p));
}
}
3. After the sequence traversal
// positive sequence
void perorder(RTreeNode* p)
{
if (p != NULL)
{
perorder(p->lchild);
perorder(p->rchild);
printf("->%c", p->data);
}// The first sequence traversal
}
// The reverse
void sperorder(RStack* S, RTreeNode* p)
{
if (p != NULL)
{
sperorder(S, p->lchild);
sperorder(S, p->rchild);
push(S, p->data);// Into the stack
}
}// Tree traversal requires recursion , The condition for ending recursion is p Point to empty time
// Print the results in reverse order
void treeout(RStack p)
{
while (!empty(&p))
{
printf("->%c", pop(&p));
}
}
Code implementation
// The positive and negative order of subsequent traversal are examples
//
// Created by xhh on 2021/6/30.
//
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#include<string>
#define MAX 50
typedef struct TreeNode
{
struct TreeNode* lchild, * rchild; // Left and right subtree pointers
char data; // Elements in nodes
}RTreeNode;
// Initialize a tree
RTreeNode* initnode(char x, RTreeNode* lchild, RTreeNode* rchild)
{
RTreeNode* SS;
SS = (struct TreeNode*)malloc(sizeof(struct TreeNode));
SS->data = x;
SS->lchild = lchild;
SS->rchild = rchild;
return SS;
}
// Define a stack
typedef struct Stack
{
int top; // Shaping variables is convenient for addition and subtraction , Easy to move
char sa[MAX]; // Maximum stack space
}RStack;
// Initialize a stack
RStack init()
{
RStack* SS;
SS = (struct Stack*)malloc(sizeof(struct Stack)); // Dynamically allocate memory space
SS->top = 0;
return *SS;
}
// Into the stack
void push(RStack *S, char x)
{
if (S->top == MAX)
{
printf("the stack is full");
exit(0);
}
S->sa[S->top] = x; //top Point to empty stack , So get into the position it points to
S->top++;
}
// Out of the stack
char pop(RStack* S)
{
if (S->top == 0) // Empty stack or not
{
printf(" The stack is empty ");
exit(0);
}
return S->sa[--S->top];
}
// Judge stack empty
int empty(RStack* S)
{
if (S->top == 0)
{
return 1;
}
return 0;
}
// Empty stack elements
void clear(RStack* S)
{
S->top = 0;
}
// Make a tree
RTreeNode* inittree()
{
RTreeNode* root, * b, * c, * d, * e, * f;
d = initnode('D', NULL, NULL);
e = initnode('E', NULL, NULL);
f = initnode('F', NULL, NULL);
b = initnode('B', d, NULL);
c = initnode('C', e, f);
root = initnode('A', b, c); // There is only one following node , And put it last
return(root);
}
// positive sequence
void perorder(RTreeNode* p)
{
if (p != NULL)
{
perorder(p->lchild);
perorder(p->rchild);
printf("->%c", p->data);
}// The first sequence traversal
}
// The reverse
void sperorder(RStack* S, RTreeNode* p)
{
if (p != NULL)
{
sperorder(S, p->lchild);
sperorder(S, p->rchild);
push(S, p->data);// Into the stack
}
}// Tree traversal requires recursion , The condition for ending recursion is p Point to empty time
void treeout(RStack p)
{
while (!empty(&p))
{
printf("->%c", pop(&p));
}
}
int main()
{
RTreeNode* tree;
tree = inittree(); // Make a tree
RStack SS;
SS = init(); // Generated a stack
perorder(tree);
printf("\n");
sperorder(&SS, tree);
treeout(SS);
return 0;
}
The subsequent traversal result is :
positive sequence :
->D->B->E->F->C->A
The reverse :
->A->C->F->E->B->D
summary : Preface , Middle preface , The way of post order traversal only needs to printf Function and recursive traversal can be changed , The same is true in reverse order .
版权声明
本文为[Li Feiyu_ smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210618260129.html
边栏推荐
- mysql表删除重复值,只保留一个
- [WPF] custom control
- 【CANdelaStudio编辑CDD】-2.3-实现$27服务多个SecurityLevel之间的跳转(UDS诊断)
- [WPF] making navigation bar with RadioButton
- 【WPF】UpdateSourceTrigger
- Mengxin sees the recruitment of volunteers in the open source community of wedatasphere
- Use render texture to display 3D model animation on the UI
- [untitled] gbase 8s V8 8 SQL Guide: tutorial-6
- Temporary data node usage based on unitygameframework framework
- Interpreter mode (3.7-3.13)
猜你喜欢

Supporting early and scalable discovery of information websites

The eleventh job of MySQL database - Application of view

【WPF】UpdateSourceTrigger

Realization of mathematical function curve editor with minscript script language

IT配电及防火限流式保护器应用及选型

用minscript脚本语言实现数学函数曲线编辑器

Spark 之 unsafeRow

Visitor mode from 2022-1-3 to 2022-1-16
![[WPF] custom control](/img/33/c0b467805162a57966967b09eec5f0.png)
[WPF] custom control

Feign calls the service, and the called service Seata transaction is not opened or XID is empty
随机推荐
Topology optimization of heat transfer based on finite volume method
IO流..
Leetcode 1561. Maximum number of coins you can get
Codeforces round 783 (Div. 2) d - optimal partition (DP / weight segment tree 2100)
Machine learning -- under fitting, over fitting and regularization
Von Neumann architecture
我的创作纪念日
Unity about the real machine failure of ispointerovergameobject interface
Access denied for user ‘root‘@‘% mysql在问题(解决)
Defining "disinformation"
【无标题】GBase 8s V8.8 SQL 指南:教程-6
[network protocol] why learn network protocol
MySQL installation and configuration - detailed tutorial
Security challenges in an increasingly tangled web
GBase 8s V8.8 SQL 指南:教程-6.1.1(1)
水处理控制系统采用信号隔离器解决因某些现场非电量安装条件的限制问题
Codeforces Round #781 (Div. 2) ABCD
GBase 8s V8.8 SQL 指南:教程-5.3.1
Sourcetree version backtracking and single change version backtracking
Ezyslice cut characters