2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								id: 587d825b367417b2b2512c8b
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								title: Remove an Element from a Max Heap
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								forumTopicId: 301710
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								dashedName: remove-an-element-from-a-max-heap
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# --description--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								Now that we can add elements to our heap let's see how we can remove elements. Removing and inserting elements both require similar logic. In a max heap you will usually want to remove the greatest value, so this involves simply extracting it from the root of our tree. This will break the heap property of our tree, so we must reestablish it in some way. Typically, for a max heap this is done in the following way:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-08 17:54:02 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< ol > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  < li > Move the last element in the heap into the root position.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  < li > If either child of the root is greater than it, swap the root with the child of greater value.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  < li > Continue swapping until the parent is greater than both children or you reach the last level in the tree.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / ol > 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# --instructions--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Instructions: Add a method to our max heap called `remove` . This method should return the greatest value that has been added to our max heap and remove it from the heap. It should also reorder the heap so the heap property is maintained. After removing an element, the next greatest element remaining in the heap should become the root.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --hints--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The MaxHeap data structure should exist.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof MaxHeap !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new MaxHeap();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return typeof test == 'object';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								MaxHeap should have a method called print.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof MaxHeap !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new MaxHeap();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return typeof test.print == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								MaxHeap should have a method called insert.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof MaxHeap !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new MaxHeap();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return typeof test.insert == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								MaxHeap should have a method called remove.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof MaxHeap !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new MaxHeap();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return typeof test.remove == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The remove method should remove the greatest element from the max heap while maintaining the max heap property.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof MaxHeap !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new MaxHeap();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.insert(30);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.insert(300);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.insert(500);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.insert(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    let result = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    result.push(test.remove());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    result.push(test.remove());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    result.push(test.remove());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    result.push(test.remove());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return result.join('') == '5003003010';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --seed--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## --seed-contents--
 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var MaxHeap = function() {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 11:40:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  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.print = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return this.heap.slice(1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 12:31:21 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  // Only change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 11:40:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 12:31:21 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  // Only change code above this line
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# --solutions--
 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// solution required
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```