csc 212 lecture 19: splay trees, (2,4) trees, and red-black trees

42
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red- Black Trees

Post on 21-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

CSC 212

Lecture 19: Splay Trees, (2,4) Trees, and Red-Black

Trees

Page 2: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

all the keys in the yellow region are 20

all the keys in the blue region are 20

Splay Trees are Binary Search Trees

BST Rules: entries stored only at

internal nodes nodes in the left subtree

have smaller key values nodes in right subtree

have larger key values nodes with equal key

value must all be in left or right subtree

Inorder traversal returns entries in order of the key values

(20,Z)

(37,P)(21,O)(14,J)

(7,T)

(35,R)(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

note that two keys of equal value may be well-separated

(§ 9.3)

Page 3: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Searching in a Splay Tree: Starts the Same as in a BST

Search proceeds down the tree to found item or an external node.Example: Search for key 11 Since key not

in tree, ends atexternal node

(20,Z)

(37,P)(21,O)(14,J)

(7,T)

(35,R)(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

Page 4: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Example Searching in a BST, continued

Search for key 8, ends at an internal node

(20,Z)

(37,P)(21,O)(14,J)

(7,T)

(35,R)(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

Page 5: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Searching in BST

If tree is balanced, search takes O(log n) time If tree is not balanced, search can

take O(n) time Unfortunately, the worst case is quite

common What is this worst case?

Page 6: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Splay Trees do Rotations after Every Operation (Even Search)

new operation: splay splaying moves a node to the root using rotations

right rotation makes the left child (x) of a node y

into y’s parent; y becomes the right child of x

y

x

T1 T2

T3

y

x

T1

T2T3

left rotation makes the right child (y) of a node x

into x’s parent; x becomes the left child of y

y

x

T1 T2

T3

y

x

T1

T2T3

(structure of tree above y is not modified)

(structure of tree above x is not modified)

a right rotation about y a left rotation about x

Page 7: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Splaying:

is x the root?

stop

is x a child of the root?

right-rotate about the root

left-rotate about the root

is x the left child of the

root?

is x a left-left grandchild?

is x a left-right grandchild?

is x a right-right grandchild?

is x a right-left grandchild?

right-rotate about g, right-rotate about p

left-rotate about g, left-rotate about p

left-rotate about p, right-rotate about g

right-rotate about p, left-rotate about g

start with node x

“x is a left-left grandchild” means x is a left child of its parent, which is itself a left child of its parent

p is x’s parent; g is p’s parent

no

yes

yes

yes

yes

yes

yes

no

no

yes zig-zig

zig-zag

zig-zag

zig-zig

zigzig

Page 8: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Visualizing the Splaying Cases

zig-zag

y

x

T2 T3

T4

z

T1

y

x

T2 T3 T4

z

T1

y

x

T1 T2

T3

z

T4

zig-zig

y

z

T4T3

T2

x

T1

zig

x

w

T1 T2

T3

y

T4

y

x

T2 T3 T4

w

T1

Page 9: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Splaying Examplelet x = (8,N)

x is right child of its parent, which is the left child of the grandparent

left-rotate around p, then right-rotate around g

(20,Z)

(37,P)(21,O)(14,J)

(7,T)

(35,R)(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

x

g

p

(10,A)

(20,Z)

(37,P)(21,O)

(35,R)

(36,L) (40,X)(7,T)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(14,J)(8,N)

(7,P)

(10,U)

x

g

p (10,A)

(20,Z)

(37,P)(21,O)

(35,R)

(36,L) (40,X)

(7,T)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(14,J)

(8,N)

(7,P)

(10,U)

x

g

p

1.(before rotating)

2.(after first rotation) 3.

(after second rotation)

x is not yet the root, so we splay again

Page 10: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Splaying Example, Continued

now x is the left child of the root right-rotate around root

(10,A)

(20,Z)

(37,P)(21,O)

(35,R)

(36,L) (40,X)

(7,T)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(14,J)

(8,N)

(7,P)

(10,U)

x

(10,A)

(20,Z)

(37,P)(21,O)

(35,R)

(36,L) (40,X)

(7,T)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(14,J)

(8,N)

(7,P)

(10,U)

x

1.(before applying rotation)

2.(after rotation)

x is the root, so stop

Page 11: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Example Result of Splayingtree might not be more balancede.g. splay (40,X)

before, the depth of the shallowest leaf is 3 and the deepest is 7

after, the depth of shallowest leaf is 1 and deepest is 8

(20,Z)

(37,P)(21,O)(14,J)

(7,T)

(35,R)(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

(20,Z)

(37,P)

(21,O)

(14,J)(7,T)

(35,R)

(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P) (36,L)(10,U)

(40,X)

(20,Z)

(37,P)

(21,O)

(14,J)(7,T)

(35,R)

(10,A)

(1,C)

(1,Q)

(5,G)(2,R)

(5,H)

(6,Y)(5,I)

(8,N)

(7,P)

(36,L)

(10,U)

(40,X)

before

after first splay after second splay

Page 12: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Splay Tree Definition

Splay tree is a binary search tree where node is splayed any time it is accessed Each search or update, splay only one node Splay internal node furthest from root Splaying costs O(h), where h is tree height

What is big-Oh for BST?

What is total cost of splaying?

Page 13: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Splay Trees & Ordered Dictionaries

Which nodes are splayed after each operation?

use parent of the internal node actually removed from the tree (e.g., parent of the node whose Entry was moved to node that was removed)remove(k)

splay the new node containing the inserted entryinsert(k,v)

if key found, use that node

if key not found, use last internal node in searchfind(k)

splay nodemethod

Page 14: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Performance of Splay Trees

Amortized cost of any splay operation is O(log n) Splay trees actually adapt to perform

searches on frequently-requested items much faster than O(log n)!

Page 15: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Your Turn

Insert 1, 3, 5, 7, 9, 11, 13, 15 into splay treeThen search for 1, 2, 16, 15 within tree

Page 16: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Multi-Way Search Tree (§ 9.4.1)

Multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d

1 entries ei = (ki, vi), where d is the number of children For a node with children v1 v2 … vd storing entries e1 e2 … ed1

keys in the subtree of v1 are less than k1

keys in the subtree of vi are between ki1 and ki (i = 2, …, d1) keys in the subtree of vd are greater than kd1

External nodes still used to mark when tree has ended11 24

2 6 8 15

30

27 32

Page 17: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Multi-Way Inorder Traversal

We can extend inorder traversals to multi-way search treesNamely, visit entry i within node v between recursive traversals of the subtrees of v rooted at children vi and vi1

Like with a BST, inorder traversal of a multi-way search tree visits keys in increasing order

11 24

2 6 8 15

30

27 32

1 3 5 7 9 11 13 19

15 17

2 4 6 14 18

8 12

10

16

Page 18: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Multi-Way SearchingSimilar to search in a binary search treeAt internal node with children v1 v2 … vd and entries e1 e2 … ed1

If k ki (i = 1, …, d1): the search terminates successfully If k k1: we continue the search in child v1 If ki1 k ki (i = 2, …, d1): we continue the search in child vi If k kd1: we continue the search in child vd

Key is not in tree if we reach an external nodeExample: search for 30

11 24

2 6 8 15

30

27 3227 32

30

Page 19: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

(2,4) Trees (§ 9.4.2)A (2,4) tree is multi-way search tree with following properties

Node-Size Property: every internal node has at most four children Depth Property: all the external nodes have the same depth

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

10 15 24

2 8 12 27 3218

Page 20: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Height of a (2,4) Tree(2,4) tree of size n has height O(log n) At worst, each node in (2,4) tree has only 2 children But (2,4) tree must be balanced So, this worst case scenario is similar to a balanced

binary tree!

1

2

2h1

0

items

0

1

h1

h

depth

Page 21: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

InsertionWe insert a new entry (k, o) at the parent v of the last leaf reached searching for k

Preserve the depth property, but… this may cause an overflow (i.e., node v becomes a 5-node)

Example: inserting key 30 causes an overflow

27 32 35

10 15 24

2 8 12 18

10 15 24

2 8 12 27 30 32 3518

v

v

27 32 35

Page 22: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Overflow and SplitHandle overflow at a 5-node v with split operation:

Let v1 … v5 be children of v and e1 … e4 be entries at v replace node v with nodes v' and v"

v' is a 3-node with keys e1 e2 and children v1 v2 v3

v" is a 2-node with key e4 and children v4 v5

Promote e3 to the parent, u, of node v (may create new root)

Overflow may propagate to the parent node (u)

15 24

12 27 30 32 3518v

u

v1 v2 v3 v4 v5

15 24 32

12 27 3018v'

u

v1 v2 v3 v4 v5

35v"

Page 23: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Analysis of InsertionAlgorithm insert(k, o)

1. Search for key k to find insert node v

2. Add the new entry (k, o) at node v

3. while overflow(v)if isRoot(v)

create a new empty root above vsplit(v)v parent(v)

What is big-Oh time for:Step 1?

Step 2?

Checking overflow(v)?

Calling isRoot(v)?

Calling split(v)?

Step 3?

insert(k,o)?

Page 24: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

DeletionConsider deletion of entry where item is at the node with leaf children

Otherwise, replace entry with inorder successor and delete the latter entry

Example: delete key 24 by replacing it with 27

27 32 35

10 15 24

2 8 12 18

32 35

10 15 27

2 8 12 18

Page 25: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Underflow and FusionDeleting entry from node v may cause underflow

Node v is 1-node (has one child, but no entries)

Handle underflow at v with parent u, in two cases:Case 1: v has adjacent siblings that is a 2-node

Fusion operation: merge v with adjacent sibling w and move entry from u into merged node v'

Fusion may propagate underflow to the parent node (u)

9 14

2 5 7 10

u

v

9

10 14

u

v'w2 5 7

Page 26: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Underflow and TransferCase 2: v has adjacent sibling w that is 3- or 4-node

Transfer operation:1. move child of w to v 2. move entry from u to v3. move entry from w to u

After transfer, no more underflows exist

4 9

6 82

u

vw

4 8

62 9

u

vw

Page 27: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Analysis of DeletionWhat is big-Oh time needed for deletion?

Height of tree =

Time to find entry to delete

Time needed for underflow

Maximum possible number of underflows

Time needed for transfer

Maximum possible number of transfers

Total time needed for deletion

Page 28: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Your Turn

Insert 1, 2, 3, 4, 5, 6, 7, 8 into (2,4) treeThen delete 1, 2, 3, 4, 5, 6, 7, 8 from tree

Page 29: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

From (2,4) to Red-Black Trees

Red-black tree is a representation of (2,4) tree using a binary tree whose nodes are colored red or blackCompared with (2,4) tree, red-black tree has

same performance, but simpler implementation!

2 6 73 54

4 6

2 7

5

3

3

5OR

Page 30: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Red-Black Trees (§ 9.5)Red-black tree is a binary search tree satisfying following properties:

Root Property: root is black External Property: every leaf is black Internal Property: children of red nodes are black Depth Property: all leaves have the same black depth

9

154

62 12

7

21

Page 31: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Height of a Red-Black TreeRed-black tree storing n entries has height O(log n)

Red-black tree is functionally equivalent to (2,4) tree which has height O(log n)

The search algorithm for red-black tree is same as for binary search treeSo, searching in a red-black tree takes O(log n) time

Page 32: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Insertioninsert(k, o):

execute BST insertion algorithm and color z, the new node, red (except when inserting a new root)

preserve root, external, and depth properties If parent, v, of z is black, internal property is also preserved Else (v is red) have double red (violation of internal property)

and need to reorganize the treeExample where an insertion (inserting 4) causes double red:

6

3 8

6

3 8

4z

v v

z

Page 33: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Remedying a Double RedDouble red with child z, parent v, and sibling of v, w

4

6

7z

vw2

4 6 7

.. 2 ..

Case 1: w is black Double red is incorrect

replacement of 4-node Restructuring: change 4-

node replacement

Case 2: w is red Double red corresponds

to an overflow Recoloring: split

equivalent

4

6

7z

v

2 4 6 7

2w

Page 34: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

RestructuringRestructuring remedies child-parent double red when parent has a black siblingEquivalent to restoring correct replacement of 4-nodeRestores internal property and preserves other properties

4

6

7z

vw2

4 6 7

.. 2 ..

4

6

7

z

v

w2

4 6 7

.. 2 ..

Page 35: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Restructuring (cont.)Four restructuring possibilites depending on whether double red nodes are left or right children

2

4

6

6

2

4

6

4

2

2

6

4

2 6

4

Page 36: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

RecoloringRecoloring remedies double red when parent node has red siblingParent v and its sibling w become black and the grandparent u becomes red (unless u is the root)Equivalent to performing split on 5-nodeDouble red violation may propagate to the grandparent u

4

6

7z

v

2 4 6 7

2w

4

6

7z

v

6 7

2w

… 4 …

2

Page 37: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Analysis of InsertionRed-black tree has O(log n) heightStep 1 takes O(log n) timeStep 2 takes O(1) timeStep 3 takes O(log n) time because we perform at most

O(log n) recolorings, each taking O(1) time, and

at most one restructuring taking O(1) time

Insertion takes O(log n) time!

Algorithm insert(k, o)

1. Search for k to find insert node z

2. Add new entry at node z and color z red

3. while doubleRed(z)if isBlack(sibling(parent(z)))

z restructure(z)return

else // isRed(sibling(parent(z)) recolor(z) z parent(z)

Page 38: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Deletionremove(k) begins with deletion algorithm for binary search treesLet v be node removed, w the external node removed, and r the sibling of w

If either v or r was red, color r black Else (v and r were both black), color r double black,

violating internal property and triggering reorganization of the tree

Example: deleting of 8 causes double black:6

3 8

4

v

r w

6

3

4

r

Page 39: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Remedying a Double BlackRemedying double black node w with sibling y has three cases:Case 1: y is black and has a red child

Perform restructuring and then we are doneCase 2: y is black and children are both black

Perform recoloring, which may propagate up double black violation

Case 3: y is red Perform adjustment, equivalent to choosing different

representation of 3-node, after which Case 1 or Case 2 will apply

Deletion also takes O(log n) time!

Page 40: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Red-Black Tree ReorganizationInsertion remedy double

red

Red-black tree action (2,4) tree action result

restructuringchange of 4-node representation

double red removed

recoloring splitdouble red removed or propagated upDeletion remedy double

black

Red-black tree action (2,4) tree action result

restructuring transferdouble black removed

recoloring fusiondouble black removed or propagated up

adjustmentchange of 3-node representation

restructuring or recoloring follows

Page 41: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Your Turn

Insert 1, 2, 3, 4, 5, 6, 7, 8 into (2,4) treeThen delete 1, 2, 3, 4, 5, 6, 7, 8 from tree

Page 42: CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

Daily Quiz

Write node class for Red-Black tree