Red–black tree

Created
Created
2023 Sep 27 6:48
Editor
Creator
Creator
Seonglae Cho
Edited
Edited
2023 Oct 26 5:40
Refs
Refs

Results

  • smallest is alway left-most
 
 

Properties

  • Every node is either red or black
  • The root and leaves are black
  • If a node is red, then its parent is black
  • All simple paths from any node xx to a descendant leaf have the same number of black nodes
    • blackheight(x)black-height(x)
    • Simple paths means path without repetition
 
 
 

Theorem

A red-black tree with nn keys has height
h2lg(n+1)h \leq 2lg(n+1)
  • Merge red nodes into their black parents
  • This process produces a tree in which each node has 2,3, or 4 children
  • The 2-3-4 trees has uniform depth hh' of leaves
 
 
 

Corollary

  • search, min, max, successor, predecessor all run in lg(n)lg(n)
 

insertion and deletion through rotation run in lg(n)lg(n)

A rotation can be performed in O(1)O(1) time
notion image
  1. insert
  1. recolor
  1. right rotate child
  1. left rotate parent
  1. recolor
delete is
 
 
 

Enhancement

you can enhance in various ways
Let kk as max value, we can achieve performance O(lgk)O(lgk)
where n>>kn >> k, Therefore, the array consists of many duplicated small values.
 
 
 
 
 
 

Recommendations