二叉搜索树实现

二叉搜索树又称二叉排序树他是一颗空树或者具有一下性质的树
若他的左子树不为空那么它的左子树的所有节点的键值都小于根节点的键值
若他的右子树不为空那么它的右子树的所有节点的键值都大于根节点的键值
它的左右字数也是二叉搜索树

对于二叉搜索树查找一个节点在一般情况下具有O(log2(n))的时间复杂度对于单支的情况下退化为O(n)

本文采用键值对的方式实现二叉搜索树,建立二叉搜索树是通过插入一个个的节点来实现

#pragma once
#include <iostream>
using namespace std;


template<class K, class V>
struct BSTNode
{
    BSTNode(const K& key, const V& value)
        : _pLeft(NULL)
        , _pRight(NULL)
        , _key(key)
        , _value(value)
    {}

    BSTNode<K, V>* _pLeft;
    BSTNode<K, V>* _pRight;

    K _key;
    V _value;
};


template<class K, class V>
class BinarySearchTree
{
    typedef BSTNode<K, V> Node;
    typedef BinarySearchTree<K, V> Self;
public:
    BinarySearchTree()
        : _pRoot(NULL)
    {}

    BinarySearchTree(const Self& bst)
    {
        if (bst._pRoot == NULL)
        {
            _pRoot = NULL;
            return;
        }

        _Copy(_pRoot, bst._pRoot);
    }

    Self& operator=(const Self& bst)
    {

        if (this == &bst)
        {
            return *this;
        }

        if (bst._pRoot == NULL)
        {
            _pRoot = NULL;
            return *this;
        }

        Node* Root = NULL;
        _Copy(Root, bst._pRoot);

        if (Root)
        {
            Destroy(_pRoot);
            _pRoot = Root;
        }

        return *this;
    }

    ~BinarySearchTree()
    {
        Destroy(_pRoot);
    }

    // 查找递归和非递归
    bool Find_Nor(const K& key)
    {
        if (NULL == _pRoot)
            return false;

        Node* pCur = _pRoot;

        while (pCur)
        {
            if (key == pCur->_key)
                return true;
            else if (key > pCur->_key)
                pCur = pCur->_pRight;
            else
                pCur = pCur->_pLeft;
        }

        return false;
    }

    bool Find(const K& key)
    {
        return _Find(_pRoot, key);
    }

    // 插入递归和非递归
    bool Insert_Nor(const K& key, const V& value)
    {
        if (NULL == _pRoot)
        {
            _pRoot = new Node(key, value);
            return true;
        }

        Node* pCur = _pRoot;
        Node* pParent = NULL;
        while (pCur)
        {
            if (key == pCur->_key)
                return false;
            else if (key > pCur->_key)
            {
                pParent = pCur;
                pCur = pCur->_pRight;
            }
            else
            {
                pParent = pCur;
                pCur = pCur->_pLeft;
            }
        }

        if (pParent->_key > key)
        {
            pParent->_pLeft = new Node(key, value);
            return true;
        }
        else
        {
            pParent->_pRight = new Node(key, value);
            return true;
        }

        return false;
    }

    bool Insert(const K& key, const V& value)
    {
        return _Insert(_pRoot, key, value);
    }

    // 删除递归和非递归
    bool Remove_Nor(const K& key)
    {
        if (_pRoot == NULL)
        {
            return false;
        }

        if (_pRoot->_pLeft == NULL && _pRoot->_pRight == NULL && _pRoot->_key == key)
        {
            delete _pRoot;
            _pRoot = NULL;
            return true;
        }

        Node* pCur = _pRoot;
        Node* pParent = NULL;
        while (pCur)
        {
            if (key > pCur->_key)
            {
                pParent = pCur;
                pCur = pCur->_pRight;
            }
            else if (key < pCur->_key)
            {
                pParent = pCur;
                pCur = pCur->_pLeft;
            }
            else
                break;
        }

        if (pCur)
        {
            if (pCur->_pLeft ==  NULL)
            {
                if (_pRoot == pCur)
                {
                    _pRoot = pCur->_pRight;
                    delete pCur;
                    pCur = NULL;
                    return true;
                }
                else
                {
                    if (pCur == pParent->_pLeft)
                    {
                        pParent->_pLeft = pCur->_pRight;
                        delete pCur;
                        pCur = NULL;
                        return true;
                    }
                    else
                    {
                        pParent->_pRight = pCur->_pRight;
                        delete pCur;
                        pCur = NULL;
                        return true;
                    }
                }
            }
            else  if (pCur->_pRight == NULL)
            {
                if (_pRoot == pCur)
                {
                    _pRoot = pCur->_pLeft;
                    delete pCur;
                    pCur = NULL;
                    return true;
                }
                else if (pParent->_pLeft == pCur)
                {
                    pParent->_pLeft = pCur->_pLeft;
                    delete pCur;
                    pCur = NULL;
                    return true;
                }
                else
                {
                    pParent->_pRight = pCur->_pLeft;
                    delete pCur;
                    pCur = NULL;
                    return true;
                }
            }
            else
            {
                Node* firstInOrder = pCur->_pRight;
                pParent = pCur;

                while (firstInOrder->_pLeft)
                {
                    pParent = firstInOrder;
                    firstInOrder = firstInOrder->_pLeft;
                }

                pCur->_key = firstInOrder->_key;
                pCur->_value = firstInOrder->_value;

                if (pParent ==  pCur)
                {   
                    pCur->_pRight = firstInOrder->_pRight;
                    delete firstInOrder;
                    return true;
                }
                else
                {
                    pParent->_pLeft = firstInOrder->_pRight;
                    delete firstInOrder;
                    return true;
                }
            }
        }

        return false;
    }

    bool Remove(const K& key)
    {
        return _Remove(_pRoot, key);
    }

    void InOrder()
    {
        cout << "InOrder:";
        _InOrder(_pRoot);
        cout << endl;
    }

private:
    bool _Find(Node* pRoot, const K& key)
    {
        if (NULL == pRoot)
            return false;

        if (key == pRoot->_key)
            return true;

        if (pRoot->_key < key)
            return _Find(pRoot->_pRight, key);
        else
            return _Find(pRoot->_pLeft, key);       
    }

    bool _Insert(Node* &pRoot, const K& key, const V& value)
    {
        if (NULL == pRoot)
        {
            pRoot = new Node(key, value);
            return true;
        }

        if (key == pRoot->_key)
        {
            return false;
        }

        if (pRoot->_key < key)
            return _Insert(pRoot->_pRight, key, value);
        else
            return _Insert(pRoot->_pLeft, key, value);
    }

    bool _Remove(Node*& pRoot, const K& key)
    {
        if (pRoot == NULL)
        {
            return false;
        }

        if (pRoot->_key == key)
        {
            if (pRoot->_pLeft == NULL)
            {
                Node* pCur = pRoot;
                pRoot = pRoot->_pRight;
                delete pCur;
                return true;
            }
            else if (pRoot->_pRight == NULL)
            {
                Node* pCur = pRoot;
                pRoot = pRoot->_pLeft;
                delete  pCur;
                return true;
            }
            else
            {
                Node* firstInOrder = pRoot->_pRight;

                while (firstInOrder->_pLeft)
                    firstInOrder = firstInOrder->_pLeft;

                pRoot->_key = firstInOrder->_key;
                pRoot->_value = firstInOrder->_value;

                return _Remove(pRoot->_pRight, firstInOrder->_key);
            }
        }

        if (key > pRoot->_key)
            return  _Remove(pRoot->_pRight, key);
        else
            return _Remove(pRoot->_pLeft, key);
    }

    void _InOrder(Node* pRoot)
    {
        if (pRoot)
        {
            _InOrder(pRoot->_pLeft);
            cout << pRoot->_key << " ";
            _InOrder(pRoot->_pRight);
        }
    }

    void Destroy(Node*& pRoot)
    {
        if (NULL == pRoot)
        {
            return;
        }

        Destroy(pRoot->_pLeft);
        Destroy(pRoot->_pRight);

        delete pRoot;
        pRoot = NULL;
    }

    void _Copy(Node*& pRoot, const Node* pDes)
    {
        if (pDes)
        {
            pRoot = new Node(pDes->_key, pDes->_value);
            _Copy(pRoot->_pLeft, pDes->_pLeft);
            _Copy(pRoot->_pRight, pDes->_pRight);
        }
    }
private:
    Node* _pRoot;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
4S店客户管理小程序-毕业设计,基于微信小程序+SSM+MySql开发,源码+数据库+论文答辩+毕业论文+视频演示 社会的发展和科学技术的进步,互联网技术越来越受欢迎。手机也逐渐受到广大人民群众的喜爱,也逐渐进入了每个用户的使用。手机具有便利性,速度快,效率高,成本低等优点。 因此,构建符合自己要求的操作系统是非常有意义的。 本文从管理员、用户的功能要求出发,4S店客户管理系统中的功能模块主要是实现管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理,用户客户端:首页、车展、新闻头条、我的。门店客户端:首页、车展、新闻头条、我的经过认真细致的研究,精心准备和规划,最后测试成功,系统可以正常使用。分析功能调整与4S店客户管理系统实现的实际需求相结合,讨论了微信开发者技术与后台结合java语言和MySQL数据库开发4S店客户管理系统的使用。 关键字:4S店客户管理系统小程序 微信开发者 Java技术 MySQL数据库 软件的功能: 1、开发实现4S店客户管理系统的整个系统程序; 2、管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理等。 3、用户客户端:首页、车展、新闻头条、我的 4、门店客户端:首页、车展、新闻头条、我的等相应操作; 5、基础数据管理:实现系统基本信息的添加、修改及删除等操作,并且根据需求进行交流信息的查看及回复相应操作。
现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本微信小程序医院挂号预约系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此微信小程序医院挂号预约系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。微信小程序医院挂号预约系统有管理员,用户两个角色。管理员功能有个人中心,用户管理,医生信息管理,医院信息管理,科室信息管理,预约信息管理,预约取消管理,留言板,系统管理。微信小程序用户可以注册登录,查看医院信息,查看医生信息,查看公告资讯,在科室信息里面进行预约,也可以取消预约。微信小程序医院挂号预约系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。
以下是Java实现二叉搜索树的基本步骤和代码示例: 1.定义二叉搜索树节点类 ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } ``` 2.定义二叉搜索树类 ```java class BST { private TreeNode root; public BST() { root = null; } // 向二叉搜索树中插入元素 public void insert(int val) { root = insert(root, val); } private TreeNode insert(TreeNode node, int val) { if (node == null) { return new TreeNode(val); } if (val < node.val) { node.left = insert(node.left, val); } else if (val > node.val) { node.right = insert(node.right, val); } return node; } // 在二叉搜索树中查找元素 public boolean search(int val) { return search(root, val); } private boolean search(TreeNode node, int val) { if (node == null) { return false; } if (val == node.val) { return true; } else if (val < node.val) { return search(node.left, val); } else { return search(node.right, val); } } // 删除二叉搜索树中的元素 public void delete(int val) { root = delete(root, val); } private TreeNode delete(TreeNode node, int val) { if (node == null) { return null; } if (val < node.val) { node.left = delete(node.left, val); } else if (val > node.val) { node.right = delete(node.right, val); } else { if (node.left == null) { return node.right; } else if (node.right == null) { return node.left; } TreeNode minNode = findMin(node.right); node.val = minNode.val; node.right = delete(node.right, node.val); } return node; } private TreeNode findMin(TreeNode node) { while (node.left != null) { node = node.left; } return node; } } ``` 3.测试代码 ```java public class TestBST { public static void main(String[] args) { BST bst = new BST(); bst.insert(5); bst.insert(3); bst.insert(7); bst.insert(1); bst.insert(9); System.out.println(bst.search(3)); // 输出:true bst.delete(3); System.out.println(bst.search(3)); // 输出:false } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值