C#实现二叉树的各种遍历
1. 引言
在实际的项目中,树还是用的比较多的一种,尤其是对于具有层次结构的数据。相信很多人都学过树的遍历,比如先序遍历,后序遍历等,利用递归还是很容易理解的。
今天给大家介绍下二叉树的几种遍历算法,包括递归和非递归的实现。
首先建立一棵二叉树 如:
[DebuggerDisplay("Value={Value}")]
public class Tree
{
public string Value;
public Tree Left;
public Tree Right;
}
public static Tree CreatFakeTree()
{
Tree tree = new Tree() {Value = "A"};
tree.Left = new Tree()
{
Value = "B",
Left = new Tree() {Value = "D", Left = new Tree() {Value = "G"}},
Right = new Tree() {Value = "E", Right = new Tree() {Value = "H"}}
};
tree.Right = new Tree() {Value = "C", Right = new Tree() {Value = "F"}};
return tree;
}
一棵简单的二叉树

2. 先序遍历
先序遍历还是很好理解的,一次遍历根节点,左子树,右子数
递归实现
public static void PreOrder(Tree tree)
{
if (tree == null)
return; System.Console.WriteLine(tree.Value);
PreOrder(tree.Left);
PreOrder(tree.Right);
}
非递归实现
public static void PreOrderNoRecursion(Tree tree)
{
if(tree == null)
return; System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree; while (node != null || stack.Any())
{
if (node != null)
{
stack.Push(node);
System.Console.WriteLine(node.Value);
node = node.Left;
}
else
{
var item = stack.Pop();
node = item.Right;
}
}
}
输出结果: 
3. 中序遍历
递归实现
public static void InOrder(Tree tree)
{
if(tree == null)
return; InOrder(tree.Left);
System.Console.WriteLine(tree.Value);
InOrder(tree.Right);
}
非递归实现
public static void InOrderNoRecursion(Tree tree)
{
if (tree == null)
return; System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree; while (node != null || stack.Any())
{
if (node != null)
{
stack.Push(node);
node = node.Left;
}
else
{
var item = stack.Pop();
System.Console.WriteLine(item.Value); node = item.Right;
}
}
}
输出结果:
4. 后序遍历
递归实现
public static void PostOrder(Tree tree)
{
if (tree == null)
return; PostOrder(tree.Left);
PostOrder(tree.Right);
System.Console.WriteLine(tree.Value);
}
非递归实现 比前两种稍微复杂一点。要保证左右节点都被访问后,才能访问根节点。这里给出两种形式。
public static void PostOrderNoRecursion(Tree tree)
{
if (tree == null)
return; System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree;
Tree pre = null;
stack.Push(node); while (stack.Any())
{
node = stack.Peek();
if ((node.Left == null && node.Right == null) ||
(pre != null && (pre == node.Left || pre == node.Right)))
{
System.Console.WriteLine(node.Value);
pre = node; stack.Pop();
}
else
{
if(node.Right != null)
stack.Push(node.Right); if(node.Left != null)
stack.Push(node.Left);
}
}
} public static void PostOrderNoRecursion2(Tree tree)
{
HashSet<Tree> visited = new HashSet<Tree>();
System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree; while (node != null || stack.Any())
{
if (node != null)
{
stack.Push(node);
node = node.Left;
}
else
{
var item = stack.Peek();
if (item.Right != null && !visited.Contains(item.Right))
{
node = item.Right;
}
else
{
System.Console.WriteLine(item.Value);
visited.Add(item);
stack.Pop();
}
}
}
}
输出结果: 
5. 层序遍历
层序遍历就是按照层次由左向右输出
public static void LevelOrder(Tree tree)
{
if(tree == null)
return; Queue<Tree> queue = new Queue<Tree>();
queue.Enqueue(tree); while (queue.Any())
{
var item = queue.Dequeue();
System.Console.Write(item.Value); if (item.Left != null)
{
queue.Enqueue(item.Left);
} if (item.Right != null)
{
queue.Enqueue(item.Right);
}
}
}
输出结果:
6. Z-型层序遍历
Z-层序遍历就是奇数层按照由左向右输出,偶数层按照由右向左输出,这里定义了几个辅助函数,比如计算节点所在的层次。算法思想是按照层次保存树形节点,应该是有更加优化的算法,希望大家指出。
public static int GetDepth(Tree tree, Tree node)
{
if (tree == null)
return ; if (tree == node)
return ; if (tree.Left == node || tree.Right == node)
return ; int lDepth = GetDepth(tree.Left, node);
lDepth = lDepth == ? : lDepth + ; int rDepth = GetDepth(tree.Right, node);
rDepth = rDepth == ? : rDepth + ; return lDepth >= rDepth ? lDepth : rDepth;
} public static void Z_LevelOrder(Tree tree, Dictionary<int, List<Tree>> dictionary)
{
if (tree == null)
return; Queue<Tree> queue = new Queue<Tree>();
queue.Enqueue(tree); while (queue.Any())
{
var item = queue.Dequeue();
var depth = GetDepth(tree, item); List<Tree> list;
if (!dictionary.TryGetValue(depth, out list))
{
list = new List<Tree>();
dictionary.Add(depth, list);
}
list.Add(item); if (item.Left != null)
{
queue.Enqueue(item.Left);
} if (item.Right != null)
{
queue.Enqueue(item.Right);
}
}
} public static void Z_LevelOrder(Tree tree)
{
if (tree == null)
return; Dictionary<int, List<Tree>> dictionary = new Dictionary<int, List<Tree>>();
Z_LevelOrder(tree, dictionary); foreach (KeyValuePair<int, List<Tree>> pair in dictionary)
{
if (pair.Key% == )
{
pair.Value.Reverse();
} pair.Value.ForEach(t=> { System.Console.Write(t.Value); });
}
}
输出结果:
C#实现二叉树的各种遍历的更多相关文章
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- codevs3143 二叉树的序遍历
难度等级:白银 3143 二叉树的序遍历 题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点 ...
- codevs 3143 二叉树的序遍历
传送门 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 Input 第一行一个整数n,表示这棵树的节点个数. 接下来n行每行2个整数L和R.第i行的两个整数Li和Ri代表编号为i的 ...
- lintcode : 二叉树的层次遍历II
题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...
- lintcode : 二叉树的层次遍历
题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历 ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 二叉树中序遍历 (C语言实现)
在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构.二叉树是每个节点最多有两个子树的有序树.通常子树被称作“左子树”(left subtre ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
随机推荐
- 12小时包你学会基于ReactMix框架的ReactNativeApp开发(一)Hello World!
ReactMixhttps://github.com/xueduany/react-mix自从昨天发布起来,得到了不少小伙伴的热捧,很高兴帮助大家解决了憋在心中很久的问题“如果我只会HTML,Css, ...
- ios UIView autoresizingSubview 属性
自动尺寸调整行为 当您改变视图的边框矩形时,其内嵌子视图的位置和尺寸往往也需要改变,以适应原始视图的新尺寸.如果视图的autoresizesSubviews属性声明被设置为YES,则其子视图会根据au ...
- IoC组件Unity再续~根据类型字符串动态生产对象
回到目录 这个根据类型字符串动态去生产一个接口的对象,在实现项目中用途很广,这即省去了配置config文件的麻烦,又使用生产对象变更可配置,你完全可以把这种多态持久化到数据库里或者XML文件里,在使用 ...
- 第三天 vi编辑器使用和软件安装
[复习] 判断题: 查看某文件权限为rwxr-xr-- ,则其所属组权限为只读. 对一个目录有w权限,表示可以修改目录下文件内容. 3..tar.gz格式的压缩包可以使用tar -xjf解压缩 4.m ...
- JS checkbox 全选 全不选
/* JS checkbox 全选 全不选 Html中checkbox: <input type="checkbox" name="cbx" value= ...
- CSS层模型
参考:慕课网 点此可进 如何让html元素在网页中精确定位,就像图像软件PhotoShop中的图层一样可以对每个图层能够精确定位操作.CSS定义了一组定位(positioning)属性来支持层布局模型 ...
- web工作流
web工作流之Gulp学习 Gulp.js 是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务. Gulp.js 是基于 Node.js 构建的,利用 Node.js 流的威力,你 ...
- iOS9和Xcode7
2015.06.08苹果放出了iOS9以及Xcode7的 Beta测试版本.有一句话非常引入注意: https://developer.apple.com/xcode/ Now everyone ca ...
- SQL Server中关于跟踪(Trace)那点事
前言 一提到跟踪俩字,很多人想到警匪片中的场景,同样在我们的SQL Server数据库中“跟踪”也是无处不在的,如果我们利用好了跟踪技巧,就可以针对某些特定的场景做定向分析,找出充足的证据来破案. 简 ...
- IO流-文件管理
File f = new File(“test.txt”); File的构造器不会在文件不存在的情况下新建一个文件,从File对象中创建文件是由文件流的构造器或File类的createNewFile方 ...