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

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.mfbz.cn/a/394518.html

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

相关文章

基于 Python 深度学习的车辆特征分析系统,附源码

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

算法之力扣数青蛙

题目连接 文章目录 题目解析算法原理第一步第二步第三步第三步第四步指向o 代码讲解代码实现 题目解析 先给大家来讲解一下这个题目的意思吧&#xff0c;这个题目是说呢给你一个蛙叫的字符串让你去设计一个算法求出发出这种蛙叫最少需要几只青蛙。比如说第一个样例发出这种叫声…

端口号被占用怎么解决

1、快捷键"winR"打开运行&#xff0c;在其中输入"cmd"命令&#xff0c;回车键打开命令提示符。 2、进入窗口后&#xff0c;输入"netstat -ano"命令&#xff0c;可以用来查看所有窗口被占用的情况。 比如端口号为7680的端口被占用了&#xff0c…

Python入门:常用模块—logging模块

logging日志的分级&#xff1a; debug(),info(),warning(),error(),critical() 5个级别 最简单用法 1 2 3 4 import logging logging.warning("user [mike] attempted wrong password more than 3 times") logging.critical("server is down") 输出&…

【洛谷题解】P1601 A+B Problem(高精)

题目链接&#xff1a;AB Problem&#xff08;高精&#xff09; - 洛谷 题目难度&#xff1a;普及- 涉及知识点&#xff1a;高精度加法 题意&#xff1a; 分析&#xff1a;直接套用高精度加法模版即可 AC代码&#xff1a; #include<bits/stdc.h> using namespace std…

【搭建跨境电商独立站】跨境电商独立站的6大模式,任你选择!

在几年前跨境电商独立站和第三方平台基本上是同步发展起来的&#xff0c;但在后期的发展过程中&#xff0c;独立站经过不同时期的革新&#xff0c;形成了自己的模式。 当你准备好搭建独立站的时候&#xff0c;首先你需要了解的就是独立站运营的模式类型&#xff0c;并找到最适合…

LiveGBS流媒体平台GB/T28181功能-redis订阅国标设备状态redis订阅通道状态subscribe device操作及示例

支持Redis订阅国标设备状态及国标通道状态上线离线 1、设备状态监听的烦恼2、device订阅2.1、设备上线消息2.2、设备离线消息2.2、通道上线消息2.2、通道离线消息 3、订阅示例3.1、连接REDIS3.2、订阅device示例3.3、设备上线示例3.3.1、注册上线后 3.4、设备离线示例3.4.1、注…

批评openai

杨立昆对OpenAI的批评主要集中在几个方面。首先&#xff0c;他反驳了OpenAI首席科学家Ilya Sutskever关于AI可能已经拥有某种自主意识的观点。杨立昆认为&#xff0c;当前的神经网络并不具备这种特定宏架构&#xff0c;因此无法拥有自主意识。他强调&#xff0c;即使是最轻微的…

Stable Diffusion系列(六):原理剖析——从文字到图片的神奇魔法(潜空间篇)

文章目录 LDM概述原理模型架构自编码器模型扩散模型条件引导模型图像生成过程 实验结果指标定义IS&#xff08;越大越好&#xff09;FID&#xff08;越小越好&#xff09; 训练成本与采样质量分析不带条件的图片生成基于文本的图片生成基于语义框的图片生成基于语义图的图片生成…

MyBatis-Plus:通用分页实体封装

分页查询实体&#xff1a;PageQuery package com.example.demo.demos.model.query;import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.Data; import org.springframework.util.St…

milvus insert api的数据结构源码分析

insert api的数据结构 一个完整的insert例子: import numpy as np from pymilvus import (connections,FieldSchema, CollectionSchema, DataType,Collection, )num_entities, dim 10, 3print("start connecting to Milvus") connections.connect("default&q…

基于SSM的电影购票系统(有报告)。Javaee项目。ssm项目。

演示视频&#xff1a; 基于SSM的电影购票系统&#xff08;有报告&#xff09;。Javaee项目。ssm项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring Spri…

Eclipse 分栏显示同一文件

Eclipse 分栏显示同一文件 1. Window -> EditorReferences 1. Window -> Editor Toggle Split Editor (Horizontal) &#xff1a;取消或设置水平分栏显示 Toggle Split Editor (Vertical) &#xff1a;取消或设置垂直分栏显示 References [1] Yongqiang Cheng, https:/…

综合特征融合的实用图像恢复技术-CMFNet

综合特征融合的实用图像恢复技术-CMFNet 综合特征融合的实用图像恢复技术-CMFNet项目背景与意义模型架构与关键思想代码实现与功能函数恢复效果展示参考资料 综合特征融合的实用图像恢复技术-CMFNet 图像恢复一直是计算机视觉领域的重要研究方向之一。它涵盖了诸多任务&#x…

K8s服务发现组件之CoreDNS/NodeLocalDNS /kubeDNS

1 coredns 1.1 概述 1.1.1 什么是CoreDNS CoreDNS 是一个灵活可扩展的 DNS 服务器&#xff0c;可以作为 Kubernetes 集群 DNS&#xff0c;在Kubernetes1.12版本之后成为了默认的DNS服务。 与 Kubernetes 一样&#xff0c;CoreDNS 项目由 CNCF 托管。 coredns在K8S中的用途,…

Docker原理及概念相关

Docker最核心的组件 image&#xff1a;镜像&#xff0c;构建容器&#xff0c;也可以通过Dockerfile文本描述镜像的内容。 (我们将应用程序运行所需的环境&#xff0c;打包为镜像文件) Container&#xff1a;容器 (你的应用程序&#xff0c;就跑在容器中 ) 镜像仓库(dockerhub)(…

云原生之容器编排实践-在K8S集群中使用Registry2搭建私有镜像仓库

背景 基于前面搭建的3节点 Kubernetes 集群&#xff0c;今天我们使用 Registry2 搭建私有镜像仓库&#xff0c;这在镜像安全性以及离线环境下运维等方面具有重要意义。 Note: 由于是测试环境&#xff0c;以下创建了一个 local-storage 的 StorageClass &#xff0c;并使用本地…

CSP-201812-1-小明上学

CSP-201812-1-小明上学 解题思路 #include <iostream> using namespace std; int main() {int red, yellow, green, n, timeSum 0;cin >> red >> yellow >> green;cin >> n;for (int i 0; i < n; i){int flag, time;cin >> flag &g…

什么是“感知机”?

感知机&#xff08;神经网络和支持向量机的理论基础&#xff09; 概念&#xff1a;简单来说&#xff0c;感知机就是一个旨在建立一个线性超平面对线性可分的数据集进行分类的线性模型 分类&#xff1a; 单层感知机多层感知机&#xff08; Multi-Layer Perceptron&#xff0c…

不同品牌和种类的电容与电感实测对比(D值、Q值、ESR、X)

最近买了个LCR电桥&#xff0c;就想测一下手头上的各种电容电感的参数&#xff0c;对比一下。 测试设备是中创ET4410&#xff0c;测量的参数有&#xff1a;电容值、电感值、D(损耗角正切值)、Q(品质因数)、ESR(等效串联电阻)、X(电抗&#xff0c;通常表示为感抗XL或容抗XC)。 …
最新文章