Js heap

Developer
Size
1,724 Kb
Views
14,168

How do I make an js heap?

What is a js heap? How do you make a js heap? This script and codes were developed by Elliot on 22 December 2022, Thursday.

Js heap Previews

Js heap - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>js heap</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Js heap - Script Codes JS Codes

class Heap { constructor() { this.heapAr = [] } insert(el) { this.heapAr[this.heapAr.length] = el this.restoreInsert() } pop() { if (this.heapAr.length > 0) { const max = this.heapAr[0] this.heapAr[0] = this.heapAr[this.heapAr.length-1] this.heapAr.splice(this.heapAr.length-1, 1) this.restoreDelete() return max } throw "heap popped with no elements." } right(i) { return 2*i+2 } left(i) { return 2*i+1 } parent(i) { return Math.floor((i-1)/2) } restoreDelete() { let j = 0 while(j < this.heapAr.length) { const l = this.left(j) const r = this.right(j) let largest = j if (l < this.heapAr.length && this.heapAr[l] > this.heapAr[largest]) { largest = l } if (r < this.heapAr.length && this.heapAr[r] > this.heapAr[largest]) { largest = r } if (largest != j) { const temp = this.heapAr[j] this.heapAr[j] = this.heapAr[largest] this.heapAr[largest] = temp j = largest } else { j = this.heapAr.length } } } restoreInsert() { if (this.heapAr.length != 1) { let j = this.heapAr.length-1 while (j > 0 && this.heapAr[j] > this.heapAr[this.parent(j)]) { const temp = this.heapAr[j] this.heapAr[j] = this.heapAr[this.parent(j)] this.heapAr[this.parent(j)] = temp j = this.parent(j) } } }
}
var track = new Heap()
Js heap - Script Codes
Js heap - Script Codes
Home Page Home
Developer Elliot
Username vez
Uploaded December 22, 2022
Rating 3
Size 1,724 Kb
Views 14,168
Do you need developer help for Js heap?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Elliot (vez) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!