Red-Black Trees

  • Target: Balanced binary search tree

image-20210308102442191

[Definition] A red-black tree is a binary search tree that satisfies the following red-black properties:

  • Every node is either red or black
  • The root is black
  • Every leaf (NIL) is black
  • If a node is red, then both its children are black
  • For each node, all simple paths from the node to descendant leaves contain the same number of black nodes

[Definition] The black-height of any node xx, denoted by bh(x)\text{bh}(x), is the number of black nodes on any simple path from xx (xx not included) down to a leaf.

  • bh(Tree)=bh(root)\text{bh}(\text{Tree}) = \text{bh}(\text{root})

[Lemma] A red-black tree with NN internal nodes has height at most 2ln(N+1)2\ln(N +1).

  • For any node xx, sizeof(x)2bh(x)1\text{sizeof}(x) \geq 2^{\text{bh}(x)} – 1. Proved by induction.
  • bh(Tree)h(Tree)/2\text{bh}(\text{Tree}) \geq \text{h}(\text{Tree}) / 2 (一条从根结点到叶结点的路径上最多有一半是红色结点,否则会有两个红色结点相邻)

Insert

  • 插入的结点设为红色
image-20210308105907461
  • T=O(h)=O(lnN)T=O(h)=O(\ln N)

Delete

  • Delete a leaf node: Reset its parent link to NIL and adjust only if the node is black
  • Delete a degree 1 node: Replace the node by its single child
  • Delete a degree 2 node:
    • Replace the node by the largest one in its left subtree or the smallest one in its right subtree
    • Delete the replacing node from sub tree
  • Must add 1 black node to the path of the replacing node.

Note: xx is the location of the node to be deleted initially, and a black node needs to be added to the path of xx.

image-20210309145212684

image-20210309145233513

image-20210309145244209

image-20210309145254126

Red-Black Trees VS AVL Trees

  • Number of rotations
Red-Black Trees AVL Trees
Insertion 2\leq2 2\leq2
Deletion 3\leq3 O(logN)O(\log N)
  • AVL Trees is faster for search time because the tree height is smaller.

B+ Trees

[Definition] A B+ tree of order MM is a tree with the following structural properties:

  • The root is either a leaf or has between 22 and MM children.
  • All nonleaf nodes (except the root) have between M/2\lceil M/2\rceil and MM children.
  • All leaves are at the same depth.

Note: Assume each nonroot leaf also has between M/2\lceil M/2\rceil and MM children.

image-20210309155120602

  • All the actual data are stored at leaves
  • Each interior node contains MM pointers to the children, and M1M-1 smallest key values in the subtrees except the first one.
  • Deletion is similar to insertion except that the root is removed when it loses two children.
1
2
3
4
5
6
7
8
9
10
11
12
Btree Insert ( ElementType X,  Btree T ) 
{
Search from root to leaf for X and find the proper leaf node;
Insert X;
while ( this node has M+1 keys )
{
split it into 2 nodes with \lceil(M+1)/2\rceil and \lfloor(M+1)/2\rfloor keys, respectively;
if (this node is the root)
create a new root with two children;
check its parent;
}
}

Depth(M,N)=O(logM/2N)\text{Depth}(M,N)=O(\lceil\log_{\lceil M/2\rceil}N\rceil)

TFind(M,N)=O(logN)T_\text{Find}(M,N)=O(\log N)

Note: The best choice of MM is 3 or 4.

image-20210511190603002