当前位置:网站首页>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();
}
边栏推荐
- 【LeetCode】14、最长公共前缀
- After class assignment of module 5 of the construction practice camp
- Earth observation satellite data
- nacos启动报错
- Pygame 对图像进行翻转
- Inferiority complex and transcendence the meaning of life to you
- Gbase8s database select has order by Clause 6
- [cloud native] 2.4 kubernetes core practice (middle)
- Gbase8s database select has order by Clause 4
- 测试--自动化测试:关于unittest框架
猜你喜欢

Can software related inventions be patented in India?

墨菲安全入选中关村科学城24个重点项目签约

Unexpected ‘debugger‘ statement no-debugger

Recurrence of recommended models (IV): multi task models esmm and MMOE

高校如何基于云原生构建面向未来的智慧校园?全栈云原生架构VS传统IT架构

模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!

Proteus软件初学笔记

Go高级工程师必修课 | 真心建议你来听听,别错过~

Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection

Problem solving: modulenotfounderror: no module named 'pip‘
随机推荐
NvtBack
Interpolated scatter data
Gbase8s database select has order by Clause 6
535. encryption and decryption of tinyurl: design a URL simplification system
面试突击61:说一下MySQL事务隔离级别?
Set operator of gbase8s database in combined query
地球观测卫星数据
Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
Gbase8s database into standard and into raw clauses
Gbase8s database for read only clause
Interview shock 61: tell me about MySQL transaction isolation level?
面试突击61:说一下MySQL事务隔离级别?
ERP Kingdee for preparing BOM
[cloud native] 2.4 kubernetes core practice (middle)
Gbase8s database select has a having clause
Pygame 精准检测图像碰撞
MFC dialog program core -isdialogmessage function -msg message structure -getmessage function -dispatchmessage function
oracle 19c : change the user sys/system username pasword under Linux
nvtmpp
MySQL主从同步之 异步复制 半同步复制 全同步复制