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

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

View Full Document Right Arrow Icon
Red-Black Trees Data Structures and Algorithms Andrei Bulatov
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Algorithms – Red-Black Trees 13-5 Example nil[T]
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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 +
Background image of page 8
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)
Background image of page 9

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

View Full DocumentRight Arrow Icon
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]
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online