RBTree实作

Nginx 红黑树结构 ngx_rbtree_t 本文分析了 Nginx 红黑树的源码实现,其基本实现跟算法导论中的红黑树是一样的。 阅读详情

#ifndef _KOK_RBTREE_H_
#define _KOK_RBTREE_H_
#pragma once

#include "Alloc.h"
#include "Utility.h"

namespace KOK
{
    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc = KOK::alloc>
    class CRBTree
    {
        enum __Color { R = 0, B = 1 };

        template <class NodeValue>
        struct CRBTreeNode
        {
            typedef CRBTreeNode<NodeValue>* LinkType;
            LinkType pParent;
            LinkType pLeft;
            LinkType pRight;
            __Color color;
            NodeValue value;

            static LinkType Minimun(LinkType x)
            {
                while(x->pLeft != NULL)
                {
                    x = x->pLeft;
                }
                return x;
            }
            static LinkType Maximun()
            {
                while(x->pRight != NULL)
                {
                    x = x->pRight;
                }
                return x;
            }

        };

    public:
        template <class V, class R, class P>
        struct CRBTreeIterator
        {
            typedef V ValueType;
            typedef R Reference;
            typedef P Pointer;
            typedef CRBTreeNode<ValueType>* LinkType;
            typedef CRBTreeIterator<ValueType, Reference, Pointer> Self;
            typedef CRBTreeIterator<Value, Value&, Value*> Iterator;
            typedef CRBTreeIterator<Value, const Value&, const Value*> ConstIterator;

        private:
            LinkType pNode_;

        private:
            void Increment()
            {
                if(pNode_->pParent->pParent == pNode_ && pNode_->color == __Color::R)
                    return;
                else if(pNode_->pRight)
                {
                    pNode_ = pNode_->pRight;
                    while(pNode_->pLeft)
                        pNode_ = pNode_->pLeft;
                }
                else
                {
                    LinkType pParent = pNode_->pParent;
                    while(pParent->pRight == pNode_)
                    {
                        pNode_ = pParent;
                        pParent = pParent->pParent;
                    }
                    if(pNode_->pRight != pParent)
                        pNode_ = pParent;
                }
            }

            void Decrement()
            {
                if(pNode_->pParent->pParent == pNode_ && pNode_->color == __Color::R)
                    pNode_ = pNode_->pRight;
                else if(pNode_->pLeft)
                {
                    pNode_ = pNode_->pLeft;
                    while(pNode_->pRight)
                        pNode_ = pNode_->pRight;
                }
                else
                {
                    LinkType pParent = pNode_->pParent;
                    while(pParent->pLeft == pNode_)
                    {
                        pNode_ = pParent;
                        pParent = pParent->pParent;
                    }

                    pNode_ = pParent;

                }
            }

        public:
            CRBTreeIterator(const LinkType& pNode)
            {
                this->pNode_ = pNode;
            }

            Self& operator ++()
            {
                Increment();
                return *this;
            }

            Self& operator ++(int)
            {
                Self temp = *this;
                Increment();
                return temp;
            }

            Self& operator --()
            {
                Decrement();
                return *this;
            }

            Self& operator --(int)
            {
                Self temp = *this;
                    Decrement();
                return temp;
            }

            Reference operator *() const
            {
                return pNode_->value;
            }

            Pointer operator ->() const
            {
                return &pNode_->value;
            }

            bool operator ==(const Self& other)
            {
                return (&other)->pNode_ == this->pNode_;
            }

            bool operator !=(const Self& other)
            {
                return !operator==(other);
            }

        };

    private:
        typedef CRBTreeNode<Value> Node;
        typedef KOK::CAllocAdapter<Node, Alloc> NodeAlloc;

    public:
        typedef Key KeyType;
        typedef Value ValueType;
        typedef Value* Pointer;
        typedef const Value* ConstPointer;
        typedef Value& Reference;
        typedef const Value& ConstReference;
        typedef size_t SizeType;
        typedef ptrdiff_t DiffType;

        typedef CRBTreeIterator<ValueType, Reference, Pointer> Iterator;

    protected:
        typedef CRBTreeNode<Value>* LinkType;

    protected:
        SizeType count_;
        Compare compare_;
        LinkType pHeader_;
        KeyOfValue keyOfValue_;

    protected:
        void DeleteNode(LinkType& pNode)
        {
            Destroy(&pNode->value);
            PutNode(pNode);
            pNode = NULL;
        }
        LinkType CreateNode(ConstReference value)
        {
            LinkType pNode = GetNode();
            pNode->pLeft = pNode->pRight = pNode->pParent = NULL;
            pNode->color = __Color::R;
            Construct(&pNode->value, value);
            return pNode;
        }
        LinkType GetNode() { return NodeAlloc::Allocate();}
        void PutNode(LinkType pNode) { NodeAlloc::Deallocate(pNode); }

        void Init()
        {
            pHeader_ = GetNode();
            pHeader_->color = __Color::R;
            pHeader_->pLeft = pHeader_->pRight = pHeader_;
            pHeader_->pParent = NULL;
            count_ = 0;
        }

        inline LinkType& LeftMost() const { return pHeader_->pLeft; }
        inline LinkType& RightMost() const { return pHeader_->pRight; }
        inline LinkType& Root() const { return pHeader_->pParent; }
        inline LinkType& Left(LinkType& pNode) const { return pNode->pLeft; }
        inline LinkType& Right(LinkType& pNode) const { return pNode->pRight; }
        inline LinkType& Parent(LinkType& pNode) const { return pNode->pParent; }

        void RotateLeft(LinkType pNode, LinkType& pRoot);
        void RotateRight(LinkType pNode, LinkType& pRoot);
        void BalanceAdjust(LinkType pNewNode, LinkType& pRoot);
        Iterator InsertAux(LinkType pParent, LinkType& pRoot, ConstReference value);
        bool DeleteAux(LinkType pNode, LinkType pParent, ConstReference value);
        void DeleteAdjust(LinkType pNode, LinkType pParent, LinkType& pRoot);
        void ClearAux(LinkType& pNode);
    public:
        CRBTree();
        ~CRBTree();

        inline KeyOfValue GetKeyOfValue() const { return keyOfValue_; }
        inline Compare GetCompare() const { return compare_; }

        inline SizeType GetSize() const { return count_; }
        inline ValueType MaxValue() const { return RightMost()->value; }
        inline ValueType MinValue() const { return LeftMost()->value; }

        inline Iterator Begin() const { return LeftMost(); }
        inline Iterator End() const { return pHeader_; }
        inline bool Empty() const { return count_ == 0;}

        Pair<Iterator, bool> InsertUnique(ConstReference value);
        Iterator InsertEqual(ConstReference value);
        int Delete(ConstReference value);

        void Clear();
        Iterator Find(const KeyType& key);
       
    private:
        CRBTree& operator =(const CRBTree&);
        CRBTree(const CRBTree&);
    };

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    void CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::ClearAux(LinkType& pNode)
    {
        if(pNode->pLeft != NULL)
            ClearAux(pNode->pLeft);
        if(pNode->pRight != NULL)
            ClearAux(pNode->pRight);
        DeleteNode(pNode);
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    void CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Clear()
    {
        if(Empty())
            return;
        ClearAux(Root());
        RightMost() = LeftMost() = NULL;
        count_ = 0;
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::~CRBTree()
    {
        Clear();
        PutNode(pHeader_);
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::CRBTree()
    {
        Init();
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    typename CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Iterator CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Find(const KeyType& key)
    {
        LinkType y = pHeader_;
        LinkType x = Root();
        while(x != NULL)
        {
            if(!compare_(keyOfValue_(x->value), key))
            {
                y = x;
                x = x->pLeft;
            }
            else
            {
                x = x->pRight;
            }

        }
        Iterator i = Iterator(y);
        return i == End() || compare_(key, keyOfValue_(y->value)) ? End() : i;
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    void CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::RotateLeft(LinkType pNode, LinkType& pRoot)
    {
        LinkType pRight = pNode->pRight;
        pNode->pRight = pRight->pLeft;
        if(pRight->pLeft)
            pRight->pLeft->pParent = pNode;
        pRight->pParent = pNode->pParent;

        if(pRoot == pNode)
            pRoot = pRight;
        else if(pNode == pNode->pParent->pRight)
            pNode->pParent->pRight = pRight;
        else
            pNode->pParent->pLeft = pRight;

        pRight->pLeft = pNode;
        pNode->pParent = pRight;
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    void CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::RotateRight(LinkType pNode, LinkType& pRoot)
    {
        LinkType pLeft = pNode->pLeft;
        pNode->pLeft = pLeft->pRight;
        if(pLeft->pRight)
            pLeft->pRight->pParent = pNode;
        pLeft->pParent = pNode->pParent;

        if(pRoot == pNode)
            pRoot = pLeft;
        else if(pNode == pNode->pParent->pRight)
            pNode->pParent->pRight = pLeft;
        else
            pNode->pParent->pLeft = pLeft;

        pLeft->pRight = pNode;
        pNode->pParent = pLeft;
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    void CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::BalanceAdjust(LinkType pNewNode, LinkType& pRoot)
    {
        pNewNode->color = __Color::R;
        while(pNewNode != pRoot && pNewNode->pParent->color == __Color::R)
        {
            if(pNewNode == pNewNode->pParent->pLeft)
            {
                LinkType pSParent = pNewNode->pParent->pParent->pRight;
                if(pSParent && pSParent->color == __Color::R)
                {
                    pNewNode->pParent->pParent->color = __Color::R;
                    pNewNode->pParent->color = __Color::B;
                    pSParent->color = __Color::B;
                    pNewNode = pNewNode->pParent->pParent;
                }
                else
                {
                    if(pNewNode->pParent->pRight == pNewNode)
                    {
                        RotateLeft(pNewNode->pParent, pRoot);
                        pNewNode = pNewNode->pLeft;
                    }
                    pNewNode->pParent->color = __Color::B;
                    pNewNode->pParent->pParent->color = __Color::R;
                    RotateRight(pNewNode->pParent->pParent, pRoot);
                }
            }
            else
            {
                LinkType pSParent = pNewNode->pParent->pParent->pLeft;
                if(pSParent && pSParent->color == __Color::R)
                {
                    pNewNode->pParent->pParent->color = __Color::R;
                    pNewNode->pParent->color = __Color::B;
                    pSParent->color = __Color::B;
                    pNewNode = pNewNode->pParent->pParent;
                }
                else
                {
                    if(pNewNode->pParent->pLeft == pNewNode)
                    {
                        RotateRight(pNewNode->pParent, pRoot);
                        pNewNode = pNewNode->pRight;
                    }
                    pNewNode->pParent->color = __Color::B;
                    pNewNode->pParent->pParent->color = __Color::R;
                    RotateLeft(pNewNode->pParent->pParent, pRoot);
                }
            }
        }

        pRoot->color = __Color::B;
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    typename CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Iterator CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::InsertAux(LinkType pParent, LinkType& pRoot, ConstReference value)
    {
        LinkType pNewNode;
        if(pParent == pHeader_ || compare_(keyOfValue_(value), keyOfValue_(pParent->value)))
        {
            pNewNode = CreateNode(value);
            Left(pParent) = pNewNode;

            if(pParent == pHeader_)
            {
                Root() = pNewNode;
                Right(pParent) = pNewNode;
            }
            else if(pParent == LeftMost())
            {
                LeftMost() = pNewNode;
            }
        }
        else
        {
            pNewNode = CreateNode(value);
            Right(pParent) = pNewNode;
            if(pParent == RightMost())
                RightMost() = pNewNode;
        }

        Parent(pNewNode) = pParent;

        BalanceAdjust(pNewNode, Root());
        count_++;
        return Iterator(pNewNode);
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    Pair<typename CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Iterator, bool> CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::InsertUnique(ConstReference value)
    {
        LinkType pParent = pHeader_;
        LinkType pNode = Root();
        bool comp = true;

        while(pNode != NULL)
        {
            pParent = pNode;
            comp = compare_(keyOfValue_(value), keyOfValue_(pNode->value));
            pNode = comp ? Left(pNode) : Right(pNode);
        }

        Iterator iter = Iterator(pParent);
        if(comp)
            if(iter == Begin())
               return Pair<Iterator, bool>(InsertAux(pParent, Root(), value), true);
            else
                iter--;
        if(compare_(keyOfValue_(*iter), value))
            return Pair<Iterator, bool>(InsertAux(pParent, Root(), value), true);

        return Pair<Iterator, bool>(iter, false);
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    typename CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Iterator CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::InsertEqual(ConstReference value)
    {
        LinkType pParent = pHeader_;
        LinkType pNode = Root();
        while(pNode != NULL)
        {
            pParent = pNode;
            pNode = compare_(keyOfValue_(value), keyOfValue_(pNode->value)) ? Left(pNode) : Right(pNode);
        }

        return InsertAux(pParent, Root(), value);
    }


    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    void CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::DeleteAdjust(LinkType pNode, LinkType pParent, LinkType& pRoot)
    {
        if(pNode == pParent->pLeft) //both of pParent's child node can not be NULL
        {
            Node node;
            LinkType pSNode; // pSNode can not be NULL due to the rule of RBTree

            if(pNode == NULL)
            {
                pNode = &node;
                pNode->color = __Color::B;
                Parent(pNode) = pParent;
            }
            else if(pNode->color == __Color::R)
            {
                pNode->color = __Color::B;
                return;
            }

            while(pNode != pRoot && pNode->color == __Color::B)
            {
                pSNode = Right(Parent(pNode));
                if(pSNode->color == __Color::R)
                {
                    pSNode->color = Parent(pNode)->color;
                    Parent(pNode)->color = __Color::R;
                    RotateLeft(Parent(pNode), pRoot);
                }
                else if((!Right(pSNode) || Right(pSNode)->color == __Color::B) && (!Left(pSNode) || Left(pSNode)->color == __Color::B))
                {
                    pSNode->color = __Color::R;
                    pNode = pNode->pParent;
                }
                else if((Left(pSNode) && Left(pSNode)->color == __Color::R) && (!Right(pSNode) || Right(pSNode)->color == __Color::B))
                {
                    pSNode->color == __Color::R;
                    Left(pSNode)->color == __Color::B;
                    RotateRight(pSNode, pRoot);
                }
                else if(Right(pSNode) || Right(pSNode)->color == __Color::R)
                {
                    pSNode->color = Parent(pNode)->color;
                    Parent(pNode)->color = __Color::B;
                    RotateLeft(Parent(pNode), pRoot);
                    pNode = Right(pSNode);
                }
            }

            pNode->color = __Color::B;
        }
        else if(pNode == pParent->pRight)
        {
            Node node;
            LinkType pSNode; // pSNode can not be NULL due to the rule of RBTree

            if(pNode == NULL)
            {
                pNode = &node;
                pNode->color = __Color::B;
                Parent(pNode) = pParent;
            }
            else if(pNode->color == __Color::R) // case 1: must in the front of all the cases.
            {
                pNode->color = __Color::B;
                return;
            }

            while(pNode != pRoot && pNode->color == __Color::B)
            {
                pSNode = Left(Parent(pNode));
                if(pSNode->color == __Color::R) //case 2:
                {
                    pSNode->color = Parent(pNode)->color;
                    Parent(pNode)->color = __Color::R;
                    RotateRight(Parent(pNode), pRoot);
                }
                else if((!Right(pSNode) || Right(pSNode)->color == __Color::B) && (!Left(pSNode) || Left(pSNode)->color == __Color::B)) // case 3:
                {
                    pSNode->color = __Color::R;
                    pNode = pNode->pParent;
                }
                else if((Right(pSNode) && Right(pSNode)->color == __Color::R) && (!Left(pSNode) || Left(pSNode)->color == __Color::B)) //case 4:
                {
                    pSNode->color == __Color::R;
                    Right(pSNode)->color == __Color::B;
                    RotateLeft(pSNode, pRoot);
                }
                else if(Left(pSNode) || Left(pSNode)->color == __Color::R) //case 5:
                {
                    pSNode->color = Parent(pNode)->color;
                    Parent(pNode)->color = __Color::B;
                    RotateRight(Parent(pNode), pRoot);
                    pNode = Left(pSNode);
                }
            }

            pNode->color = __Color::B;
        }
        else //when delete root node
            return;
    }

    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    bool CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::DeleteAux(LinkType pNode, LinkType pParent, ConstReference value)
    {
        if(pNode->pLeft == NULL && pNode->pRight == NULL)
        {
            __Color colTemp = pNode->color;

            if(LeftMost() == pNode)
                LeftMost() = pNode->pParent;
            if(RightMost() == pNode)
                RightMost() = pNode->pParent;
           
            if(pParent == pHeader_)
                pParent->pParent = NULL;

            if(pNode == pParent->pLeft)
                pParent->pLeft = NULL;
            else if(pNode == pParent->pRight)
                pParent->pRight = NULL;

            DeleteNode(pNode);
            if(colTemp != __Color::R) // dont need to fixup while the color of node deled is red.
                DeleteAdjust(NULL, pParent, Root());
            count_--;
        }
        else if(pNode->pLeft == NULL) //only condition: parent->color = black; rchild->color = red;
        {
            if(LeftMost() == pNode)
            {
                LinkType pRChild = pNode->pRight;
                while(pRChild->pLeft)
                    pRChild= pRChild->pLeft;
                LeftMost() = pRChild;
            }

            pNode->pRight->pParent = pNode->pParent;
            LinkType pTemp = pNode->pRight;

            if(pParent == pHeader_)
                pParent->pParent = pTemp;

            if(pNode == pParent->pLeft)
                pParent->pLeft = pTemp;
            else if(pNode == pParent->pRight)
                pParent->pRight = pTemp;

            DeleteNode(pNode);
            DeleteAdjust(pTemp, pParent, Root());
            count_--;
        }
        else if(pNode->pRight == NULL) //only condition: parent->color = black; lchild->color = red;
        {
            if(RightMost() == pNode)
            {
                LinkType pLChild = pNode->pLeft;
                while(pLChild->pRight)
                    pLChild= pLChild->pRight;
                RightMost() = pLChild;
            }
            pNode->pLeft->pParent = pNode->pParent;
            LinkType pTemp = pNode->pLeft;

            if(pParent == pHeader_)
                pParent->pParent = pTemp;

            if(pNode == pParent->pLeft)
                pParent->pLeft = pTemp;
            else if(pNode == pParent->pRight)
                pParent->pRight = pTemp;

            DeleteNode(pNode);
            DeleteAdjust(pTemp, pParent, Root());
            count_--;
        }
        else // dont need to fixup again, Delete() method has already done it.
        {
            LinkType pLChild = pNode->pLeft;
            while(pLChild->pRight)
                pLChild= pLChild->pRight;
            ValueType temp = pLChild->value;
            Delete(pLChild->value);
            pNode->value = temp;
            return true;
        }

        return false;
    }
   
    template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>
    int CRBTree<Key, Value, KeyOfValue, Compare, Alloc>::Delete(ConstReference value)
    {
        LinkType pParent = pHeader_;
        LinkType pNode = Root();

        while(pNode != NULL)
        {
            if(compare_(keyOfValue_(value), keyOfValue_(pNode->value)))
            {
                pParent = pNode;
                pNode = Left(pNode);
            }
            else if(compare_(keyOfValue_(pNode->value), keyOfValue_(value)))
            {
                pParent = pNode;
                pNode = Right(pNode);
            }
            else
                break;
        }

        if(pNode == NULL)
            return 0;
       
        DeleteAux(pNode, pParent, value);
        return 1;
    }
   
}

#endif

Linux C 内核rbtree算法(支持用户态) rbtree算法内核实现,支持用户态程序使用 阅读详情

相关推荐

不用编码器也能玩转BLDC?手把手教你反电动势定位实战

本文详细介绍了无刷直流电机(BLDC)的无传感器控制方案——反电动势定位法,通过STM32实战演示如何实现基本控制。文章涵盖核心原理、硬件准备、信号采集技巧及代码实现,特别适合静音风扇、小型机器人等对成本敏感的应用场景,帮助开发者降低硬件成本并提升系统可靠性。

lg888的博客 898

Linux内核的红黑树源码实现以及调用

红黑树可以说是程序员经常遇到的一种数据结构,不管是工作还是面试都会涉及,有时候还会让你写一段红黑树代码。 本文主要是讲Linux中的红黑树,关于红黑树定义参考wiki:https://en.wikipedia.org/wiki/Red%E2%80%93black_tree 其中《算法导论》中的定义最为清晰: 红黑树图示: Linux中的红黑树(rbtree) 下面...

深度Java 8058

基于ResNet-50的猫狗分类训练

基于ResNet-50的猫狗分类python训练

weixin_46352497的博客 1004

【Linux 内核】CFS 调度器 ⑥ ( CFS 调度器就绪队列 cfs_rq | Linux 内核调度实体 sched_entity | “ 红黑树 “ 数据结构 rb_root_cached )

一、CFS 调度器就绪队列 cfs_rq、 二、Linux 内核调度实体 sched_entity、 三、" 红黑树 " 数据结构 rb_root_cached、

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 2329

yakit 通过codec 解决RSA加密

靶场就不过多叙述,具体的可以看yakit系列的前几篇文章。yakit 热加载进行 RSA 加密如何显示想要的payloads。

Arkmornings的博客 2686

【数据降维】数据降维方法分类

数据降维基本原理是将样本点从输入空间通过线性或非线性变换映射到一个低维空间,从而获得一个关于原数据集紧致的低维表示。 数据降维工具箱drtoolbox中众多算法,这里简单做个分类。 因为很多并没有仔细了解,在此次只对八种方法做分类:主成分分析(Principal Component Analysis,PCA)、线性判别分析(Linear Discriminant Analysis,LDA)、等

小魏的修行路 6万+

11红黑树(RB-tree)

1、平衡二叉树 平衡二叉树有许多种类型,包括AVL-tree、RB-tree、AA-tree,其中最被广泛运用于STL的是RB-tree(红黑树)。二叉搜索树的节点放置规则是:任何节点的键值一定大于其左子树中的每一个节点的键值,并小于其右子树中的每一个节点的键值。 2、AVL-tree AVL-tree是一个“加上了额外平衡条件”的二叉搜索树,其平衡条件的建立是为了确保整棵树的深度为O(logN)。AVL-tree要求任何节点的左右子树高度相差最多1。由于只有插入点至根节点路径是哪个的各节点可能改变平

Revendell的博客 494

csp-j/s

CSP-J/S 是中国计算机学会(CCF)主办的 ‌‌(Certified Software Professional Junior/Senior),是信息学奥赛(NOI)体系的入门级选拔考试,也是国内最具权威性的青少年编程能力认证之一。

gxyxxrYYDS的博客 2028

红黑树的实现

红黑树性质 (1)每个节点非黑及红 (2)根是黑色 (3)每一片叶子是黑色 (4)若一节点为红,则其孩子为黑色 (5)对每个节点,所以从该节点到后代叶子的路径含有同样多的黑色节点 插入一个节点的时间复杂度O(lgn) 删除一个节点的时间复杂度O(lgn) RBTree.h typedef enum {RED, BLACK} Color; typedef struct RBNode

protoss_penguin的博客 475

应用软件——Altium Designer 19 Gerber/坐标文件

如何输出Gerber文件 步骤如下: 打开需要输出的PCB文件 2. 设置原点坐标 3. 打开Gerber Files 选项,File–>Fabrication Opuputs–>Gerber Files。 4. General参数设置。 用于指定输出Gerber文件中使用的单位(Units)和格式(Format)。单位可以是公制(Millimeters)和英制(Inches);格式栏中2:3,2:4,2:5代表文件中使用的不同数据精度,其中2:3表示数据含2位整数3位小数;相应的,另外两个分

染血黑兔的博客 7203

RACECAR底层平台安装:Jetson Nano+ROS Melodic全栈部署指南

移动机器人开发中,底层平台安装是将硬件转化为可编程移动平台的关键起点。其核心在于嵌入式系统、ROS中间件与传感器驱动的深度协同——需兼顾内核模块加载、GPIO/PWM精确控制、I2C设备初始化及ROS工作空间构建等多层技术耦合。尤其在竞速级小车如RACECAR场景下,Jetson Nano的硬件加速能力、MPU6050 IMU固件加载、TB6612FNG电机驱动配置等细节,直接决定运动控制精度与实时性。本文聚焦Ubuntu 18.04 + ROS Melodic + JetPack 4.4.1技术栈,覆盖从

weixin_32025789的博客 255

rbtree

一、红黑树6条规则 1.每个节点非黑即红 2.根节点是黑色 3.新插入的节点是红色 4.从根到叶节点的所有路径经历的黑节点个数相等 5.从根到叶节点不存在两个连续红色节点的路径 6.空节点(Aunt节点)是黑色的 二、插入新节点导致红黑树规则冲突后的reblance操作 1.reblance操作分类 1)rotate —— black aunt(受影响的节点parent/grand parent/aunt/self) 2) colorfilp —— ...

736

BGP路由聚合:自动聚合和手动聚合

路由聚合是指将多条具体的路由合并为一条聚合路由,以减少路由表的条目数。这种技术通过只向对等体发送聚合后的路由,而不发送所有的具体路由,来减小路由表的规模。同时,被聚合的路由如果发生路由振荡,也不再对网络造成影响,从而提高了网络的稳定性。

网络技术联盟站 1220

rbtree C语言TreeMap rbtree 红黑树

所以根据"Sazid Ahmed" 找到值为200。TreeMap中放入了键值对。

fareast_mzh的博客 1045

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

linux 中的rbtree算法

rbtree.h #ifndef    _LINUX_RBTREE_H#define    _LINUX_RBTREE_H#include #include struct rb_node{    unsigned long  rb_parent_color;#define    RB_RED        0#define    RB_BLACK  

stephanxu的专栏 6546

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

C++ RBTree

C++实现 RBTree,没有网传的那么恐怖

m0_74922218的博客 1167

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Linux内核中rbtree的使用

在结构体内嵌struct rb_node 定义根节点struct rb_root 把节点依据key加入红黑树 移除节点 遍历红黑树 #include #include #include #define NODES 20 struct test_node { u32 key; struct rb_node rb; u32 val; }; static

雜貨鋪 1393

ngx_rbtree的基本使用方法

ngx_rbtree 导语: 红黑树是一个常用的高级数据结构,它虽然是复杂的,但它的最坏情况运行时间也是非常良好的,并且在实践中是高效的:它可以在O(log n)时间内做查找,插入和删除,这里的n是树中元素的数目. 一: 红黑树的性质 1. 每个节点不是红的就是黑的 (必须上色嘛) 2. 根节点必须是黑的 3. 每个叶节点是黑色的(ngx 是sentinel) 4. 如果该节点是

magic的博客 2062

红黑树(RBtree)

tips:这篇需要用到AVLtree的部分知识! 本人之前写的AVLtree, 不妨点击看下或阅读其他大佬的文章!谢谢~~ https://blog.youkuaiyun.com/weixin_44024891/article/details/104910691 红黑树(RBtree)是一个相对平衡的二叉搜索树。 为什么是相对呢?       相对于...

weixin_44024891的博客 336

rbtree(linux)

rbtree.h   #ifndef    _LINUX_RBTREE_H #define    _LINUX_RBTREE_H #include #include struct rb_node {     unsigned long  rb_parent_color; #define    RB_RED        0 #define    RB_BLA

yangzhengyi68的专栏 659

BSTree,AVLTree,RBTree。

BSTree BST存在的主要问题是,数在插入的时候会导致树倾斜,不同的插入顺序会导致树的高度不一样,而树的高度直接的影响了树的查找效率。理想的高度是logN,最坏的情况是所有的节点都在一条斜线上,这样的树的高度为N。 (1) 查找代价: 任何一个数据的查找过程都需要从根结点出发,沿某一个路径朝叶子结点前进。因此查找中数据比较次数与树的形态密切相关。 当树中每个结点左右子树高度大致相同时,树高为logN。则平均查找长度与logN成正比,查找的平均时间复杂度在O(logN)数量级上。 当先后插入的关键字有序

qq_37442823的博客 236

红黑树(RBTree)之插入结点图解

红黑树之建树过程图解

码不停蹄 3703

rbtree使用

从linux内核源码中获取rbtree代码,应用层修改使用

zhanglong71的博客 198

Linux数据结构之rbtree

rbtree是linux内核红黑树实现。一般在struct中嵌套struct rb_node作为树节点: 1.2 初始化根 将struct rb_root赋值为RB_ROOT: 1.3 插入节点 先通过rb_link_node插入节点,再通过rb_insert_color重新平衡红黑树: 1.4 移除节点 通过rb_erase移除节点: 注:该API会重新平衡红黑树通过rb_entry可将节点转为具体类型:......

fs3296的博客 698

算法编程_红黑树(RBTree)

RBtree.h: #ifndef _RBTREE_H_INCLUDED #define _RBTREE_H_INCLUDED enum RBtree_color_type { RBtree_red, RBtree_black }; template struct RBtree_node { typedef RBtree_color_type Color_type; typedef

911的专栏 1256

RBtree

【代码】RBtree。

yige_xiaobai_的博客 137
上一篇: MaxHeep实作
下一篇: SmartPtr实作
daeees
博客等级 码龄19年 6粉丝 51原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值