From f834d648e8e24dccd2b90ec3b30ae6de0ab63f32 Mon Sep 17 00:00:00 2001 From: Anshul Singh <56274258+anshul2807@users.noreply.github.com> Date: Fri, 27 Aug 2021 20:40:29 +0530 Subject: [PATCH] fix max heap insert function (#43285) --- .../insert-an-element-into-a-max-heap.md | 32 ++++++++++++------- .../remove-an-element-from-a-max-heap.md | 32 ++++++++++++------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.md b/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.md index 74e6b693f77..d21a47bc2a9 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.md +++ b/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.md @@ -133,20 +133,28 @@ var MaxHeap = function() { ```js var MaxHeap = function() { // Only change code below this line - this.heap = [null]; - this.insert = (ele) => { - var index = this.heap.length; - var arr = [...this.heap]; - arr.push(ele); - while (ele > arr[Math.floor(index / 2)] && index > 1) { - arr[index] = arr[Math.floor(index / 2)]; - arr[Math.floor(index / 2)] = ele; - index = arr[Math.floor(index / 2)]; - } - this.heap = arr; + this.heap = []; + this.parent = index => { + return Math.floor((index - 1) / 2); + } + this.insert = element => { + this.heap.push(element); + this.heapifyUp(this.heap.length - 1); + } + this.heapifyUp = index => { + let currentIndex = index, + parentIndex = this.parent(currentIndex); + while (currentIndex > 0 && this.heap[currentIndex] > this.heap[parentIndex]) { + this.swap(currentIndex, parentIndex); + currentIndex = parentIndex; + parentIndex = this.parent(parentIndex); + } + } + this.swap = (index1, index2) => { + [this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]]; } this.print = () => { - return this.heap.slice(1); + return this.heap; } // Only change code above this line }; diff --git a/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md b/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md index 858073f2112..6029f67938c 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md +++ b/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md @@ -114,21 +114,29 @@ assert( ## --seed-contents-- ```js -var MaxHeap = function() { - this.heap = [null]; - this.insert = (ele) => { - var index = this.heap.length; - var arr = [...this.heap]; - arr.push(ele); - while (ele > arr[Math.floor(index / 2)] && index > 1) { - arr[index] = arr[Math.floor(index / 2)]; - arr[Math.floor(index / 2)] = ele; - index = arr[Math.floor(index / 2)]; +var MaxHeap = function () { + this.heap = []; + this.parent = index => { + return Math.floor((index - 1) / 2); + } + this.insert = element => { + this.heap.push(element); + this.heapifyUp(this.heap.length - 1); + } + this.heapifyUp = index => { + let currentIndex = index, + parentIndex = this.parent(currentIndex); + while (currentIndex > 0 && this.heap[currentIndex] > this.heap[parentIndex]) { + this.swap(currentIndex, parentIndex); + currentIndex = parentIndex; + parentIndex = this.parent(parentIndex); } - this.heap = arr; + } + this.swap = (index1, index2) => { + [this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]]; } this.print = () => { - return this.heap.slice(1); + return this.heap; } // Only change code below this line