San Diego State University

A binary search tree is a red-black tree if:

- 1. Every node is either red or black
- 2. Every leaf (nil) is black
- 3. If a node is red, then both its children are black
- 4. Every simple path from a node to a descendant leaf contains the same number of black nodes

Black-height of a node x, bh(x), is the number of black nodes on any path from x to a leaf, not counting x

Lemma

- A red-black tree with n internal nodes has height at most 2lg(n+1)

proof

- Show that subtree starting at x contains at least 2
^{bh(x)}-1 internal nodes. By induction on height of x: - if x is a leaf then bh(x) = 0, 2
^{bh(x)}-1 - Assume x has height h, x's children have height h -1
- x's children black-height is either bh(x) or bh(x) -1
- By induction x's children subtree has 2
^{bh(x)-1}-1 internal nodes - So subtree starting at x contains
- 2
^{bh(x)-1}-1 + 2^{bh(x)-1}-1 + 1 = 2^{bh(x)}-1 internal nodes - let h = height of the tree rooted at x
- bh(x) >= h/2
- So n >= 2
^{h/2}-1 <=> n + 1 >= 2^{h/2}<=> lg(n+1) >= h/2 - h <= 2lg(n+1)

Color the node Red

Insert as in a regular BST

If have parent is red

x is node of interest, x's uncle is Red

Decrease x's black height by one

x's uncle is Black, x is a Right child

Transform to case 3

x's uncle is Black, x is a Left child

Terminal case, tree is Red-Black tree

Insertion takes O(lg(n)) time

Requires at most two rotations

Find node to delete

Delete node as in a regular BST

Node to be deleted will have at most one child

If we delete a Red node tree still is a Red-Black tree

Assume we delete a black node

Let x be the child of deleted node

If x is red, color it black and stop

If x is black mark it double black and apply the following:

x's sibling is red

x stays at same black height

Transforms to case 2b then terminates

x's sibling is black

x's parent is black

Decreases x black height by one

x's sibling is black

x's parent is red

Terminal case, tree is Red-Black tree

x's sibling is black

x's parent is either

x's sibling's left child is red

x's sibling's right child is black

x stays at same black height

Transforms to case 4

x's sibling is black

x's parent is either

x's sibling's left child is either

x's sibling's right child is red

Terminal case, tree is Red-Black tree

Delete time is O(lg(n))

At most three rotations are done

Let a and b be integers with a >= 2 and 2a-1 <= b. A tree T is an (a,b)-tree if

a) All leaves of T have the same depth

b) All internal nodes v of T satisfy c(v) <= b

c) All internal nodes v of T except the root satisfy c(v) >= a

d) The root of T satisfies c(v) >= 2

c(v) = number of children of node v

A tree T is a B-Trees of degree t if

a) All leaves of T have the same depth

b) All nodes of T except the root have at least t-1 keys

c) All nodes of T except the root have at most 2t-1 keys

d) The root of T at least one key

e) A node with n keys has n+1 children

c(v) = number of children of node v

proof.

take log of both sides.

Theorem. The worst case search time on a n-key B-tree T of degree t is O(lg(n)).

- A node in T has t-1 <= K <= 2t-1 keys in sorted order.
- Worst case:
- K = t-1 for all nodes
- searching for X not in the tree
- Given a node, W, in T, how much work does it take to find the subtree of W that would contain X?
- Using binary search it takes
- = = comparisons
- Since the height of the tree is in worst case the total amount of work is:

A full node is one that contains 2t-1 keys

1. Find the leaf that should contain X

2. If the path from the root to the leaf contains a full node split the node when you first search it.

3. Insert X into the proper leaf

A minimal node is one that contains t-1 keys and is not the root

In the search path from the root to node containing X, if you come across a minimal node add a key to it.

Case 3. Searching node W that does not contain X. Let c be the child of W that would contain X.

Case 3a. if c has t-1 keys and a sibling has t or more keys, steal a key from the sibling

Case 3b. if c has t-1 keys and all siblings have t-1 keys, merge c with a sibling

Case 2a. If the child y of W that precedes X in W has at least t keys, steal predecessor of W

Case 1. X is in node W a leaf. By case 3, W has at least t keys. Remove X from W

Theorem. A Red-Black tree is a B-Tree with degree 2

proof:

Must show:

- 1. If a node is red, then both its children are black