C#算法设计排序篇之11-二叉树排序(附带动画演示程序)
二叉树排序(Binary Tree Sort)
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/695 访问。
二叉树排序是构建在二叉排序树(Binary Sort Tree)上的算法,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
- 若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
- 若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;
- 左、右子树也分别为二叉排序树。
二叉树排序需要先生成一个二叉排序树,再使用中序遍历输出所有数据。
示例:
public class BinarySortTreeNode {
public int Key { get; set; }
public BinarySortTreeNode Left { get; set; }
public BinarySortTreeNode Right { get; set; }
public BinarySortTreeNode(int key) {
Key = key;
}
public void Insert(int key) {
var tree = new BinarySortTreeNode(key);
if (tree.Key <= Key) {
if (Left == null) {
Left = tree;
}
else {
Left.Insert(key);
}
}
else {
if (Right == null) {
Right = tree;
}
else {
Right.Insert(key);
}
}
}
/// <summary>
/// 中序遍历
/// </summary>
public void InorderTraversal() {
Left?.InorderTraversal();
Console.Write($"{Key} ");
Right?.InorderTraversal();
}
}
public class Program {
public static void Main(string[] args) {
int[] array = { 43, 69, 11, 72, 28, 21, 56, 80, 48, 94, 32, 8 };
BinaryTreeSort(array);
Console.ReadKey();
}
public static void BinaryTreeSort(int[] array) {
var binarySortTreeNode = new BinarySortTreeNode(array[0]);
for (int i = 1; i < array.Length; i++) {
binarySortTreeNode.Insert(array[i]);
}
binarySortTreeNode.InorderTraversal();
}
}
以上是二叉树排序算法的一种实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/695 访问。
8 11 21 28 32 43 48 56 69 72 80 94
分析:
二叉树排序算法的时间复杂度为: 。
AlgorithmMan:
AlgorithmMan by Iori,AlgorithmMan是使用C#开发的一套用于算法演示的工具。
下载链接:AlgorithmMan-BinaryTreeSort
C#算法设计排序篇之11-二叉树排序(附带动画演示程序)的更多相关文章
- C#算法设计排序篇之04-选择排序(附带动画演示程序)
选择排序(Selection Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/681 访问. 选择排序是一种简 ...
- Python数据结构与算法设计总结篇
1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...
- C#算法设计排序篇之06-堆排序(附带动画演示程序)
堆排序(Heap Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/685 访问. 堆排序是指利用堆积树(堆)这 ...
- C#算法设计排序篇之09-基数排序(附带动画演示程序)
基数排序(Radix Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/691 访问. 基数排序属于" ...
- C#算法设计排序篇之08-计数排序(附带动画演示程序)
计数排序(Counting Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/689 访问. 计数排序是一个非基 ...
- C#算法设计排序篇之07-希尔排序(附带动画演示程序)
希尔排序(Shell's Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/687 访问. 希尔排序是插入排序的 ...
- C#算法设计排序篇之05-归并排序(附带动画演示程序)
归并排序(Merge Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/683 访问. 归并排序是建立在归并操作 ...
- C#算法设计排序篇之03-直接插入排序(附带动画演示程序)
直接插入排序(Straight Insertion Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/679 访 ...
- C#算法设计排序篇之02-快速排序(附带动画演示程序)
快速排序(Quick Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/677 访问. 快速排序由C. A. R ...
随机推荐
- Burp Suite Intruder Module - 攻击模块
参考链接:https://portswigger.net/burp/documentation/desktop/tools/intruder/using 主要思路:在Intruder模块下设定Targ ...
- ASP.NET CORE之中间件-自定义异常中间件
参考资料:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1 1.一般A ...
- CppUnit使用和源码解析
前言 CppUnit是一个开源的单元测试框架,支持Linux和Windows操作系统,在linux上可以直接进行源码编译,得到动态库和静态库,直接链接就可以正常使用,在Windows上可以使用VC直接 ...
- 04 . Filebeat简介原理及配置文件和一些案例
简介 Beats轻量型数据采集器 Beats 平台集合了多种单一用途数据采集器.它们从成百上千或成千上万台机器和系统向 Logstash 或 Elasticsearch 发送数据. Beats系列 全 ...
- 推特(Twitter)如何绑定谷歌二次验证码/谷歌身份验证/双重认证?
1.下载Twitter,找到双重验证界面 手机连接VPN下载Twitter(获取免费VPN可加微信客服“Ecyzm-”),注册登陆后,点左上角账户头像-Settings and privacy - A ...
- Spring bean初始化
- Django序列化组件Serializers详解
本文主要系统性的讲解django rest framwork 序列化组件的使用,基本看完可以解决工作中序列化90%的问题,写作参考官方文档https://www.django-rest-framewo ...
- 详解 awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}每个字段的意思
用这个列子说好了如果NF代表字段 那最后应该是7 才对啊 还有最后怎么都是1呢?END前面的是查看并发吧 后面是查看 tcp连接数 是这样吗? awk下标采用字符串来表示可能你在其它语言见 ...
- hcharts生成图表
借助hcharts插件,可以很方便地在模板页面中生成图表.类似插件还有echarts. 补充...
- 为PhpStorm添加Laravel 代码智能提示功能
php artisan clear-compiled //清除bootstrap/compiled.php php artisan ide-helper:generate //为 Facades 生成 ...