· 2020. 7. 8. · created date: 7/8/2020 11:25:49 pm

45
CMSC 132: Object-Oriented Programming II Red & Black Tree 1 CMSC 132 Summer 2020

Upload: others

Post on 22-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

CMSC 132: Object-Oriented Programming II

Red & Black Tree

1CMSC 132 Summer 2020

Page 2:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

BST

Balanced BST Unbalanced BSTSearch: O(Log n) Search: O(n)

CMSC 132 Summer 2020 2

Page 3:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 1What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree?

CMSC 132 Summer 2020 3

A. O(n) for allB. O(Logn) for allC. O(Logn) for search and insert, and O(n) for deleteD. O(Logn) for search, and O(n) for insert and delete

Page 4:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 1What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree?

CMSC 132 Summer 2020 4

A. O(n) for allB. O(Logn) for allC. O(Logn) for search and insert, and O(n) for deleteD. O(Logn) for search, and O(n) for insert and delete

Page 5:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 2To delete a node X with 2 non-null children in a BST, we replace the node X with the minimum node Y from X’s right subtree. Which of the following is true about the node Y?

CMSC 132 Summer 2020 5

A. Y is always a leaf nodeB. Y is always either a leaf node or a node with empty left childC. Y may be an ancestor of the nodeD. Y is always either a leaf node or a node with empty right child

Page 6:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 2To delete a node X with 2 non-null children in a BST, we replace the node X with the minimum node Y from X’s right subtree. Which of the following is true about the node Y?

CMSC 132 Summer 2020 6

A. Y is always a leaf nodeB. Y is always either a leaf node or a node with empty left childC. Y may be an ancestor of the nodeD. Y is always either a leaf node or a node with empty right child

Page 7:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 3We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?

CMSC 132 Summer 2020 7

A. 0B. 1C. n!D. n2

Page 8:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 3We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?

CMSC 132 Summer 2020 8

A. 0B. 1C. n!D. n2

Page 9:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 4Which of the following traversal outputs the data in sorted order in a BST?

CMSC 132 Summer 2020 9

A. PreorderB. InorderC. PostorderD. Level order

Page 10:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 4Which of the following traversal outputs the data in sorted order in a BST?

CMSC 132 Summer 2020 10

A. PreorderB. InorderC. PostorderD. Level order

Page 11:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 5Numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. What is the in-order traversal sequence of the resultant tree?

CMSC 132 Summer 2020 11

A. 7 5 1 0 3 2 4 6 8 9B. 0 2 4 3 1 6 5 9 8 7C. 0 1 2 3 4 5 6 7 8 9D. 9 8 6 4 2 3 0 1 5 7

Page 12:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 5Numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. What is the in-order traversal sequence of the resultant tree?

CMSC 132 Summer 2020 12

A. 7 5 1 0 3 2 4 6 8 9B. 0 2 4 3 1 6 5 9 8 7C. 0 1 2 3 4 5 6 7 8 9D. 9 8 6 4 2 3 0 1 5 7

Page 13:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 6The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)?

CMSC 132 Summer 2020 13

A. 2B. 3C. 4D. 6

Page 14:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 6The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)?

CMSC 132 Summer 2020 14

A. 2B. 3C. 4D. 6

10 / \1 15\ / \3 12 16\5

Page 15:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 7The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?

CMSC 132 Summer 2020 15

A. 10, 20, 15, 23, 25, 35, 42, 39, 30B. 15, 10, 25, 23, 20, 42, 35, 39, 30C. 15, 20, 10, 23, 25, 42, 35, 39, 30D. 15, 10, 23, 25, 20, 35, 42, 39, 30

Page 16:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 7The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?

CMSC 132 Summer 2020 16

A. 10, 20, 15, 23, 25, 35, 42, 39, 30B. 15, 10, 25, 23, 20, 42, 35, 39, 30C. 15, 20, 10, 23, 25, 42, 35, 39, 30D. 15, 10, 23, 25, 20, 35, 42, 39, 30

Page 17:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 8Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2) Preorder 3) Postorder

CMSC 132 Summer 2020 17

A. Any one of the given three traversals is sufficientB. Either 2 or 3 is sufficientC. 2 and 3D. 1 and 3

Page 18:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Quiz 8Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2) Preorder 3) Postorder

CMSC 132 Summer 2020 18

A. Any one of the given three traversals is sufficientB. Either 2 or 3 is sufficientC. 2 and 3D. 1 and 3

Page 19:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Balanced Binary Search TreeRed & Black TreeAVL Tree2-3 TreeB-tree: Databases

CMSC 132 Summer 2020 19

Page 20:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Red & Black TreeA BST such that:• Tree edges have color: Red or Black• No node has two red edges connected to it.• Every path from root to null link has the same number of

black links. • Red links lean left. (LLRB)• New node edge is Red

CMSC 132 Summer 2020 20

Page 21:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Search: red-black BSTs Observation. Search is the same as for elementary BST (ignore color).

CMSC 132 Summer 2020 21

Page 22:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Red-black BST representation

CMSC 132 Summer 2020 22

Page 23:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Elementary OperationsLeft rotation. Orient a (temporarily) right-leaning red link to lean left.

rotate E left (before) rotate E left (after)

CMSC 132 Summer 2020 23

Page 24:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Elementary Operations cont.Left rotation. Orient a (temporarily) right-leaning red link to lean left.

CMSC 132 Summer 2020 24

Page 25:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Elementary Operations cont.

Right rotation: Orient a left-leaning red link to (temporarily) lean right.

rotate E left (before) rotate E left (after)

CMSC 132 Summer 2020 25

Page 26:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Elementary Operations cont.Right rotation: Orient a left-leaning red link to (temporarily) lean right.

CMSC 132 Summer 2020 26

Page 27:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Elementary Operations cont.

Color flip:

Color flip(before) Color flip (after)

CMSC 132 Summer 2020 27

Page 28:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Elementary Operations cont.Color flip.

CMSC 132 Summer 2020 28

Page 29:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

Insertion in a LLRB tree• Right child red, left child black: rotate left.• Left child, left-left grandchild red: rotate right.• Both children red: flip colors.

CMSC 132 Summer 2020 29

Page 30:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

InsertionNode put(Node h, Key key, Value val) {

if (h == null) return new Node(key, val, RED, 1);int cmp = key.compareTo(h.key); if (cmp < 0) h.left = put(h.left, key, val); else if (cmp > 0) h.right = put(h.right, key, val); else h.val = val;

// fix-up any right-leaning links if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h); return h;

}

CMSC 132 Summer 2020 30

Page 31:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

MInsert: M, D

D

CMSC 132 Summer 2020 31

Page 32:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

MInsert: M, X

X

Rotate Left

CMSC 132 Summer 2020 32

Page 33:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

Insert: M, X

M

X

Rotate Left

CMSC 132 Summer 2020 33

M

X

Page 34:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

Insert: M, D, X

D

M

X D

M

X

Flip Color

CMSC 132 Summer 2020 34

Page 35:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

Insert: M, D, B

D

M

B

B

D

M Flip Color

Rotate Right

CMSC 132 Summer 2020 35

Page 36:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

Insert: M, D, B

B

D

MFlip Color

B

D

M

CMSC 132 Summer 2020 36

Page 37:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

M, D, B Insert H

B

D

M

B

D

M

H

CMSC 132 Summer 2020 37

Page 38:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: Insertion

M, D, B,H Insert S

B

D

M

H

B

D

M

H S

Color flip

CMSC 132 Summer 2020 38

Page 39:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H Insert S

B

D

M

H S

Color flip

B

D

M

H S

Rotate Left

CMSC 132 Summer 2020 39

Page 40:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H Insert S

B

D

M

H S

Rotate Left

B

D

M

H

S

CMSC 132 Summer 2020 40

Page 41:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H,S Insert E,K

B

D

M

H

S

B

D

M

H

S

E K

CMSC 132 Summer 2020 41

Page 42:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H,S Insert E,K

B

D

M

H

S

E K

B

D

M

H

S

E K

Color flip

Rotate Left

CMSC 132 Summer 2020 42

Page 43:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H,S Insert E,K

B

D

M

H

S

E D

B

D

M

HS

E

K

Rotate left

CMSC 132 Summer 2020 43

Page 44:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H,S Insert E,K

B

D

M

HS

E

K

Rotate Right

B

D M

H

SE K

CMSC 132 Summer 2020 44

Page 45:  · 2020. 7. 8. · Created Date: 7/8/2020 11:25:49 PM

R&B Example: InsertionM, D, B,H,S,E,K

B

D M

H

SE K B

D M

H

SE K

Color flip

CMSC 132 Summer 2020 45