当前位置:网站首页>C # realize the hierarchical traversal of binary tree
C # realize the hierarchical traversal of binary tree
2022-06-29 12:53:00 【Dusk and starry sky】
This article explains C# Realize the level traversal of binary tree
1、 /// Level traversal
///
/// Level traversal
///
///
static void LevelOrder(BiTNode T) {
SqQueue Q = new SqQueue();
Q.data = new BiTNode[MaxSize];
InitQueue(ref Q);
BiTNode p = null;
EnQueue(ref Q, T);
while (!isEmpty(Q)) {
DeQueue(ref Q, ref p);
visit§;
if (p.lchild!=null) { EnQueue(ref Q, p.lchild); }
if (p.rchild!=null) { EnQueue(ref Q,p.rchild); }
}
}
2、 Node value access
///
/// Output of node value
///
static void visit(BiTNode T) {
Console.Write(T.data+" ");
}
3、 Queue structure
///
/// Queue structure definition
///
public struct SqQueue
{
public BiTNode[] data;// Queued elements
public int front, real;// Team leader and team leader
}
///
/// Queue initialization
///
///
static void InitQueue(ref SqQueue Q)
{
Q.real = Q.front = 0;
}
///
/// Determines if the queue is empty
///
///
///
static bool isEmpty(SqQueue Q)
{
if (Q.front == Q.real) { return true; }
else return false;
}
///
/// The team
///
///
///
///
static bool EnQueue(ref SqQueue Q, BiTNode x)
{
if ((Q.real + 1) % MaxSize == Q.front) { return false; }
Q.data[Q.real] = x;
Q.real = (Q.real + 1) % MaxSize;
return true;
}
///
/// Out of the team
///
///
///
///
static bool DeQueue(ref SqQueue Q, ref BiTNode x)
{
if (Q.real == Q.front) { return false; }
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
4、 actual main Function test
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();
}
边栏推荐
- C#线索二叉树的定义
- Murphy safety was selected for signing 24 key projects of Zhongguancun Science City
- Recurrence of recommended models (IV): multi task models esmm and MMOE
- Gbase8s database select has order by Clause 2
- [environment configuration]pwc-net
- MFC-对话框程序核心-IsDialogMessage函数-MSG 消息结构-GetMessage函数-DispatchMessage函数
- Method area of JVM
- [intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
- Gbase8s database into table clause
- Huffman coding
猜你喜欢

How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture

倍福TwinCAT3 的OPC_UA通信测试案例

Qt的信号与槽
![[leetcode] 14. Longest public prefix](/img/3b/3388ce8382ad5caaaf0a42488da2f9.png)
[leetcode] 14. Longest public prefix

推荐模型复现(一):熟悉Torch-RecHub框架与使用

Bison uses error loop records

C # clue binary tree through middle order traversal

AGCO AI frontier promotion (6.29)

QT custom control: value range

测试--自动化测试:关于unittest框架
随机推荐
ERP preparation of bill of materials Huaxia
QQ group was stolen, a large-scale social death scene caught off guard
How to calculate win/tai/loss in paired t-test
推荐模型复现(四):多任务模型ESMM、MMOE
Inferiority complex and transcendence the meaning of life to you
Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)
Gbase 8s extended external connection 1
MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
NvtBack
Gbase8s database select has a having clause
ERP preparation of BOM basis
Interview shock 61: tell me about MySQL transaction isolation level?
Understanding of P value
Aes-128-cbc-pkcs7padding encrypted PHP instance
Comment calculer Win / Tai / Loss in paired t - test
Mysql database master-slave synchronization, consistency solution
倍福PLC通过CANOpen通信控制伺服
Gbase8s database select has order by Clause 3
Newton inequality
在印度与软件相关的发明可不可以申请专利?