Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Heap {
- constructor() {
- this.items = [];
- this.size = this.items.length - 1;
- }
- getParentIndex(childIndex) {
- if (childIndex == 0) {
- return null;
- }
- return Math.floor((childIndex - 1) / 2);
- }
- getLeftChildIndex(parentIndex) {
- if (parentIndex = this.items.length - 1) {
- return null;
- }
- return Math.floor(parentIndex * 2 + 1);
- }
- getRightChildIndex(parentIndex) {
- if (parentIndex = this.items.length - 1) {
- return null;
- }
- return (parentIndex * 2 + 2);
- }
- insert(item) {
- this.items.push(item);
- this.size++;
- this.heapifyUp();
- }
- pop() {
- this.items[0] = this.items[this.items.length - 1];
- this.items.pop();
- this.size--;
- this.heapifyDown();
- }
- }
Add Comment
Please, Sign In to add comment