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

CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

CMSC 132: Object-Oriented Programming II

Red & Black Tree

1CMSC 132 Summer 2018

Page 2: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

BST

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

CMSC 132 Summer 2018 2

Page 3: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 3

Page 4: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 4

Page 5: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 5

Page 6: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

Red-black BST representation

CMSC 132 Summer 2018 6

Page 7: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

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

CMSC 132 Summer 2018 7

Page 8: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

Elementary Operations cont.

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

CMSC 132 Summer 2018 8

Page 9: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 9

Page 10: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 10

Page 11: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

Elementary Operations cont.

Color flip:

Color flip(before) Color flip (after)

CMSC 132 Summer 2018 11

Page 12: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

Elementary Operations cont.Color flip.

CMSC 132 Summer 2018 12

Page 13: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 13

Page 14: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 14

Page 15: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

MInsert: M, D

D

CMSC 132 Summer 2018 15

Page 16: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

MInsert: M, X

X

Rotate Left

CMSC 132 Summer 2018 16

Page 17: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

Insert: M, X

M

X

Rotate Left

CMSC 132 Summer 2018 17

M

X

Page 18: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

Insert: M, D, X

D

M

X D

M

X

Flip Color

CMSC 132 Summer 2018 18

Page 19: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

Insert: M, D, B

D

M

B

B

D

M Flip Color

Rotate Right

CMSC 132 Summer 2018 19

Page 20: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

Insert: M, D, B

B

D

MFlip Color

B

D

M

CMSC 132 Summer 2018 20

Page 21: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

M, D, B Insert H

B

D

M

B

D

M

H

CMSC 132 Summer 2018 21

Page 22: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

R&B Example: Insertion

M, D, B,H Insert S

B

D

M

H

B

D

M

H S

Color flip

CMSC 132 Summer 2018 22

Page 23: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 23

Page 24: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

B

D

M

H S

Rotate Left

B

D

M

H

S

CMSC 132 Summer 2018 24

Page 25: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 25

Page 26: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 26

Page 27: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 27

Page 28: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 28

Page 29: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 29

Page 30: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 30

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 31: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 31

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 32: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 32

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 33: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 33

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 34: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 34

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

Page 35: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 35

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

Page 36: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 36

A. PreorderB. InorderC. PostorderD. Level order

Page 37: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 37

A. PreorderB. InorderC. PostorderD. Level order

Page 38: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 38

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 39: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 39

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 40: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 40

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

Page 41: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 41

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

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

Page 42: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 42

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 43: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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 2018 43

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 44: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

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

CMSC 132 Summer 2018 44

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

Page 45: CMSC 132: Object-Oriented Programming II · Balanced Binary Search Tree Red & Black Tree AVL Tree 2-3 Tree B-tree CMSC 132 Summer 2018 3

Quiz 8

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

CMSC 132 Summer 2018 45

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