36
Red-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees 2 Insertion into a Red-Black Tree algorithm for insertion an elaborate example of an insert inserting a sequence of numbers 3 Recursive Insert Function pseudo code MCS 360 Lecture 36 Introduction to Data Structures Jan Verschelde, 20 April 2020 Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 1 / 36

Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

MCS 360 Lecture 36Introduction to Data StructuresJan Verschelde, 20 April 2020

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 1 / 36

Page 2: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 2 / 36

Page 3: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Binary Search Trees

Binary search trees store ordered data.Rules to insert x at node N:

if N is empty, then put x in Nif x < N, insert x to the left of Nif x ≥ N, insert x to the right of N

Balanced tree of n elements has depth is log2(n)⇒ retrieval is O(log2(n)), almost constant.

With rotation we make a tree balanced.Alternative to AVL tree: nodes with > 2 children.

Every node in a binary tree has at most 2 children.A 2-3-4 tree has nodes with 2, 3, and 4 children.A red-black tree is a binary tree equivalent to a 2-3-4 tree.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 3 / 36

Page 4: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

a red-black treered nodes havehollow rings

��

��

��

��

��

��

��

��

����11

� @��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

� @��

��

����5

��

��

����8

��

��

��

��

��

��

��

��

����14

Four invariants:1 A node is either red or black.2 The root is always black.3 A red node has always black children.4 #black nodes in path from root to any leaf is the same.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 4 / 36

Page 5: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

verification of all four invariants11 is black

2 is red1 is black7 is black

5 is red8 is red

14 is blackThe root is black.Node 2 is red and has black left child.Node 2 is red and has black right child.All red nodes have black children.The first path ends at 1 is black.The number of black nodes is 2.Reached end of path at 5 is red.The number of black nodes is 2.Reached end of path at 8 is red.The number of black nodes is 2.Reached end of path at 14 is black.The number of black nodes is 2.The tree respects all invariants.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 5 / 36

Page 6: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

a node in a red-black treetemplate<typename Item_Type>struct Node{

Item_Type data; // numbers stored at node in treebool isred; // true if red, false if blackNode *parent; // pointer to the parent nodeNode *left; // pointer to left branch of treeNode *right; // pointer to right branch of tree

Node(const Item_Type& item, bool redcolor = true,Node* parent_ptr = NULL,Node* left_ptr = NULL, Node* right_ptr = NULL) :data(item), isred(redcolor), parent(parent_ptr),left(left_ptr), right(right_ptr) {}

// Makes a node with as data field the value of item,// the default color is red, and all pointers are set// to NULL by default.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 6 / 36

Page 7: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

a class to define a red-black tree

The root of the tree is a pointer to a node,declared as private, as a hidden data attribute.

The bool isred in Node respects the first invariant:all nodes are either red (true) or black (false).

The second invariant: red nodes have black childrenis verified by a recursive tree traversal.

The #black nodes in any path from root to leaf is the sameis verified via backtracking.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 7 / 36

Page 8: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

performance

#black nodes in path from root to any leaf is the same⇒ equivalent 2-3-4 tree is balanced.

Performance of a red-black tree:

upper limit of depth of red-black tree: 2 log2(n) + 2for a search of tree with n elements.

average cost of search is empirically: 1.002 log2(n).

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 8 / 36

Page 9: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 9 / 36

Page 10: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

relation with 2-3-4 trees: nodes with 2 and 4 children

No red children: ��

��

��

��

��

��

��

��

����x� @

< x ≥ x

a 2-node��

��x

� @< x ≥ x

Two red children:��

��

��

��

��

��

��

��

����y� @�

���

����x

�� AA< x ≥ x

< y

��

��

����z

�� AA< z≥ y

≥ z

a 4-node�� ��x , y , z���

< x��

< y≥ x

AA≥ y< z

HHH≥ z

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 10 / 36

Page 11: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

relation with 2-3-4 trees: nodes with 3 childrenOne red child:

One red child:

��

��

��

��

��

��

��

��

����y� @�

���

����x

�� AA< x ≥ x

< y

≥ y

a 3-node�� ��x , y���

< x ≥ x< y

HHH≥ y

��

��

��

��

��

��

��

��

����x� @�

���

����y

�� AA

< x

< y≥ x

≥ y

a 3-node�� ��x , y���

< x ≥ x< y

HHH≥ y

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 11 / 36

Page 12: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 12 / 36

Page 13: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Changing Colors

Case 0: The color of a new leaf is always red.If parent of new leaf is black, then done.

Case 1: We just change colors, e.g.:��

��

��

��

��

��

��

��

����2

� @��

��

����1

��

��

����7

���

��

����5

��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

���

��

����5

��

��

��

��

��

��

��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

���

��

����5

1 If sibling of parent also red, change color of parent and its siblingto black, and change color of the grandparent.

2 Ensure color of the root is black.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 13 / 36

Page 14: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Tree Rotation

What if parent does not have a red sibling?

Case 2: One rotation suffices, e.g.:��

��

��

��

��

��

��

��

����2

@��

��

����5

@��

��

����7

��

��

����2

@��

��

��

��

��

��

��

��

����5

@��

��

����7

��

��

����2

��

��

��

��

��

��

��

��

����5

� @��

��

����7

1 Change the color of the parent to black,change the color of the grandparent to red.

2 Rotate to restore the 4th invariant.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 14 / 36

Page 15: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Double Rotation

One rotation works for right-right or left-left tree.

Case 3: Tree is right-left (or left-right), e.g.:��

��

��

��

��

��

��

��

����2

A��

��

����7

���

��

����5

��

��

��

��

��

��

��

��

����2

A��

��

����5

A��

��

����7

��

��

����2

A��

��

��

��

��

��

��

��

����5

A��

��

����7

��

��

����2

��

��

��

��

��

��

��

��

����5

� @��

��

����7

1 Rotate right-left tree to right-right tree.2 Change color of parent and grandparent.3 Rotate to restore the 4th invariant.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 15 / 36

Page 16: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 16 / 36

Page 17: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

inserting into a red-black tree

During insertion, many cases may occur:1 Locate the new leaf and color it red.2 If in case 0, then done.3 Apply cases 1, 2, and 3, restoring invariants.

1 case 1: parent has red sibling⇒ change colors

2 case 2: left-left or right-right tree⇒ change colors and rotate

3 case 3: right-left or left-right tree⇒ rotate, change colors, rotate

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 17 / 36

Page 18: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

inserting 4 ��

��

��

��

��

��

��

��

����11

� @��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

� @��

��

����5

���

��

����4

��

��

����8

��

��

��

��

��

��

��

��

����14

��

��

��

��

��

��

��

��

����11

� @��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

����7

� @��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14

After changing colors of 5 and 8 to black,and changing 7 to red, but then parent of 7 is red too...

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 18 / 36

Page 19: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

rotating around 2��

��

��

��

��

��

��

��

����11

� @��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

����7

� @��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14

��

��

��

��

��

��

��

��

����11

� @

��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

����7

� @

��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14

Case 3: rotate left-right tree (11-2-7) to left-left treeis the first step...

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 19 / 36

Page 20: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

changing colors ��

��

��

��

��

��

��

��

����11

� @

��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

����7

� @

��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14

��

��

����11

� @

��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

� @

��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14

The second step in Case 3 is to change the color of 7 to black and thecolor of 11 to red.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 20 / 36

Page 21: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

rotate around 11 ��

��

����11

� @

��

��

����2

� @��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

� @

��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14 ��

��

����11

� A

��

��

����2

� A��

��

��

��

��

��

��

��

����1

��

��

��

��

��

��

��

��

����7

� @

��

��

��

��

��

��

��

��

����5

���

��

����4

��

��

��

��

��

��

��

��

����8

��

��

��

��

��

��

��

��

����14

In the last step of Case 3, we restored the balance by a right rotationaround 11.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 21 / 36

Page 22: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 22 / 36

Page 23: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

inserting a sequence of numbers

Insert 48, 84, 43, 91, 38, 79, 63, 59, 49, 98 into a red-black tree.

After inserting 48, 84, 43:

��

��

����84

��

��

����43

��

��

��

��

��

��

��

��

����48

� @ ��

��

����84

A

��

��

����43

��

��

��

��

��

��

��

��

����48

� @

��

��

����91

After inserting 91 to the binary search tree,we verify the four invariants of a red-black tree.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 23 / 36

Page 24: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

a red node has always black children

After inserting 91, we have to change colors:

��

��

����84

A

��

��

����43

��

��

��

��

��

��

��

��

����48

� @

��

��

����91

��

��

��

��

��

��

��

��

����84

A

��

��

��

��

��

��

��

��

����43

��

��

��

��

��

��

��

��

����48

� @

��

��

����91

The numbers left to insert are 38, 79, 63, 59, 49, 98.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 24 / 36

Page 25: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

inserting more numbers

Inserting 38, 79, 63:

��

��

��

��

��

��

��

��

����84

A

��

��

��

��

��

��

��

��

����43

��

��

��

��

��

��

��

��

����48

� @

��

��

����91

��

��

��

��

��

��

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

����79

���

��

����63

��

��

����91

Are all four invariants of a red-black tree satisfied?

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 25 / 36

Page 26: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

changing colors

A red node must always have black children.

��

��

��

��

��

��

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

����79

���

��

����63

��

��

����91

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

��

��

��

��

��

��

����79

���

��

����63

��

��

��

��

��

��

��

��

����91

The numbers left to insert are 59, 49, 98.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 26 / 36

Page 27: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

rotate a left-left tree

After interting 59, we have a left-left tree.Coloring 59 black will violate the fourth invariant, so we rotate.

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

��

��

��

��

��

��

����79

���

��

����63

���

��

����59

��

��

��

��

��

��

��

��

����91

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

��

��

��

��

��

��

����63

� A��

��

����79

��

��

����59

��

��

��

��

��

��

��

��

����91

The numbers left to insert are 49 and 98.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 27 / 36

Page 28: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

inserting 49

After inserting 49, we first change colors:

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

��

��

��

��

��

��

����63

� A��

��

����79

��

��

����59

���

��

����49

��

��

��

��

��

��

��

��

����91

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

����63

� A��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����59

���

��

����49

��

��

��

��

��

��

��

��

����91

Not all invariants of the red-black tree are satisfied.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 28 / 36

Page 29: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

rotating once ...

We see that the right tree is left heavy.

��

��

����84

� A

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

����63

� A��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����59

���

��

����49

��

��

��

��

��

��

��

��

����91

��

��

����63

� @

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

��

��

��

��

��

��

����59

� ��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����91

��

��

����49

��

��

����84

� A

After rotation, the tree is right heavy.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 29 / 36

Page 30: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

rotating again

The new root will become the root of the right tree.

��

��

����63

� @

��

��

��

��

��

��

��

��

����43

���

��

����38

��

��

��

��

��

��

��

��

����48

� @

��

��

��

��

��

��

��

��

����59

� ��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����91

��

��

����49

��

��

����84

� A

��

��

����84

� A

��

��

����48

� A��

��

��

��

��

��

��

��

����43

��

��

��

��

��

��

��

��

����63

� @

��

��

��

��

��

��

��

��

����59

���

��

����49

��

��

����38

��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����91

The left of 63 turned to the right of 48.

One number left to insert: 98.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 30 / 36

Page 31: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

inserting 98

��

��

����84

� A

��

��

����48

� A��

��

��

��

��

��

��

��

����43

��

��

��

��

��

��

��

��

����63

� @

��

��

��

��

��

��

��

��

����59

���

��

����49

��

��

����38

��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����91

��

��

����84

� A

��

��

����48

� A��

��

��

��

��

��

��

��

����43

��

��

��

��

��

��

��

��

����63

� @

��

��

��

��

��

��

��

��

����59

���

��

����49

��

��

����38

��

��

��

��

��

��

��

��

����79

��

��

��

��

��

��

��

��

����91

A��

��

����98

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 31 / 36

Page 32: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Red-Black Trees

1 Red-Black Treesbalancing binary search treesrelation with 2-3-4 trees

2 Insertion into a Red-Black Treealgorithm for insertionan elaborate example of an insertinserting a sequence of numbers

3 Recursive Insert Functionpseudo code

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 32 / 36

Page 33: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Recursive Insert Function

We extend the node type of a binary search tree:1 a node contains data (often an integer key);2 in addition, every node has a color: red or black;3 two pointers to nodes lead to left and right children.

Prototype of a recursive insert function:

bool insert ( Tree &root, Item_Type item );

// returns false if item belonged to root

// returns true if item was added to the root

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 33 / 36

Page 34: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

pseudo code

bool insert ( Tree &root, Item_Type item )if(root == NULL)

root = new black node;return true;

else if(item == root->data)return false;

else if (item < root->data)if(left == NULL)

left = new red node;return true;

else if((left == red) && (right == red))change left and right to blackand color the local root red;

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 34 / 36

Page 35: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

pseudo code continued

if(insert(left,item))if(left grandchild == red)

change left to blackand color the local root red;rotate the local root right;

else if(right grandchild == red)rotate the left child left;change left to blackand color the local root to red;rotate the local root right;

else // item > root->data: exercise

if(local root is root of the tree)color the root black.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 35 / 36

Page 36: Red-Black Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/red_black_trees.pdfRed-Black Trees 1 Red-Black Trees balancing binary search trees relation with 2-3-4 trees

Summary + Exercises

More on chapter 11 on balancing binary search trees.

Exercises:1 Take the largest example of a red-black tree on these slides and

draw the equivalent 2-3-4 tree.2 Write the pseudo code for the case when the item is inserted to

the right child.3 Instead of inserting 4 in the elaborate example, insert 9 and

illustrate all stages in the insertion.

Introduction to Data Structures (MCS 360) Red-Black Trees L-36 20 April 2020 36 / 36