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

Indexed Heap and Its Optimization

Indexed Heap and Its Optimization

I. Concept and Introduction

An indexed heap is an optimization of the heap data structure.

An indexed heap uses a new int-type array to store index information.

Compared with a heap, its advantages are as follows:

II. Applicability Notes

If the elements stored in the heap are large, swapping consumes a lot of time. In this case, the indexed heap data structure can be used instead: the heap stores array indices, and we operate on the indices accordingly.

III. Structural diagram

We need to modify the previous heap code implementation, switching to a mindset of operating directly on indices. First, the constructor adds an index array property indexes.

protected

T

[

]

data

;

     

// Data in the maximum indexed heap

protected

int

[

]

indexes

;

   

// Indices in the maximum indexed heap

protected

int

count

;

protected

int

capacity

;

The corresponding constructor is adjusted to add initialization of the index array.

...

public

IndexMaxHeap

(

int

capacity

)

{

    data

=

(

T

[

]

)

new

Comparable

[

capacity

1

]

;

    indexes

=

new

int

[

capacity

1

]

;

    count

=

0

;

   

this

.

capacity

=

capacity

;

}

...

Adjust the insert operation: the element added to the indexes array is the index of the real data array, indexes[count+1] = i.

...

// Insert a new element into the maximum indexed heap, the new element's index is i, the element is item

// The passed-in i is 0-indexed from the user's perspective

public

void

insert

(

int

i, Item item

)

{

   

assert

count

1

<=

capacity

;

   

assert

i

1

=

1

&&

i

1

<=

capacity

;

    i

+=

1

;

    data

[

i

]

=

item

;

    indexes

[

count

1

]

=

i

;

    count

++;

    shiftUp

(

count

)

;

}

...

Adjust the shift up operation: what is compared is the size of the parent node's data in the data array, so it needs to be expressed as data[index[k/2]] < data[indexs[k]], which swaps the indexes in the index array without making any changes to the data array; the same applies to shift down.

...

// k is the index of the heap

// In an indexed heap, data is compared according to the size of data, but what is actually operated on is the indices

private

void

shiftUp

(

int

k

)

{

   

while

(

k

1

&&

data

[

indexes

[

k

/

2

]

]

.

compareTo

(

data

[

indexes

[

k

]

]

)

<

0

)

{

swapIndexes

(

k, k

/

2

)

;

        k

/=

2

;

   

}

}

...

To take an element out of the indexed heap, the largest element is the data in the root element data[index[1]], then swap index positions to perform the shift down operation.

...

public

T extractMax

(

)

{

   

assert

count

0

;

    T ret

=

data

[

indexes

[

1

]

]

;

    swapIndexes

(

1

, count

)

;

    count

--;

    shiftDown

(

1

)

;

   

return

ret

;

}

...

You can also directly take the data array index of the maximum value

...

// Take the index of the top element from the maximum indexed heap

public

int

extractMaxIndex

(

)

{

   

assert

count

0

;

   

int

ret

=

indexes

[

1

]

1

;

    swapIndexes

(

1

, count

)

;

    count

--;

    shiftDown

(

1

)

;

   

return

ret

;

}

...

Modify data at an index position

...

// Change the element with index i in the maximum indexed heap to newItem

public

void

change

(

int

i , Item newItem

)

{

    i

+=

1

;

    data

[

i

]

=

newItem

;

   

// Find indexes[j] = i, j represents the position of data[i] in the heap

   

// Then shiftUp(j), then shiftDown(j)

   

for

(

int

j

=

1

;

j

<=

count

;

j

++

)

       

if

(

indexes

[

j

]

==

i

)

{

            shiftUp

(

j

)

;

            shiftDown

(

j

)

;

           

return

;

       

}

}

...

IV. Java Example Code

Source code package download:Download

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

package

runoob.heap

;

import

java.util.Arrays

;

/**
 * Indexed heap
 */

// Maximum indexed heap, idea: elements are compared by data, swapped by indices

public

class

IndexMaxHeap

<

T

extends

Comparable

{

protected

T

[

]

data

;

     

// Data in the maximum indexed heap

   

protected

int

[

]

indexes

;

   

// Indices in the maximum indexed heap

   

protected

int

count

;

   

protected

int

capacity

;

   

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

   

public

IndexMaxHeap

(

int

capacity

)

{

        data

=

(

T

[

]

)

new

Comparable

[

capacity

1

]

;

        indexes

=

new

int

[

capacity

1

]

;

        count

=

0

;

       

this

.

capacity

=

capacity

;

   

}

   

// Return the number of elements in the indexed heap

   

public

int

size

(

)

{

       

return

count

;

   

}

   

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

   

public

boolean

isEmpty

(

)

{

       

return

count

==

0

;

   

}

   

// Insert a new element into the maximum indexed heap, the new element's index is i, the element is item

   

// The passed-in i is 0-indexed from the user's perspective

   

public

void

insert

(

int

i, T item

)

{

       

assert

count

1

<=

capacity

;

       

assert

i

1

=

1

&&

i

1

<=

capacity

;

        i

+=

1

;

        data

[

i

]

=

item

;

        indexes

[

count

1

]

=

i

;

        count

++;

        shiftUp

(

count

)

;

   

}

   

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

   

public

T extractMax

(

)

{

       

assert

count

0

;

        T ret

=

data

[

indexes

[

1

]

]

;

        swapIndexes

(

1

, count

)

;

        count

--;

        shiftDown

(

1

)

;

       

return

ret

;

   

}

   

// Take the index of the top element from the maximum indexed heap

   

public

int

extractMaxIndex

(

)

{

       

assert

count

0

;

       

int

ret

=

indexes

[

1

]

1

;

        swapIndexes

(

1

, count

)

;

        count

--;

        shiftDown

(

1

)

;

       

return

ret

;

   

}

   

// Get the top element of the maximum indexed heap

   

public

T getMax

(

)

{

       

assert

count

0

;

       

return

data

[

indexes

[

1

]

]

;

   

}

   

// Get the index of the top element of the maximum indexed heap

   

public

int

getMaxIndex

(

)

{

       

assert

count

0

;

       

return

indexes

[

1

]

1

;

   

}

   

// Get the element with index i in the maximum indexed heap

   

public

T getItem

(

int

i

)

{

       

assert

i

1

=

1

&&

i

1

<=

capacity

;

       

return

data

[

i

1

]

;

   

}

   

// Change the element with index i in the maximum indexed heap to newItem

   

public

void

change

(

int

i , T newItem

)

{

        i

+=

1

;

        data

[

i

]

=

newItem

;

       

// Find indexes[j] = i, j represents the position of data[i] in the heap

       

// Then shiftUp(j), then shiftDown(j)

       

for

(

int

j

=

1

;

j

<=

count

;

j

++

)

           

if

(

indexes

[

j

]

==

i

)

{

                shiftUp

(

j

)

;

                shiftDown

(

j

)

;

               

return

;

           

}

   

}

   

// Swap indices i and j in the indexed heap

   

private

void

swapIndexes

(

int

i,

int

j

)

{

       

int

t

=

indexes

[

i

]

;

        indexes

[

i

]

=

indexes

[

j

]

;

        indexes

[

j

]

=

t

;

   

}

   

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

   

//* Core helper function of the maximum indexed heap

   

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

   

// k is the index of the heap

   

// In an indexed heap, data is compared according to the size of data, but what is actually operated on is the indices

   

private

void

shiftUp

(

int

k

)

{

       

while

(

k

1

&&

data

[

indexes

[

k

/

2

]

]

.

compareTo

(

data

[

indexes

[

k

]

]

)

<

0

)

{

swapIndexes

(

k, k

/

2

)

;

            k

/=

2

;

       

}

   

}

   

// In an indexed heap, data is compared according to the size of data, but what is actually operated on is the indices

   

private

void

shiftDown

(

int

k

)

{

       

while

(

2

k

<=

count

)

{

           

int

j

=

2

k

;

           

if

(

j

1

<=

count

&&

data

[

indexes

[

j

1

]

]

.

compareTo

(

data

[

indexes

[

j

]

]

)

0

)

                j

++;

           

if

(

data

[

indexes

[

k

]

]

.

compareTo

(

data

[

indexes

[

j

]

]

)

=

0

)

               

break

;

            swapIndexes

(

k, j

)

;

            k

=

j

;

       

}

   

}

   

// Test IndexMaxHeap

   

public

static

void

main

(

String

[

]

args

)

{

       

int

N

=

1000000

;

        IndexMaxHeap

<

Integer

indexMaxHeap

=

new

IndexMaxHeap

<

Integer

(

N

)

;

       

for

(

int

i

=

0

;

i

<

N

;

i

++

)

            indexMaxHeap.

insert

(

i ,

(

int

)

(

Math

.

random

(

)

N

)

)

;

 

   

}

}

In the above modification of index positions, we used traversal to find the index position, which is not efficient. We can optimize once more by maintaining a reverse[i] array that represents the position of index i in indexes (the heap), reducing the time complexity of lookup to O(1).

It has the following properties:

indexes[i] = j

reverse[j] = i



indexes[reverse[i]] = i

reverse[indexes[i]] = i