https://github.com/xieqing/red-black-tree

A Red-black Tree Implementation In C

There are several choices when implementing red-black trees:

  • store parent reference or not
  • recursive or non-recursive (iterative)
  • do top-down splits or bottom-up splits (only when needed)
  • do top-down fusion or top-bottom fusion (only when needed)

This implementation's choice:

  • store parent reference
  • non-recursive (iterative)
  • do bottom-up splits (only when needed)
  • do top-bottom fusion (only when needed)

Files

  • rb.h - red-black tree header
  • rb.c - red-black tree library
  • rb_data.h - data header
  • rb_data.c - data library
  • rb_example.c - example code for red-black tree application
  • rb_test.c - unit test program
  • rb_test.sh - unit test shell script
  • README.md - implementation note

If you have suggestions, corrections, or comments, please get in touch with xieqing.

DEFINITION

A red-black tree is a binary search tree where each node has a color attribute, the value of which is either red or black. Essentially, it is just a convenient way to express a 2-3-4 binary search tree where the color indicates whether the node is part of a 3-node or a 4-node. 2-3-4 trees and red-black trees are equivalent data structures, red-black trees are simpler to implement, so tend to be used instead.

Binary search property or order property: the key in each node must be greater than or equal to any key stored in the left sub-tree, and less than or equal to any key stored in the right sub-tree.

In addition to the ordinary requirements imposed on binary search trees, we make the following additional requirements of any valid red-black tree.

Red-black properties:

  1. Every node is either red or black.
  2. The root and leaves (NIL's) are black.
  3. Parent of each red node is black.
  4. Both children of each red node are black.
  5. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.

WHY 2-3-4 TREE?

We could only keep binary search tree almostly balanced instead of completely balanced (consider AVL tree as an example). We need at least 1-3 nodes (2-4 children) to keep tree completely balanced.

    Binary search tree

             1
/ \
/ \
4 9
/ \ / \
3 5 6 nil 2-3-4 tree
(1)
/ \
/ \
(3 4 5) (6 9)
/ | | \ / | \
nil nil nil nil nil nil nil 2-children: (1)
3-children: (6 9)
4-children: (3 4 5)

Why not 2-5 tree, 2-6 tree...?

2-4 tree will guarantee O(log n) using 2, 3 or 4 subtrees per node, while implementation could be trivial (red-black tree). 2-N (N>4) tree still guarantee O(logn), while implementation could be much complicated.

INSERTION

Insertion into a 2-3-4 tree

split, and insert grandparent node into parent cluster

Insertion into a red-black tree

Insert as in simple binary search tree

  • 0-children root cluster (parent node is BLACK) becomes 2-children root cluster: paint root node BLACK, and done
  • 2-children cluster (parent node is BLACK) becomes 3-children cluster: done
  • 3-children cluster (parent node is BLACK) becomes 4-children cluster: done
  • 3-children cluster (parent node is RED) becomes 4-children cluster: rotate, and done
  • 4-children cluster (parent node is RED) splits into 2-children cluster and 3-children cluster: split, and insert grandparent node into parent cluster

DELETION

Deletion from 2-3-4 tree

  • transfer, and done
  • fuse, and done or delete parent node from parent cluster

Deletion from red-black tree

Delete as in simple binary search tree

  • 4-children cluster (RED target node) becomes 3-children cluster: done
  • 3-children cluster (RED target node) becomes 2-children cluster: done
  • 3-children cluster (BLACK target node, RED child node) becomes 2-children cluster: paint child node BLACK, and done
  • 2-children root cluster (BLACK target node, BLACK child node) becomes 0-children root cluster: done
  • 2-children cluster (BLACK target node, 4-children sibling cluster) becomes 3-children cluster: transfer, and done
  • 2-children cluster (BLACK target node, 3-children sibling cluster) becomes 2-children cluster: transfer, and done
  • 2-children cluster (BLACK target node, 2-children sibling cluster, 3/4-children parent cluster) becomes 3-children cluster: fuse, paint parent node BLACK, and done
  • 2-children cluster (BLACK target node, 2-children sibling cluster, 2-children parent cluster) becomes 3-children cluster: fuse, and delete parent node from parent cluster

References

  1. https://en.wikipedia.org/wiki/Red-black_tree
  2. https://en.wikipedia.org/wiki/2-3-4_tree
  3. https://www.cs.usfca.edu/~galles/visualization/RedBlack.html

License

Copyright (c) 2019 xieqing. https://github.com/xieqing

May be freely redistributed, but copyright notice must be retained.

红黑树(red-black tree)实现记录的更多相关文章

  1. 笔试算法题(51):简介 - 红黑树(RedBlack Tree)

    红黑树(Red-Black Tree) 红黑树是一种BST,但是每个节点上增加一个存储位表示该节点的颜色(R或者B):通过对任何一条从root到leaf的路径上节点着色方式的显示,红黑树确保所有路径的 ...

  2. C# 链表 二叉树 平衡二叉树 红黑树 B-Tree B+Tree 索引实现

    链表=>二叉树=>平衡二叉树=>红黑树=>B-Tree=>B+Tree 1.链表 链表结构是由许多节点构成的,每个节点都包含两部分: 数据部分:保存该节点的实际数据. 地 ...

  3. 2-3 树/红黑树(red-black tree)

    2-3 tree 2-3树节点: null节点,null节点到根节点的距离都是相同的,所以2-3数是平衡树 2叉节点,有两个分树,节点中有一个元素,左树元素更小,右树元素节点更大 3叉节点,有三个子树 ...

  4. 红黑树(R-B Tree)

    R-B Tree简介 R-B Tree,全称是Red-Black Tree,又称为“红黑树”,它一种特殊的二叉查找树.红黑树的每个节点上都有存储位表示节点的颜色,可以是红(Red)或黑(Black). ...

  5. 树-红黑树(R-B Tree)

    红黑树概念 特殊的二叉查找树,每个节点上都有存储位表示节点的颜色是红(Red)或黑(Black).时间复杂度是O(lgn),效率高. 特性: (1)每个节点或者是黑色,或者是红色. (2)根节点是黑色 ...

  6. 红黑树(RB Tree)

    看到一篇很好的文章 文章来源:http://www.360doc.com/content/15/0730/00/14359545_488262776.shtml 红黑树是一种高效的索引树,多于用关联数 ...

  7. 红黑树(Red-Black tree)

    红黑树又称红-黑二叉树,它首先是一颗二叉树,它具体二叉树所有的特性.同时红黑树更是一颗自平衡的排序二叉树.我们知道一颗基本的二叉树他们都需要满足一个基本性质–即树中的任何节点的值大于它的左子节点,且小 ...

  8. java数据结构——红黑树(R-B Tree)

    红黑树相比平衡二叉树(AVL)是一种弱平衡树,且具有以下特性: 1.每个节点非红即黑; 2.根节点是黑的; 3.每个叶节点(叶节点即树尾端NULL指针或NULL节点)都是黑的; 4.如图所示,如果一个 ...

  9. 吐血整理:二叉树、红黑树、B&B+树超齐全,快速搞定数据结构

    前言 没有必要过度关注本文中二叉树的增删改导致的结构改变,规则操作什么的了解一下就好,看不下去就跳过,本文过多的XX树操作图片纯粹是为了作为规则记录,该文章主要目的是增强下个人对各种常用XX树的设计及 ...

随机推荐

  1. [UE4]计算两点距离

    (Vector-Vector).VectorLength (Vector_End- Vector_Start ).Normalize,获取从起始位置指向目标位置的单位向量.

  2. 线程相关的sleep()、yield()、wait()、join()方法介绍

    1.Thread.sleep()与Thread.yield()都会暂缓当前线程执行,转为执行其他线程(忽略优先级),如果持有锁,则不会释放. 2.Thread.sleep()可以精确指定休眠的时间,而 ...

  3. Ajax异步请求阻塞情况的解决办法(asp.net MVC Session锁的问题)

    讨论今天这个问题之前,我们先来看下浏览器公布的资源并发数限制个数,如下图 不难看出,目前主流浏览器支持都是最多6个并发 需要注意的是,浏览器的并发请求数目限制是针对同一域名的 意即,同一时间针对同一域 ...

  4. Difference between hash() and id()

    https://stackoverflow.com/questions/34402522/difference-between-hash-and-id There are three concepts ...

  5. 踩坑之VC报错 error RC2104 : undefined keyword or key name

    .RC文件中第541行 MENUITEM "CNDev,                       ID_CNDEV 少了一个"    正确的应该是MENUITEM " ...

  6. Flutter windows环境安装 + 模拟设备 + 项目运行

    目录: 一.JDK安装 1.1.JDK下载 1.2.环境变量配置 1.3.测试 二.ANDROID-SDK安装 2.1.下载 2.2.环境变量配置 三.Flutter安装 3.1.下载 3.2.环境变 ...

  7. 河北省重大技术需求征集系统原型(MVC框架业务流程简介)

    这段时间了解了一些MVC框架. 一.MVC简介 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式.它的模式是JSP + se ...

  8. 如何解决vux不兼容安卓低版本问题

    最近做移动端H5页面用VUX来写UI组件这块.ios测试的时候没啥大问题,不过在4.4版本的华为手机上测试就崩了.接下来详细记述下崩的几个点. 第一:vux自带的提示框,在低版本安卓系统上全不是居中显 ...

  9. MPI 集合通信函数 MPI_Reduce(),MPI_Allreduce(),MPI_Bcast(),MPI_Scatter(),MPI_Gather(),MPI_Allgather(),MPI_Scan(),MPI_Reduce_Scatter()

    ▶ 八个常用的集合通信函数 ▶ 规约函数 MPI_Reduce(),将通信子内各进程的同一个变量参与规约计算,并向指定的进程输出计算结果 ● 函数原型 MPI_METHOD MPI_Reduce( _ ...

  10. Springboot整合druid

    目录 Springboot整合druid application.yml DruidConfig 数据监控地址:http://localhost:8080/druid Springboot整合drui ...