前言

树的前中后序遍历 是根据前中后序的顺序来查找,找到了则弹出。

正文

节点模型:

public class HeroNode
{
private int no; private string name; private HeroNode left; private HeroNode right; public HeroNode(int no, string name) {
this.no = no;
this.name = name;
} public int getNo() {
return no;
}
public void setNo(int no)
{
this.no = no;
} public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public HeroNode getLeft()
{
return left;
}
public void setLeft(HeroNode left)
{
this.left = left;
}
public HeroNode getRight()
{
return right;
}
public void setRight(HeroNode right)
{
this.right = right;
}
public override string ToString()
{
return "姓名:" + name + "编号:" + no;
}
//编写前序遍历的方法 是根、左、右
public void preOrder() {
Console.WriteLine(this); if (this.left != null)
{
this.left.preOrder();
}
if (this.right != null)
{
this.right.preOrder();
}
}
//中序遍历 是左、根、右
public void infixOrder() {
if (this.left != null)
{
this.left.infixOrder();
}
Console.WriteLine(this);
if (this.right != null)
{
this.right.infixOrder();
}
}
// 后续遍历为 左、右、根
public void postOrder()
{
if (this.left != null)
{
this.left.postOrder();
}
if (this.right != null)
{
this.right.postOrder();
}
Console.WriteLine(this);
}
//前序遍历查找
public HeroNode preOrderSearch(int no)
{
HeroNode resNode = null;
record();
if (this.no == no)
{
return this;
}
if (this.left != null)
{
resNode=this.left.preOrderSearch(no);
}
if (resNode != null)
{
return resNode;
}
if (this.right != null)
{
resNode = this.right.preOrderSearch(no);
}
return resNode;
} //中序遍历查找 public HeroNode infixOrderSearch(int no)
{
HeroNode resNode = null;
if (this.left != null)
{
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null)
{
return resNode;
}
record();
if (this.no == no)
{
return this;
}
if (this.right != null)
{
resNode = this.right.infixOrderSearch(no);
}
return resNode;
} //后序遍历查找 public HeroNode postOrderSearch(int no)
{ HeroNode resNode = null; if (this.left != null)
{
resNode = this.left.postOrderSearch(no);
}
if (resNode != null)
{
return resNode;
} if (this.right != null)
{
resNode = this.right.postOrderSearch(no);
}
if (resNode != null)
{
return resNode;
}
record();
if (this.no == no)
{
resNode=this;
}
return resNode;
}
public void record()
{
Console.WriteLine("查找步骤为:名字" + this.name + " 编号:" + this.no);
}
}

树模型:

public class BinaryTree
{
private HeroNode root; public void setRoot(HeroNode root)
{
this.root = root;
}
//前序遍历
public void preOrder()
{
if (this.root != null)
{
this.root.preOrder();
}
else
{
Console.WriteLine("二叉树为空,无法遍历");
}
} //中序遍历
public void infixOrder()
{
if (this.root != null)
{
this.root.infixOrder();
}
else
{
Console.WriteLine("二叉树为空,无法遍历");
}
}
//后序遍历
public void postOrder()
{
if (this.root != null)
{
this.root.postOrder();
}
else
{
Console.WriteLine("二叉树为空,无法遍历");
}
}
//前序遍历查找
public HeroNode preOrderSearch(int no)
{
if (root != null)
{
return this.root.preOrderSearch(no);
} else {
return null;
}
}
//中序遍历查找
public HeroNode infixOrderSearch(int no)
{
if (root != null)
{
return this.root.infixOrderSearch(no);
}else
{
return null;
}
}
//后序遍历查找
public HeroNode postOrderSearch(int no)
{
if (root != null)
{
return this.root.postOrderSearch(no);
}else {
return null;
}
}
}

测试:

static void Main(string[] args)
{
//先需要创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "关胜");
//设置节点
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
//前序遍历
Console.WriteLine("前序遍历查找");
binaryTree.preOrderSearch(5);
Console.WriteLine("中序遍历查找");
binaryTree.infixOrderSearch(5);
Console.WriteLine("后续遍历查找");
binaryTree.postOrderSearch(5);
Console.ReadKey();
}

测试结果:

重新整理数据结构与算法(c#系列)—— 树的前中后序遍历查找[十七]的更多相关文章

  1. 数据结构-C语言递归实现树的前中后序遍历

    #include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...

  2. C++实现对树的创建和前中后序遍历

    #include<iostream>#include<stdio.h> using namespace std; class BitNode{ public: char dat ...

  3. 笔试算法题(07):还原后序遍历数组 & 半翻转英文句段

    出题:输入一个整数数组,判断该数组是否符合一个二元查找树的后序遍历(给定整数数组,判定其是否满足某二元查找树的后序遍历): 分析:利用后序遍历对应到二元查找树的性质(序列最后一个元素必定是根节点,从左 ...

  4. A1135 | 红黑树判断:审题、根据“先序遍历”和“BST树”的条件生成后序遍历、递归判断

    对A1135这题有心里阴影了,今天终于拿下AC.学习自柳神博客:https://www.liuchuo.net/archives/4099 首先读题很关键: There is a kind of ba ...

  5. 前中后序递归遍历树的体会 with Python

    前序:跟->左->右 中序:左->根->右 后序:左>右->根 采用递归遍历时,编译器/解释器负责将递归函数调用过程压入栈并保护现场,在不同位置处理根节点即可实现不 ...

  6. 代码随想录算法训练营day18 | leetcode 513.找树左下角的值 ● 112. 路径总和 113.路径总和ii ● 106.从中序与后序遍历序列构造二叉树

    LeetCode 513.找树左下角的值 分析1.0 二叉树的 最底层 最左边 节点的值,层序遍历获取最后一层首个节点值,记录每一层的首个节点,当没有下一层时,返回这个节点 class Solutio ...

  7. 数据结构与算法入门系列教程-C#

    数据结构与算法入门系列教程 (一)为啥要学习数据结构与算法 曾经我也以为自己很牛逼,工作中同事也觉得我还可以,领导也看得起我,啥啥啥都好,就这样过了几年,忽然发现自己学新东西没劲.时代都变了,而我还只 ...

  8. Java数据结构与算法(20) - ch08树

    树的主要算法有插入,查找,显示,遍历,删除,其中显示和删除略微复杂. package chap08.tree; import java.io.BufferedReader; import java.i ...

  9. 【数据结构与算法】002—树与二叉树(Python)

    概念 树 树是一类重要的非线性数据结构,是以分支关系定义的层次结构 定义: 树(tree)是n(n>0)个结点的有限集T,其中: 有且仅有一个特定的结点,称为树的根(root) 当n>1时 ...

  10. python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)

    python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...

随机推荐

  1. C++ //类模板中成员函数创建时机 //类模板中成员函数和普通类中成员函数创建时机是有区别的: //1.普通类中的成员函数一开始就可以创建 //2.类模板中的成员函数在调用时才创建

    1 //类模板中成员函数创建时机 2 //类模板中成员函数和普通类中成员函数创建时机是有区别的: 3 //1.普通类中的成员函数一开始就可以创建 4 //2.类模板中的成员函数在调用时才创建 5 6 ...

  2. harbor 安装

    下载地址: https://github.com/goharbor/harbor/releases?page=1 下载了多个版本,发现仅v1.10.17版本支持GC清理,所以这里安装的v1.10.17 ...

  3. offline RL | D4RL:最常用的 offline 数据集之一

    pdf:https://arxiv.org/pdf/2004.07219.pdf html:https://ar5iv.labs.arxiv.org/html/2004.07219 GitHub:ht ...

  4. 使用 libreoffice 批量化转化文件为 .pdf 并合并

    介绍使用 libreoffice 批量化将文件转化为 .pdf 然后合并.pdf文件的方法 很多人知道,在 Linux 系统中 WPS 是办公软件中很棒的选择.但其实 libreoffice 也是一个 ...

  5. 【stars-one】JetBrains产品试用重置工具

    原文[stars-one]JetBrains产品试用重置工具 | Stars-One的杂货小窝 一款可重置JetBrains全家桶产品的试用时间的小工具,与其全网去找激活码,还不如每个月自己手动重置试 ...

  6. 修改阿里云DNS 解决蓝奏云无法访问问题

    某些地区的宽带连接不上蓝奏云服务器,需要手动改一下DNS配置,改为阿里云的即可 PS:阿里云DNS服务器地址为223.5.5.5 和 223.6.6.6 下面以win10系统为例,具体步骤如下 1.进 ...

  7. 什么叫运行时的Java程序?

    Java程序的运行包含编写.编译和运行三个主要步骤. 1.在编写阶段: 开发人员在Java开发环境中输入程序代码,形成后缀名为.java的Java源文件. 2.在编译阶段: 使用Java编译器对源文件 ...

  8. 一个简单的RTMP服务器实现 --- RTMP实现要点

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  9. 关于Android studio无法勾选SDK的问题

    这是我遇到的问题,相信也是大多数人遇到的问题,我的经历是之前下载过一次Android studio,用过一段时间后虚拟机出问题了,一直连不上,我都是用手机代替运行,发现太麻烦了,还是决定重新一遍,于是 ...

  10. 一道题开始认识Symbol

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 最近每天学习的时候,发现了一道很有趣的面试题 1.const [a, b] = { a: 100, b: 200 } 2.console. ...