Balanced Search Trees 2-3-4 trees red-black trees References: - - PowerPoint PPT Presentation

balanced search trees
SMART_READER_LITE
LIVE PREVIEW

Balanced Search Trees 2-3-4 trees red-black trees References: - - PowerPoint PPT Presentation

Balanced Search Trees 2-3-4 trees red-black trees References: Algorithms in Java (handout) Balanced search trees Dynamic sets Search Insert Delete Maximum Minimum Successor(x) (find minimum element x)


slide-1
SLIDE 1

Balanced Search Trees

2-3-4 trees red-black trees

References: Algorithms in Java (handout)

slide-2
SLIDE 2

Balanced search trees

Dynamic sets

  • Search
  • Insert
  • Delete
  • Maximum
  • Minimum
  • Successor(x) (find minimum element ≥ x)
  • Predecessor(x) (find maximum element ≤ x)

This lecture: 2-3-4 trees, red-black trees Next time: Tiered vektor (not a binary search tree, but maintains a dynamic set). In two weeks time: Splay trees

2

slide-3
SLIDE 3

Dynamic set implementations

Worst case running times In worst case h=n. In best case h= log n (fully balanced binary tree) Today: How to keep the trees balanced.

3

Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)

  • rdered array

O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h)

slide-4
SLIDE 4

2-3-4 trees

slide-5
SLIDE 5

2-3-4 trees

2-3-4 trees. Allow nodes to have multiple keys. Perfect balance. Every path from root to leaf has same length. Allow 1, 2, or 3 keys per node

  • 2-node: one key, 2 children
  • 3-node: 2 keys, 3 children
  • 4-node: 3 keys, 4 children

5

S V K R M O X A C L N Q Y Z

smaller than K larger than R between K and R

B D F G J E

slide-6
SLIDE 6

Search.

  • Compare search key against keys in node.
  • Find interval containing search key
  • Follow associated link (recursively)

Searching in a 2-3-4 tree

6

S V F G J K R C E M O X A D L N Q Y Z

slide-7
SLIDE 7

Search.

  • Compare search key against keys in node.
  • Find interval containing search key
  • Follow associated link (recursively)
  • Ex. Search for L

Searching in a 2-3-4 tree

7

between K and R smaller than M

S V F G J K R C E M O X A D L N Q Y Z

found L

slide-8
SLIDE 8

Where is the predecessor of L? And the successor of L?

Predecessor and successor in a 2-3-4 tree

8

between K and R smaller than M

S V F G J K R C E M O X A D L N Q Y Z

found L

slide-9
SLIDE 9

Insertion in a 2-3-4 tree

9

S V F G J K R C E M O X A D L N Q Y Z

slide-10
SLIDE 10

Insertion in a 2-3-4 tree

Insert.

  • Search to bottom for key.
  • Ex. Insert B

10

S V F G J K R C E M O X A D L N Q Y Z

smaller than K B not found smaller than C

slide-11
SLIDE 11

Insertion in a 2-3-4 tree

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • Ex. Insert B

11

S V F G J K R C E M O X A B D L N Q Y Z

smaller than K smaller than C B fits here

slide-12
SLIDE 12

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • Ex. Insert X

Insertion in a 2-3-4 tree

12

S T F G J K R C E M O U A D L N Q Y Z

X not found larger than R larger than U

slide-13
SLIDE 13

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • 3-node at bottom: convert to 4-node
  • Ex. Insert X

Insertion in a 2-3-4 tree

13

S T F G J K R C E M O U A D L N Q X Y Z

X fits here larger than R larger than W

slide-14
SLIDE 14

larger than E H not found

Insertion in a 2-3-4 tree

14

S V F G J K R C E M O X A D L N Q Y Z

smaller than K

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • 3-node at bottom: convert to 4-node
  • Ex. Insert H
slide-15
SLIDE 15

larger than E H does not fit here!

Insertion in a 2-3-4 tree

15

S V F G J K R C E M O X A D L N Q Y Z

smaller than K

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • 3-node at bottom: convert to 4-node
  • 4-node at bottom: ??
  • Ex. Insert H
slide-16
SLIDE 16

Splitting a 4-node in a 2-3-4 tree

Idea: split the 4-node to make room Problem: Doesn’t work if parent is a 4-node Solution 1: Split the parent (and continue splitting while necessary). Solution 2: Split 4-nodes on the way down.

16

H does not fit here

F G J C E D A B D

H does fit here!

A B C E G F J D A B C E G F H J

slide-17
SLIDE 17

Idea: split 4-nodes on the way down the tree.

  • Ensures last node is not a 4-node.
  • Transformations to split 4-nodes:
  • Invariant. Current node is not a 4-node.
  • Consequence. Insertion at bottom is easy

since it's not a 4-node.

Splitting 4-nodes in a 2-3-4 tree

17

F G J B G B F J

x y z v x y z v

F G J

x y z v

F

x y

J

z v

B D B D G

x y z v

B D G D B G

x y z v

root

slide-18
SLIDE 18

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • 3-node at bottom: convert to 4-node
  • 4-node at bottom: ??
  • Ex. Insert H

Insertion in a 2-3-4 tree

18

S V F G J K R C E M O X A D L N Q Y Z

not a 4-node not a 4-node 4-node

slide-19
SLIDE 19

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • 3-node at bottom: convert to 4-node
  • 4-node at bottom: ??
  • Ex. Insert H

Insertion in a 2-3-4 tree

19

S V K R C E G M O X A D L N Q Y Z F J

slide-20
SLIDE 20

Insert.

  • Search to bottom for key.
  • 2-node at bottom: convert to 3-node
  • 3-node at bottom: convert to 4-node
  • 4-node at bottom: ??
  • Ex. Insert H

Insertion in a 2-3-4 tree

20

S V K R C E G M O X A D L N Q Y Z F H J

slide-21
SLIDE 21

Local transformations that work anywhere in the tree.

  • Ex. Splitting a 4-node attached to a 2-node

Splitting 4-nodes in a 2-3-4 tree

A-C E-J L-P R-V X-Z A-C E-J L-P R-V X-Z

K Q W D Q D K W

could be huge unchanged

21

slide-22
SLIDE 22

Local transformations that work anywhere in the tree

  • Ex. Splitting a 4-node attached to a 3-node

Splitting 4-nodes in a 2-3-4 tree

A-C I-J L-P R-V X-Z I-J L-P R-V X-Z

K Q W K W

could be huge unchanged E-G

D H

A-C E-G

D H Q

22

slide-23
SLIDE 23

Splitting 4-nodes in a 2-3-4 tree

Local transformations that work anywhere in the tree. Splitting a 4-node attached to a 4-node never happens when we split nodes on the way down the tree.

  • Invariant. Current node is not a 4-node.

23

slide-24
SLIDE 24

Insertion 2-3-4 trees

24

S A B C E R H I N A B C E R H I N S U

Insert G Insert U Split

A B C E I R S U H N A B C E I R S U N G H A B C S U N G H E R I A B C S T U N G H E R I

Insert G Split Insert T

Insert U Insert G Insert T

slide-25
SLIDE 25

Deletions in 2-3-4 trees

Delete minimum:

  • minimum always in leftmost leaf
  • If 3- or 4-node: delete key
  • Ex. Delete minimum

25

S V F G J K R C E M O X A B D L N Q Y Z

A is minimum

slide-26
SLIDE 26

Deletions in 2-3-4 trees

Delete minimum:

  • minimum always in leftmost leaf
  • If 3- or 4-node: delete key
  • Ex. Delete minimum

26

S V F G J K R C E M O X B D L N Q Y Z

Delete A

slide-27
SLIDE 27

Deletions in 2-3-4 trees

Delete minimum:

  • minimum always in leftmost leaf
  • If 3- or 4-node: delete key
  • 2-node??
  • Ex. Delete minimum

27

S V F G J K R C E M O X B D L N Q Y Z

Delete B?

slide-28
SLIDE 28

Idea: On the way down maintain the invariant that current node is not a 2-node.

  • Child of root and root is a 2-node:
  • on the way down:

Deletions in 2-3-4 trees

28

z w y x z w y x

A B C A C B

r s w z y x r s w z y x

  • r

A B C D E A B C D E

r s w z y x r s w z y x

A C D E B F G C F G A B D E

z w y x z w y x

  • r

A B D E C D E A B C

slide-29
SLIDE 29

Deletions in 2-3-4 trees

Delete minimum:

  • minimum always in leftmost leaf
  • If 3- or 4-node: delete key
  • 2-node: split/merge on way down.
  • Ex. Delete minimum

29

S V F G J K R C E M O X B D L N Q Y Z

not a 2-node 2-node

slide-30
SLIDE 30

Deletions in 2-3-4 trees

Delete minimum:

  • minimum always in leftmost leaf
  • If 3- or 4-node: delete key
  • 2-node: split/merge on way down.
  • Ex. Delete minimum

30

S V F G J K R E M O X B C D L N Q Y Z

slide-31
SLIDE 31

Deletions in 2-3-4 trees

Delete minimum:

  • minimum always in leftmost leaf
  • If 3- or 4-node: delete key
  • 2-node: split/merge on way down.
  • Ex. Delete minimum

31

S V F G J K R E M O X C D L N Q Y Z

slide-32
SLIDE 32

Deletions in 2-3-4 trees

Delete:

32

S V F G J K R C E M O X B D L N Q Y Z

slide-33
SLIDE 33

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node

33

S V F G J K R C E M O X B D L N Q Y Z

slide-34
SLIDE 34

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key

34

S V F G J K R C E M O X B D L N Q Y Z

slide-35
SLIDE 35

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

35

S V F G J K R C E M O X B D L N Q Y Z

slide-36
SLIDE 36

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K

36

S V F G J K R C E M O X B D L N Q Y Z

slide-37
SLIDE 37

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor

37

S V F G J K R C E M O X B D L N Q Y Z

slide-38
SLIDE 38

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor

38

S V F G J K R C E M O X B D L N Q Y Z

slide-39
SLIDE 39

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor

39

S V F G J K R C E M O X B D L N Q Y Z

not a 2-node

slide-40
SLIDE 40

Deletions in 2-3-4 trees

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor

40

S V F G J K R C E M O X B D L N Q Y Z

not a 2-node 2-node

slide-41
SLIDE 41

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor

Deletions in 2-3-4 trees

41

S V F G J K R C E O X B D Q Y Z L M N

slide-42
SLIDE 42

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor
  • Delete L from leaf

Deletions in 2-3-4 trees

42

S V F G J K R C E O X B D Q Y Z L M N

slide-43
SLIDE 43

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor
  • Delete L from leaf

Deletions in 2-3-4 trees

43

S V F G J K R C E O X B D Q Y Z M N

slide-44
SLIDE 44

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor
  • Delete L from leaf
  • Replace K with L

Deletions in 2-3-4 trees

44

S V F G J K R C E O X B D Q Y Z M N

slide-45
SLIDE 45

Deletions in 2-3-4 trees

45

S V F G J L R C E O X B D Q Y Z M N

Delete:

  • During search maintain invariant that current node is not a 2-node
  • If key is in a leaf: delete key
  • Key not in leaf: replace with successor (always leaf in subtree) and delete

successor from leaf.

  • Ex. Delete K
  • Find successor
  • Delete L from leaf
  • Replace K with L
slide-46
SLIDE 46

2-3-4 Tree: Balance

  • Property. All paths from root to leaf have same length.

Tree height. Worst case: lg N [all 2-nodes] Best case: log4 N = 1/2 lg N [all 4-nodes] Between 10 and 20 for a million nodes. Between 15 and 30 for a billion nodes.

46

slide-47
SLIDE 47

Dynamic set implementations

Worst case running times

47

Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)

  • rdered array

O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n)

slide-48
SLIDE 48

Red-black trees

slide-49
SLIDE 49

Represent 2-3-4 tree as a binary search tree

  • Use colors on nodes to represent 3- and 4-nodes.

Red-black tree (Guibas-Sedgewick, 1979)

G K O K G O H L

  • r

H L H L

B B

49

≈ ≈ ≈

slide-50
SLIDE 50

Represent 2-3-4 tree as a binary search tree

  • Use colors on nodes to represent 3- and 4-nodes.
  • Connection between 2-3-4 trees and red-black trees:

Red-black tree (Guibas-Sedgewick, 1979)

A A C H I N E R S

R H N C A

E I A S

50

  • r

slide-51
SLIDE 51

Represent 2-3-4 tree as a binary search tree

  • Use colors on nodes to represent 3- and 4-nodes.
  • Connection between 2-3-4 trees and red-black trees:

Red-black tree (Guibas-Sedgewick, 1979)

A A C H I N E R S

R H N C A

E I A S

51

  • r

slide-52
SLIDE 52

Red-black tree

Properties of red-black trees:

  • The root is always black
  • All root-to-leaf paths have the same number of black nodes.
  • Red nodes do not have red children

52

slide-53
SLIDE 53

Red-black tree

Connection between 2-3-4 trees and red-black trees:

A A C H I N E R S

53

slide-54
SLIDE 54

Red-black tree

Connection between 2-3-4 trees and red-black trees:

A A C H I N E R S

R H N C A

E I A S

54

slide-55
SLIDE 55

Red-black tree

Connection between 2-3-4 trees and red-black trees:

A A C H I N E R S

R H N C A

E I A S

E

R

H N

I

C A

A S

  • r

55

slide-56
SLIDE 56

Insertion in red-black trees

Insertion: Insert a new red leaf.

56

F C H F C F C F

Insert C Insert H

slide-57
SLIDE 57

Red-black tree: Parent is red

What if the parent is also red? Easy case:

I D M B B D I M

C C

slide-58
SLIDE 58

Red-black tree: Parent is red

What if both the parent and the grandparent are red?

I D M B B D I M

C

??

slide-59
SLIDE 59

Red-black tree: Parent is red

What if both the parent and the grandparent are red?

I D M B B D I M

C C recurse

slide-60
SLIDE 60

Red-black tree: Parent is red

What if the parent is also red?

I D M B B D I M

C

??

slide-61
SLIDE 61

Rotations in red-black trees

Two types of rotations

61

A B C

a b c d

B A C

b c d a

A B C

a b c d

A B C

a b c d

slide-62
SLIDE 62

Rotations in red-black trees

Two types of rotations:

62

A B C a b c d B A C b c d a A B C a b c d A B C a b c d B A C b c d a C B A a b c d C A B a b c d C B A a b c d

slide-63
SLIDE 63

Insert x: Search to bottom after key (x) Insert red leaf Balance: 3 cases (+ symmetric)

Insertion in red-black tree

63

a a b Keep balancing with z b d z c d b d b c z a c b d z d a b c z z a b a z c c d a b c z d d

slide-64
SLIDE 64

64

Example

U C

E

R H N

B I S

A C

E

R H N

B I S

A

Insert U Insert V

S C

E

R H N

B I U

A V V U C

E

R H N

B I S

A

Rotate U

slide-65
SLIDE 65

65

Example

S C

E

R H N

B I U

A V

Insert G

S C

E

R H N

B I

U A V G S C

E

R H N

B

I

U

A V G

C E R H N B I A S U V G

I C

B

A R N S

U

V H G

E

Rotate I Rotate I

slide-66
SLIDE 66

Running times in red-black trees

  • Time for insertion:
  • Search to bottom after key:
  • Insert red leaf:
  • Perform recoloring and rotations on way up:
  • Can recolor many times (but at most h)
  • At most 2 rotations.
  • Total O(h).
  • Time for search
  • Same as BST: O(h)
  • Height: O(log n)

66

O(h) O(1) O(h)

slide-67
SLIDE 67

Dynamic set implementations

Worst case running times

67

Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)

  • rdered array

O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) red-black tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n)

slide-68
SLIDE 68

Balanced trees: implementations

Redblack trees: Java: java.util.TreeMap, java.util.TreeSet. C++ STL: map, multimap, multiset. Linux kernel: linux/rbtree.h.

68