ttit33 algorithms and optimization – dalg lecture 6 jan maluszynski - ht 20066.1 ttit33-...

27
Jan Maluszynski - HT 2006 6.1 TTIT33 Algorithms and Optimization – DALG Lecture 6 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search Trees, Heaps

Post on 21-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.1

TTIT33 Algorithms and Optimization – DALG Lecture 6

TTIT33- Algorithms and optimization

Algorithms Lecture 5

Balanced Search Trees,Heaps

Page 2: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.2

TTIT33 Algorithms and Optimization – DALG Lecture 6

Content:

• Balanced Search Trees – AVL Trees [GT 10.2, LD 7.1]

– Multi-way search trees [GT 10.4, LD 7.2, CL 18.1-2]

– Information on B-trees [GT 14.3, LD 7.2, CL 18.1-2]

• Piority Queues – Heaps [GT 8.1,8.3, LD 9.1, CL 6.1-5]

Page 3: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.3

TTIT33 Algorithms and Optimization – DALG Lecture 6

AVL Trees

A BST

LeftHeight(v)-RightHeight(v)

Page 4: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.4

TTIT33 Algorithms and Optimization – DALG Lecture 6

Worst AVL Tree• A worst AVL tree is a maximally unbalanced AVL

tree……or a tree of a given height with a minimum number of internal nodes

(Example: insert 44, 17, 78, 32, 50, 88, 62)

Page 5: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.5

TTIT33 Algorithms and Optimization – DALG Lecture 6

AVL-Tree Insertion – Rebalance…

Page 6: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.6

TTIT33 Algorithms and Optimization – DALG Lecture 6

AVL-Tree Insertion… (Goodrich/Tamassia)How to insert an item with key 50?• Perform a LookUp(50)• While searching for 50,

keep track of last passednode with balance 0critical node

• If not found, insert at theleaf where search ended

• Recompute balance onthe way back

• Check critical node!If balance [-1..1], rebalance!

30b: 0

20b: 1

60b: 0

10b: 0

40b: 0

70b: 0

50b: 0

40b: -1

60b: 1

30b: -1

Page 7: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.7

TTIT33 Algorithms and Optimization – DALG Lecture 6

AVL-Tree Insertion & Re-balance…

Now try an item with key 15...• Perform a LookUp(15)• While searching for 15,

keep track of last passednode with balance 0critical node

• If not found, insert at theleaf where search ended

• Recompute balance onthe way back

• Check critical node!If balance [-1..1], rebalance!

30b: -1

20b: 1

60b: 1

10b: 0

40b: -1

70b: 0

50b: 0

10b: -1

20b: 1

30b: ?

15b: 0

20b: 2

Page 8: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.8

TTIT33 Algorithms and Optimization – DALG Lecture 6

Time to re-balance…

60b: 1

70b: 0

50b: 0

k 15b: 0

b

l m

10b: -1

a

20b: 2

c

n

30b: ?

40b: -1

• Label the critical nodeand its 2 descendants on thepath to ”15” as a, b, c,such that a < b < c, inan in-order traversal

• Re-structure the nodesa, b and c so thatb has a and c aschildren!

Page 9: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.9

TTIT33 Algorithms and Optimization – DALG Lecture 6

Time to re-balance…

60b: 1

70b: 0

50b: 0

15b: 0

b

k

10b: -1

a

20b: 2

c

n15b: 0

b

k

10b: -1

a20

b: 2

c

n

15b: 0

b

k

10b: -1

a20

b: 2

c

n

15b: 0

b

k

10b: 0

a20

b: 0

c

n

30b: ?

40b: -1

l m

• Label the critical nodeand its 2 descendants on thepath to ”15” as a, b, c,such that a < b < c, inan in-order traversal

• Re-structure the nodesa, b and c so thatb has a and c aschildren!

• Update balance!

30b: -1

Page 10: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.10

TTIT33 Algorithms and Optimization – DALG Lecture 6

Removal of a node...

Same thing, but in reverse!

1. Perform a LookUp and Remove as inan ordinary binary tree

2. Update the balance on the way backto the root

3. If too unbalanced: Re-structure! ...but:• Label the critical node, the child on the deepest side,

and its descendants on the deepest side as a, b, c,such that a < b < c, in an in-order traversal

• Re-structure as previous• Go to #2 and continue update and check towards the

root (we may have to re-balance more than once!)

Page 11: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.11

TTIT33 Algorithms and Optimization – DALG Lecture 6

Tri-node restructuring = rotations....

Other authors use left and right rotations:

Single left rotation:• left part of the sub-

tree (a and j) is lowered• We have ”rotated (up)

b over a”...

c

b

a

j

k

l m

c

b

a

j k l m

Page 12: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.12

TTIT33 Algorithms and Optimization – DALG Lecture 6

Double rotations...

Two rotations are needed when the nodes to

re-balance are placed in a zig-zag pattern...

1. Rotate up b over a

2. Rotate up b over c

c

b

a

j

k l

m

cb

a

j k l m

c

b

a

j k l m

Note: Labeling of a, b and c same as before!

Page 13: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.13

TTIT33 Algorithms and Optimization – DALG Lecture 6

New approach: relax some condition...

• AVL-Tree: binary tree, accepts a small unbalance• Recall:

Full binary tree: nonempty; degree is either 0 or 2 for each nodePerfect binary tree: full, all leaves have the same depth

• Can we build and maintain a perfect tree (if we skip ”binary”) ??

we would always know the worstsearch time exactly!

Page 14: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.14

TTIT33 Algorithms and Optimization – DALG Lecture 6

(2,3) trees [or (2,4) trees, or (a,b)-trees…]

Previously:• A single ”pivot element”• If larger we search to the right• If smaller we search to the left

Now:• Multiple pivot elements• No. of children =

no. of pivot elements + 1

8

5

2

12

5 10

2 8

Page 15: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.15

TTIT33 Algorithms and Optimization – DALG Lecture 6

(2,3) trees, (2,4) trees, or (a,b) trees...

• Each node is either a leaf,or has c children where a c b

• LookUp works aproximately as before

• Insert must check that a node does not overflow (then we split the node)

• Delete must check that a node does not become empty (then we transfer or merge nodes)

Page 16: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.16

TTIT33 Algorithms and Optimization – DALG Lecture 6

Insert in (a,b)-tree where a=2 and b=3

• As long as there is room inthe child we find, add elementto that child...

• If full, split and push the selected pivot element upwards......may happen repeatedly

10

5 10

Insert(10)

5 10 15

Insert(15)

5 15

5

Insert(18)10

5 15 18

Insert(17)10

5 15 17 18

10 17

5 15 18

Page 17: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.17

TTIT33 Algorithms and Optimization – DALG Lecture 6

Delete in a (2,3)-tree

Three cases:

1. No constraints are violated by removal2. A leaf is removed (becomes empty)

transfer some other key to that leaf,…ok if we have a sibling with 2+ elements

10 17

5 15 18

30

25 35 40

20

10 17

5 15 18

35

30 40

20Delete(25)

30

? 35 40

?

30 35 40

Transfer of30 and 35

Page 18: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.18

TTIT33 Algorithms and Optimization – DALG Lecture 6

Delete in a (2,3)-tree

2. A leaf is removed (becomes empty) transfer some other key to that leaf, or merge (fuse) it with a neighbor

10 17

5 15 18

35

30 40

20 Delete(18)

10 17

5 15 ?

10

5 15 17

No pivot ??Too many children!

10

5 15 17

35

30 40

20

Page 19: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.19

TTIT33 Algorithms and Optimization – DALG Lecture 6

Delete in a (2,3)-tree

3. An internal node becomes emptyRoot: replace with in-order pred. or succ. repair inconsistencies with suitable

merge and transfer operations…

10

5 17

35

30 40

20

Delete(20)

10

5 ?

35

30 40

?

Replace......merge leafs

17 ?

5 10

35

30 40

17

Internal underflow......merge nodes

5 10 30 40

17 35

Page 20: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.20

TTIT33 Algorithms and Optimization – DALG Lecture 6

Properties of a (2,3) tree

• Always a perfect tree• A minimal tree of height h will have

nodes (it’s a full binary tree with full set of nodes at all levels)

• A maximal (2,3)-tree will have a branching factor of 3, thus

…2 keys in every node

• Thus the height

12 1 hn

2/)13(3 1

0

hh

i

in

13 1 hk

kh 3logNo of keys

in a tree

Page 21: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.21

TTIT33 Algorithms and Optimization – DALG Lecture 6

B-Tree

• Used to keep an index over external data (e.g. content of a disc)

• It’s only an (a,b)-tree where a = b/2

• We may now choose b so that b-1 references to children (other disc blocks) fit into a single disc block

• By defining a = b/2 we will always fill up a disc block when two blocks are merged!

Page 22: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.22

TTIT33 Algorithms and Optimization – DALG Lecture 6

ADT Priority Queue: – Linearly ordered set K of keys– We store pairs <k,I> (as in Dictionary), multiple pairs

with same key are allowed. The key denotes priority– Items retrieved by priority: i.e. by the minimal key.

Page 23: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.23

TTIT33 Algorithms and Optimization – DALG Lecture 6

Implementing Priority Queues

”last” leaf

This is a complete

binary tree!

This is also called a HEAP

Page 24: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.24

TTIT33 Algorithms and Optimization – DALG Lecture 6

Updates on a Heap Structure

• DeleteMin = deletion of the root– Replace root by last leaf– Restore partial order by swaping nodes

downwards”down-heap bubbling”

• Insert– Insert new node after last leaf– Restore partial ordering by ”up-heap bubbling”

Page 25: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.25

TTIT33 Algorithms and Optimization – DALG Lecture 6

HEAP Properties:

• size(), minElemenent(): O(1)• minElement(), minKey(): O(1)• insertItem(), removeMin(): O(log n)

>> HeapSort O(n log n) sorting algorithm

Recall vector representation of BST!

A complete binary tree...• Compact vector representation• Bubble-up and bubble-down have fast

implementations

Page 26: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.26

TTIT33 Algorithms and Optimization – DALG Lecture 6

HEAP example – bubble-up after insert(4)

We store pairs of <key, data> in the tree. Recall tree representation in a table:• getLeftChild(i) = 2*i+1• getRightChild(i) = 2*i+2...where i is the table index, not the key!

In [G/T] the root starts at index 1 and representation is:-getLeft(i) = 2*I-getRight(i) = 2*i+1

...in [L/D] the root starts at 0...

Page 27: TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search

Jan Maluszynski - HT 2006 6.27

TTIT33 Algorithms and Optimization – DALG Lecture 6

Heap Sort

Kinds of heaps :• minKey in the root => (Min)Heap• maxKey in the root => MaxHeap

Heap Sort: input D[0..n]• Heapify D => Max Heap

worst case O(n log n)• For i from 0 to n-1

DeleteMax => put in D[n-i] (worst case O(log n) for restoring heap D[0..i]