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.hqwc.cn/news/483736.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

uni-app 开发调试自动打开手机屏幕大小界面(Aidex移动端开发项目)

上效果&#xff1a; 下载Aidex的移动端项目并打开&#xff1a; 若依-ruoyi-AiDex-Uniapp: 若依-Ruoyi APP 移动解决方案&#xff0c;基于uniappuView封装的一套基础模版&#xff0c;开箱即用&#xff0c;免费开源&#xff0c;一份代码多终端适配&#xff0c;支持H5、支付宝小程…

Kotlin基础 7

1.apply函数详解 1.1. DSL /*** 为什么要传入扩展函数(泛型),而不是一个普通的匿名函数* T.()->Unit* 扩展函数里自带了接收者对象的this隐式调用* 为什么是泛型的扩展函数?* 因为是由this 隐式调用 this 类型就是泛型类型&#xff0c; 相当于this的扩展函数&#xff0c;…

Aidex移动端项目入门

运行效果 项目源码下载 若依-ruoyi-AiDex-Uniapp: 若依-Ruoyi APP 移动解决方案&#xff0c;基于uniappuView封装的一套基础模版&#xff0c;开箱即用&#xff0c;免费开源&#xff0c;一份代码多终端适配&#xff0c;支持H5、支付宝小程序、微信小程序、APP&#xff0c;实现了…

解决MobaXterm网络错误连接超时问题

报错页面&#xff1a; 报错原因&#xff1a; ①网络断开了 ②网络端口&#xff0c;端口号改变 解决办法&#xff1a; ①重新连接网络按R ②固定端口号 第一步&#xff1a;编辑------>虚拟机网络编辑器&#xff08;我的Linux在虚拟机里&#xff09; 第二步&#xff1a;用…

Chrome插件精选 — 缓存清理

Chrome实现同一功能的插件往往有多款产品&#xff0c;逐一去安装试用耗时又费力&#xff0c;在此为某一类型插件挑选出比较好用的一款或几款&#xff0c;尽量满足界面精致、功能齐全、设置选项丰富的使用要求&#xff0c;便于节省一个个去尝试的时间和精力。 1. Chrome清理大师…

如何画架构图:从概念到实践

如何画架构图&#xff1a;从概念到实践 免费在线作图工具&#xff0c;点击邀请链接注册赠送7天会员&#xff1a;https://www.processon.com/u/5d3967afe4b0208611113845 在软件开发和系统设计中&#xff0c;架构图是一种重要的工具&#xff0c;它能够帮助开发人员和利益相关者…

嵌入式Qt 计算器核心算法_2

一.中缀表达式转后缀表达式 中缀表达式是最常用的算术表达式形式——运算符在运算数中间。但运算时需要考虑运算符优先级。 ​后缀表达式是计算机容易运算的表达式&#xff0c;运算符在运算数后面&#xff0c;从左到右进行运算,无需考虑优先级,运算呈线性结构。 1 2 * 3// …

打败茅台的“老酒”

作者&#xff1a;翻篇 琥珀酒研社快评&#xff1a; 最可恨的 从来不是什么强大敌人 而是美名其曰的猪队友 要不怎么有网友说 酒鬼酒太惨了 当年要不是败给内鬼 又曝出塑化剂事件 错过白酒发展的黄金十年 不说打败茅台、五粮液 但成为另一个茅台、五粮液 那完全有希…

Vue3_基础使用_4_路由器Router

概念&#xff1a; 路由&#xff1a;是一个key-value的对应关系叫路由。 路由器&#xff1a;管理多个路由的集合或者叫设备称为路由器。 由于现在组件替代了以前的mvc中的cshtml, 组件的菜单切换也不用我手动去写&#xff0c;vue给我们通过配置完成。 实现简单的路由跳转&…

10.vue学习笔记(组件数据传递-props回调函数子传父+透传Attributes+插槽slot)

文章目录 1.组件数据传递2.透传Attributes&#xff08;了解&#xff09;禁用Attributes继承 3.插槽slot 1.组件数据传递 我们之前讲解过了组件之间的数据传递&#xff0c;props 和 自定义事件 两种方式 props&#xff1a;父传子 自定义事件&#xff1a;子传父 props通过额外方…

通过eeprom验证FPGA实现的单字节/页读写IIC接口时序

1、概括 前文设计基于FPGA的IIC接口模块&#xff0c;本文将使用eeprom来验证该模块的设计。为了便于查看读写波形&#xff0c;采用两个按键来控制对eeprom数据的读写&#xff0c;当按键0按下后&#xff0c;FPGA向eeprom的前64个存储地址写入地址对应的数据&#xff0c;当按键1按…

2024 全国水科技大会暨第二届智慧水环境管理与技术创新论坛

论坛二&#xff1a;第二届智慧水环境管理与技术创新论坛 召集人&#xff1a;刘炳义 武汉大学智慧水业研究所所长、教授 为贯彻落实中共中央国务院印发《数字中国建设整体布局规划》和国务院关于印发《“十四五”数字经济发展规划》的通知&#xff0c;推动生态环境智慧治理&…