Left-leaning red–black tree

{{Short description|Self-balancing binary search tree data structure}}

{{Infobox data structure

|name=Left-leaning red–black tree

|image = Left-leaning_red–black_tree.png

|type=tree

|invented_by=Robert Sedgewick

|invented_year=2008

|space_avg={{math|O(n)}}

|space_worst={{math|O(n)}}

|search_avg={{math|O(log n)}}

|search_worst={{math|O(log n)}}

|insert_avg={{math|O(log n)}}

|insert_worst={{math|O(log n)}}

|delete_avg={{math|O(log n)}}

|delete_worst={{math|O(log n)}}

}}

A left-leaning red–black (LLRB) tree is a type of self-balancing binary search tree, introduced by Robert Sedgewick. It is a variant of the red–black tree and guarantees the same asymptotic complexity for operations, but is designed to be easier to implement.{{cite web |last=Sedgewick |first=Robert |year=2008 |title=Left-Leaning Red–Black Trees |url=http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf |publisher=Department of Computer Science, Princeton University}}

Properties

A left-leaning red-black tree satisfies all the properties of a red-black tree:

  1. Every node is either red or black.
  2. A NIL node is considered black.
  3. A red node does not have a red child.
  4. Every path from a given node to any of its descendant NIL nodes goes through the same number of black nodes.
  5. The root is black (by convention).

Additionally, the left-leaning property states that:

  1. If a node has only one red child, it must be the left child.

The left-leaning property reduces the number of cases that must be considered when implementing search tree operations.

Relation to 2–3 and 2–3–4 trees

File:Left-leaning red-black trees and 2–3–4 trees.svg

LLRB trees are isomorphic 2–3–4 trees. Unlike conventional red-black trees, the 3-nodes always lean left, making this relationship a 1 to 1 correspondence. This means that for every LLRB tree, there is a unique corresponding 2–3–4 tree, and vice versa.

If we impose the additional requirement that a node may not have two red children, LLRB trees become isomorphic to 2–3 trees, since 4-nodes are now prohibited. Sedgewick remarks that the implementations of LLRB 2–3 trees and LLRB 2–3–4 trees differ only in the position of a single line of code.

Analysis

All of the red-black tree algorithms that have been proposed are characterized by a worst-case search time bounded by a small constant multiple of {{math|log N}} in a tree of {{mvar|N}} keys, and the behavior observed in practice is typically that same multiple faster than the worst-case bound, close to the optimal {{math|log N}} nodes examined that would be observed in a perfectly balanced tree.

Specifically, in a left-leaning red-black 2–3 tree built from {{mvar|N}} random keys, Sedgewick's experiments suggest that:

  • A random successful search examines {{math|log2 N − 0.5}} nodes.
  • The average tree height is about {{math|2 ln N}}.
  • The average size of left subtree exhibits log-oscillating behavior.

Bibliography

  • Robert Sedgewick's Java implementation of LLRB from [https://sedgewick.io/wp-content/themes/sedgewick/papers/2008LLRB.pdf his 2008 paper]
  • [http://www.cs.princeton.edu/~rs/talks/LLRB/movies/ Robert Sedgewick. 20 Apr 2008. Animations of LLRB operations]
  • [http://opendatastructures.org/versions/edition-0.1e/ods-java/9_2_RedBlackTree_Simulated_.html#SECTION001222000000000000000 Open Data Structures - Section 9.2.2 - Left-Leaning Red–Black Trees], Pat Morin

References

{{reflist}}