当前位置:网站首页>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();
}
边栏推荐
- Newton inequality
- Gbase8s database select has a having clause
- Gbase8s database select has order by Clause 6
- 23、 1-bit data storage (delay line / core /dram/sram/ tape / disk / optical disc /flash SSD)
- 深入理解 volatile 关键字
- Cereal mall project
- 解决问题:ModuleNotFoundError: No module named ‘pip‘
- Gbase8s database select has order by Clause 6
- C # output the middle order traversal through the clue binary tree
- Proteus Software beginner notes
猜你喜欢
![[cloud native] 2.4 kubernetes core practice (middle)](/img/1e/b1b22caa03d499387e1a47a5f86f25.png)
[cloud native] 2.4 kubernetes core practice (middle)

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

oracle 19c : change the user sys/system username pasword under Linux

Testing -- automated testing: about the unittest framework

Method area of JVM

Cache consistency, delete cache, write cache, cache breakdown, cache penetration, cache avalanche

如何計算win/tai/loss in paired t-test

Uncover the practice of Baidu intelligent test in the field of automatic test execution

How to install oracle19c in Centos8

Problem solving: modulenotfounderror: no module named 'pip‘
随机推荐
Cocos star meetings at Hangzhou station in 2022
qt json
Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
go 学习-搭建开发环境vscode开发环境golang
Inferiority complex and transcendence the meaning of life to you
解决问题:ModuleNotFoundError: No module named ‘pip‘
2022.6.28-----leetcode. three hundred and twenty-four
Recurrence of recommended models (III): recall models youtubednn and DSSM
[Junzheng T31] decompression and packaging of read-only rootfs file system squashfs
Gbase8s database into external clause
qt 自定义控件 :取值范围
File contained log poisoning (user agent)
InDesign插件-常规功能开发-JS调试器打开和关闭-js脚本开发-ID插件
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
Set operator of gbase8s database in combined query
LR、CR纽扣电池对照表
How to install oracle19c in Centos8
Earth observation satellite data
[cloud native] 2.4 kubernetes core practice (middle)
求大数的阶乘 ← C语言