Red-Black Trees and B+ Trees
Red-Black Trees
- Target: Balanced binary search tree
[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 , denoted by , is the number of black nodes on any simple path from ( not included) down to a leaf.
[Lemma] A red-black tree with internal nodes has height at most .
- For any node , . Proved by induction.
- (一条从根结点到叶结点的路径上最多有一半是红色结点,否则会有两个红色结点相邻)
Insert
- 插入的结点设为红色
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: is the location of the node to be deleted initially, and a black node needs to be added to the path of .
Red-Black Trees VS AVL Trees
- Number of rotations
Red-Black Trees | AVL Trees | |
---|---|---|
Insertion | ||
Deletion |
- AVL Trees is faster for search time because the tree height is smaller.
B+ Trees
[Definition] A B+ tree of order is a tree with the following structural properties:
- The root is either a leaf or has between and children.
- All nonleaf nodes (except the root) have between and children.
- All leaves are at the same depth.
Note: Assume each nonroot leaf also has between and children.
- All the actual data are stored at leaves
- Each interior node contains pointers to the children, and 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 | Btree Insert ( ElementType X, Btree T ) |
Note: The best choice of is 3 or 4.
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 OE.Heart's Blog!