Coder Social home page Coder Social logo

liuxinglong / hashtable-tree Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 1.14 MB

Hash Map、二叉搜索树、红黑树、epoll、socket、tcp、udp、多进程、共享内存、原子操作、惊群、协议栈

C++ 37.88% C 61.97% Shell 0.15%
epoll tcp udp bst-tree bstree rbtree socket process bst

hashtable-tree's Introduction

BST

Hash Map (基于 Binary Search Tree 实现)

哈希表 + 二叉搜索树 实现 key => value 数据存储与修改。

特点:千万级 key => value 数据,插入、查询、修改、删除 毫秒级实现。

红黑树实现

插入节点初始都为红色

  • 1、节点必须是红色或者黑色。
  • 2、根节点必须是黑色。
  • 3、叶节点(NIL)是黑色的。(NIL节点无数据,是空节点)
  • 4、红色节点必须有两个黑色儿子节点。
  • 5、从任一节点出发到其每个叶子节点的路径,黑色节点的数量是相等的。

插入操作

1、情况一:插入节点为根节点,将插入节点修改为黑色。

2、情况二:插入节点的父亲节点为黑色,直接插入即可。

3、情况三:插入N节点的叔父(U)都为红色;将父亲(P)和叔父(U)修改为黑色祖父修改为红色,然后对其祖父做递归进行调整。

             G(黑)                              G(红)
           /       \                          /       \
         P(红)     U(红)      ===》          P(黑)     U(黑) 
        /                                  /
      N(红)                               N(红)

4、情况四:插入N节点的叔父(U)为黑色,且插入N为右节点,N的父亲同为左节点。对N的父亲节点(P)执行左旋操作,进入情况五状态。 (对称情况:插入N节点的叔父(U)为黑色,且插入N为左节点,N的父亲同为右节点,类似反向处理)

              G(黑)                              G(黑)
            /       \                         /       \
         P(红)     U(黑)      ===》          N(红)     U(黑) 
        /   \                              /   \
       1     N(红)                       P(红)   3
            /   \                        /   \
           2     3                      1     2

5、情况五:插入N节点的叔父(U)为黑色,且插入N和父亲同为左节点。对N的祖父节点(G)进行右旋,将祖父节点G修改为红色,父亲节点P修改为黑色。 (对称情况:插入N节点的叔父(U)为黑色,且插入N和父亲同为右节点,类似反向处理)

              G(黑)                              P(红)                            P(黑)
            /       \                          /      \                        /       \
          P(红)     U(黑)      ===》          N(红)    G(黑)        ===》      N(红)     G(红)     
        /      \                            /    \    /    \                /   \     /   \
       N(红)    3                          1      2   3     U(黑)           1    2    3     U(黑)
      /  \
     1    2

hashtable-tree's People

Contributors

liuxinglong avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.