• 二叉搜索树的C++ 实现


    参考:https://blog.csdn.net/liuchenjane/article/details/53046715
    头文件

    #ifndef DEBUG_LRN_TREENODE_H
    #define DEBUG_LRN_TREENODE_H
    
    
    void echo(int i);
    
    class TreeNode;
    void inorder_tree_walk(TreeNode* x);
    void preorder_tree_walk(TreeNode* x);
    TreeNode* tree_minmum(TreeNode* x);
    TreeNode* tree_maxmum(TreeNode* x);
    TreeNode* tree_search(TreeNode* x, int k);
    TreeNode* tree_successor(TreeNode* x);
    TreeNode* tree_predecessor(TreeNode* x);
    void tree_insert(TreeNode* T, TreeNode* z);
    void transplant(TreeNode* T, TreeNode* u,TreeNode* v);
    void tree_delete(TreeNode* T, TreeNode* z);
    
    
    class TreeNode{
    public:
        int key;
        TreeNode* left;
        TreeNode* right;
        TreeNode* p;
    public:
        TreeNode();
        explicit TreeNode(int val);
        ~TreeNode();
    };
    
    
    #endif //DEBUG_LRN_TREENODE_H
    
    

    源文件

    #include <iostream>
    #include <vector>
    #include "TreeNode.h"
    
    using namespace std;
    
    
    void echo(int i){
        std::cout << i << std::endl;
    }
    
    
    TreeNode::TreeNode()
    {
        key=0;
        left=right=p=nullptr;
    }
    
    TreeNode::TreeNode(int val) {
        key = val;
        left = right = p = nullptr;
    }
    
    TreeNode::~TreeNode() = default;
    
    
    void inorder_tree_walk(TreeNode* x) {
        if (x != nullptr) {
            inorder_tree_walk(x->left);
            cout << x->key << " ";
            inorder_tree_walk(x->right);
        } else
            return;
    }
    
    
    void preorder_tree_walk(TreeNode*x) {
        if (x != nullptr) {
            cout << x->key << " ";
            preorder_tree_walk(x->left);
            preorder_tree_walk(x->right);
        }
    }
    
    
    TreeNode* tree_minmum(TreeNode* x)
    {
        while(x->left != nullptr)
            x = x->left;
        return x;
    }
    TreeNode* tree_maxmum(TreeNode* x)
    {
        while(x->right != nullptr)
            x = x->right;
        return x;
    }
    
    TreeNode* tree_search(TreeNode* x, int k)
    {
        if(x == nullptr || k == x->key)
            return x;
        if(k < x->key)
            return tree_search(x->left, k);
        else return tree_search(x->right, k);
    }
    
    TreeNode* tree_successor(TreeNode* x)//大于x.key的最小关键字的节点为后继
    {
        if(x->right)
            return tree_minmum(x->right);
        TreeNode* y=x->p;
        while(y&& x==y->right)
        {
            x=y;
            y=y->p;
        }
        return y;
    }
    
    
    TreeNode* tree_predecessor(TreeNode* x)//小于x.key的最大关键字的节点为前驱
    {
        if(x->left)
            return tree_maxmum(x->left);
        TreeNode* y=x->p;
        while(y&& x==y->left)
        {
            x=y;
            y=y->p;
        }
        return y;
    }
    
    
    void tree_insert(TreeNode* T, TreeNode* z) //T是现在的BST的树根
    {
        if(T == nullptr){
            T = z;
            return;
        };
        TreeNode* y = nullptr;
        TreeNode* x=T;
    
        while(x != nullptr)
        {
            y = x;
            if(z->key < x->key)
                x = x->left;
            else x = x->right;
        }
        z->p = y;
        if(z->key < y->key)
            y->left = z;
        else y->right = z;
    }
    
    
    //删除节点
    void transplant(TreeNode* T, TreeNode* u,TreeNode* v)//用一颗子树代v替另一个子树u
    {
        if(u->p == nullptr) {
            T = v;
        }
        else if(u == u->p->left) {
            u->p->left = v;
        }
        else{
            u->p->right = v;
        };
        if(v != nullptr){
            v->p = u->p;
        }
    }
    
    
    void tree_delete(TreeNode* T, TreeNode* z) {
        if (!z->left)
            transplant(T, z, z->right);
        else if (!z->right)
            transplant(T, z, z->left);
        else {
            TreeNode *y = tree_minmum(z->right);
            if (y->p != z) {
                transplant(T, y, y->right);
                y->right = z->right;
                y->right->p = y;
            }
            transplant(T, z, y);
            y->left = z->left;
            y->left->p = y;
        }
    }
    

    测试:

    #include <iostream>
    #include <vector>
    #include <string>
    #include <map>
    #include <set>
    #include <algorithm>
    #include "TreeNode.h"
    
    
    using namespace std;
    using namespace cv;
    
    
    void test_bst(){
        auto T = new TreeNode(15);
        vector<int> a = {6, 18, 3, 7, 17, 20, 2, 4, 13, 9};
        for(auto i:a){
            auto z = new TreeNode(i);
            tree_insert(T, z);
        }
        inorder_tree_walk(T);cout<<endl;
        preorder_tree_walk(T);
        TreeNode* min=tree_minmum(T);
        TreeNode* max=tree_maxmum(T);cout<<endl;
        cout<<"最大值:"<<max->key<<"  最小值:"<<min->key<<endl;
        cout<<"查找z=13的节点:";
        TreeNode* z=tree_search(T,13);cout<<z->key<<endl;
        TreeNode* prez=tree_predecessor(z);
        TreeNode* sucz=tree_successor(z);
        cout<<"z的前驱:"<<prez->key<<" "<<"z的后继:"<<sucz->key<<endl;
        tree_delete(T,z);
        cout<<"删除z之后:";
        inorder_tree_walk(T);cout<<endl;
    
    }
    
    
    
    
    int main(int argc, char* argv[])
    {
        test_bst();
        return 0;
    }
    
    
    
  • 相关阅读:
    Swagger配置和使用
    请求SpringMVC接口如何传参数
    ssm搭建配置文件
    永久关闭windows10更新
    VSCode搭建java开发环境
    idea全局设置
    mybatis-plus查询指定字段
    mybayis-plus条件构造器
    Java日期时间操作的一些方法
    C#编写聊天软件客户端
  • 原文地址:https://www.cnblogs.com/theodoric008/p/9462935.html
Copyright © 2020-2023  润新知