首页 > 其他分享 >二叉搜索树

二叉搜索树

时间:2024-10-19 14:21:23浏览次数:1  
标签:node Node Right Parent 二叉 current 搜索 null

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

标签:node,Node,Right,Parent,二叉,current,搜索,null
From: https://www.cnblogs.com/readafterme/p/18475838

相关文章

  • Notepad++将搜索内容所在行选中,并进行复制等操作
    背景Notepad++在非常多的数据行内容中,按照指定内容检索,并定位到具体行,而后对内容行的数据进行复制、剪切、删除等处理动作。操作说明检索并标记所在行弹出搜索框:按下Ctrl+F。输入查找字符串:在搜索框中输入要查找的字符串。标记记录:在查找框顶部菜单中选择【标......
  • 111. 二叉树的最小深度
    思路递归时考虑几种情况:1.左右子树都为空,则最小深度=1(只有根节点)(也可理解为min(0,0)+1)2.左子树为空,右子树不空,则最小深度=右子树最小深度+13.左子树不为空,右子树为空,最小深度=左子树最小深度+14.左右子树不为空,最小深度=左右子树最小深度+1+1原因:递归的是左右子树,......
  • 代码随想录算法训练营day19| 235. 二叉搜索树的最近公共祖先 701.二叉搜索树中的插
    学习资料:https://programmercarl.com/0235.二叉搜索树的最近公共祖先.html****学习记录:235.二叉搜索树的最近公共祖先(加一个函数traversal)点击查看代码#Definitionforabinarytreenode.#classTreeNode(object):#def__init__(self,x):#self.val=x......
  • 代码随想录算法训练营day18 |530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数
    学习资料:https://programmercarl.com/0530.二叉搜索树的最小绝对差.html530.二叉搜索树的最小绝对差(双指针法,pre&cur,设置最小差值初始为无穷大,当差值<最小差值就更新最小差值)点击查看代码#Definitionforabinarytreenode.#classTreeNode(object):#def__init__(......
  • 二叉查找树和笛卡尔树
    目录二叉查找树定义作用操作查找插入删除缺点笛卡尔树定义操作构造二叉查找树定义​ 二叉查找树(BinarySearchTree,BST),又名二叉搜索树或二叉排序树。​ 它是一类特殊规定的二叉树,它应当满足以下条件:每个节点有唯一确定的权值非叶子节点的权值比其左子树中所有节点权值大非......
  • 图论之搜索遍历
    前言一个重要的板块,倒是有很多有趣的题,从搜索开始吧MazeTacToeS暴力即可,\(3^9\times25\times25\)绰绰有余,把状态转换为三进制\(dfs\)ConnectedComponents?根据鸽巢原理,必定有一个点被割去的边\(\le\frac{m}{n}\),然后我们找到这个点,对于连接他的边均在同一个联......
  • 二叉树和度为二的有序树的区别
    一、定义与结构度为二的有序树:在这种树结构中,每个节点最多有两个子节点。子节点的顺序是重要的,即使两个子节点的值相同,只要他们的位置不同,他们就被视为是不同的子节点。当一个节点只有一个子节点时,该子节点的位置(左或右)并无特定要求,也即无需区分其左右次序。二叉树:二叉树......
  • RRT*路径搜索算法matlab代码
    一、算法简介      RRT*路径搜索算法相比于RRT路径搜索算法多了重选父节点和重布线的过程:二、实现效果对比(比RRT算法更光滑) RRT路径搜索算法实现效果RRT*路径搜索算法实现效果三、代码完整代码私聊!......
  • 信息收集-IP查询和利用搜索引擎收集
    IP查询IP地址定位:高精度IP定位4-openGPS.cn利用搜索引擎搜集信息建议用bing搜索语法:关键词:关键内容索引描述inurl:搜索包含指定url的网页intitle:限制搜索的网页标题intext:只搜索网页部分包含的文字(忽略标题、url)site:限制搜索想要的域名filetyp......
  • 【智能算法应用】引力搜索算法求解二维路径规划问题
    摘要引力搜索算法(GSA)是一种基于引力学说的启发式算法,用于解决复杂的优化问题。本文应用GSA于二维路径规划问题,通过优化路径来避开障碍物并达到目标点。实验结果表明,GSA在路径规划中具有良好的表现,尤其在多障碍场景中,其优化路径平滑且避障效果显著。理论引力搜索算法是......