/** * 二叉堆 * @author BrightLi * @since 2020/4/18 */ export default class BinaryHeap { private _content:any[]; private _scoreFunction:(node:any)=>number; // 構造函數 public constructor(scoreFunction:(node:any)=>number){ this._content=[]; this._scoreFunction=scoreFunction; } // 是否為空 public get isEmpty():boolean{ return this._content.length<1; } // 是否包含節點 public contain(node:any){ var pos=this._content.indexOf(node); return pos != -1; } // 壓入節點 public push(node:any){ this._content.push(node); this.siftDown(this._content.length-1); } // 彈出節點,一定是最小值 public pop():any{ var result=this._content[0]; var end=this._content.pop(); if(this._content.length>0){ this._content[0]=end; this.siftDown(0); } return result; } // 更新節點 public update(node:any){ var n=this._content.indexOf(node); if(n==-1){ return; } this.siftDown(n); this.siftUp(n); } private siftDown(n:number){ var element=this._content[n]; while(n>0){ var parentNode=((n+1)>>1)-1, parent=this._content[parentNode]; if(this._scoreFunction(element)