当前位置:网站首页>C # implementation of binary tree non recursive middle order traversal program
C # implementation of binary tree non recursive middle order traversal program
2022-06-29 12:53:00 【Dusk and starry sky】
This article explains C# Implementation of binary tree non recursive middle order traversal program
1、 Non recursive middle order traversal
///
/// Non recursive middle order traversal
///
///
static void InOrder2(BiTNode T) {
SqStack S = new SqStack();
S.data = new BiTNode[MaxSize];
InitStack(ref S);
BiTNode p = T;
while (p!=null||!StackEmpty(S)) {
if (p != null)
{
Push(ref S, p);
p = p.lchild;
}
else {
PoP(ref S, ref p);
visit§;
p = p.rchild;
}
}
}
2、 Node access
///
/// Output of node value
///
static void visit(BiTNode T) {
Console.Write(T.data+" ");
}
3、 Implementation of stack structure
///
/// Stack definition
///
public struct SqStack
{
public BiTNode[] data;
public int top;// To the top of the stack
}
/// <summary>
/// Judge whether the stack is empty
/// </summary>
/// <param name=""></param>
/// <returns></returns>
static bool StackEmpty(SqStack S)
{
if (S.top == -1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Stack initialization
/// </summary>
/// <param name="S"></param>
static void InitStack(ref SqStack S)
{
S.top = -1;
}
/// <summary>
/// Pressing stack
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
static bool Push(ref SqStack S, BiTNode e)
{
if (S.top >= MaxSize - 1)
{
return false;
}
S.top = S.top + 1;
S.data[S.top] = e;// First plus 1, Go back to the stack
return true;
}
/// <summary>
/// Out of the stack
/// </summary>
/// <param name="S"></param>
/// <param name="e"></param>
/// <returns></returns>
static bool PoP(ref SqStack S, ref BiTNode e)
{
if (S.top == -1) { return false; }
e = S.data[S.top--];// Out of the stack
return true;
}
/// <summary>
///
/// Read the top element of the stack
/// </summary>
/// <param name="S"></param>
/// <param name="e"></param>
/// <returns></returns>
bool GetTop(ref SqStack S, ref BiTNode e)
{
if (S.top == -1) { return false; }
e = S.data[S.top];// Read elements
return true;
}
5、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();
}
边栏推荐
- 535. encryption and decryption of tinyurl: design a URL simplification system
- 推荐模型复现(一):熟悉Torch-RecHub框架与使用
- C#通过线索二叉树进行中序遍历输出
- nacos启动报错
- 1. Opencv实现简单颜色识别
- 倍福TwinCAT配置、调试第三方伺服详细讲解--以汇川IS620N为例子
- Adjacency matrix and adjacency table structure of C # realization graph
- C # indexe l'arbre binaire en traversant l'ordre moyen
- 超 Nice 的表格响应式布局小技巧
- 揭秘百度智能测试在测试自动执行领域实践
猜你喜欢

File contained log poisoning (user agent)

Murphy safety was selected for signing 24 key projects of Zhongguancun Science City

Aes-128-cbc-pkcs7padding encrypted PHP instance
![[JUC series] ThreadLocal of synchronization tool class](/img/15/2f8ce68b9e5ee8dab03fb688712935.png)
[JUC series] ThreadLocal of synchronization tool class

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

Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)

1. opencv realizes simple color recognition

Interview shock 61: tell me about MySQL transaction isolation level?

中职网络安全技能竞赛之应用服务漏洞扫描与利用(SSH私钥泄露)

Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
随机推荐
【LeetCode】14、最长公共前缀
Recommended model reproduction (II): fine arrangement model deepfm, DIN
Gbase 8s extended external connection 1
内插散点数据
Bison uses error loop records
MIT linear algebra Chinese Notes
After class assignment of module 5 of the construction practice camp
Cereal mall project
Interview shock 61: tell me about MySQL transaction isolation level?
Yunlong fire version aircraft battle (full version)
535. encryption and decryption of tinyurl: design a URL simplification system
Set operator of gbase8s database in combined query
ERP Kingdee for preparing BOM
C # clue binary tree through middle order traversal
求大数的阶乘 ← C语言
C#线索二叉树的定义
面试突击61:说一下MySQL事务隔离级别?
Viewing splitchunks code segmentation from MPX resource construction optimization
Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released
Murphy safety was selected for signing 24 key projects of Zhongguancun Science City