BI NARY HEAPS Binary Trees in Contiguous Storage - - PDF document

bi nary heaps binary trees in contiguous storage
SMART_READER_LITE
LIVE PREVIEW

BI NARY HEAPS Binary Trees in Contiguous Storage - - PDF document

BI NARY HEAPS Binary Trees in Contiguous Storage 1 array 2 3 4 5 6 7 9 10 8 11 15 12 13 14 23 16 18 21 22 25 26 27 30 17 19 20 24


slide-1
SLIDE 1

BI NARY HEAPS

slide-2
SLIDE 2
  • S. Prasitjutrakul 1994

Binary Trees in Contiguous Storage

1 2 3 4 5 6 7 8 11 9 10 12 14 13 31 15 18 19 20 21 22 23 24 25 26 27 28 29 30 16 17

1 2 3 4 5 6 7 26 27 28 29 30 31

ลูกทางซายของจุดที่ตํ าแหนง k จะอยูที่ตํ าแหนงที่ 2k " ขวา " " " " 2k+1

ตัวเลขแทนตํ าแหนง ของขอมูลใน array

slide-3
SLIDE 3
  • S. Prasitjutrakul 1994

Binary Trees in Contiguous Storage

1 2 3 4 5 6 7 10 8 9 11 13 12 30 14 17 18 19 20 21 22 23 24 25 26 27 28 29 15 16

0 1 2 3 4 5 6 25 26 27 28 29 30

ลูกทางซายของจุดที่ตํ าแหนง k จะอยูที่ตํ าแหนงที่ 2k+1 " ขวา " " " " 2k+2

ตัวเลขแทนตํ าแหนง ของขอมูลใน array

slide-4
SLIDE 4
  • S. Prasitjutrakul 1994

Binary Trees in Contiguous Storage

h b d e f g c a y r a 0 1 2 3 a y - r y r a 0 1 2 3 4 5 6 a - y - - - r 0 1 2 3 4 5 6 7 a b c d e f g h

slide-5
SLIDE 5
  • S. Prasitjutrakul 1994

Heaps

Heap คือตนไมแบบทวิภาคที่มีคียของขอมูลกํ ากับจุด โดยที่

!

ทุกๆใบในตนไมจะอยูในระดับเดียวกัน หรือระดับที่ติดกัน

!

จุดในทุกๆระดับจะเต็ม ยกเวนระดับลางสุด

!

ใบในระดับลางสุด จะอยูชิดทางซาย

!

คียของขอมูลที่จุดระดับพอ/แม จะมีคามากกวาคียของลูกหลาน

!

ตนไมยอยทั้งสองของจุดใดๆ ก็คือ heap เชนกัน

Y R P D B F K A C E

0 1 2 3 4 5 6 7 8 9 Y R P D F B K A C E

slide-6
SLIDE 6
  • S. Prasitjutrakul 1994

Heapify

B R P F E D K A C

0 1 2 3 4 5 6 7 8 B R P D F E K A C

R P D E F K A B

R > P

0 1 2 3 4 5 6 7 8 B R P D F E K A C

R P D E F K A B

D < F

0 1 2 3 4 5 6 7 8 R B P D F E K A C

R P F E D K A B

0 1 2 3 4 5 6 7 8 R F P D B E K A C

Heap Heap

C C C

Heap

O n (log )

slide-7
SLIDE 7
  • S. Prasitjutrakul 1994

Heapify

void Heapify( ListType *pHeap, int i ) { int largest; left = LeftHeap( i ); right = RightHeap( i ); largest = i; if ( left <= pHeap->count-1 ) && GT( pHeap->entry[left].key, pHeap->entry[largest].key ) ) largest = left; if ( right <= pHeap->count-1 ) && GT( pHeap->entry[right].key, pHeap->entry[largest].key ) ) largest = right; if ( largest != i ) { Swap( pHeap, i, largest ); Heapify( pHeap, largest ); } }

slide-8
SLIDE 8
  • S. Prasitjutrakul 1994

Heaps : Deletion

Y R P F B D K A C

0 1 2 3 4 5 6 7 8 Y R P D F B K A C

deleted R P D B F K A C

R > P

0 1 2 3 4 5 6 7 8 C R P D F B K A -

R P D B F K A C

D < F

0 1 2 3 4 5 6 7 8 R C P D F B K A -

R P F B D K A C

0 1 2 3 4 5 6 7 8 R F P D C B K A -

Heapify

O n (log )

slide-9
SLIDE 9
  • S. Prasitjutrakul 1994

Heaps : I nsertion

Y R P F B D K A X

0 1 2 3 4 5 6 7 8 Y R P D F B K A

R P D B F K A Y

0 1 2 3 4 5 6 7 8 Y R P D F B K A X

X R P X B F K A Y

0 1 2 3 4 5 6 7 8 Y R P X F B K A D

D X P R B F K A Y

0 1 2 3 4 5 6 7 8 Y X P R F B K A D

D

O n (log )

slide-10
SLIDE 10
  • S. Prasitjutrakul 1994

Building a Heap

D A C I F B G H E

0 1 2 3 4 5 6 7 8 D A C B I F G H E

A C I F B G H E

0 1 2 3 4 5 6 7 8 D A C B I F G H E

D A C I F H G B E

0 1 2 3 4 5 6 7 8 D A C H I F G B E

D

Heapify

A C I F H G B E

0 1 2 3 4 5 6 7 8 D A C H I F G B E

D

Heapify Heapify

slide-11
SLIDE 11
  • S. Prasitjutrakul 1994

Building a Heap

H C I F E G B A

0 1 2 3 4 5 6 7 8 D H C E I F G B A

D

Heapify

I C D F E G B A

0 1 2 3 4 5 6 7 8 H I C E D F G B A

H

O n (log )

slide-12
SLIDE 12
  • S. Prasitjutrakul 1994

Heapsort

A H K W

X

R A H K R A H K A H A

W R K H A X W R K H A

Heap

slide-13
SLIDE 13
  • S. Prasitjutrakul 1994

Heapsort

Y R P D B F K A C C Y R P D F B K A P F K A B R Y D C C P F K A R B Y D

BuildHeap

O(n)

R P D B F K A C Y R P D F B K A C Y P K D B F A R C Y P K D F B A R C Y

slide-14
SLIDE 14
  • S. Prasitjutrakul 1994

Heapsort

K B D A F P R C Y K B D F A P R C Y F B A K D P R C Y F B A D K P R C Y D B A K C P R F Y D B A C K P R F Y C B D K A P R F Y C B D A K P R F Y

slide-15
SLIDE 15
  • S. Prasitjutrakul 1994

Heapsort

B C D K A P R F Y B C D A K P R F Y A C D K B P R F Y A C D B K P R F Y

The total number of comparisons 2 2 1 2 2 1 2 2 2 3

2 1 2 2 2 2 2 2

log (log log ... log ) log ( ... ) log ! log ( log ) i n n n n n n O n n

i n =

= + + + = ⋅ ⋅ ⋅ = ≈ − =

slide-16
SLIDE 16
  • S. Prasitjutrakul 1994

Heapsort

void Heapsort( ListType *pList ) { int i; BuildHeap( pList ); for ( i=pList->count-1; i>=1; i-- ) { Swap( pList, 0, i ); Heapify( pList, i ); } } BuildHeap( ListType *pList ) { int i; for (i=pList->count/2; i>=0; i--) Heapify( pList, i ); }

slide-17
SLIDE 17
  • S. Prasitjutrakul 1994

Priority Queues

Two operations

!

Insert : Insert an item

!

ExtractMax : Remove the item having the largest key

Insert : 23 17 31 * * 99 87 * * * ExtractMax : 31 23 99 87 23

Implementations :

Insert

ExtractMax Sorted contiguous list

O(n) O(1)

Binary search tree

O(log n) avg. O(log n) avg.

Heap

O(log n) O(log n)