二叉树排序(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-二叉树排序(附带动画演示程序)的更多相关文章

  1. C#算法设计排序篇之04-选择排序(附带动画演示程序)

    选择排序(Selection Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/681 访问. 选择排序是一种简 ...

  2. Python数据结构与算法设计总结篇

    1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...

  3. C#算法设计排序篇之06-堆排序(附带动画演示程序)

    堆排序(Heap Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/685 访问. 堆排序是指利用堆积树(堆)这 ...

  4. C#算法设计排序篇之09-基数排序(附带动画演示程序)

    基数排序(Radix Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/691 访问. 基数排序属于" ...

  5. C#算法设计排序篇之08-计数排序(附带动画演示程序)

    计数排序(Counting Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/689 访问. 计数排序是一个非基 ...

  6. C#算法设计排序篇之07-希尔排序(附带动画演示程序)

    希尔排序(Shell's Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/687 访问. 希尔排序是插入排序的 ...

  7. C#算法设计排序篇之05-归并排序(附带动画演示程序)

    归并排序(Merge Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/683 访问. 归并排序是建立在归并操作 ...

  8. C#算法设计排序篇之03-直接插入排序(附带动画演示程序)

    直接插入排序(Straight Insertion Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/679 访 ...

  9. C#算法设计排序篇之02-快速排序(附带动画演示程序)

    快速排序(Quick Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/677 访问. 快速排序由C. A. R ...

随机推荐

  1. GPO - General GPO Settings(1)

    Prohibit access to Control Panel and PC settings Disable GPO for Administrators and /or User Groups  ...

  2. OSCP Learning Notes - Exploit(2)

    Compiling an Exploit Exercise: samba exploit 1. Search and download the samba exploit source code fr ...

  3. Python Ethical Hacking - WEB PENETRATION TESTING(5)

    Guessing Login Information on Login Pages Our target website: http://10.0.0.45/dvwa/login.php #!/usr ...

  4. package.json中dependencies和devDependencies区别

    package.json中dependencies和devDependencies区别 dependencies: 应用能够正常运行依赖的包.用户发布环境,依赖的包不仅开发环境能够使用,生产环境也能使 ...

  5. nginx 日志功能详解

    nginx 日志功能 在 nginx 中有两种日志: access_log:访问日志,通过访问日志可以获取用户的IP.请求处理的时间.浏览器信息等 error_log:错误日志,记录了访问出错的信息, ...

  6. python-闭包和装饰器-02-装饰器(decorator)

    装饰器(decorator) 理解了上一章的闭包之后,装饰器就是闭包的一种应用,只是外部函数的参数传入的不是普通的变量类型,而是传入一个函数名.装饰器一般用于:不修改被装饰函数(即外部函数传入的参数) ...

  7. stringsream用法

    stringstream: 头文件: #include <sstream> 简单整理一下这玩意的作用,主要有三个吧. 类型转化 字符串拼接 字符串整合(这一个用处特别大!!!!!!!) 先 ...

  8. springboot整合邮件发送(163邮箱发送为例)

    先登录163邮箱获取授权  勾选后安装提示会叫你设置授权密码之类的:记住授权的密码 1.引入maven依赖 <dependency> <groupId>org.springfr ...

  9. SpringMVC学习之使用注解编写SpringMVC程序

    SpringMVC介绍 Spring的web框架围绕DispatcherServlet设计.DispatcherServlet的作用是将请求分发到不同的处理器.从Spring 2.5开始,使用Java ...

  10. PHP array_uintersect_assoc() 函数

    实例 比较两个数组的键名和键值(使用内建函数比较键名,使用用户自定义函数比较键值),并返回交集: <?phpfunction myfunction($a,$b){if ($a===$b){ret ...