Red-Black Trees Outline From (2,4) trees to Red-Black trees - - PowerPoint PPT Presentation

red black trees outline
SMART_READER_LITE
LIVE PREVIEW

Red-Black Trees Outline From (2,4) trees to Red-Black trees - - PowerPoint PPT Presentation

Red-Black Trees Outline From (2,4) trees to Red-Black trees Definition and height Search Insertion Restructuring Recoloring Deletion Restructuring Recoloring Adjustment Red-Black Trees 2 (2,4)


slide-1
SLIDE 1

Red-Black Trees

slide-2
SLIDE 2

Outline

  • From (2,4) trees to Red-Black trees
  • Definition and height
  • Search
  • Insertion

– Restructuring – Recoloring

  • Deletion

– Restructuring – Recoloring – Adjustment

Red-Black Trees 2

slide-3
SLIDE 3

(2,4) Trees

A multi-way search tree, where an internal node has k children and stores k-1 elements, and it has the following additional properties:

  • Node-Size property: all internal nodes have at most four children

(i.e., k = 2,3,4)

  • Depth property: all external nodes have the same depth

Depending on the number of children, an internal node is called either a 2-node, 3-node, or 4-node

Red-Black Trees 3

11 24 2 6 8 15 27 32

3-node 2-node 4-node

slide-4
SLIDE 4

From (2,4) to Red-Black Trees

  • A red-black tree is a representation of a (2,4) tree by means of a

binary tree whose nodes are colored red or black.

  • In comparison with a (2,4) tree, a red-black tree has

– same logarithmic time performance – simpler implementation with a single node type

Red-Black Trees 4

2 6 7 3 5 4

4 6 2 7 5 3 3 5

  • r
slide-5
SLIDE 5

Red-Black Trees

A binary search tree with nodes colored red and black in a way that satisfies the following color properties: 1. Root property: the root is black. 2. External property: every leaf is black. 3. Internal property: the children of a red node are black. 4. Depth property: all leaves have the same black depth.

Red-Black Trees 5

9 15 4 6 2 12 7 21

slide-6
SLIDE 6

Ex: Is it a Red-Black Tree?

Red-Black Trees 6

Yes

9 15 4 9 15 4

Yes

9 15 4

No Violates root & internal property

9 15 4

No Violates external property

slide-7
SLIDE 7

Ex: Is it a Red-Black Tree?

Red-Black Trees 7

15 21 3 5 17 8 22 23

No Violates depth property

Black depth = 4 Black depth = 2

slide-8
SLIDE 8

Height of a Red-Black Tree

Theorem: A red-black tree storing n items has height O(log n) Proof: Let T* be the portion of the tree T consisting of all nodes with depth ≤ h* T* is complete. Thus, h* ≤ logn. Because h ≤ 2h*, h ≤ 2logn ∈ O(log n).

  • The search algorithm for a red-black tree is the same as that for a

binary search tree.

  • By the above theorem, searching takes O(log n) time

8

… … … … … h* h ≤ 2h* T*

Consider the shortest path (left) and longest path (right) from the root to an external node.

slide-9
SLIDE 9

Insertion

  • Use insertion algorithm for binary search trees and color red the newly

inserted node z, unless it’s the root. – we preserve the root, external, and depth properties – if the parent v of z is black, we also preserve the internal property and we are done

Red-Black Trees 9

6 3 8 6 3 8 4 z v v z

slide-10
SLIDE 10

Insertion

  • Use insertion algorithm for binary search trees and color red the newly

inserted node z, unless it’s the root. – we preserve the root, external, and depth properties – if the parent v of z is black, we also preserve the internal property and we are done – if the parent v of z is red, we have a double red (a violation of the internal property), which requires a reorganization of the tree

  • Ex: Insert 4 causes a double red

Red-Black Trees 10

6 3 8 6 3 8 4 z v v z

slide-11
SLIDE 11

Fixing a Double Red

Consider a double red with child z and parent v, and let w be the sibling of v

  • Case 1: w is black
  • Case 2: w is red

Red-Black Trees 11

4 6 7

z v w

2 4 6 7

z v w

2

Restructuring Recoloring

Note: pictures with dangling edges are a visualization of a small portion of larger tree

slide-12
SLIDE 12

Restructuring

Red-Black Trees 12

Consider a double red with child z and parent v and let w be the sibling of v. Let u be the parent of v. 1. Relabel nodes z, v, u temporarily as a, b, c so that a, b, c will be visited in this order by an inorder tree traversal. 2. Replace u with the node labeled b (colored black). Make nodes a and c the left and right child of b (each colored red).

4 6 7

z v w

2

u 4 6 7 b c w 2 a a=u b=z c=v

slide-13
SLIDE 13

Restructuring

Red-Black Trees 13

2 4 6 2 6 4

There are four restructuring configurations depending on the in-order traversal of nodes z, v, u

6 2 4 6 4 2 2 6 4

u u u u v v v v z z z z

u, z, v v, z, u z, v, u u, v, z Inorder traversal:

slide-14
SLIDE 14

Fixing a Double Red

Consider a double red with child z and parent v, and let w be the sibling of v

  • Case 1: w is black
  • Case 2: w is red

Red-Black Trees 14

4 6 7

z v w

2 4 6 7

z v w

2

Restructuring Recoloring

slide-15
SLIDE 15

Recoloring

Consider a double red with child z and parent v, and let w be the sibling of v. Let u be the parent of v. 1. Color v and w black. 2. Color u red, unless it’s the root. 3. If the double-red problem reappears at u, then repeat the process for fixing two reds at u (either with restructuring or recoloring). Fixes problem locally, but can propagate double-red problem up the tree.

Red-Black Trees 15

4 6 7

z v w

2 4 6 7

z v w

2

u u

slide-16
SLIDE 16

9/14/19 4:20 AM Red-Black Trees 16

Analysis of Insertion

  • Recall that a red-black tree has

O(log n) height

  • Step 1 takes O(log n) time

because we visit O(log n) nodes

  • Step 2 takes O(1) time
  • Step 3 takes O(log n) time

because we perform

– O(log n) recolorings, each taking O(1) time, and – at most one restructuring taking O(1) time

  • Thus, an insertion in a red-black

tree takes O(log n) time Algorithm insertItem(k, o)

  • 1. We search for key k to locate the

insertion node z

  • 2. We add the new item (k, o) at

node z and color z red

  • 3. while doubleRed(z)

if isBlack(sibling(parent(z))) restructure(z) return else { sibling(parent(z) is red } z ¬ recolor(z)

slide-17
SLIDE 17

Deletion

  • Use deletion algorithm for binary search trees so as to delete internal

node v and its external child w. Let r be the sibling of w. – if v is red or r is red, then color r black and we are done.

Red-Black Trees 17

6 3 8 v r w 6 3 r 6 3 8 v r w 7 6 3 7 r

slide-18
SLIDE 18

Deletion

  • Use deletion algorithm for binary search trees so as to delete internal

node v and its external child w. Let r be the sibling of w. – if v is red or r is red, then color r black and we are done. – otherwise (v and r are black) we color r double black, which requires a reorganization of the tree

  • Ex: Delete 8 causes a double black

Red-Black Trees 18

6 3 8 4 v r w 6 3 4 r

slide-19
SLIDE 19

Fixing a Double Black

Let y be the sibling and x be the parent of the double black node. The algorithm to fix a double black node considers three cases: Case 1: y is black and has a red child z

  • We perform a restructuring on y, x, z, and we are done

Red-Black Trees 19

6 3 4 r y x z 4 3 r 6

slide-20
SLIDE 20

Fixing a Double Black

Let y be the sibling and x be the parent of the double black node. The algorithm to fix a double black node considers three cases: Case 1: y is black and has a red child z

  • We perform a restructuring on y, x, z, and we are done

Case 2: y is black and its children are both black

  • We perform a recoloring. Color r black, and y red.

– If x is red, color it black. Otherwise, color x double-black. – This may propagate up the double black violation

Red-Black Trees 20

6 3 r y x z 6 3 r y z x

slide-21
SLIDE 21

Fixing a Double Black

Let y be the sibling and x be the parent of the double black node. The algorithm to fix a double black node considers three cases: Case 1: y is black and has a red child z

  • We perform a restructuring on y, x, z, and we are done

Case 2: y is black and its children are both black

  • We perform a recoloring. Color r black, and y red.

– If x is red, color it black. Otherwise, color x double-black. – This may propagate up the double black violation Case 3: y is red

  • We perform an adjustment, after which either Case 1 or Case 2 applies

Deletion in a red-black tree takes O(log n) time.

21

slide-22
SLIDE 22

9/14/19 4:20 AM Red-Black Trees 22

Red-Black Tree Reorganization

Insertion

(fix double red) result

restructuring double red removed recoloring double red removed or propagated up

Deletion

(fix double black) result

restructuring double black removed recoloring double black removed or propagated up adjustment restructuring or recoloring follows