当前位置:网站首页>C#二叉树的遍历方法(通过递归)
C#二叉树的遍历方法(通过递归)
2022-07-30 05:47:00 【Misaki_Me】
C#二叉树的遍历方法(通过递归)
遍历二叉树的四种方法:先序遍历、中序遍历、后序遍历、层次遍历。
1.先序遍历递归过程:若二叉树为空,遍历结束。若不为空。则先访问根结点。再访问根结点的左子树。再访问根节点的右子树。
2.中序遍历:先访问根结点的左子树;再访问根结点,再访问根结点的右子树。
3.后序遍历:先访问根结点的左子树,再访问根结点的右子树。最后访问根结点。
4.层次遍历:从二叉树的第一层开始,从上到下逐层遍历,。在同一层中按照从左到右对结点访问。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tree
{
class LinkBiTree<T>
{
private Node<T> head;
public Node<T> Head
{
get
{
return head;
}
set
{
head = value;
}
}
public LinkBiTree()
{
head = null;
}
public LinkBiTree(T data)
{
Node<T> p = new Node<T>(data);
head = p;
}
public LinkBiTree(T data,Node<T> leftChild = null,Node<T> rightChild = null)
{
Node<T> p = new Node<T>(data,leftChild,rightChild);
head = p;
}
//判断树是否为空
public bool IsEmpty()
{
if (head == null)
{
return true;
}
return false;
}
//获取根节点
public Node<T> Root()
{
return head;
}
//获取结点的左孩子结点
public Node<T> LeftChild(Node<T> p)
{
return p.LeftChild;
}
//获取结点的右孩子结点
public Node<T> RightChild(Node<T> p)
{
return p.RightChild;
}
//将结点p的左子树插入值为data的新结点
//原来的左子树成为新结点的左子树
public void InsertLeft(T data,Node<T> p)
{
Node<T> temp = new Node<T>(data);
temp.LeftChild = p.LeftChild;
p.LeftChild = temp;
}
//将结点p的右子树插入值为data的新结点
//原来的右子树成为新结点的右子树
public void InsertRight(T data, Node<T> p)
{
Node<T> temp = new Node<T>(data);
temp.RightChild = p.RightChild;
p.RightChild = temp;
}
//删除p的左子树
public Node<T> DelLeft(Node<T> p)
{
if (p == null || p.LeftChild == null)
{
return null;
}
else
{
Node<T> temp = p.LeftChild;
p.LeftChild = null;
return temp;
}
}
//删除p的右子树
public Node<T> DelRight(Node<T> p)
{
if (p == null || p.RightChild == null)
{
return null;
}
else
{
Node<T> temp = p.RightChild;
p.RightChild = null;
return temp;
}
}
//查找某个值为value的结点
public Node<T> Search(Node<T> root, T value)
{
Node<T> p = root;
if (p == null)
{
return null;
}
if (!p.Data.Equals(value))
{
return p;
}
if (p.LeftChild != null)
{
return Search(p.LeftChild, value);
}
if (p.RightChild != null)
{
return Search(p.RightChild, value);
}
return null;
}
//判断是否是叶子结点
public bool IsLeaf(Node<T> p)
{
if ((p != null && p.LeftChild == null && p.RightChild == null))
{
return true;
}
return false;
}
#region //遍历方法及递归实现
//中序遍历
public void Inorder(Node<T> ptr)
{
if (IsEmpty())
{
Console.WriteLine("Tree is Empty");
return;
}
if (ptr != null)
{
Inorder(ptr.LeftChild);
Console.WriteLine(ptr.Data + " ");
Inorder(ptr.RightChild);
}
}
//先序遍历
public void Preorder(Node<T> ptr)
{
if (IsEmpty())
{
Console.WriteLine("Tree is Empty");
}
if (ptr != null)
{
Console.WriteLine(ptr.Data + " ");
Preorder(ptr.LeftChild);
Preorder(ptr.RightChild);
}
}
//后序遍历
public void Postorder(Node<T> ptr)
{
if (IsEmpty())
{
Console.WriteLine("Tree is Empty");
return;
}
if (ptr != null)
{
Postorder(ptr.LeftChild);
Postorder(ptr.RightChild);
Console.WriteLine(ptr.Data + " ");
}
}
//层次遍历
public void LevelOrder(Node<T> root)
{
if (root == null)
{
return;
}
//设置一个队列保存层次遍历的结点
Queue<Node<T>> sq = new Queue<Node<T>>(50);
//根结点入队
sq.Enqueue(root);
while (sq.Count != 0)
{
//结点出队
Node<T> temp = sq.Dequeue();
//处理该结点
Console.WriteLine("{0}",temp);
//将当前结点的左孩子结点入队
if (temp.LeftChild != null)
{
sq.Enqueue(temp.LeftChild);
}
//将当前结点的右孩子结点入队
if (temp.RightChild != null)
{
sq.Enqueue(temp.RightChild);
}
}
}
}
}
边栏推荐
- 【部分项目展示】
- xxx is not in the sudoers file.This incident will be reported错误
- QT serial and CAN dynamic real-time display the log data
- 《C陷阱和缺陷》void (*signal(int , void(*)(int)))(int)的深刻解读
- 表格比手机屏幕宽时不压缩,可左右滚动,格子内容不换行
- [Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
- Explore the efficiency of make_shared
- 力扣题解7.27
- QT Weekly Skills (1) ~~~~~~~~~ Running Icon
- 删除当前路径下含某个关键字的所有文件
猜你喜欢

VsCode打开终端的方法

给Vscode配置ESlint语法检查 — ESLint 插件自动格式化设置(实现Ctrl+S 按照ESLint规则自动格式化代码)

Acwing Brush Questions Section 1

二、2队列
![[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations](/img/7f/d9f480ab9a1e542e4fa1fda7978e4c.png)
[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations

QT Weekly Skills (1) ~~~~~~~~~ Running Icon

jvm之方法区

华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长

Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)

【已解决:el-input标签无法输入或不显示文字】
随机推荐
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
动态规划入门 JS
超详细的PCB高可靠辨别方法
独立按键控制led
sizeof和strlen最全区别,以及指针和数组运算解析
闭包和作用域(你不知道的JS自用笔记)
C 语言之学生管理系统-多文件编程
四、6、前缀、中缀、后缀表达式(逆波兰表达式)
ssh script space character conversion
华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
华秋电子成为开放原子开源基金会openDACS捐赠人,共建 openDACS开源生态
Vim find character
C语言,库函数中qsort的用法,及解释
ipconfig Command Guide
二、1稀疏sparsearray数组
2020-09-03解决pip install安装非常慢[Errno 101] 网络不可达问题
Deep Interpretation of void (*signal(int , void(*)(int)))(int) in "C Traps and Defects"
基于STM32F103的消防系统之驱动电机风扇
信号链模拟芯片是什么?
Comparison of advantages and disadvantages of VsCode and Sublime editors