【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现(三)

简介: 【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现

【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现(二)https://developer.aliyun.com/article/1617405


六、Binary_Search_Tree.h

#pragma once
#include <string>
template<class K>
    struct  BSTreeNode
    {
        BSTreeNode(const K& key = K())
            :_left(nullptr)
                , _right(nullptr)
                , _key(key)
            {}
        BSTreeNode<K>* _left;
        BSTreeNode<K>* _right;
        K _key;
    };
template<class K>
    class  BSTree
    {
        public:
        typedef BSTreeNode<K> Node;
        //插入操作
        bool Insert(const K& key)
        {
            if (_root == nullptr)
            {
                _root = new Node(key);
                return true;
            }
            Node* parent = nullptr;
            Node* cur = _root;
            while (cur)
            {
                if (cur->_key < key)
                {
                    parent = cur;
                    cur = cur->_right;
                }
                else if (cur->_key > key)
                {
                    parent = cur;
                    cur = cur->_left;
                }
                else
                {
                    return false;
                }
            }
            cur = new Node(key);
            if (parent->_key < key)
            {
                parent->_right = cur;
            }
            else if (parent->_key > key)
            {
                parent->_left = cur;
            }
            return true;
        }
        bool Erase(const K& key)
        {
            Node* parent = nullptr;
            Node* cur = _root;
            while (cur)
            {
                if (cur->_key < key)
                {
                    parent = cur;
                    cur = cur->_right;
                }
                else if (cur->_key > key)
                {
                    parent = cur;
                    cur = cur->_left;
                }
                else
                {
                    //找到位置
                    //删除
                    //先判断谁为空
                    if (cur->_left == nullptr)
                    {
                        if (cur == _root)
                        {
                            _root = cur->_right;
                        }
                        else
                        {
                            if (parent->_left == cur)
                            {
                                parent->_left = cur->_right;
                            }
                            else if (parent->_right == cur)
                            {
                                parent->_right = cur->_right;
                            }
                        }
                        delete cur;
                    }
                    else if (cur->_right == nullptr)
                    {
                        if (cur == _root)
                        {
                            _root = cur->_right;
                        }
                        else
                        {
                            if (parent->_left == cur)
                            {
                                parent->_right = cur->_left;
                            }
                            else if (parent->_right == cur)
                            {
                                parent->_left = cur->_left;
                            }
                        }
                        delete cur;
                    }
                    //替换法实现
                    else
                    {
                        Node* RightMinParent = cur;
                        Node* RightMin = cur->_right;
                        //找到右子树最大的值
                        while (RightMin->_left)
                        {
                            RightMinParent = RightMin;
                            RightMin = RightMin->_left;
                        }
                        //找到
                        swap(cur->_key, RightMin->_key);
                        if (RightMinParent->_left == RightMin)
                        {
                            RightMinParent->_left = RightMin->_right;
                        }
                        else
                        {
                            RightMinParent->_right = RightMin->_right;
                        }
                        delete RightMin;
                    }
                    return true;
                }
            }
            return false;
        }
        bool Find(const K& key)
        {
            Node* cur = _root;
            while (cur)
            {
                if (cur->_key < key)
                {
                    cur = cur->_right;
                }
                else if (cur->_key > key)
                {
                    cur = cur->_left;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }
        void InOrder()
        {
            _InOrder(_root);
            cout << endl;
        }
        private:
        void _InOrder(Node* root)
        {
            if (root == nullptr)
            {
                return;
            }
            _InOrder(root->_left);
            cout << root->_key << " ";
            _InOrder(root->_right);
        }
        private:
        Node* _root = nullptr;
    };

感谢大家的观看!以上就是本篇文章的全部内容。我是店小二,希望这些高阶数据结构笔记能为你在学习旅途中提供帮助!

相关文章
|
1天前
【高阶数据结构】二叉树进阶探秘:AVL树的平衡机制与实现详解(三)
【高阶数据结构】二叉树进阶探秘:AVL树的平衡机制与实现详解
|
1天前
【高阶数据结构】二叉树进阶探秘:AVL树的平衡机制与实现详解(二)
【高阶数据结构】二叉树进阶探秘:AVL树的平衡机制与实现详解
|
1天前
|
存储
【高阶数据结构】二叉树进阶探秘:AVL树的平衡机制与实现详解(一)
【高阶数据结构】二叉树进阶探秘:AVL树的平衡机制与实现详解
|
1天前
|
存储
【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现(二)
【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现
|
1天前
【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现(一)
【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现
|
9天前
|
算法 安全 测试技术
golang 栈数据结构的实现和应用
本文详细介绍了“栈”这一数据结构的特点,并用Golang实现栈。栈是一种FILO(First In Last Out,即先进后出或后进先出)的数据结构。文章展示了如何用slice和链表来实现栈,并通过golang benchmark测试了二者的性能差异。此外,还提供了几个使用栈结构解决的实际算法问题示例,如有效的括号匹配等。
golang 栈数据结构的实现和应用
|
10天前
01_设计一个有getMin功能的栈
01_设计一个有getMin功能的栈
|
10天前
|
前端开发
07_用队列实现栈
07_用队列实现栈
|
10天前
06_用栈来求解汉诺塔问题
06_用栈来求解汉诺塔问题
|
10天前
05_用一个栈实现另一个栈的排序
05_用一个栈实现另一个栈的排序