Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Heap

A heap is a specialized tree-based data structure which is essentially an almost complete tree that satisfies the heap property :in a max heap, for any given node C, if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of C. In a min heap, the key of P is less than or equal to the key of C. The node at the "top" of the heap (with no parents) is called the root node.

Installation

Install with npm

npm i javascript-algo-ds

Import in your javascript file as:

const Heap = require('javascript-algo-ds/src/data-structure/heap/heap')
const MinHeap = require('javascript-algo-ds/src/data-structure/heap/minHeap')
const MaxHeap = require('javascript-algo-ds/src/data-structure/heap/maxHeap')

API

Constructor

Syntax
const minHeap = new MinHeap(compareFunc)
const maxHeap = new MaxHeap(compareFunc)

Parameters

  • compareFunction - function
    • Default value - undefined

Properties

  • heap - array of objects in ordered way
  • size - number of elements in the heap
  • compare - comparator of type Comparator

Methods

Static methods

heap.isEmpty()

Checks if the heap contains at least one element or not.

Returns

Returns boolean

heap.add(item)

Adds item to the heap followed by heapifying.

Parameters

  • item - T
    • The value to be added.

Returns

Returns this

heap.peek()

Returns the element at root of the heap.

Returns

Type - T | null

heap.poll()

Remove and returns the element at root of the heap followed by heapifying.

Returns

Type - T | null

heap.remove(item)

Parameters

  • item - T
    • The item to be removed. Remove the element matching item using custom compare function (if passed) at root of the heap followed by heapifying.

Returns

Type - this

heap.find(item)

Finds the element matching item using custom compare function (if passed) and returns their indices in heap.heap array.

Parameters

  • item - T
    • The item to be found.

Returns

Type - number[]

heap.toString()

Converts the heap in array and returns it

Returns

Type - String

Heap.fromArray()

@static
Creates the heap from an array

Parameters

  • array - T[]
    • Array of objects
  • compareFunction - function
    • Default value - undefined

Returns

Type - MinHeap | MaxHeap | Heap