Last active
April 19, 2021 03:34
-
-
Save peterwzhang/80aa990c232e19b19565c9c3c83bc5e3 to your computer and use it in GitHub Desktop.
This script adds "Insert Keys" and "Remove K Smallest" functionality to the usfca Heap visualizer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name MinHeap++ | |
// @version 1 | |
// @description This script adds "Insert Keys" and "Remove K Smallest" functionality to the usfca Heap visualizer. | |
// @author Peter Zhang | |
// @match https://www.cs.usfca.edu/~galles/visualization/Heap.html | |
// @grant none | |
// ==/UserScript== | |
//copy everything inside of the setup function below if you want to just paste the code in the console | |
function setup(){ | |
const cParent = document.getElementById("algoControlSection"); | |
const controller = cParent.getElementsByTagName("td"); | |
function toggleUIOff(){ | |
controller[5].getElementsByTagName("input")[0].setAttribute("disabled",""); | |
controller[6].getElementsByTagName("input")[0].setAttribute("disabled",""); | |
controller[7].getElementsByTagName("input")[0].setAttribute("disabled",""); | |
controller[8].getElementsByTagName("input")[0].setAttribute("disabled",""); | |
} | |
function toggleUIOn(){ | |
controller[5].getElementsByTagName("input")[0].removeAttribute("disabled"); | |
controller[6].getElementsByTagName("input")[0].removeAttribute("disabled"); | |
controller[7].getElementsByTagName("input")[0].removeAttribute("disabled"); | |
controller[8].getElementsByTagName("input")[0].removeAttribute("disabled"); | |
} | |
function insClickHandler(event) { | |
toggleUIOff(); | |
const keys = insKeysInp.getElementsByTagName("input")[0].value.split(',') | |
for (let i = 0; i <= keys.length; i++){ | |
const disabled = setInterval(function() { | |
if (i < keys.length && controller[0].getElementsByTagName("input")[0].disabled == false){ | |
controller[0].getElementsByTagName("input")[0].value = keys[i]; | |
controller[1].getElementsByTagName("input")[0].click(); | |
toggleUIOff(); | |
clearInterval(disabled); | |
} | |
else if (controller[0].getElementsByTagName("input")[0].disabled == false){ | |
toggleUIOn(); | |
} | |
}, 100); | |
} | |
} | |
function delClickHandler(event) { | |
toggleUIOff(); | |
const numInp = parseInt(delKInp.getElementsByTagName("input")[0].value); | |
for (let i = 0; i <= numInp; i++){ | |
const disabled = setInterval(function() { | |
if (i < numInp && controller[2].getElementsByTagName("input")[0].disabled == false){ | |
controller[2].getElementsByTagName("input")[0].click(); | |
toggleUIOff(); | |
clearInterval(disabled); | |
if (i == numInp - 1) | |
toggleUIOn(); | |
} | |
}, 100); | |
} | |
} | |
const insKeysInp = document.createElement("td"); | |
insKeysInp.innerHTML = "<input type='Text' value='' size='12'>"; | |
cParent.appendChild(insKeysInp); | |
const insKeysBtn = document.createElement("td"); | |
insKeysBtn.innerHTML = "<input type='Button' value='Insert Keys'>"; | |
insKeysBtn.addEventListener("click", insClickHandler); | |
cParent.appendChild(insKeysBtn); | |
const delKInp = document.createElement("td"); | |
delKInp.innerHTML = "<input type='Text' value='' size='3'>"; | |
cParent.appendChild(delKInp); | |
const delKBtn = document.createElement("td"); | |
delKBtn.innerHTML = "<input type='Button' value='Delete K Smallest'>"; | |
delKBtn.addEventListener("click", delClickHandler); | |
cParent.appendChild(delKBtn); | |
document.getElementById("header").innerHTML = " \n\t\t\t\t<h1>Min Heap++</h1>\n\t\t\t"; | |
} | |
window.addEventListener("load", setup, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment