树作为一种重要的非线性数据结构,以分支关系定义其层次结构,在客观世界中应用广泛。通过对树遍历,将树进行线性化处理,即遍历的结果是将非线性结构的树种节点排列成一个线性序列。其中,最常见的遍历方式包括先序中序后序遍历3种。此外,还有一种按照“从上到下,从左到右”的层次遍历方式。

  以下列二叉树为例,对其进行遍历及实现。

1 先序遍历

1.1 遍历操作  

  先序遍历二叉树的操作定义如下:

  若二叉树为空,则操作空,否则

  • 先访问树的根节点
  • 再遍历左子树
  • 遍历右子树

  上例遍历结果为:ABDCE

1.2 遍历实现    

  前序遍历的递归实现如下:

//递归实现前序遍历
public static void preorder(Node root)
{
if (root == null)
{
return;
}
Console.Write("{0} ", root.value);
preorder(root.left);
preorder(root.right);
}
//非递归前序遍历
public static void preOrder_Nonrec(Node root)
{
Console.Write("前序遍历为:");
Stack<Node> st = new Stack<Node>();
st.Push(root);
while (st.Count != )
{
Node cur = st.Pop();
Console.Write("{0}", cur.value);
if (cur.right != null)
{
st.Push(cur.right);
}
if (cur.left != null)
{
st.Push(cur.left);
}
}
Console.WriteLine();
}

2 中序遍历

2.1 操作定义

  中序遍历二叉树的操作定义如下:

  若二叉树为空,则操作空,否则

  • 先遍历树的左子树
  • 访问根节点
  • 遍历右子树

2.2 中序遍历实现  

//递归实现二叉树中序遍历
public static void midOrder(Node root){ if (root == null)
{
return;
}
midOrder(root.left);
Console.Write("{0} ", root.value);
midOrder(root.right);
}
//非递归实现二叉树中序遍历
public static void inOrder_nonrec(Node root)
{
Console.Write("中序遍历为:");
if(root!=null){
Stack<Node> st = new Stack<Node>();
while(st.Count!= || root!=null){
if(root!=null){
st.Push(root);
root=root.left;
}else{
root=st.Pop();
Console.Write("{0}",root.value);
root=root.right;
}
}
}

3 后序遍历

3.1 操作定义

  后序遍历二叉树的操作定义如下:

  若二叉树为空,则操作空,否则

  • 遍历树的左子树
  • 遍历右子树
  • 访问根节点

3.2 后序遍历实现 

//递归实现后序遍历
public static void postOrder(Node root)
{
if(root ==null){
return;
}
postOrder(root.left);
postOrder(root.right);
Console.Write("{0} ", root.value);
}
//非递归实现后序遍历
public static void post_nonrec(Node root)
{
Console.Write("后序遍历为:");
if(root!=null){
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.Push(root);
while(s1.Count!=){
root = s1.Pop();
s2.Push(root);
if(root.left!=null){
s1.Push(root.left);
}
if (root.right != null)
{
s1.Push(root.right);
}
}
while(s2.Count!=){
Console.Write("{0}",s2.Pop().value);
}
}
}

整体代码

namespace treeTrace
{
class Program
{
static void Main(string[] args)
{
Node nodeA = new Node();
Node nodeB = new Node();
Node nodeC= new Node();
Node nodeD = new Node();
Node nodeE= new Node();
Node.buileTree(ref nodeE,nodeC,null,null);
Node.buileTree(ref nodeD,nodeB,null,null);
Node.buileTree(ref nodeC,nodeA,nodeE,null);
Node.buileTree(ref nodeB,nodeA,null,nodeD);
Node.buileTree(ref nodeA,null,nodeB,nodeC);
//递归实现
Console.Write("前序遍历为:");
Node.preorder(nodeA);
//Console.Write("中序遍历为:");
//Node.midOrder(nodeA);
//Console.Write("后序遍历为:");
//Node.postOrder(nodeA);
//非递归实现
// Node.preOrder_Nonrec(nodeA);
//Node.inOrder_nonrec(nodeA);
// Node.post_nonrec(nodeA);
Console.Read();
}
}
public class Node
{
public int value;
public Node _root;
private Node _left;
public Node _right;
public Node root
{
get { return _root; }
set { _root = value; }
}
public Node left
{
get { return _left; }
set { _left = value; }
}
public Node right
{
get { return _right; }
set { _right = value; }
} public Node(int data)
{
this.value = data;
}
//创建二叉树
public static void buileTree(ref Node node,Node root,Node left,Node right)
{
node.left=left;
node.right=right;
node.root = root;
}
//public static void build(Node root)
//{
// if (root == null)
// return;
// build(root.left);
// build(root.right);
//} #region 递归实现前序、中序、后序遍历
public static void preorder(Node root)
{
if (root == null)
{
return;
}
Console.Write("{0} ", root.value);
preorder(root.left);
preorder(root.right);
}
public static void midOrder(Node root){ if (root == null)
{
return;
}
midOrder(root.left);
Console.Write("{0} ", root.value);
midOrder(root.right);
}
public static void postOrder(Node root)
{
if(root ==null){
return;
}
postOrder(root.left);
postOrder(root.right);
Console.Write("{0} ", root.value);
}
#endregion #region 非递归实现树的前序、中序、后序遍历 public static void preOrder_Nonrec(Node root)
{
Console.Write("前序遍历为:");
Stack<Node> st = new Stack<Node>();
st.Push(root);
while (st.Count != )
{
Node cur = st.Pop();
Console.Write("{0}", cur.value);
if (cur.right != null)
{
st.Push(cur.right);
}
if (cur.left != null)
{
st.Push(cur.left);
}
}
Console.WriteLine();
}
public static void inOrder_nonrec(Node root)
{
Console.Write("中序遍历为:");
if(root!=null){
Stack<Node> st = new Stack<Node>();
while(st.Count!= || root!=null){
if(root!=null){
st.Push(root);
root=root.left;
}else{
root=st.Pop();
Console.Write("{0}",root.value);
root=root.right;
}
}
}
Console.WriteLine();
}
public static void post_nonrec(Node root)
{
Console.Write("后序遍历为:");
if(root!=null){
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.Push(root);
while(s1.Count!=){
root = s1.Pop();
s2.Push(root);
if(root.left!=null){
s1.Push(root.left);
}
if (root.right != null)
{
s1.Push(root.right);
}
}
while(s2.Count!=){
Console.Write("{0}",s2.Pop().value);
}
}
}
#endregion }
}

树的遍历——c#实现的更多相关文章

  1. 数据结构--树(遍历,红黑,B树)

    平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...

  2. YTU 3023: 树的遍历

    原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 2 题 ...

  3. 团体程序设计天梯赛-练习集L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  4. leetcode404-----简单的树的遍历

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  5. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  6. L2-006. 树的遍历

    题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...

  7. js实现对树深度优先遍历与广度优先遍历

    深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...

  8. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  9. 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树

    L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  10. L2-006 树的遍历 (后序中序求层序)

    题目: 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序 ...

随机推荐

  1. C#中基于GDI+(Graphics)图像处理系列

    https://blog.csdn.net/lhtzbj12/article/details/54024821

  2. Python判断、运算符

    1.Python之if判断 2.Python运算符 3.Python综合案例

  3. 【dbdiff】数据库比对工具安装

    安装git 忽略 安装visual c++组件(和mysql workbench需要的一样) https://www.microsoft.com/en-us/download/details.aspx ...

  4. SpringCloud启动类指定扫描包路径

    //如果这个启动类所在的包路径隐藏的很深,则需要指定扫描包.否则默认扫描启动类所在的子包路径下 @SpringBootApplication(scanBasePackages="com.jo ...

  5. EMQ消息队列初体验

    使用命令创建admin用户,密码123 emqx_ctl users add admin 配置规则/etc/emqx/acl.conf(除管理员,其他用户只能订阅限定的测试主题路径) %% 允许'ad ...

  6. WKWebView使用方法

    基本使用方法 WKWebView有两个delegate,WKUIDelegate 和 WKNavigationDelegate.WKNavigationDelegate主要处理一些跳转.加载处理操作, ...

  7. springmvc功能以及源码实现分析

    每当我学习一个新技术时,我总是会问自己这个技术有哪些功能,能帮我解决哪些问题. 但是当我在网上进行搜索时,答案并不让我满意. 在使用springmvc很久以后,我将在这篇文章里对springmvc功能 ...

  8. 记一次解决cmd中执行java提示"找不到或无法加载主类"的问题

    今天遇到一个问题:在cmd命令行中,用javac编译java文件可以成功,但是用java执行却提示“找不到或无法加载主类”.现将该问题的原因以及解决办法记录一下. 先理解一下系统变量path和clas ...

  9. pip install pytest on Mac (EI Capitan 10.11.6)

    升级了Mac 系统后发现用pip安装pytest出现下面链接中的问题,解决方法是在install时候加上--user选项: 1. 切到home directory: cd - 2. install p ...

  10. 最近想学Json,请问大家有没有什么好的Json教程介绍一下?

    最近想学json,请问大家有没有什么好的Json教程介绍一下? 最近学完java的框架了,想了解一下json,可是找不到相关视频,请大家有这方面的Json教程好资料就介绍下啦,最后有网址链接啦. {} ...