📜  nodejs 堆使用 - Javascript (1)

📅  最后修改于: 2023-12-03 15:03:16.005000             🧑  作者: Mango

堆使用 in Node.js - Javascript

In computer science, a heap is a special kind of tree-based data structure that satisfies the heap property: if P is a parent node in such a heap, and C is a direct child of P, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C.

Heap is a very useful data structure and is widely used in various algorithms and applications. In this article, we'll explore how to use heap in Node.js with Javascript.

Heap Implementation in Javascript

There are several ways to implement a heap in Javascript. One way is to use an array to represent the heap. In this implementation, the root of the heap will be located at index 0 of the array, and for any node i in the heap, its left child will be located at index 2i+1 and its right child will be located at index 2i+2. Here's an example implementation of a max heap in Javascript:

class MaxHeap {
  constructor() {
    this.heap = [];
  }

  // Add a new node to the heap
  add(value) {
    this.heap.push(value);
    this.bubbleUp(this.heap.length - 1);
  }

  // Remove and return the maximum element from the heap
  extractMax() {
    const max = this.heap[0];
    const last = this.heap.pop();
    if (this.heap.length > 0) {
      this.heap[0] = last;
      this.bubbleDown(0);
    }
    return max;
  }

  // Bubble up the node at index i until it satisfies the heap property
  bubbleUp(i) {
    const parent = Math.floor((i - 1) / 2);
    if (this.heap[parent] < this.heap[i]) {
      [this.heap[parent], this.heap[i]] = [this.heap[i], this.heap[parent]];
      this.bubbleUp(parent);
    }
  }

  // Bubble down the node at index i until it satisfies the heap property
  bubbleDown(i) {
    const left = 2 * i + 1;
    const right = 2 * i + 2;
    let max = i;
    if (left < this.heap.length && this.heap[left] > this.heap[max]) {
      max = left;
    }
    if (right < this.heap.length && this.heap[right] > this.heap[max]) {
      max = right;
    }
    if (max !== i) {
      [this.heap[max], this.heap[i]] = [this.heap[i], this.heap[max]];
      this.bubbleDown(max);
    }
  }
}
Using Heap in Node.js

Now that we have a max heap implementation in Javascript, let's see how we can use it in Node.js. Here's an example of using the heap to find the kth largest element in an array:

const MaxHeap = require('./max-heap');

function findKthLargest(nums, k) {
  const heap = new MaxHeap();
  for (let i = 0; i < nums.length; i++) {
    heap.add(nums[i]);
    if (heap.heap.length > k) {
      heap.extractMax();
    }
  }
  return heap.extractMax();
}

const nums = [3, 2, 1, 5, 6, 4];
const k = 2;
console.log(findKthLargest(nums, k)); // Output: 5

In this example, we first create a new instance of MaxHeap. Then, we iterate through the input array nums and add each element to the heap. If the size of the heap is greater than k, we extract the maximum element from the heap. Finally, we extract the maximum element from the heap, which will be the kth largest element in the array.

Conclusion

This article provided an introduction to using heap in Node.js with Javascript. We saw how to implement a max heap in Javascript, and how to use the heap to solve a problem. Heap is a very useful data structure and can help us solve various algorithmic problems efficiently.