当前位置:网站首页>C # realizes the first order traversal, middle order traversal and second order traversal of binary tree
C # realizes the first order traversal, middle order traversal and second order traversal of binary tree
2022-06-29 12:53:00 【Dusk and starry sky】
In this paper, C# The implementation of binary tree traversal 、 In the sequence traversal 、 After the sequence traversal
1、 The first sequence traversal
///
/// The first sequence traversal
///
///
static void PreOrder(BiTNode T) {
if (T!=null) {
visit(T);
PreOrder(T.lchild);
PreOrder(T.rchild);
}
2、 In the sequence traversal
///
/// In the sequence traversal
///
///
static void InOrder(BiTNode T)
{
if (T != null)
{
InOrder(T.lchild);
visit(T);
InOrder(T.rchild);
}
}
3、 After the sequence traversal
///
/// After the sequence traversal
///
///
static void PostOrder(BiTNode T)
{
if (T != null)
{
PostOrder(T.lchild);
PostOrder(T.rchild);
visit(T);
}
}
4、 Node access
///
/// Output of node value
///
static void visit(BiTNode T) {
Console.Write(T.data+" ");
}
The test of main Function as follows
public const int MaxSize = 50;
static void Main(string[] args)
{
BiTNode T = new BiTNode() ;
int x=0;
// Create a binary tree
T.data = 1;
T.lchild= new BiTNode();
T.lchild.data = 2;
T.rchild = new BiTNode();
T.rchild.data = 3;
T.lchild.rchild=new BiTNode();
T.lchild.rchild.data = 4;
T.lchild.rchild.lchild= new BiTNode();
T.lchild.rchild.lchild.data = 6;
T.rchild.rchild = new BiTNode();
T.rchild.rchild.data = 5;
Console.WriteLine(“ The value of traversal first :”);
PreOrder(T);
Console.WriteLine();
Console.WriteLine(“ The value of middle order traversal :”);
InOrder(T);
Console.WriteLine();
Console.WriteLine(“ The value of post order traversal :”);
PostOrder(T);
Console.WriteLine();
Console.WriteLine(“ The value of an ordered traversal in non recursion :”);
InOrder2(T);
Console.WriteLine();
Console.WriteLine(“ Value of hierarchy traversal :”);
LevelOrder(T);
Console.ReadLine();
}
边栏推荐
- Huffman coding
- C#实现图的邻接矩阵和邻接表结构
- Gbase8s database select has order by Clause 6
- Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)
- Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing
- Murphy safety was selected for signing 24 key projects of Zhongguancun Science City
- Gbase8s database select has order by Clause 6
- LeetCode_双指针_中等_328.奇偶链表
- Understanding of P value
- Gbase8s database sorts standard or raw result tables
猜你喜欢

智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中

How to create new user for ORACLE 19c (CDB & PDB)

Qt中的UI文件介绍

QQ集体被盗号,猝不及防的大型社死名场面

AGCO AI frontier promotion (6.29)

Blurred pictures become clear, one button two-color pictures, quickly organize local pictures These 8 online picture tools apply to join your favorites!

LeetCode_ Double pointer_ Medium_ 328. parity linked list

Cereal mall project

How to create new user for ORACLE 19c (CDB & PDB)

Earth observation satellite data
随机推荐
中职网络安全技能竞赛之应用服务漏洞扫描与利用(SSH私钥泄露)
1. Opencv实现简单颜色识别
Viewing splitchunks code segmentation from MPX resource construction optimization
[Junzheng T31] decompression and packaging of read-only rootfs file system squashfs
Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
Cmake error
【君正T31】只读rootfs文件系统squashfs的解压和打包
C#实现二叉树的层次遍历
1. opencv realizes simple color recognition
Gbase 8s extended external connection 1
Factorization of large numbers ← C language
在印度与软件相关的发明可不可以申请专利?
nacos启动报错
Interview shock 61: tell me about MySQL transaction isolation level?
535. encryption and decryption of tinyurl: design a URL simplification system
Go question bank · 14 the pit of waitgroup
C#实现图的邻接矩阵和邻接表结构
ERP preparation of BOM basis
Gbase8s database select has order by Clause 3
多项目开发入门-业务场景关联基础入门测试 工资表