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

    一.节点中使用(cpp,python) 1. ros wiki 提供的tutorials 2. https://blog.csdn.net/start_from_scratch/article/det ...

  2. enable-ssh-key-logon-disable-password-password-less-logon-centos/

    cat ~/.ssh/id_rsa.pub | ssh root@destination_server_address "cat >> ~/.ssh/authorized_key ...

  3. Office2010安装出现“错误1907”的解决方法(未验证)

    http://bbs.pcbeta.com/viewthread-1627988-1-5.html 这个问题我遇到过.解决方法:1.安装时提示错误选择忽略,安装完成后.2.如果能正常使用OFFICE软 ...

  4. 饿了么测试专场技术沙龙实况回顾&PPT 下载

    PPT下载和视频观看链接 链接:https://pan.baidu.com/s/1dE8uXHZ 密码:6j5z视频直播回顾: http://www.itdks.com/dakashuo/playba ...

  5. WKWebView使用方法

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

  6. Tomcat修改service.xml性能调优 增加最大并发连接数

    详细配置: <Connector executor="tomcatThreadPool"               port="80" protocol ...

  7. [STM32F103]定时器中断

    l 使能定时器时钟. RCC_APB1PeriphClockCmd(); l 初始化定时器,配置ARR,PSC. TIM_TimeBaseInit(); l 开启定时器中断,配置NVIC. void ...

  8. var let const

    你真的永远都不用var了吗? javascript的一些争论已经浮现出了一些经典的案例,因此,es6的拥护者你们应该讲var遗忘吗?这篇博客将带你走进被遗忘的角落 首先举例反对者的几个观点: 1.如果 ...

  9. Linux下Oracle开机启动

    参考:http://blog.csdn.net/huangyanlong/article/details/36942155 一.保证dbstart能用:vi $ORACLE_HOME/bin/dbst ...

  10. 一、Python入门

    一.语法特点: 注释规则: 单行注释:“#”作为单行注释符号(从“#”开始到换行都为注释):Alt+F3/4快捷添加/取消注释 多行注释:宝行一对三引号('''…''')或(""& ...