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

Heap shift up

Heap shift up

This section introduces how to add an element to a maximum heap, called shift up。

Suppose we add a new element 52 to the maximum heap below, placing it at the last position of the array. 52 is greater than its parent 16, which does not satisfy the heap definition, so adjustment is needed.

First swap the values in the array at indices 5 and 11, that is, 52 and 16 swap positions.

At this point 52 is still greater than the value 41 at parent index 2, so we need to move further.

Now compare 52 and 62; 52 is already smaller than the parent, so no need to move up further, satisfying the maximum heap definition. We call this process the shift up of a maximum heap.

Java Example Code

Source code package download:Download

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

package

runoob.heap

;

/**
 * Add an element to the heap
 */

public

class

HeapShiftUp

<

T

extends

Comparable

{

protected

T

[

]

data

;

   

protected

int

count

;

   

protected

int

capacity

;

   

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

   

public

HeapShiftUp

(

int

capacity

)

{

        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

)

;

   

}

   

// 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

;

       

}

   

}

   

// Test HeapShiftUp

   

public

static

void

main

(

String

[

]

args

)

{

        HeapShiftUp

<

Integer

heapShiftUp

=

new

HeapShiftUp

<

Integer

(

100

)

;

       

int

N

=

50

;

// Number of elements in the heap

       

int

M

=

100

;

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

       

for

(

int

i

=

0

;

i

<

N

;

i

++

)

            heapShiftUp.

insert

(

new

Integer

(

(

int

)

(

Math

.

random

(

)

M

)

)

)

;

       

System

.

out

.

println

(

heapShiftUp.

size

(

)

)

;

   

}

}