树作为一种重要的非线性数据结构,以分支关系定义其层次结构,在客观世界中应用广泛。通过对树遍历,将树进行线性化处理,即遍历的结果是将非线性结构的树种节点排列成一个线性序列。其中,最常见的遍历方式包括先序中序后序遍历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. 获取邮箱使用情况、以及最后一次logon时间

    # 每天收发邮件数 # https://gallery.technet.microsoft.com/scriptcenter/Count-sent-and-recceived-f9c66cf7 # 获 ...

  2. RTP协议

    RTP协议格式: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+ ...

  3. python列表生成式

    在Python编程中,list列表应用的比较广泛,而列表生成式是一个非常强大但是使用很简单的功能. 比如要生成1到100的数组列表[1,2,3,4......,97,98,99,100],可以使用li ...

  4. nginx1.14.0版本负载均衡配置

    upstream配置: upstream upstream1 { server 192.168.10.130:8080; server 192.168.10.140:8080; #server 192 ...

  5. 2018-2019-2 20165312《网络攻防技术》Exp5 MSF基础应用

    2018-2019-2 20165312<网络攻防技术>Exp5 MSF基础应用 目录 一.知识点总结 二.攻击实例 主动攻击的实践 ms08_067 payload/generic/sh ...

  6. php变量函数

    这个东西相当于C语言中的函数指针,C#里的委托   function come() {                   //定义com函数 echo "来了<p>" ...

  7. Keras bug in model.predict

    When I use Keras to predict behind a web service, it occurred an error. and the error message is lik ...

  8. Two kinds of item classification model architecture

    Introduction: Introduction to Fusing-Probability model: Cause the input has two parts, one is item i ...

  9. orcad 自带常用原理图库解析

    在cadence 安装目录下文件夹\Cadence\SPB_16.6\tools\capture\library 中有如下常用库,只是部分常用的库,还有很多不常用的也都在目录下面, 1.AMPLIFI ...

  10. Spring Boot学习大全(入门)

    Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...