# 13 - Red-Black Trees Data Structures and Algorithms Andrei...

This preview shows pages 1–11. Sign up to view the full content.

Red-Black Trees Data Structures and Algorithms Andrei Bulatov

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithms – Red-Black Trees 13-2 Red-Black Trees All binary search tree operations take O(h) time, where h is the height of the tree Therefore, it is important to `balance’ the tree so that its height is as small as possible There are many ways to achieve this One of them: Red-Black trees Every node of such a tree contains one extra bit, its color Another agreement: the Nil pointer is treated as a leaf, an extra node The rest of the nodes are called internal
Algorithms – Red-Black Trees 13-3 Red-Black Properties A binary search tree is a red-black tree if it satisfies the following red-black properties: s Every node is either red or black s The root is black s Every leaf (Nil) is black s If a node is red, then both its children are black s For each node, all paths from the node to descendant leaves contain the same number of black nodes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithms – Red-Black Trees 13-4 Example Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil Nil
Algorithms – Red-Black Trees 13-5 Example nil[T]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithms – Red-Black Trees 13-6 Black Height The number of black nodes on paths from node x to its descendant leaves in a red-black tree is called its black height , denoted bh(T) Lemma A red-black tree with n internal nodes has height at most 2 log(n + 1) Proof We show first that the subtree rooted at x contains at least nodes Induction on bh(x) Base Case: If bh(x) = 0, then x is a leaf, nil[T] In this case, internal nodes 1 2 ) ( - x bh 0 1 2 1 2 0 ) ( = - = - x bh
Algorithms – Red-Black Trees 13-7 Black Height (cntd) Inductive hypothesis: the claim is true for any y with height less than that of x Inductive Case: Let bh(x) > 0 and has two children The black height of the children is either bh(x) or bh(x) – 1, depending on its color Since the children have smaller height, we can apply the induction hypothesis Thus the subtree rooted at x contains at least nodes 1 2 1 ) 1 2 ( ) 1 2 ( ) ( 1 ) ( 1 ) ( - = + - + - - - x bh x bh x bh

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithms Red-Black Trees 13-8 Black Height (cntd) Suppose h is the height of the tree By the red-black property at least half of nodes on every root-to-leaf path are black (not including the root) Therefore the black height of the root is at least h/2 Thus QED 1 2 2 / - h n 2 / ) 1 log( h n +
Algorithms – Red-Black Trees 13-9 Rotations Sometimes we will need to rearrange pointers inside an RB-tree x y Left-Rotate(T,x) y x α β γ α β γ Right-Rotate(T,x)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithms – Red-Black Trees 13-10 Rotations: Pseudocode Left-Rotate(T,x) set y:=right[x] set right[x]:=left[y] set parent[left[y]]:=x set parent[y]:=parent[x]
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

13 - Red-Black Trees Data Structures and Algorithms Andrei...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online