__rain1

Untitled

Jul 9th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Heap {
  2.  
  3.     constructor() {
  4.         this.items = [];
  5.         this.size = this.items.length - 1;
  6.     }
  7.  
  8.     getParentIndex(childIndex) {
  9.         if (childIndex == 0) {
  10.             return null;
  11.         }
  12.         return Math.floor((childIndex - 1) / 2);
  13.     }
  14.  
  15.     getLeftChildIndex(parentIndex) {
  16.         if (parentIndex = this.items.length - 1) {
  17.             return null;
  18.         }
  19.         return Math.floor(parentIndex * 2 + 1);
  20.     }
  21.  
  22.     getRightChildIndex(parentIndex) {
  23.         if (parentIndex = this.items.length - 1) {
  24.             return null;
  25.         }
  26.         return (parentIndex * 2 + 2);
  27.     }
  28.  
  29.     insert(item) {
  30.         this.items.push(item);
  31.         this.size++;
  32.         this.heapifyUp();
  33.     }
  34.  
  35.     pop() {
  36.         this.items[0] = this.items[this.items.length - 1];
  37.         this.items.pop();
  38.         this.size--;
  39.         this.heapifyDown();
  40.     }
  41. }
Add Comment
Please, Sign In to add comment