当前位置:网站首页>C#实现二叉树的层次遍历
C#实现二叉树的层次遍历
2022-06-29 12:05:00 【黄昏和星空】
本文讲解C#实现二叉树的层次遍历
1、 /// 层次遍历
///
/// 层次遍历
///
///
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、节点值访问
///
/// 结点值的输出
///
static void visit(BiTNode T) {
Console.Write(T.data+" ");
}
3、队列结构
///
/// 队列结构体定义
///
public struct SqQueue
{
public BiTNode[] data;//队列存放的元素
public int front, real;//队头和队尾指针
}
///
/// 队列初始化
///
///
static void InitQueue(ref SqQueue Q)
{
Q.real = Q.front = 0;
}
///
/// 判断队列是否为空
///
///
///
static bool isEmpty(SqQueue Q)
{
if (Q.front == Q.real) { return true; }
else return false;
}
///
/// 入队
///
///
///
///
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;
}
///
/// 出队
///
///
///
///
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、实际main函数测试
public const int MaxSize = 50;
static void Main(string[] args)
{
BiTNode T = new BiTNode() ;
int x=0;
//创建二叉树
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(“先序遍历的值:”);
PreOrder(T);
Console.WriteLine();
Console.WriteLine(“中序遍历的值:”);
InOrder(T);
Console.WriteLine();
Console.WriteLine(“后序遍历的值:”);
PostOrder(T);
Console.WriteLine();
Console.WriteLine(“非递归中序遍历的值:”);
InOrder2(T);
Console.WriteLine();
Console.WriteLine(“层次遍历的值:”);
LevelOrder(T);
Console.ReadLine();
}
边栏推荐
- Huffman coding
- 535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
- Gbase8s database select has order by Clause 2
- 牛顿不等式
- How to create new user for ORACLE 19c (CDB & PDB)
- 推荐模型复现(二):精排模型DeepFM、DIN
- 23、 1-bit data storage (delay line / core /dram/sram/ tape / disk / optical disc /flash SSD)
- AES-128-CBC-Pkcs7Padding加密PHP实例
- Gbase8s database select has order by Clause 1
- 对p值的理解
猜你喜欢

AES-128-CBC-Pkcs7Padding加密PHP实例

Problem solving: modulenotfounderror: no module named 'pip‘

Bison uses error loop records

地球观测卫星数据

File contained log poisoning (user agent)

MIT线性代数中文笔记

QQ group was stolen, a large-scale social death scene caught off guard

MySQL 主从复制原理以及流程

在印度与软件相关的发明可不可以申请专利?

Interpolated scatter data
随机推荐
推荐模型复现(四):多任务模型ESMM、MMOE
智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中
[cloud native] 2.4 kubernetes core practice (middle)
Gbase 8s extended external connection 1
Nacos startup error
《自卑与超越》生活对你应有的意义
【LeetCode】14、最长公共前缀
Difficult conversation breaks through the bottleneck of conversation and achieves perfect communication
Gbase8s database select has order by Clause 2
Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing
Weekly recommended short video: How did Einstein think?
解决问题:ModuleNotFoundError: No module named ‘pip‘
多项目开发入门-业务场景关联基础入门测试 工资表
ZALSM_EXCEL_TO_INTERNAL_TABLE 导入数据大问题解决
Gbase8s database select has an order by clause
Huffman coding
Pygame 对图像进行翻转
超 Nice 的表格响应式布局小技巧
InDesign plug-in - general function development -js debugger open and close -js script development -id plug-in
Qt的信号与槽