二叉搜索树

news/2024/10/19 14:28:28
public class BinarSearchTree
{private Node _tree;public BinarSearchTree(List<int> datas){if (datas != null){foreach (var item in datas){Insert(item);}}}/// <summary>/// 插入/// </summary>/// <param name="data"></param>public void Insert(int data){if (_tree == null){_tree = new Node(data);}else{var current = _tree;while (current != null){if (data > current.Data){if (current.Right == null){current.Right = new Node(data, current);break;}current = current.Right;}else if (data < current.Data){if (current.Left == null){current.Left = new Node(data, current);break;}current = current.Left;}}}}/// <summary>/// 删除/// </summary>/// <param name="data"></param>public void Delete(int data){var node = Find(data);if (node == null) return;if (node.Left == null && node.Right == null){if (node.Parent == null){_tree = null;}else{// 直接删除即可DeleteFromParent(node);}}else if (node.Left != null && node.Right != null){if (node.Parent == null){_tree = null;}else{// 查找右子树最大节点var minNode = node.Right;var currentNode = node.Right;while (currentNode != null){minNode = currentNode;currentNode = minNode.Left;}DeleteFromParent(minNode);ReplaceChildNode(node, minNode);}}else{// 有一个子节点,则直接将子节点挂到父节点下var child = node.Left ?? node.Right;if (node.Parent == null){_tree = child;}else{ReplaceChildNode(node, child);}}}private void ReplaceChildNode(Node node, Node newNode){newNode.Parent = node.Parent;if (node.Parent.Left == node){node.Parent.Left = newNode;}if (node.Parent.Right == node){node.Parent.Right = newNode;}}private void DeleteFromParent(Node node){if (node.Parent.Left == node){node.Parent.Left = null;}if (node.Parent.Right == node){node.Parent.Right = null;}}/// <summary>/// 查找/// </summary>/// <param name="data"></param>/// <returns></returns>public Node Find(int data){var current = _tree;while (current != null){if (data > current.Data){current = current.Right;}else if (data < current.Data){current = current.Left;}else{return current;}}return null;}
}public class Node
{public Node(int data, Node parent = null){this.Data = data;Parent = parent;}public int Data { get; set; }public Node Left { get; set; }public Node Right { get; set; }public Node Parent { get; set; }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/73445.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

航飞参数计算

作者:太一吾鱼水 宣言:在此记录自己学习过程中的心得体会,同时积累经验,不断提高自己! 声明:博客写的比较乱,主要是自己看的。如果能对别人有帮助当然更好,不喜勿喷! 文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家…

第4课 SVN

1、svn的定义: svn是一个开放源代码的版本控制系统,通过采用分支管理系统的高效管理,简而言之就是用于多个人共同开发同一个项目,实现共享资源,实现最终集中式管理。 2.snv的作用: 在项目中对需求规格说明书,测试用例,代码,以及项目项目的文件进项管理和分享。 3、svn…

npm run的时候报错: this[kHandle] = new _Hash(algorithm, xofLen);

在前面加入以下配置信息 set NODE_OPTIONS=--openssl-legacy-provider && 后面跟原来的启动配置信息 凡哥,别他妈吹牛逼了

MiGPT让你的小爱音响更聪明hA

合集 - 经验分享(29)1.记一次由于操作失误致使数据库瘫痪的故障分析与解决方案2023-09-082.网络之谜:记一次失败排查的故事2023-11-153.你是否想知道如何应对高并发?Go语言为你提供了答案!2023-12-294.2023年终总结:拉帮结伙,拼搏探索新机遇2023-12-305.谁说后端不能画出美…

Nuxt.js 应用中的 app:templatesGenerated 事件钩子详解

title: Nuxt.js 应用中的 app:templatesGenerated 事件钩子详解 date: 2024/10/19 updated: 2024/10/19 author: cmdragon excerpt: app:templatesGenerated 是 Nuxt.js 的一个生命周期钩子,在模板编译到虚拟文件系统(Virtual File System, VFS)之后被调用。这个钩子允许…

链路与应用负载

为什么需要负载 如今越来越多的服务选择上云 加入到互联网 方便人们的使用 人们对服务的访问质量要求更高 对于高可靠性:电源: 往往采取双电源模式 当电源出现故障 网络不会陷入瘫痪线路: 有静态聚合 将多条线路逻辑变成一条线路 数据包会负载均衡的形式从多条逻辑成一条的链路…

HTTP客户端框架之UniHttp讲解

目录1 UniHttp1.1 简介1.1.1 前言1.1.2 简介1.2 简单使用1.2.1 引入依赖1.2.2 对接接口1.2.3 声明定义 HttpAPI 包扫描路径1.2.4 依赖注入使用即可1.3 说明介绍1.3.1 @HttpApi注解1.3.2 @HttpInterface注解1.3.3 @Par注解1.3.3.1 @QueryPar注解1.3.3.2 @PathPar注解1.3.3.3 @He…