当前位置:网站首页>Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
2022-07-03 08:50:00 【Cap07】
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
typedef struct BiTNode {
DataType data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
BiTree CreateBiTree() { // Create a binary tree ( Pre order creation )
BiTree T;
char ch;
scanf("%c", &ch);
if (ch == '#') T = NULL; // With ‘#’ Indicates that the location is empty
else {
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T; // Return root node
}
void PreOrderTraverse(BiTree T) // Traversing a binary tree in order
{
if (!T) return;
printf("%c", T->data);
PreOrderTraverse(T->lchild); // Recursively traverses the left subtree
PreOrderTraverse(T->rchild); // Recursively traverses the right subtree
}
void InOrderTraverse(BiTree T) // Middle order ergodic binary tree
{
if (!T) return;
InOrderTraverse(T->lchild);
printf("%c", T->data);
InOrderTraverse(T->rchild);
}
void RearOrderTraverse(BiTree T) // Post order traversal binary tree
{
if (!T) return;
RearOrderTraverse(T->lchild);
RearOrderTraverse(T->rchild);
printf("%c", T->data);
}
int main()
{
BiTree T;
printf(" Please input the binary tree in the first order , For vacant positions “#” Instead of :\n");
T = CreateBiTree();
PreOrderTraverse(T);
printf("\n");
InOrderTraverse(T);
printf("\n");
RearOrderTraverse(T);
return 0;
}test result :

边栏推荐
- Annotations simplify configuration and loading at startup
- 单调栈-84. 柱状图中最大的矩形
- Deep parsing (picture and text) JVM garbage collector (II)
- MySQL 8
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- How to deal with the core task delay caused by insufficient data warehouse resources
- 分配异常的servlet
- [rust notes] 08 enumeration and mode
- Get the link behind? Parameter value after question mark
- Concurrent programming (III) detailed explanation of synchronized keyword
猜你喜欢
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

Deep parsing JVM memory model

Really explain the five data structures of redis

Parameters of convolutional neural network

Annotations simplify configuration and loading at startup

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

Monotonic stack -503 Next bigger Element II

Unity editor expansion - controls, layouts

Life cycle of Servlet

UE4 source code reading_ Bone model and animation system_ Animation process
随机推荐
Creation and content of mapnode -- osgearth rendering engine series (2)
DOM 渲染系统(render mount patch)响应式系统
请求参数的发送和接收
Osgearth target selection
First Servlet
Explain sizeof, strlen, pointer, array and other combination questions in detail
Get the link behind? Parameter value after question mark
Binary to decimal, decimal to binary
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
Find the intersection of line segments
20220630学习打卡
Es8 async and await learning notes
URL backup 1
Visual Studio (VS) shortcut keys
Unity editor expansion - the framework and context of unity imgui
[concurrent programming] concurrent security
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
ES6 promise learning notes
MySQL 8
Location of package cache downloaded by unity packagemanager