← RETURN_TO_CORE

堆与优先级队列

PROTOCOL: PRIORITY_QUEUES | AVG_O: O(log n)

LIVE_VISUALIZATION
[ MODULE_VISUALIZER_LOADING ]

SOURCE_CODE // DECRYPTED

最大堆/最小堆 // 在无序的混沌中维持极值的绝对统治。

binary_heap.tsREAD_ONLY
class PriorityMatrix {
  private heap: number[] = [];

  // 能量注入与向上跃迁 (Sift-Up)
  insert(val: number): void {
    this.heap.push(val);
    let idx = this.heap.length - 1;
    
    while (idx > 0) {
      const parentIdx = Math.floor((idx - 1) / 2);
      if (this.heap[idx] <= this.heap[parentIdx]) break;
      
      // 夺取极值王座
      [this.heap[idx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[idx]];
      idx = parentIdx;
    }
  }

  // 极值剥离与系统重组 (Extract-Max)
  extractMax(): number | null {
    if (this.heap.length === 0) return null;
    if (this.heap.length === 1) return this.heap.pop()!;
    
    const max = this.heap[0];
    // 末位节点补位,准备向下剥离 (Sift-Down)
    this.heap[0] = this.heap.pop()!; 
    this.siftDown(0);
    
    return max; // 返回捕获的最高优先级实体
  }

  private siftDown(idx: number): void { /* 维持秩序的向下沉降逻辑 */ }
}