Improve formatting in Max Heap lessons (#38835)

* Improve formatting in max heap lessons

* Switch to HTML ordered lists
pull/39216/head
Ty Mick 2020-07-08 17:54:02 -04:00 committed by GitHub
parent e13bfca62b
commit ff9148ca51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 15 deletions

View File

@ -12,22 +12,24 @@ Now we will move on to another tree data structure, the binary heap. A binary he
While binary heaps may be implemented as tree structures with nodes that contain left and right references, the partial ordering according to the heap property allows us to represent the heap with an array. The parent-children relationship is what we're interested in and with simple arithmetic we can compute the children of any parent and the parent of any child node. While binary heaps may be implemented as tree structures with nodes that contain left and right references, the partial ordering according to the heap property allows us to represent the heap with an array. The parent-children relationship is what we're interested in and with simple arithmetic we can compute the children of any parent and the parent of any child node.
For instance, consider this array representation of a binary min heap: For instance, consider this array representation of a binary min heap:
<code>[ 6, 22, 30, 37, 63, 48, 42, 76 ]</code> <code>[ 6, 22, 30, 37, 63, 48, 42, 76 ]</code>
The root node is the first element, 6. Its children are 22 and 30. If we look at the relationship between the array indices of these values, for index i the children are 2 * i + 1 and 2 * i + 2. Similarly, the element at index 0 is the parent of these two children at indices 1 and 2. More generally, we can find the parent of a node at any index with the following: Math.floor( (i - 1) / 2 ). These patterns will hold true as the binary tree grows to any size. Finally, we can make a slight adjustment to make this arithmetic even easier by skipping the first element in the array. Doing this creates the following relationship for any element at a given index i: The root node is the first element, <code>6</code>. Its children are <code>22</code> and <code>30</code>. If we look at the relationship between the array indices of these values, for index <code>i</code> the children are <code>2 * i + 1</code> and <code>2 * i + 2</code>. Similarly, the element at index <code>0</code> is the parent of these two children at indices <code>1</code> and <code>2</code>. More generally, we can find the parent of a node at any index with the following: <code>Math.floor((i - 1) / 2)</code>. These patterns will hold true as the binary tree grows to any size. Finally, we can make a slight adjustment to make this arithmetic even easier by skipping the first element in the array. Doing this creates the following relationship for any element at a given index <code>i</code>:
Example Array representation: Example array representation:
<code>[ null, 6, 22, 30, 37, 63, 48, 42, 76 ]</code> <code>[ null, 6, 22, 30, 37, 63, 48, 42, 76 ]</code>
An element's left child: i * 2 An element's left child: <code>i * 2</code>
An element's right child: i * 2 + 1 An element's right child: <code>i * 2 + 1</code>
An element's parent: Math.floor( i / 2 ) An element's parent: <code>Math.floor(i / 2)</code>
Once you wrap your head around the math, using an array representation is very useful because node locations can be quickly determined with this arithmetic and memory usage is diminished because you don't need to maintain references to child nodes. Once you wrap your head around the math, using an array representation is very useful because node locations can be quickly determined with this arithmetic and memory usage is diminished because you don't need to maintain references to child nodes.
</section> </section>
## Instructions ## Instructions
<section id='instructions'> <section id='instructions'>
Instructions: Here we will create a max heap. Start by just creating an insert method which adds elements to our heap. During insertion, it is important to always maintain the heap property. For a max heap this means the root element should always have the greatest value in the tree and all parent nodes should be greater than their children. For an array implementation of a heap, this is typically accomplished in three steps: Instructions: Here we will create a max heap. Start by just creating an <code>insert</code> method which adds elements to our heap. During insertion, it is important to always maintain the heap property. For a max heap this means the root element should always have the greatest value in the tree and all parent nodes should be greater than their children. For an array implementation of a heap, this is typically accomplished in three steps:
Add the new element to the end of the array. <ol>
If the element is larger than its parents, switch them. <li>Add the new element to the end of the array.</li>
Continue switching until the new element is either smaller than its parent or you reach the root of the tree. <li>If the element is larger than its parents, switch them.</li>
Finally, add a print method which returns an array of all the items that have been added to the heap. <li>Continue switching until the new element is either smaller than its parent or you reach the root of the tree.</li>
</ol>
Finally, add a <code>print</code> method which returns an array of all the items that have been added to the heap.
</section> </section>
## Tests ## Tests

View File

@ -9,15 +9,16 @@ forumTopicId: 301710
## Description ## Description
<section id='description'> <section id='description'>
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: 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:
Move the last element in the heap into the root position. <ol>
If either child of the root is greater than it, swap the root with the child of greater value. <li>Move the last element in the heap into the root position.</li>
Continue swapping until the parent is greater than both children, or you reach the last level in the tree. <li>If either child of the root is greater than it, swap the root with the child of greater value.</li>
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. Add your insert method again here as well. <li>Continue swapping until the parent is greater than both children or you reach the last level in the tree.</li>
</ol>
</section> </section>
## Instructions ## Instructions
<section id='instructions'> <section id='instructions'>
Instructions: Add a method to our max heap called <code>remove</code>. 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. Add your <code>insert</code> method again here as well.
</section> </section>
## Tests ## Tests