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);
        }
    }
}

 

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 19:20:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 19:20:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 19:20:03       82 阅读
  4. Python语言-面向对象

    2024-02-20 19:20:03       91 阅读

热门阅读

  1. Sql Server 视图

    2024-02-20 19:20:03       51 阅读
  2. K8s Deployment挂载ConfigMap权限设置

    2024-02-20 19:20:03       64 阅读
  3. 练习:鼠标类设计之2_类和接口

    2024-02-20 19:20:03       51 阅读
  4. 基于python+django+vue.js开发的健身房管理系统

    2024-02-20 19:20:03       48 阅读
  5. 某movie搜索接口

    2024-02-20 19:20:03       45 阅读
  6. 深入理解C语言中的联合体(union)

    2024-02-20 19:20:03       45 阅读
  7. js之事件循环

    2024-02-20 19:20:03       50 阅读
  8. Qt之QChar类的字符判断

    2024-02-20 19:20:03       40 阅读
  9. Qt标准对话框设置

    2024-02-20 19:20:03       50 阅读
  10. 算法训练营day31,贪心算法5

    2024-02-20 19:20:03       52 阅读