当前位置:网站首页>C # output the middle order traversal through the clue binary tree
C # output the middle order traversal through the clue binary tree
2022-06-29 12:47:00 【Dusk and starry sky】
The procedure is as follows
Build a threaded binary tree , Or to clue binary trees , It's essentially traversing a binary tree . In the process of traversing , The operation of accessing a node is to check the current left , Whether the right pointer field is empty , Change them to clues to the precursor node or the successor node . To achieve this process , Set pointer pre Always point to the node you just visited , If the pointer p Point to the current node , be pre Point to its precursor , To set up clues .
in addition , When adding a cable to a binary tree , You must first apply for a header , Establish the direction relationship between the head node and the root node of the binary tree , After the binary tree is threaded , We also need to establish the clue between the last node and the head node .
The following is a recursive algorithm for building a medium order binary tree , among pre Is a global variable .
///
/// Cue binary tree through middle order traversal
///
///
///
static void InThread(ref ThreadNode p, ref ThreadNode pre)
{
if (p != null)
{
InThread(ref p.lchild,ref pre);
if (p.lchild == null) { p.lchild = pre; p.ltag = 1; }
if (pre!=null&&pre.rchild==null) { pre.rchild = p;pre.rtag = 1; }
pre = p;
InThread(ref p.rchild, ref pre);
}
}
///
/// Create clue binary tree main logic
///
///
static void CreateInThread(ref ThreadNode T) {
ThreadNode pre = null;
if (T!=null) {
InThread(ref T,ref pre);
pre.rchild = null;
pre.rtag = 1;
}
}
/// <summary>
/// Find the first node of the middle order traversal clue binary tree
/// </summary>
/// <returns></returns>
static ThreadNode FirstNode(ThreadNode T) {
while (T.lchild!=null&&T.ltag==0) { T = T.lchild; }
return T;
}
/// <summary>
/// Find the nodes in the middle order clue binary tree p Next, the following nodes under the ordered sequence
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
static ThreadNode Nextnode(ThreadNode p) {
if (p.rtag==0) { return FirstNode(p.rchild); }else {
return p.rchild;
}
}
/// <summary>
/// The middle order traversal is realized through the middle order clue binary tree
/// </summary>
/// <param name="T"></param>
static void Inorder(ThreadNode T) {
for (ThreadNode p=FirstNode(T);p!=null;p=Nextnode(p)) {
Console.WriteLine(p.data);
}
}
}
public class ThreadNode{
public ThreadNode() { }
public int data;// data elements
public ThreadNode lchild, rchild;// Left and right child nodes
public int ltag, rtag;// Left and right cue marks
}
Bold style
边栏推荐
- Cocos star meetings at Hangzhou station in 2022
- asp.net 项目使用aspnet_compiler.exe发布
- Mysql database master-slave synchronization, consistency solution
- Can software related inventions be patented in India?
- MySQL主从同步之 异步复制 半同步复制 全同步复制
- [Junzheng T31] decompression and packaging of read-only rootfs file system squashfs
- 缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩
- Artbench: the first class balanced, high-quality, clean annotated and standardized artwork generation data set
- An interpretable geometric depth learning model for structure based protein binding site prediction
- 二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
猜你喜欢

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

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

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

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

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

MIT linear algebra Chinese Notes

Weekly recommended short video: How did Einstein think?

How to install oracle19c in Centos8

智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中

Blurred pictures become clear, one button two-color pictures, quickly organize local pictures These 8 online picture tools apply to join your favorites!
随机推荐
Introduction to multi project development - business scenario Association basic introduction test payroll
Proteus软件初学笔记
MATLAB求极限
Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing
Pangolin compilation error: 'numeric_ limits’ is not a member of ‘std’
2022.6.28-----leetcode.324
《Go题库·14》WaitGroup的坑
How to calculate win/tai/loss in paired t-test
多项目开发入门-业务场景关联基础入门测试 工资表
推荐模型复现(四):多任务模型ESMM、MMOE
[cloud native] 2.4 kubernetes core practice (middle)
Problem solving: modulenotfounderror: no module named 'pip‘
Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
Unexpected ‘debugger‘ statement no-debugger
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture
Gbase8s database select has a having clause
在印度与软件相关的发明可不可以申请专利?
Syntax of gbase8s database incompatible with for update clause
Gbase8s database select has order by Clause 4
MySQL数据库主从同步,一致性解决方案