JSFiddle - React, Tailwind, and code Playground

by Jerome De Cuyper

JavaScript

var priority_queue = {

};


var binary_heap = function (capacity) {
 	// if capacity is empty then use 10 ?
	this.heap = new Array(capacity);
   this.current_size = 0;
};

binary_heap.prototype.enlarge_array = function() {
	console.log("enlarge array");
}

binary_heap.prototype.insert = function(node) {
   if (this.current_size + 1 > this.heap.length)
   	this.enlarge_array();
  
	this.heap[this.current_size + 1] = node;
   
   // percolate up
   var hole = ++this.current_size;
   while(hole > 1 && this.heap[parseInt(hole / 2)] > node) {
   	this.heap[hole] = this.heap[parseInt(hole / 2)];
   	hole = parseInt(hole / 2); 
      console.log("Move up on level to: " + hole);
      console.log(this.heap[hole]);
      console.log(node);
      console.log(this.heap[parseInt(hole) / 2] > node);
   }
   console.log(hole);
	this.heap[hole] = node;
};

var bh = new binary_heap(10);
console.log(bh.heap);
bh.insert(13);
console.log(bh.heap);
bh.insert(21);
console.log(bh.heap);
bh.insert(16);
console.log(bh.heap);
bh.insert(24);
console.log(bh.heap);
bh.insert(31);
console.log(bh.heap);
bh.insert(19);
console.log(bh.heap);
bh.insert(68);
console.log(bh.heap);
bh.insert(65);
console.log(bh.heap);
bh.insert(26);
console.log(bh.heap);
bh.insert(2);
console.log(bh.heap);
bh.insert(11);
console.log(bh.heap);