树的遍历——c#实现
树作为一种重要的非线性数据结构,以分支关系定义其层次结构,在客观世界中应用广泛。通过对树遍历,将树进行线性化处理,即遍历的结果是将非线性结构的树种节点排列成一个线性序列。其中,最常见的遍历方式包括先序、中序、后序遍历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#实现的更多相关文章
- 数据结构--树(遍历,红黑,B树)
平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...
- YTU 3023: 树的遍历
原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题 ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- L2-006. 树的遍历
题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...
- js实现对树深度优先遍历与广度优先遍历
深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树
L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...
- L2-006 树的遍历 (后序中序求层序)
题目: 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序 ...
随机推荐
- 黄聪:如何正确在Vue框架里使用Swiper
实例: 错误(无法显示出分页器按钮,此功能不适用与for循环出来的图片,只有当该页面图片固定几张时能正常用) 第一步: 安装 npm i swiper (vue插件自带) 第二步: 在当前页面里引入 ...
- RDLC报表系列--------初级报表
前面记录下了很多平时开发遇到的问题,RLDC之前也是不会,只会水晶报表,后来也慢慢的也上手了.把这些记录下来,以后用的着 1.打开VS添加新建项,选择Reporting,选择报表,后缀名为RLDC的名 ...
- Realm 处理List<String> 问题 Type parameter 'java.lang.String' is not within its bound; should implement 'io.realm.RealmModel
public class InitAppBean extends RealmObject { private String sapling; private String logistics; pri ...
- activity--常见属性总结
15.Activit的几个重要属性总结? 12.onNewIntent()使用Tips?11.launchMode的属性介绍?及其常用的Intent Flag? ==== 15.Activit的几个重 ...
- AD中修改OU下面用户的属性
第一种方法可行: get-ADuser -searchbase "ou=Wireless,dc=lstech,dc=com" -filter * | set-ADuser -Giv ...
- CentOS7的内核优化
修改内核配置文件 vim /etc/sysctl.conf 刷新配置文件 sysctl -p 关ipv6 net.ipv6.conf.all.disable_ipv6 = net.ipv6.conf. ...
- Shiro的FormAuthenticationFilter登陆成功不跳转
http://jinnianshilongnian.iteye.com/blog/2024723 张开涛的这个配置信息有误,导致默认authc登陆成功后无法跳转 FormAuthenticationF ...
- 转:APP开发浅谈-Fiddler抓包详解
原文地址:http://www.luoxudong.com/?p=306 Fiddler抓包工具在APP开发过程中使用非常频繁,对开发者理解HTTP网络传输原理以及分析定位网络方面的问题非常有帮助.今 ...
- 如何简单理解js中this的指向
前序 每个人学js都会被this指向这个东西搞得很蒙,那就是this的指向问题.首先,我们要明白 this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上thi ...
- 2018-2019-2 20165205 《网络对抗技术》 Exp1 PC平台逆向破解
2018-2019-2 20165205 <网络对抗技术> Exp1 PC平台逆向破解 1. 实验任务 1.1实验概括 用一个pwn1文件. 该程序正常执行流程是:main调用foo函数, ...