C#算法设计查找篇之05-二叉树查找
二叉树查找(Binary Tree Search)
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/706 访问。
二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
- 若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
- 若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;
- 左、右子树也分别为二叉排序树。
二叉树查找需要先生成一个二叉排序树,再遍历所有节点逐一比较其值与关键字是否相同,相同则返回;若一直找不到,则返回-1。
示例:
public class BSTNode {
public int Key { get; set; }
public int Index { get; set; }
public BSTNode Left { get; set; }
public BSTNode Right { get; set; }
public BSTNode(int key, int index) {
Key = key;
Index = index;
}
public void Insert(int key, int index) {
var tree = new BSTNode(key, index);
if (tree.Key <= Key) {
if (Left == null) {
Left = tree;
}
else {
Left.Insert(key, index);
}
}
else {
if (Right == null) {
Right = tree;
}
else {
Right.Insert(key, index);
}
}
}
public int Search(int key) {
//找左子节点
var left = Left?.Search(key);
if (left.HasValue && left.Value != -1) return left.Value;
//找当前节点
if (Key == key) return Index;
//找右子节点
var right = Right?.Search(key);
if (right.HasValue && right.Value != -1) return right.Value;
//找不到时返回-1
return -1;
}
}
public class Program {
public static void Main(string[] args) {
int[] array = { 43, 69, 11, 72, 28, 21, 56, 80, 48, 94, 32, 8 };
Console.WriteLine(BinaryTreeSearch(array));
Console.ReadKey();
}
public static int BinaryTreeSearch(int[] array) {
var bstNode = new BSTNode(array[0], 0);
for (int i = 1; i < array.Length; i++) {
bstNode.Insert(array[i], i);
}
return bstNode.Search(80);
}
}
以上是二叉树查找算法的一种实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/706 访问。
7
分析:
在最坏的情况下二叉树查找的时间复杂度为: ,在平均情况下的时间复杂度为:
。
C#算法设计查找篇之05-二叉树查找的更多相关文章
- C#算法设计排序篇之04-选择排序(附带动画演示程序)
选择排序(Selection Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/681 访问. 选择排序是一种简 ...
- C#算法设计查找篇之02-二分查找
二分查找(Binary Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/699 访问. 二分查找也称折半查 ...
- Python数据结构与算法设计总结篇
1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...
- C#算法设计查找篇之03-插值查找
插值查找(Interpolation Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/701 访问. 插值 ...
- C#算法设计查找篇之01-顺序查找
顺序查找(Sequential Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/697 访问. 顺序查找也 ...
- C#算法设计排序篇之06-堆排序(附带动画演示程序)
堆排序(Heap Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/685 访问. 堆排序是指利用堆积树(堆)这 ...
- C#算法设计排序篇之11-二叉树排序(附带动画演示程序)
二叉树排序(Binary Tree Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/695 访问. 二叉树排序 ...
- C#算法设计排序篇之09-基数排序(附带动画演示程序)
基数排序(Radix Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/691 访问. 基数排序属于" ...
- C#算法设计排序篇之08-计数排序(附带动画演示程序)
计数排序(Counting Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/689 访问. 计数排序是一个非基 ...
随机推荐
- OSCP Learning Notes - WebApp Exploitation(4)
Local File Inclusion[LFI] Target Pentester Lab: Download from the following website: https://www.vul ...
- 跳过Google开机设置/验证/向导
Google 的开机设置向导,亦或称作开机验证,对于刷机党来说最熟悉不过了.一般情况下,刷类原生或是原生系统,再刷 Gapps,开机就需要进行一些 Google 验证.这些验证,与国内的手机厂商所设置 ...
- 理解Linux的硬链接与软链接-转载
理解Linux的硬链接与软链接 来自:https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/index.html
- .Net Core 项目开发中的Errors,Exceptions
这个错误是在连接数据库的时候,没有找到对应的表, namespace TodoApi.Models { public class TodoContext : DbContext { public To ...
- Linux上搭建文件浏览的web服务(创建软件仓库)(一)
软件仓库的创建方式有很多,这是一种很简单的创建方式: python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web服务. 使用:Python SimpleH ...
- Get与Post的区别?(面试官最想听到的答案)
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- numpy的random方法和常用数据类型
NumPy 的常用数据类型 np.random 随机数模块
- Seaborn实现多变量分析
import seaborn as sns import numpy as np import pandas as pd import matplotlib.pyplot as plt sns.set ...
- PHP acos() 函数
实例 返回不同数的反余弦: <?phpecho(acos(0.64) . "<br>");echo(acos(-0.4) . "<br>&q ...
- PHP str_ireplace() 函数
实例 把字符串 "Hello world!" 中的字符 "WORLD"(不区分大小写)替换成 "Peter": <?php高佣联盟 w ...