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 访问. 计数排序是一个非基 ...
随机推荐
- ATX 学习 (二)-Atx Weditor
1.Atx的安装 安装adb使用以下命令安装atx最新版pip install --pre -U uiautomator2 手机接到电脑上之后,需要先运行一下命令:python -m uiautoma ...
- 在docker中写个Hello World
Hello World Docker 示例 准备hello.cpp #include<stdio.h> int main(){ printf("Hello World Docke ...
- python爬虫入门(1)----- requests
介绍 requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 基本使用 requests.get("http://www.baidu.com") ...
- SpringBoot + Spring Cloud Eureka 服务注册与发现
什么是Spring Cloud Eureka Eureka是Netflix公司开发的开源服务注册发现组件,服务发现可以说是微服务开发的核心功能了,微服务部署后一定要有服务注册和发现的能力,Eureka ...
- Golang获取目录下的文件及目录信息
一.获取当前目录下的文件或目录信息(不包含多级子目录) func main() { pwd,_ := os.Getwd() //获取当前目录 //获取文件或目录相关信息 fileInfoList ...
- nginx Dockerfile
FROM centos MAINTAINER zengxh RUN yum install -y epel-release vim pcre-devel wget net-tools gcc zlib ...
- c++ string类find总结
c++ string类的find:1.find string s = "My cat's breath smells like cat food."; int a=s.find(& ...
- CentOS6下yum搭建LNMP环境
1.关闭防火墙[root@CentOS ~]# chkconfig iptables off 2.关闭selinuxvi /etc/sysconfig/selinux //将SELINUX=enfor ...
- 如何单页面不引用移动端的适配 (postcss)
由于pc端移动端同时开发所以同时有vant跟elementui,我的pc端登录界面直接引用之前项目做的 因为postcss全局引用,全局的px会自动转换自适应,然后页面的布局就呈现了放大的趋势, 查阅 ...
- PHP xml_parse() 函数
定义和用法 xml_parse() 函数解析 XML 文档.高佣联盟 www.cgewang.com 如果成功,该函数则返回 TRUE.如果失败,则返回 FALSE. 语法 xml_parse(par ...