skew heaps advanced)

Post on 14-Nov-2014

153 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

NUCES-FAST • Advanced Data Structures • Spring 2007

Skew Heaps

Rabea Aden

NUCES-FAST • Advanced Data Structures • Spring 2007

Leftist Tree

• Invented by Daniel Sleator and Robert Trajan

• A self-adjusting heap similar to Leftist Heaps

• No structural constraint

• Amortized time complexity of O(log n)

NUCES-FAST • Advanced Data Structures • Spring 2007

Heap-order Property

• Min Leftist Heap

• Max Leftist Heap

(X))key(Parent key(X) Heap X

(X))key(Parent key(X) Heap X

If X is root Parent (X) = root

NUCES-FAST • Advanced Data Structures • Spring 2007

Structure of a Node

No balance information is stored in the node

DataLeft

PointerRight

Pointer

NUCES-FAST • Advanced Data Structures • Spring 2007

Operations

• BuildHeap O(N)

• FindMin O(1)

• Merge O(log N)

• Insert O(log N)

• DeleteMin O(log N)

NUCES-FAST • Advanced Data Structures • Spring 2007

ld(X))w(rightChi d(X))w(leftChil 1 w(X)

Weight

• denoted by w(X)• Number of internal nodes in subtree with root X

NUCES-FAST • Advanced Data Structures • Spring 2007

Heavy and Light Nodes

• X — a nonroot node

• X is heavy if

• X is light if

2/))X(Parent(w)X(w

2/))X(Parent(w)X(w

NUCES-FAST • Advanced Data Structures • Spring 2007

Potential Function

NUCES-FAST • Advanced Data Structures • Spring 2007

General Idea

• All insertions and merges are performed on the rightmost path containing at most nodes

1)(nlog2

NUCES-FAST • Advanced Data Structures • Spring 2007

Merge

• O(log N) time

• It is a fundamental operation– Both Insert and DeleteMin are based on Merge– Insert:

• The key to be inserted is a one-node heap• Merge this one-node heap with the given min leftist heap

– DeleteMin• Delete root, left and right child of root are two min leftist heaps• Merge the two min leftist heaps

NUCES-FAST • Advanced Data Structures • Spring 2007

Merge Algorithm (Recursive)Merge(H1, H2)

If both heaps are Leftist

If one heap is NULL then return the other

If data(H1) > data(H1) then swap H1 and H2

If rightChild(H1) is NULL then rightChild(H1) = H2

Otherwise,

Recursively Merge rightChild(H1) and H2

If H1 ≠ last node on Rightmost Path then swap children of H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16 36

8

1

5

6

40

H1

Merge H1 & H2

Both H1 and H2 are Min Leftist Heaps

2

14

4

10

28 25

2218

7

H2

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16 36

8

1

5

6

40

H1

12 16 36

8

1

5

6

40

H1

2

14

4

10

28 25

2218

7

H2

2

14

4

10

28 25

2218

7

H2

Merge (H1, H2)

Compare root(H1) '1' & root(H2) '2' — H1 has smaller root

Both H1 and H2 are not NULL

Merge H1 & H2

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16 36

8

1

5

6

40

12 16 36

8

1

5

6

40

H1H1

2

14

4

10

28 25

2218

7

H2

Merge H1 & H2

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

2

14

4

10

28 25

2218

7

2

14

4

10

28 25

2218

7

Merge H1 & H2

Merge (H1, H2)

Compare root(H1) '8' & root(H2) '2' — H2 has smaller root swap(H1, H2)

Now, root(H1) = '2' & root(H2) = '8'

H1

12

1

5

6 16 36

8

40

Both H1 and H2 are not NULL

H2

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10

28 25

2218

712

1

5

6

2

14

4

10

29 26

2218

7

Merge H1 & H2

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

H2

16 36

8

4012

1

5

6

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10

28 25

2218

7

Merge H1 & H2

Merge (H1, H2)

Compare root(H1) '7' & root(H2) '8' — H1 has smaller root

H2

16 36

8

40

Both H1 and H2 are not NULL

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10

28 25

2218

712

1

5

6

2

14

4

10

28 25

2218

7

Merge H1 & H2

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

H2

16 36

8

40

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10

28

18

7

Merge H1 & H2

Merge (H1, H2)

Compare root(H1) '22' & root(H2) '8' — H2 has smaller root swap(H1, H2)

Now, root(H1) = '8' & root(H2) = '23'

1737941

Both H1 and H2 are not NULL

25

22

H2

16 36

8

40H112

1

5

6

2

14

4

10 18

7

16 36

8

40

28

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

16 36

8

40

28

12

1

5

6

2

14

4

10 18

7

16 36

8

40

28

Merge H1 & H2

1737941

H2

25

22

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

Merge H1 & H2

1737941

H2

25

22

Merge (H1, H2)

Compare root(H1) '36' & root(H2) '22' — H2 has smaller root swap(H1, H2)

Now, root(H1) = '22' & root(H2) = '36'

Both H1 and H2 are not NULL

12

1

5

6

2

14

4

10 18

7

16

8

28 36

40

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

16 22

8

25

28

36

40

12

1

5

6

2

14

4

10 18

7

16 22

8

25

28

Merge H1 & H2

Since, rightChild(H1) is NULL rightChild(H1) = H2

1737941

H2

NULL

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

16 22

8

25

28

36

40

Merge H1 & H2

NULL

H1 is NULL

H1

H2

Merge (H1, H2)

Since root(H1) is 'NULL' swap(H1, H2)

Now, root(H1) = '36' & root(H2) = 'NULL'

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

16 22

8

25

28

36

40

Merge H1 & H2

NULL

H2

H2 is NULL

H1

NULL

rightChild(H1) is NULL

12

1

5

6

2

14

4

10 18

7

16 22

8

25

28

36

40

Swap children of each nodeon the Rightmost Path

except the last node

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

16 22

8

36

28

25

40

12

1

5

6

2

14

4

10 18

7

16 22

8

25

28

36

40

H1 moves up to '22'

Merge H1 & H2swap( leftChild(H1), rightChild(H1) )

12

1

5

6

2

14

4

10 18

7

16 22

8

28

36

40

25

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

16 22

8

36

28

25

40

12

1

5

6

2

14

4

10 18

7

8

28

H1 moves up to '8'

Merge H1 & H2swap( leftChild(H1), rightChild(H1) )

H1

22

36 25

40

16

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

2

14

4

10 18

7

22 16

8

36

28

25

40

12

1

5

6

2

14

4

10

7 H1 moves up to '7'

Merge H1 & H2swap( leftChild(H1), rightChild(H1) )

H1

18

28 22 16

8

36 25

40

NUCES-FAST • Advanced Data Structures • Spring 2007

16

12

1

5

6

2

14

4

10 8

7

28

18

36

22

25

40

16

12

1

5

6

2

7

8

4

28

18

36

22

25

40

14 10

H1 moves up to '2'

Merge H1 & H2swap( leftChild(H1), rightChild(H1) )

H1

14

4

10

16

8

7

28

18

36

22

25

40

12

1

5

6

2

NUCES-FAST • Advanced Data Structures • Spring 2007

16

1

2 5

7

8

4

28

18

36

22

25

40

14 10

126

16

12

1

5

6

2

7

8

4

28

18

36

22

25

40

14 10

H1 moves up to '1'

Merge H1 & H2swap( leftChild(H1), rightChild(H1) )

1

H1

12

5

6

16

2

7

8

4

28

18

36

22

25

40

14 10

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

16

1

2 5

7

8

4

28

18

36

22

25

40

14 10

126

Merge H1 & H2H1

16

1

2 5

7

8

4

28

18

36

22

25

40

14 10

126 H1 is returned Root pointer now points to H1

Root

NUCES-FAST • Advanced Data Structures • Spring 2007

Merge Algorithm (Iterative)

Merge(H1, H2)

Pass 1 — Down the HeapSort nodes on rightmost paths of H1 and H2 in ascending order (leftChild of each node remains)A new tree H is created by merging the sorted nodes

Pass 2 — Up the Heap nodes X Є rightmost path of H from leaf to the root

If X ≠ last node on rightmost path then then swap children of X

NUCES-FAST • Advanced Data Structures • Spring 2007

Time Complexity of Merge

• O(log n)

• As only shortest path is traversed

NUCES-FAST • Advanced Data Structures • Spring 2007

Insert

• O(log N) time

Insert is based on Merge

Existing min leftist heap — H1 The key to be inserted is a one-node heap — H2 Merge this one-node heap H1 with the given min

leftist heap H2

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16 36

8

1

5

6

40

H1

Insert 2

H1 — existing HeapH2 — one-node heap containing node to be inserted

Both H1 and H2 are Min Leftist Heaps

2

H2

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16 36

8

1

5

6

400

H1

12 16 36

8

1

5

6

40

H1

Insert 2

30

H2

2

H2

Both H1 and H2 are not NULL

Merge (H1, H2)

Compare root(H1) '1' & root(H2) '2' — H1 has smaller root

NUCES-FAST • Advanced Data Structures • Spring 2007

Insert 2

12 16 36

8

1

5

6

40

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

H1

H1 2

H2

NUCES-FAST • Advanced Data Structures • Spring 2007

Insert 2

H1

12

1

5

6

Merge (H1, H2)

Compare root(H1) '8' & root(H2) '2' — H2 has smaller root swap(H1, H2)

Now, root(H1) = '2' & root(H2) = '8'

Both H1 and H2 are not NULL2

H2

16 36

8

40

NUCES-FAST • Advanced Data Structures • Spring 2007

12

1

5

6

Insert 2

2

H1

Since, rightChild(H1) == NULL rightChild(H1) = H2

16 36

8

40

H2

NULL12

2

1

5

6

16 36

8

40

NUCES-FAST • Advanced Data Structures • Spring 2007

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild '36'

12

2

1

5

6

16 36

8

40

Insert 2

H1

H2

NULL

H2 is NULL

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild '8'

H1

12

2

1

5

6

16 36

8

40

H1

NULL

rightChild(H1) is NULL

Swap children of each nodeon the Rightmost Path

except the last node

12

2

1

5

6

16 36

8

40

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

12

2

1

5

6

36 16

8

40

12

2

1

5

6

16 36

8

40

Insert 2

H1 H1 moves up to '8'

swap( leftChild(H1), rightChild(H1) )

12

2

1

5

6 8

16 36

40

NUCES-FAST • Advanced Data Structures • Spring 2007

12

2

1

5

6

36 16

8

40

12

2

1

5

6

36 16

8

40

Insert 2

H1 moves up to '2'

swap( leftChild(H1), rightChild(H1) )

36 16

8

40

12

2

1

5

6 NULL

H1

NUCES-FAST • Advanced Data Structures • Spring 2007

5

1

2

36 16

8

40

12612

2

1

5

6

36 16

8

40

Insert 2

H1 moves up to '1'

swap( leftChild(H1), rightChild(H1) )

H1

NULL

1

12

5

6

2

36 16

8

40

NUCES-FAST • Advanced Data Structures • Spring 2007

5

1

2

36 16

8

40

126

Insert 2H1

H1 is returned Root pointer now points to H1

Root

5

1

2

36 16

8

40

126

NUCES-FAST • Advanced Data Structures • Spring 2007

Time Complexity of Insert

• O(log n)

• Create a one-node heap — O(1)• Merge — O(log n)

– As only shortest path is traversed

NUCES-FAST • Advanced Data Structures • Spring 2007

DeleteMin

• O(log N) time

DeleteMin is based on Merge

Delete root — (minimum value is in root) Left and right children of root are two min

leftist heaps — H1 and H2 respectively Merge the two min leftist heaps H1 and H2

NUCES-FAST • Advanced Data Structures • Spring 2007

DeleteMin

130

170

370

91

22

61

70

410

Root

Delete Minimum Key '2'

12 16 36

8

1

5

6

40

NUCES-FAST • Advanced Data Structures • Spring 2007

1

DeleteMin

Save old value of rootH1 = root→leftChildH2 = root→rightChild

12

5

6 16 36

8

40

H1 H2

RootoldRoot

1

NUCES-FAST • Advanced Data Structures • Spring 2007

16 36

8

40

13

6

7 12

5

6

1

DeleteMin

16 36

8

41

H1 H2

Merge (H1, H2)

Compare root(H1) '5' & root(H2) '8' — H1 has smaller root

Both H1 and H2 are not NULL

oldRoot

NUCES-FAST • Advanced Data Structures • Spring 2007

12

5

6 12

5

6

DeleteMin

H1 H2

oldRoot

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

H1

16 36

8

40

1

NUCES-FAST • Advanced Data Structures • Spring 2007

DeleteMin

H2

oldRoot

H1

16 36

8

40

Both H1 and H2 are not NULL5

6 12

Merge (H1, H2)

Compare root(H1) '8' & root(H2) '2' — H2 has smaller root swap(H1, H2)

Now, root(H1) = '2' & root(H2) = '8'

1

NUCES-FAST • Advanced Data Structures • Spring 2007

16 36

8

5

6

40

16 36

8

5

6

40

DeleteMin

H2

oldRoot

H1 12

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild

1

NUCES-FAST • Advanced Data Structures • Spring 2007

DeleteMin

H2

oldRoot

12

H1

Merge (H1, H2)

Compare root(H1) '36' & root(H2) '12' — H2 has smaller root swap(H1, H2)

Now, root(H1) = '36' & root(H2) = '12'

170

8

5

6

36

40

Both H1 and H2 are not NULL

1

NUCES-FAST • Advanced Data Structures • Spring 2007

36

40

DeleteMin

H2

oldRoot

H1

1

16 12

8

5

6

36

40rightChild(H1) is NULL

NUCES-FAST • Advanced Data Structures • Spring 2007

NULL

DeleteMin

H2

oldRoot

H1

16

8

5

6

Since, rightChild(H1) == NULL rightChild(H1) = H2

12

1

16 12

8

5

6

36

40

NULL36

40

NUCES-FAST • Advanced Data Structures • Spring 2007

16 12

8

5

6

36

40

16 12

8

5

6

36

40

DeleteMin

H2

oldRoot

H1

1

NULL

H2 is NULL

NULL

Swap children of each nodeon the Rightmost Path

except the last node

Since, rightChild(H1) ≠ NULL H1 now points to its rightChild '36'

rightChild(H1) is NULL

NUCES-FAST • Advanced Data Structures • Spring 2007

NULL

16 12

8

5

6

36

40

DeleteMinoldRoot

1

16 12

8

5

6

36

40

H1

H1 moves up to '12'

swap( leftChild(H1), rightChild(H1) )

16 12

8

5

6

36

40

NULL

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16

8

5

6

36

40

NULL

16 12

8

5

6

36

40

DeleteMinoldRoot

1

H1 moves up to '8'

swap( leftChild(H1), rightChild(H1) )

H18

5

6

16 12

36

40

NUCES-FAST • Advanced Data Structures • Spring 2007

12 16

8

5

6

36

40

DeleteMinoldRoot

1

H1 moves up to '5'

swap( leftChild(H1), rightChild(H1) )

H1

12 16

6

5

8

36

40

5

12 16

8

36

40

6

NUCES-FAST • Advanced Data Structures • Spring 2007

DeleteMinoldRoot

1H1

12 16

6

5

8

36

40

H1 is returned Root pointer now points to H1

Root

12 16

6

5

8

36

40

Delete old Root

NUCES-FAST • Advanced Data Structures • Spring 2007

Time Complexity of DeleteMin

• O(log n)

• Delete root — O(1)• Initialize H1 with left child of root — O(1)• Initialize H2 with right child of root — O(1)• Merge — O(log n)

– As only shortest path is traversed

NUCES-FAST • Advanced Data Structures • Spring 2007

Leftist Heaps vs Skew Heaps

• More space• Explicit structural

constraints guarantees efficiency

• Less space• No structural

constraint guarantees efficiency

• Easier to implement

top related