🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

107w-heap

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

107w-heap

提供js堆排序(优先级队列)数据结构

latest
npmnpm
Version
1.0.0
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

安装

npm install 107w-heap

导入

const Heap = require('107w-heap')

实例

const Heap = require('107w-heap')
// 默认最小堆
const heap = new Heap()
const arr = [5, 4, 3, 2, 1, 0]
const res = []
for(let item of arr) {
  heap.insert(item)
}
while(heap.count) {
  res.push(heap.delMax())
}
// res [0,1,2,3,4,5]

方法 & 属性

// 向堆插入元素
heap.insert()

// 删除并返回堆顶元素
heap.delMax()

// 堆元素个数
heap.count

// 创建堆实例,可以传参自定义比较回调函数,默认创建最小堆
// 创建最大堆
new Heap(function(i, j) {return this.heap[i] < this.heap[j];})

开源协议

ISC

Keywords

heap

FAQs

Package last updated on 09 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts