2021-08-10

这篇博客介绍了红黑树的基础知识,包括其C++实现,主要关注查找、插入操作。文章详细展示了如何通过循环遍历找到合适位置插入新节点,并在插入后进行平衡调整,确保红黑树性质得以维护。同时,提供了查找功能的实现,便于查找特定节点。此外,还展示了前序遍历等树的遍历方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

红黑树的代码实现,实现了红黑树的查找和插入,删除比较复杂后面弄
以下为C++代码,后面有解析。主要是选择操作和情况判断

#pragma once
#include <iostream>
#include "main_avl.hpp"     //单独跑该程序,可以注释这行,rbtree() 改成main去掉 inline即可
constexpr auto black = 0;
constexpr auto red = 1;
constexpr auto left_uncle = 0;
constexpr auto right_uncle = 1;

template <class T>
class rbt_node {
public:
    rbt_node(T tvalue, int col = red) :color(col), value(tvalue) {
        this->rchild = nullptr;
        this->lchild = nullptr;
        this->parent = nullptr;
    }
    ~rbt_node() = default;    //析构如何实现
    rbt_node* rchild;
    rbt_node* lchild;
    rbt_node* parent;   //父节点指针增加了空间开销
    int color=red;
    T value;
};

template <class T>
class rbt_tree {
public:
    rbt_tree(rbt_node<T>* root_node) {
        rbt_root = root_node;
        rbt_root->color = black;
    }
    rbt_tree() = default;
    ~rbt_tree() = default;

    /*以下为针对树的操作的实现*/
    //查找
    rbt_node<T>* find(rbt_node<T>* root, T key) {
        // 按照BST原则查询节点
        if (root == nullptr) return root;
        if (root->value > key) {
            return this->find(root->lchild, key);
        }
        else if (root->value < key) {
            return this->find(root->rchild, key);
        }
        else {
            cout << "key= " << key << " is finded" << endl;
            return root;
        }
    }
    // 新增节点,循环遍历找到叶子节点加入
    void push_node(rbt_node<T>* root, rbt_node<T>* new_node) {
        rbt_node<T>* temp = find(root, new_node->value);
        if (temp != nullptr) { cout << "node is already exist" << endl; return; }
        root = rb_insert(root, new_node);
        deal_unbalance(new_node);
    }
    rbt_node<T>* rb_insert(rbt_node<T>* root, rbt_node<T>* new_node) {
        if (root == nullptr)  return new_node;
        // 按照BST原则插入新节点
        new_node->parent = root;
        if (root->value > new_node->value) {
            root->lchild = rb_insert(root->lchild, new_node);
        }
        else if (root->value < new_node->value) {
            root->rchild = rb_insert(root->rchild, new_node);
        }
        return root;
    }
    //判断不平衡类型,并处理
    void deal_unbalance(rbt_node<T>* new_node) {
        if (new_node->parent == nullptr) {
            new_node->color = black;
            this->rbt_root = new_node;
            return ;
        }
        if (new_node->parent->color == black)   return ;
        int flag = right_uncle;  //0表示uncle为grant的左孩子
        rbt_node<T>* proot = new_node->parent;
        rbt_node<T>* uncle = proot->parent->lchild == proot ? proot->parent->rchild : (flag = left_uncle, proot->parent->lchild);
        cout << "--*-- " ;
        if (flag) cout << "right" << endl;else cout << "left" << endl;

        if (uncle == nullptr || uncle->color == black) {
            if (flag == right_uncle) {
                if (proot->rchild != nullptr && proot->rchild == new_node) {
                    new_node->color = black;
                    proot->parent->color = red;
                    leftright_rotation(proot);
                    cout << " leftright_rotation " << endl;
                }
                else {
                    proot->color = black;
                    proot->parent->color = red;
                    right_rotation(proot->parent);
                    cout << " right_rotation " << endl;
                    
                }
            }
            else {
                if (proot->lchild != nullptr && proot->lchild == new_node) {
                    new_node->color = black;
                    proot->parent->color = red;
                    rightleft_rotation(proot);
                    cout << " rightleft_rotation " << endl;
                }
                else {
                    proot->color = black;
                    proot->parent->color = red;
                    left_rotation(proot->parent);
                    cout << " left_rotation " << endl;
                }
            }
        }
        else {
            proot->color = black;
            proot->parent->color = red;
            uncle->color = black;
            cout << "情况一、二变三节点" << proot->parent->value << endl;
            deal_unbalance(proot->parent);
        }
    }
    // 删除节点循环遍历更新
    // 左旋转
    void left_rotation(rbt_node<T>* rootp) {
        rbt_node<T>* temp = rootp->rchild;
        rootp->rchild = temp->lchild;
        temp->lchild = rootp;
        temp->parent = rootp->parent;
        rootp->parent = temp;
        getparent(temp, rootp);
    }
    // 右旋转
    void right_rotation(rbt_node<T>* rootp) {
        rbt_node<T>* temp = rootp->lchild;
        rootp->lchild = temp->rchild;
        temp->rchild = rootp;
        temp->parent = rootp->parent;
        rootp->parent = temp;
        getparent(temp,rootp);
    }
    //左右旋转
    void leftright_rotation( rbt_node<T>* rootp) {
        left_rotation(rootp);
        right_rotation(rootp->parent->parent);
    }
    //右左旋转
    void rightleft_rotation(  rbt_node<T>* rootp) {
        right_rotation(rootp);
        left_rotation(rootp->parent->parent);
    }
    void getparent(rbt_node<T>* temp, rbt_node<T>* lost_ptr) {
        if (temp->parent != nullptr) {
            if (temp->parent->rchild == lost_ptr)
                temp->parent->rchild = temp;
            else temp->parent->lchild = temp;
        }
        else this->rbt_root = temp;
    }
    //前序遍历,递归
    void pre_traverse(rbt_node<T>* root, int ex_dep = 0) {
        if (root == nullptr) return;
        cout << root->value << " " << root->color << endl;
        pre_traverse(root->lchild, ex_dep);
        pre_traverse(root->rchild, ex_dep);
    }
    //中序遍历
    void in_traverse(rbt_node<T>* root, int ex_dep = 0) {
        if (root == nullptr) return;
        in_traverse(root->lchild, ex_dep);
        cout << root->value << " " << root->color << endl;
        in_traverse(root->rchild, ex_dep);
    }
    //后序遍历
    void past_traverse(rbt_node<T>* root, int ex_dep = 0) {
        if (root == nullptr) return;
        past_traverse(root->lchild, ex_dep);
        past_traverse(root->rchild, ex_dep);
        cout << root->value << " " << root->color << endl;
    }
public:
    rbt_node<T>* rbt_root = nullptr;
};

inline int Rbtree() {
    rbt_node<int> root_node(100);
    rbt_tree<int>   s(&root_node);
    rbt_node<int> new_n1(13), new_n2(15), new_n3(15), new_n4(35), new_n5(45),new_n6(72), new_n7(82), new_n8(92), new_n9(87), new_n10(52);
    s.push_node(s.rbt_root, &new_n5);
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n6);
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n2);
    s.push_node(s.rbt_root, &new_n1);
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n3);   //因为是地址传递如果是同一个对象插入多次,会改变new的父节点
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n9);
    cout << endl;s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n8);
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n1);
    s.push_node(s.rbt_root, &new_n7);
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n4);
    cout << endl; s.pre_traverse(s.rbt_root);
    s.push_node(s.rbt_root, &new_n10);
    cout << endl; s.pre_traverse(s.rbt_root);
    int key = 12;
    rbt_node<int>* temp = s.find(s.rbt_root, key);
    cout << "Hello world!    key= " << key << endl;
    if(temp==nullptr) cout <<" key = " << key<<" not find" << endl;
    else cout << " key = " << key << " is finded " <<  " color= " << temp->color << endl;
    //s.rbt_root = s.pop_node(s.rbt_root, 12);
    //s.pre_traverse(s.rbt_root);
    //cout << "--------***-------" << endl;
    //s.in_traverse(s.rbt_root);
    //cout << "--------***-------" << endl;
    //s.past_traverse(s.rbt_root);
    //s.pop_node(s.rbt_root, 15);
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值