Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>二叉堆</title>
</head>

<body>
  <h1 style="text-align:center;">最小堆算法</h1>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                // 最小二叉堆,所有节点都小于等于它的两个子节点,所以根节点一定是最小的值

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

//   insert(val) {
//     if (this.size() === 0) {
//       this.heap.push(val);
//     } else {
//       this.heap.push(val);
//       this.siftUp(this.heap.length - 1);
//     }
//   }

//   siftUp(index) {
//     let parent = this.parent(index);
//     while (this.heap[parent] > this.heap[index]) {
//       const temp = this.heap[parent];
//       this.heap[parent] = this.heap[index];
//       this.heap[index] = temp;
//       index = parent;
//       parent = this.parent(parent);
//     }
//   }

//   extract() {
//     if (this.size() <= 2) {
//       return this.heap.shift();
//     } else {
//       const result = this.heap[0];
//       this.heap[0] = this.heap.pop();
//       this.siftDown(0);
//       return result;
//     }
//   }

//   siftDown(index) {
//     let leftChild = this.leftChild(index);
//     let rightChild = this.rightChild(index);
//     let current = index;
//     if (
//       typeof this.heap[leftChild] !== undefined &&
//       this.heap[leftChild] < this.heap[current]
//     ) {
//       current = leftChild;
//     }
//     if (
//       typeof this.heap[rightChild] !== undefined &&
//       this.heap[rightChild] < this.heap[current]
//     ) {
//       current = rightChild;
//     }
//     // current is the maximum node
//     if (current !== index) {
//       const temp = this.heap[index];
//       this.heap[index] = this.heap[current];
//       this.heap[current] = temp;
//       this.siftDown(current);
//     }
//   }

//   size() {
//     return this.heap.length;
//   }

//   leftChild(index) {
//     return 2 * index + 1;
//   }

//   rightChild(index) {
//     return 2 * index + 2;
//   }

//   parent(index) {
//     return Math.floor((index - 1) / 2);
//   }

//   display() {
//     console.log(this.heap);
//   }

//   findMinimum() {
//     return this.head[0];
//   }
// }

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

  insert(val) {
    if (!this.heap.length) {
      this.heap.push(val);
    } else {
      this.heap.push(val);
      this._siftUp(this.heap.length - 1);
    }
  }

  extract() {
    const result = this.heap.shift();
    if (this.heap.length > 1) {
      const last = this.heap.pop();
      this.heap.unshift(last);
      this._siftDown(0);
    }

    return result;
  }

  _siftUp(index) {
    const parent = this.parent(index);
    if (parent < 0) return;
    const { heap } = this;
    if (heap[parent] > heap[index]) {
      [heap[parent], heap[index]] = [heap[index], heap[parent]];
      this._siftUp(parent);
    }
  }

  _siftDown(index) {
    let current = index;
    const { heap } = this;
    const left = this.leftChild(index);
    const right = this.rightChild(index);
    let next = current;
    if (heap[left] && heap[left] < heap[current]) {
      next = left;
    }
    if (heap[right] && heap[right] < heap[current]) {
      next = right;
    }

    if (next !== current) {
      [heap[next], heap[current]] = [heap[current], heap[next]];
      this._siftDown(next);
    }
  }

  getMinimum() {
    return this.heap[0];
  }

  display() {
    console.log(this.heap);
  }
  leftChild(index) {
    return index * 2 + 1;
  }
  rightChild(index) {
    return index * 2 + 2;
  }
  parent(index) {
    return Math.floor((index - 1) / 2);
  }
}

// 堆排序

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

//   insert(val) {
//     const { heap } = this;
//     heap.push(val);
//     if (heap.length > 1) {
//       this._siftUp(heap.length - 1);
//     }
//   }

//   _siftUp(index) {
//     if (index === 0) return;
//     const { heap } = this;
//     const parent = this.parent(index);
//     if (heap[parent] < heap[index]) {
//       [heap[parent], heap[index]] = [heap[index], heap[parent]];
//       this._siftUp(parent);
//     }
//   }

//   sort() {
//     const { heap } = this;
//     let length = heap.length - 1;
//     while (length > 1) {
//       [heap[0], heap[length]] = [heap[length], heap[0]];
//       this._siftDown(0, length--);
//     }
//   }

//   _siftDown(index, last) {
//     const { heap } = this;
//     let current = index;
//     const left = this.leftChild(current);
//     const right = this.rightChild(current);
//     if (left >= last || right >= last) return;
//     if (heap[current] < heap[left]) {
//       current = left;
//     }
//     if (heap[current] < heap[right]) {
//       current = right;
//     }

//     if (current !== index) {
//       [heap[current], heap[index]] = [heap[index], heap[current]];
//       this._siftDown(current, last);
//     }
//   }

//   leftChild(index) {
//     return 2 * index + 1;
//   }

//   rightChild(index) {
//     return 2 * index + 2;
//   }
//   parent(index) {
//     return Math.floor((index - 1) / 2);
//   }

//   display() {
//     console.log(this.heap);
//   }
// }

const minHeap = new MinHeap();
minHeap.insert(2);
minHeap.insert(2);
minHeap.insert(6);
minHeap.insert(5);
minHeap.insert(4);
minHeap.insert(3);
minHeap.insert(1);

// minHeap.sort();
// minHeap.extract();
// minHeap.extract();
minHeap.display();

              
            
!
999px

Console