Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c l a s s Heap : @classmethod def p a ren t ( cl s , i ) : return i // 2 @classmethod

c l a s s Heap :

@classmethod

def p a ren t ( cl s , i ) :

return i // 2

@classmethod

def l e f t C h i l d ( cl s , i ) :

return 2 i 1

@classmethod

def ri g h t C hil d ( cl s , i ) :

return 2 i + 1

def i n i t ( s e l f , maxSize ) :

s e l f . maxSize = maxSize

s e l f .H = ( maxSize + 1 ) [ 0 ]

s e l f . s i z e = 0

def si f t U p ( s e l f , i ) :

j = Heap . p a ren t ( i )

while i > 1 and s e l f .H[ j ] < s e l f .H[ i ] :

s e l f .H[ i ] , s e l f .H[ j ] = s e l f .H[ j ] , s e l f .H[ i ]

i = j

j = Heap . p a ren t ( i )

def i n s e r t ( s e l f , p ) :

i f s e l f . s i z e == s e l f . maxSize :

ra is e Excep ti on ( Heap i s f u l l )

s e l f . s i z e += 1

s e l f .H[ s e l f . s i z e ] = p

s e l f . si f t U p ( s e l f . s i z e )

def si f tDown ( s e l f , i ) :

maxIndex = i

l = Heap . l e f t C h i l d ( i )

i f l <= s e l f . s i z e and s e l f .H[ l ] > s e l f .H[ maxIndex ] :

maxIndex = l

r = Heap . ri g h t C hil d ( i )

i f r <= s e l f . s i z e and s e l f .H[ r ] > s e l f .H[ maxIndex ] :

maxIndex = r

i f i != maxIndex :

s e l f .H[ i ] , s e l f .H[ maxIndex ] = s e l f .H[ maxIndex ] , s e l f .H[ i ]

s e l f . si f tDown ( maxIndex )

def extractMax ( s e l f ) :

r e s u l t = s e l f .H[ 1 ]

s e l f .H[ 1 ] = s e l f .H[ s e l f . s i z e ]

s e l f . s i z e = 1

s e l f . si f tDown ( 1 )

return r e s u l t

def remove ( s e l f , i ) :

s e l f .H[ i ] = f l o a t ( i n f )

s e l f . si f t U p ( i )

s e l f . extractMax ( )

def t e s t ( ) :

a s s e r t Heap . l e f t C h i l d ( 1 ) == 2

a s s e r t Heap . ri g h t C hil d ( 1 ) == 3

a s s e r t Heap . p a ren t ( 2 ) == 1

a s s e r t Heap . p a ren t ( 3 ) == 1

h = Heap ( 1 0 )

h . i n s e r t ( 2 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 4 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 8 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 4 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 7 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 6 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 0 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 9 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 3 ) ; pr int ( h .H [ : h . s i z e +1])

pr int ( 4 0 )

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( h . extractMax ( ) , h .H [ : h . s i z e +1])

pr int ( 4 0 )

h . i n s e r t ( 2 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 4 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 8 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 4 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 7 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 6 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 1 0 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 9 ) ; pr int ( h .H [ : h . s i z e +1])

h . i n s e r t ( 3 ) ; pr int ( h .H [ : h . s i z e +1])

pr int ( 4 0 )

h . remove ( 3 ) ; pr int ( h .H [ : h . s i z e +1])

h . remove ( 3 ) ; pr int ( h .H [ : h . s i z e +1])

h . remove ( 3 ) ; pr int ( h .H [ : h . s i z e +1]) 3

pr int ( 4 0 )

t e s t ( )

Question:

Show the changes that are necessary to turn it into a min heap. Indicate the changes by writing comments on your max heap code. the code is written above.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions