Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Heap shift down

Heap shift down

This section introduces how to take an element out of a maximum heap, called shift down. Only the element with the highest priority can be taken out, that is, the root node. After taking out the original 62, the following describes how to fill this maximum heap.

First, we put the last element of the array at the root node, which does not satisfy the definition of a maximum heap.

The adjustment process moves this root node 16 downward step by step. Since 16 is smaller than both children, first compare which child is larger, 52 or 30, and swap with the larger one.

Continue comparing 16's children 28 and 41; 41 is larger, so 16 and 41 swap positions.

Continue comparing 16 with child node 15; 16 is larger, so no swap is needed now. Finally our shift down operation is complete, maintaining the property of a maximum heap.

IV. Java Example Code

Source code package download:Download

src/runoob/heap/HeapShiftDown.java file code:

package

runoob.heap

;

/**
 * Take an element out of the maximum heap
 */

public

class

HeapShiftDown

<

T

extends

Comparable

{

protected

T

[

]

data

;

   

protected

int

count

;

   

protected

int

capacity

;

   

// Constructor, construct an empty heap that can hold capacity elements

   

public

HeapShiftDown

(

int

capacity

)

{

       

// Adding 1 here refers to the original number of elements that can be held; removing position 0, it can only hold capacity elements

        data

=

(

T

[

]

)

new

Comparable

[

capacity

1

]

;

        count

=

0

;

       

this

.

capacity

=

capacity

;

   

}

   

// Return the number of elements in the heap

   

public

int

size

(

)

{

       

return

count

;

   

}

   

// Return a boolean value indicating whether the heap is empty

   

public

boolean

isEmpty

(

)

{

       

return

count

==

0

;

   

}

   

// Insert a new element item into the maximum heap

   

public

void

insert

(

T item

)

{

       

assert

count

1

<=

capacity

;

        data

[

count

1

]

=

item

;

        count

++;

        shiftUp

(

count

)

;

   

}

   

// Take the top element from the maximum heap, i.e., the maximum data stored in the heap

   

public

T extractMax

(

)

{

       

assert

count

0

;

        T ret

=

data

[

1

]

;

        swap

(

1

, count

)

;

        count

--;

        shiftDown

(

1

)

;

       

return

ret

;

   

}

   

// Get the top element of the maximum heap

   

public

T getMax

(

)

{

       

assert

(

count

0

)

;

       

return

data

[

1

]

;

   

}

   

// Swap the two elements with indices i and j in the heap

   

private

void

swap

(

int

i,

int

j

)

{

        T t

=

data

[

i

]

;

        data

[

i

]

=

data

[

j

]

;

        data

[

j

]

=

t

;

   

}

   

//********************

   

//* Core helper function of the maximum heap

   

//********************

   

private

void

shiftUp

(

int

k

)

{

       

while

(

k

1

&&

data

[

k

/

2

]

.

compareTo

(

data

[

k

]

)

<

0

)

{

swap

(

k, k

/

2

)

;

            k

/=

2

;

       

}

   

}

   

// shiftDown operation

   

private

void

shiftDown

(

int

k

)

{

       

while

(

2

k

<=

count

)

{

           

int

j

=

2

k

;

// In this loop iteration, data[k] and data[j] swap positions

           

if

(

j

1

<=

count

&&

data

[

j

1

]

.

compareTo

(

data

[

j

]

)

0

)

                j

++;

           

// data[j] is the maximum of data[2*k] and data[2*k+1]

           

if

(

data

[

k

]

.

compareTo

(

data

[

j

]

)

=

0

)

break

;

            swap

(

k, j

)

;

            k

=

j

;

       

}

       

System

.

out

.

println

(

"shiftDown finished"

)

;

   

}

   

// Test HeapShiftDown

   

public

static

void

main

(

String

[

]

args

)

{

        HeapShiftDown

<

Integer

heapShiftDown

=

new

HeapShiftDown

<

Integer

(

100

)

;

       

// Number of elements in the heap

       

int

N

=

100

;

       

// The value range of elements in the heap is [0, M)

       

int

M

=

100

;

       

for

(

int

i

=

0

;

i

<

N

;

i

++

)

            heapShiftDown.

insert

(

new

Integer

(

(

int

)

(

Math

.

random

(

)

M

)

)

)

;

       

Integer

[

]

arr

=

new

Integer

[

N

]

;

       

// Gradually take the data out of the maximum heap using extractMax

       

// The order of taking out should be from largest to smallest

       

for

(

int

i

=

0

;

i

<

N

;

i

++

)

{

            arr

[

i

]

=

heapShiftDown.

extractMax

(

)

;

           

System

.

out

.

print

(

arr

[

i

]

" "

)

;

       

}

       

// Ensure the arr array is arranged from largest to smallest

       

for

(

int

i

=

1

;

i

<

N

;

i

++

)

           

assert

arr

[

i

1

]

=

arr

[

i

]

;

   

}

}