C#,二叉搜索树(Binary Search Tree)的迭代方法与源代码

news2024/10/5 21:16:10

1 二叉搜索树 

二叉搜索树(BST,Binary Search Tree)又称二叉查找树或二叉排序树。
一棵二叉搜索树是以二叉树来组织的,可以使用一个链表数据结构来表示,其中每一个结点就是一个对象。
一般地,除了key和位置数据之外,每个结点还包含属性Left、Right和Parent,分别指向结点的左、右子节点和父结点。
如果某个子结点或父结点不存在,则相应属性的值为空(null)。
根结点是树中唯一父指针为null的结点。
叶子结点的孩子结点指针也为null。

2 节点数据定义


    /// <summary>
    /// 二叉树的节点类
    /// </summary>
    public class BinaryNode
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; } = "";
        /// <summary>
        /// 数据
        /// </summary>
        public string Data { get; set; } = "";
        /// <summary>
        /// 左节点
        /// </summary>
        public BinaryNode Left { get; set; } = null;
        /// <summary>
        /// 右节点
        /// </summary>
        public BinaryNode Right { get; set; } = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public BinaryNode()
        {
        }
        /// <summary>
        /// 单数值构造函数
        /// </summary>
        /// <param name="d"></param>
        public BinaryNode(string d)
        {
            Name = d;
            Data = d;
        }
        public BinaryNode(int d)
        {
            Name = d + "";
            Data = d + "";
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="n"></param>
        /// <param name="d"></param>
        public BinaryNode(string n, string d)
        {
            Name = n;
            Data = d;
        }
        /// <summary>
        /// 返回邻接的三元组数据
        /// </summary>
        /// <returns></returns>
        public string[] ToAdjacency()
        {
            string adj = "";
            if (Left != null)
            {
                adj += Left.Name;
            }
            if (Right != null)
            {
                if (adj.Length > 0) adj += ",";
                adj += Right.Name;
            }
            return new string[] { Name, Data, adj };
        }
        /// <summary>
        /// 邻接表
        /// </summary>
        /// <returns></returns>
        public List<string> ToList()
        {
            return new List<string>(ToAdjacency());
        }

        public int Key
        {
            get
            {
                return Int32.Parse(Data);
            }
            set
            {
                Data = value.ToString();
            }
        }
    }
 


    /// <summary>
    /// 二叉树的节点类
    /// </summary>
    public class BinaryNode
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; } = "";
        /// <summary>
        /// 数据
        /// </summary>
        public string Data { get; set; } = "";
        /// <summary>
        /// 左节点
        /// </summary>
        public BinaryNode Left { get; set; } = null;
        /// <summary>
        /// 右节点
        /// </summary>
        public BinaryNode Right { get; set; } = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public BinaryNode()
        {
        }
        /// <summary>
        /// 单数值构造函数
        /// </summary>
        /// <param name="d"></param>
        public BinaryNode(string d)
        {
            Name = d;
            Data = d;
        }
        public BinaryNode(int d)
        {
            Name = d + "";
            Data = d + "";
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="n"></param>
        /// <param name="d"></param>
        public BinaryNode(string n, string d)
        {
            Name = n;
            Data = d;
        }
        /// <summary>
        /// 返回邻接的三元组数据
        /// </summary>
        /// <returns></returns>
        public string[] ToAdjacency()
        {
            string adj = "";
            if (Left != null)
            {
                adj += Left.Name;
            }
            if (Right != null)
            {
                if (adj.Length > 0) adj += ",";
                adj += Right.Name;
            }
            return new string[] { Name, Data, adj };
        }
        /// <summary>
        /// 邻接表
        /// </summary>
        /// <returns></returns>
        public List<string> ToList()
        {
            return new List<string>(ToAdjacency());
        }

        public int Key
        {
            get
            {
                return Int32.Parse(Data);
            }
            set
            {
                Data = value.ToString();
            }
        }
    }

3 二叉树的节点插入与搜索与验证代码

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    /// <summary>
    /// BST(二叉搜索树的迭代方法)
    /// </summary>
    public static partial class Algorithm_Gallery
    {
        public static BinaryNode Insert(BinaryNode node, int key)
        {
            if (node == null)
            {
                return new BinaryNode(key);
            }
            if (key < node.Key)
            {
                node.Left = Insert(node.Left, key);
            }
            else if (key > node.Key)
            {
                node.Right = Insert(node.Right, key);
            }
            return node;
        }

        public static int BST_Find_Floor(BinaryNode root, int key)
        {
            BinaryNode curr = root;
            BinaryNode ans = null;
            while (curr != null)
            {
                if (curr.Key <= key)
                {
                    ans = curr;
                    curr = curr.Right;
                }
                else
                {
                    curr = curr.Left;
                }
            }
            if (ans != null)
            {
                return ans.Key;
            }
            return -1;
        }

        public static int BST_Drive()
        {
            int N = 25;

            BinaryNode root = new BinaryNode("19");
            Insert(root, 2);
            Insert(root, 1);
            Insert(root, 3);
            Insert(root, 12);
            Insert(root, 9);
            Insert(root, 21);
            Insert(root, 19);
            Insert(root, 25);

            return BST_Find_Floor(root, N);
        }
    }
}
 

POWER BY TRUFFER.CN
BY 315SOFT.COM

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    /// <summary>
    /// BST(二叉搜索树的迭代方法)
    /// </summary>
    public static partial class Algorithm_Gallery
    {
        public static BinaryNode Insert(BinaryNode node, int key)
        {
            if (node == null)
            {
                return new BinaryNode(key);
            }
            if (key < node.Key)
            {
                node.Left = Insert(node.Left, key);
            }
            else if (key > node.Key)
            {
                node.Right = Insert(node.Right, key);
            }
            return node;
        }

        public static int BST_Find_Floor(BinaryNode root, int key)
        {
            BinaryNode curr = root;
            BinaryNode ans = null;
            while (curr != null)
            {
                if (curr.Key <= key)
                {
                    ans = curr;
                    curr = curr.Right;
                }
                else
                {
                    curr = curr.Left;
                }
            }
            if (ans != null)
            {
                return ans.Key;
            }
            return -1;
        }

        public static int BST_Drive()
        {
            int N = 25;

            BinaryNode root = new BinaryNode("19");
            Insert(root, 2);
            Insert(root, 1);
            Insert(root, 3);
            Insert(root, 12);
            Insert(root, 9);
            Insert(root, 21);
            Insert(root, 19);
            Insert(root, 25);

            return BST_Find_Floor(root, N);
        }
    }
}

 

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

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

相关文章

prometheus安装

https://cloud.tencent.com/developer/article/1449258 https://www.cnblogs.com/jason2018524/p/16995927.html https://developer.aliyun.com/article/1141712 prometheus docker安装 https://prometheus.io/docs/prometheus/latest/installation/ docker run --name prometh…

二.西瓜书——线性模型、决策树

第三章 线性模型 1.线性回归 “线性回归”(linear regression)试图学得一个线性模型以尽可能准确地预测实值输出标记. 2.对数几率回归 假设我们认为示例所对应的输出标记是在指数尺度上变化&#xff0c;那就可将输出标记的对数作为线性模型逼近的目标&#xff0c;即 由此&…

unity-firebase-Analytics分析库对接后数据不显示原因,及最终解决方法

自己记录一下unity对接了 FirebaseAnalytics.unitypackage&#xff08;基于 firebase_unity_sdk_10.3.0 版本&#xff09; 库后&#xff0c;数据不显示的原因及最终显示解决方法&#xff1a; 1. 代码问题&#xff08;有可能是代码写的问题&#xff0c;正确的代码如下&#xff…

分布式系统一致性与共识算法

分布式系统的一致性是指从系统外部读取系统内部的数据时&#xff0c;在一定约束条件下相同&#xff0c;即数据&#xff08;元数据&#xff0c;日志数据等等&#xff09;变动在系统内部各节点应该是一致的。 一致性模型分为如下几种&#xff1a; ① 强一致性 所有用户在任意时…

vue源码分析之nextTick源码分析-逐行逐析-错误分析

nextTick的使用背景 在vue项目中&#xff0c;经常会使用到nextTick这个api&#xff0c;一直在猜想其是怎么实现的&#xff0c;今天有幸研读了下&#xff0c;虽然源码又些许问题&#xff0c;但仍值得借鉴 核心源码解析 判断当前环境使用最合适的API并保存函数 promise 判断…

【RL】Actor-Critic Methods

Lecture 10: Actor-Critic Methods The simplest actor-critic (QAC) 回顾 policy 梯度的概念&#xff1a; 1、标量指标 J ( θ ) J(\theta) J(θ)&#xff0c;可以是 v ˉ π \bar{v}_{\pi} vˉπ​ 或 r ˉ π \bar{r}_{\pi} rˉπ​。 2、最大化 J ( θ ) J(\theta)…

计算机服务器中了DevicData勒索病毒怎么办?DevicData勒索病毒解密数据恢复

网络技术的发展与更新为企业提供了极大便利&#xff0c;让越来越多的企业走向了正规化、数字化&#xff0c;因此&#xff0c;企业的数据安全也成为了大家关心的主要话题&#xff0c;但网络是一把双刃剑&#xff0c;即便企业做好了安全防护&#xff0c;依旧会给企业的数据安全带…

Prometheus+Grafana 监控

第1章Prometheus 入门 Prometheus 受启发于 Google 的 Brogmon 监控系统&#xff08;相似的 Kubernetes 是从 Google的 Brog 系统演变而来&#xff09;&#xff0c;从 2012 年开始由前 Google 工程师在 Soundcloud 以开源软件的 形式进行研发&#xff0c;并且于 2015 年早期对…

如何在Linux搭建Inis网站,并发布至公网实现远程访问【内网穿透】

如何在Linux搭建Inis网站&#xff0c;并发布至公网实现远程访问【内网穿透】 前言1. Inis博客网站搭建1.1. Inis博客网站下载和安装1.2 Inis博客网站测试1.3 cpolar的安装和注册 2. 本地网页发布2.1 Cpolar临时数据隧道2.2 Cpolar稳定隧道&#xff08;云端设置&#xff09;2.3.…

论文阅读:How Do Neural Networks See Depth in Single Images?

是由Technische Universiteit Delft(代尔夫特理工大学)发表于ICCV,2019。这篇文章的研究内容很有趣,没有关注如何提升深度网络的性能&#xff0c;而是关注单目深度估计的工作机理。 What they find&#xff1f; 所有的网络都忽略了物体的实际大小&#xff0c;而关注他们的垂直…

全球最强开源大模型一夜易主!谷歌Gemma 7B碾压Llama 2 13B,今夜重燃开源之战

一声炸雷深夜炸响&#xff0c;谷歌居然也开源LLM了&#xff1f;&#xff01; 这次&#xff0c;重磅开源的Gemma有2B和7B两种规模&#xff0c;并且采用了与Gemini相同的研究和技术构建。 有了Gemini同源技术的加持&#xff0c;Gemma不仅在相同的规模下实现SOTA的性能。 而且更令…

嵌入式学习-qt-Day3

嵌入式学习-qt-Day3 一、思维导图 二、作业 完善对话框&#xff0c;点击登录对话框&#xff0c;如果账号和密码匹配&#xff0c;则弹出信息对话框&#xff0c;给出提示”登录成功“&#xff0c;提供一个Ok按钮&#xff0c;用户点击Ok后&#xff0c;关闭登录界面&#xff0c;跳…

Transformer 架构—Encoder-Decoder

文章目录 前言 一、Encoder 家族 1. BERT 2. DistilBERT 3. RoBERTa 4. XML 5. XML-RoBERTa 6. ALBERT 7. ELECTRA 8. DeBERTa 二、Decoder 家族 1. GPT 2. GPT-2 3. CTRL 4. GPT-3 5. GPT-Neo / GPT-J-6B 三、Encoder-Decoder 家族 1. T5 2. BART 3. M2M-100 4. BigBird 前言 …

SpringBoot---集成MybatisPlus

介绍 使用SpringBoot集成MybatisPlus框架。 第一步&#xff1a;添加MybatisPlus依赖 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.4</version> </dependenc…

MIT6.S081学习——一、环境搭建、资料搜集

MIT6.S081学习——一、环境搭建、资料搜集 1、环境准备2、资料搜集2、环境搭建2.1 Linux环境准备2.2 环境搭建2.2.1 根据官网指导代码进行相关工具的安装2.2.2 下载并且编译QEMU 3、VSCode远程连接Ubuntu3.1 安装remote-ssh3.1.1 安装插件3.1.2 配置config文件 3.2 Ubuntu安装S…

springcloud:2.OpenFeign 详细讲解

OpenFeign 是一个基于 Netflix 的 Feign 库进行扩展的工具,它简化了开发人员在微服务架构中进行服务间通信的流程,使得编写和维护 RESTful API 客户端变得更加简单和高效。作为一种声明式的 HTTP 客户端,OpenFeign 提供了直观的注解驱动方式,使得开发人员可以轻松定义和调用…

Redis突现拒绝连接问题处理总结

一、问题回顾 项目突然报异常 [INFO] 2024-02-20 10:09:43.116 i.l.core.protocol.ConnectionWatchdog [171]: Reconnecting, last destination was 192.168.0.231:6379 [WARN] 2024-02-20 10:09:43.120 i.l.core.protocol.ConnectionWatchdog [151]: Cannot reconnect…

win32 汇编读文件

做了2个小程序&#xff0c;没有读成功&#xff1b;文件打开了&#xff1b; .386.model flat, stdcalloption casemap :noneinclude windows.inc include user32.inc includelib user32.lib include kernel32.inc includelib kernel32.lib include Comdlg32.inc includelib …

Pormise---如何解决javascript中回调的信任问题?【详解】

如果阅读有疑问的话&#xff0c;欢迎评论或私信&#xff01;&#xff01; 本人会很热心的阐述自己的想法&#xff01;谢谢&#xff01;&#xff01;&#xff01; 文章目录 回调中的信任问题回调给我们带来的烦恼&#xff1f;调用过早调用过晚调用的次数太少或太多调用回调时未能…

数据结构之链表经典算法QJ题目

目录 单链表经典算法题目1. 单链表相关经典算法OJ题&#xff1a;移除链表元素思路一&#xff1a;思路二&#xff1a; 2. 单链表相关经典算法QI题&#xff1a;链表的中间节点思路一思路二 3. 单链表相关经典算法QJ题&#xff1a;反转链表思路一思路二 4. 单链表相关经典算法QJ题…