当前位置:网站首页>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();
}
边栏推荐
- JVM之方法区
- [environment configuration]pwc-net
- AGCO AI frontier promotion (6.29)
- Interview shock 61: tell me about MySQL transaction isolation level?
- Principle and process of MySQL master-slave replication
- Gbase8s database select has order by Clause 6
- 推荐模型复现(二):精排模型DeepFM、DIN
- Proteus软件初学笔记
- Gbase8s database select has an order by clause
- 超 Nice 的表格响应式布局小技巧
猜你喜欢

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

Can software related inventions be patented in India?

After class assignment of module 5 of the construction practice camp

ERP preparation of BOM basis

Difficult conversation breaks through the bottleneck of conversation and achieves perfect communication

Qt的信号与槽

推荐模型复现(四):多任务模型ESMM、MMOE

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

C # indexe l'arbre binaire en traversant l'ordre moyen

多项目开发入门-业务场景关联基础入门测试 工资表
随机推荐
ERP preparation of bill of materials Huaxia
二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
Qt中的UI文件介绍
Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing
[leetcode] 14. Longest public prefix
测试--自动化测试:关于unittest框架
LeetCode_ Double pointer_ Medium_ 328. parity linked list
Pangolin编译error: ‘numeric_limits’ is not a member of ‘std’
Adjacency matrix and adjacency table structure of C # realization graph
QQ集体被盗号,猝不及防的大型社死名场面
[JUC series] ThreadLocal of synchronization tool class
oracle 19c : change the user sys/system username pasword under Linux
高校如何基于云原生构建面向未来的智慧校园?全栈云原生架构VS传统IT架构
面试突击61:说一下MySQL事务隔离级别?
Murphy safety was selected for signing 24 key projects of Zhongguancun Science City
Interview shock 61: tell me about MySQL transaction isolation level?
2022.6.28-----leetcode. three hundred and twenty-four
Golang image/png 处理图片 旋转 写入
C#通过中序遍历对二叉树进行线索化
倍福TwinCAT3 的OPC_UA通信测试案例