← Runnable Projects

阅读中文版本

Node Binary Heap CLI

Node Binary Heap — a runnable typescript project with source, install and run commands, and expected output.

intermediatetypescriptunverified

Project

node-binary-heap-cli

Entry point: pq.mjs

Install

node pq.mjs

Run

node pq.mjs

Test

node --test pq.test.mjs

Expected output

Binary-heap priority queue; pushes items with priorities then pops them in order.

Dependencies

  • node >=20

Verification evidence

Source reviewed against typescript standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Node Binary Heap CLI is a self-contained typescript command-line program. Binary-heap priority queue; pushes items with priorities then pops them in order.

Install and run

node pq.mjs
node pq.mjs

Project structure

node-binary-heap-cli/
├── pq.mjs
└── README snippet above

Key source

// pq.mjs
class PQ{constructor(){this.h=[];}push(v,p){this.h.push([p,v]);this._up(this.h.length-1);}pop(){const t=this.h[0],e=this.h.pop();if(this.h.length){this.h[0]=e;this._down(0);}return t[1];}_up(i){while(i>0){const p=(i-1)>>1;if(this.h[p][0]<=this.h[i][0])break;[this.h[p],this.h[i]]=[this.h[i],this.h[p]];i=p;}}_down(i){for(;;){let s=i*2+1;if(s+1<this.h.length&&this.h[s+1][0]<this.h[s][0])s++;if(s>=this.h.length||this.h[i][0]<=this.h[s][0])break;[this.h[i],this.h[s]]=[this.h[s],this.h[i]];i=s;}}}
const q=new PQ();[["a",3],["b",1],["c",2]].forEach(([v,p])=>q.push(v,p));
console.log([q.pop(),q.pop(),q.pop()].join(","));

Testing

node --test pq.test.mjs

Expected output

When run with the documented command, the program prints:

b,c,a   (popped in priority order)

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.