当前位置:网站首页>C#实现二叉树非递归中序遍历程序
C#实现二叉树非递归中序遍历程序
2022-06-29 12:05:00 【黄昏和星空】
本文讲解C#实现二叉树非递归中序遍历程序
1、非递归中序遍历
///
/// 非递归中序遍历
///
///
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、节点访问
///
/// 结点值的输出
///
static void visit(BiTNode T) {
Console.Write(T.data+" ");
}
3、栈结构的实现
///
/// 栈定义
///
public struct SqStack
{
public BiTNode[] data;
public int top;//栈顶
}
/// <summary>
/// 判断栈是否为空
/// </summary>
/// <param name=""></param>
/// <returns></returns>
static bool StackEmpty(SqStack S)
{
if (S.top == -1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 栈初始化
/// </summary>
/// <param name="S"></param>
static void InitStack(ref SqStack S)
{
S.top = -1;
}
/// <summary>
/// 压栈
/// </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;//先加1,再进栈
return true;
}
/// <summary>
/// 出栈
/// </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--];//出栈
return true;
}
/// <summary>
///
///读取栈顶元素
/// </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];//读取元素
return true;
}
5、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();
}
边栏推荐
- 测试--自动化测试:关于unittest框架
- Comment calculer Win / Tai / Loss in paired t - test
- LR、CR纽扣电池对照表
- JVM之方法区
- 墨菲安全入选中关村科学城24个重点项目签约
- An interpretable geometric depth learning model for structure based protein binding site prediction
- 超 Nice 的表格响应式布局小技巧
- How to calculate win/tai/loss in paired t-test
- Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)
- [Junzheng T31] decompression and packaging of read-only rootfs file system squashfs
猜你喜欢

Qt的信号与槽

AGCO AI frontier promotion (6.29)

【综合案例】信用卡虚拟交易识别

Weekly recommended short video: How did Einstein think?

参加2022年杭州站Cocos Star Meetings

Problem solving: modulenotfounderror: no module named 'pip‘

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

Understanding of P value
![[comprehensive case] credit card virtual transaction identification](/img/85/9915ab9a6100a022dfa1c0050d554f.png)
[comprehensive case] credit card virtual transaction identification

How to install oracle19c in Centos8
随机推荐
Cache consistency, delete cache, write cache, cache breakdown, cache penetration, cache avalanche
1. opencv realizes simple color recognition
MIT线性代数中文笔记
缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩
求大数的阶乘 ← C语言
二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
Deep understanding of volatile keyword
[环境配置]PWC-Net
Gbase8s database sorts standard or raw result tables
Codeforces Round #803 (Div. 2)
How to fix ORA-01017:用户名/口令无效 登录拒绝
NvtBack
《自卑与超越》生活对你应有的意义
qt json
535. encryption and decryption of tinyurl: design a URL simplification system
[cloud native] 2.4 kubernetes core practice (middle)
Recommended model recurrence (I): familiar with torch rechub framework and use
推荐模型复现(一):熟悉Torch-RecHub框架与使用
Is it safe for Orient Fortune Securities to open an account? Handling of securities account opening
Matlab简单入门