1.3.2 • Published 12 months ago

heapq-ts v1.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

heapq-ts

Library for using heap-queue with ts

install

npm i heapq-ts

import

import Heap_Queue from "heapq-ts";

examples

maxheap

const heapq: Heap_Queue = new Heap_Queue();
heapq.push(4);
heapq.push(1);
heapq.push(2);
console.log(heapq.top()); // 4

minheap

const heapq: Heap_Queue = new Heap_Queue(true);
heapq.push(4);
heapq.push(1);
heapq.push(2);
console.log(heapq.top()); // 1

custom comparison

  interface Person {
    height:number;
    weight:number;
    grade:number;
  }

  const p1 = {
    height: 3222,
    weight: 22,
    grade: 1
  }

  const p2 = {
    height: 3222,
    weight: 22,
    grade: 9
  }

  const p3 = {
    height: 88,
    weight: 4532,
    grade: 1
  }

  const p4 = {
    height: 88,
    weight: 184,
    grade: 2
  }

  const heapq: Heap_Queue = new Heap_Queue<Person>((p1, p2) => {
    if (p1.height === p2.height) {
      if (p1.weight === p2.weight) {
        return p1.grade - p2.grade;
      }
      return p2.weight - p1.weight;
    }
    return p2.height - p1.height;
  });

  heapq.push(p1);
  heapq.push(p2);
  heapq.push(p3);
  heapq.push(p4);

  console.log(heapq.top());  //   { height: 3222, weight: 22, grade: 1 }
  heapq.pop();
  console.log(heapq.top());  //   { height: 3222, weight: 22, grade: 9 }

method

push(item)

push item in the heap

pop()

pop the smallest item or largest item of the heap

top()

get the smallest item or largest item

size()

get the length of heap

empty()

check if the length is empty or not

1.2.0

12 months ago

1.3.2

12 months ago

1.3.1

12 months ago

1.3.0

12 months ago

1.1.0

1 year ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago