Skip to content

Commit b0d1bd7

Browse files
committed
updated heaps guide
1 parent 94cd2e9 commit b0d1bd7

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

heaps/heaps.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
# Heaps
2-
31
## Introduction
42
Heaps are an often overlooked data structure, but come up quite often in interview problems. **Heaps** are special tree based data structures that satisfy two properties:
53

64
1. All nodes are ordered in a specific way, depending on the type of heap. There are two types of heaps: min heaps and max heaps.
75
* In **min heaps**, the root node contains the smallest element and all the nodes in the heap contain elements that are less than their child nodes.
86
* In **max heaps**, the root node contains the largest element and all the nodes in the heap contain elements that are greater than their child nodes.
97

10-
2. It is a complete binary tree. A **binary treeâ™s** nodes will have at most two children: a left child, and right child. A heap is a complete binary tree, which means that it fills each level entirely except the last level. Another way of thinking about this is that all the nodes in one level will have children before any of those nodes will have grandchildren.
8+
2. It is a complete binary tree. A **binary tree's** nodes will have at most two children: a left child, and right child. A heap is a complete binary tree, which means that it fills each level entirely except the last level. Another way of thinking about this is that all the nodes in one level will have children before any of those nodes will have grandchildren.
119

12-
[Insert diagram here of min heap/ max heap]
10+
<img src="https://i.imgur.com/1mghTRv.png"/>
1311

1412
## Heap Operations
1513
In order to understand the runtimes of heap operations, it is vital to understand how insertion and deletion work within a heap.
@@ -19,14 +17,14 @@ When a new element is inserted into a heap, it is added in the next empty spot i
1917

2018
In a min heap, if the parent of the new element is greater than it, it gets swapped with the parent. This element keeps getting **bubbled up** in the tree until it either reaches the root of the heap or it has been placed in the right order. This same process applies to max heaps as well, but the check to ensure that the node is in the proper position is that the parent node is greater than the new node.
2119

22-
[Diagram of a swapping procedure]
20+
<img src="https://i.imgur.com/kx7DM60.png" width="700" height="681"/>
2321

2422
### Removal
2523
When removing from a heap, the root node is always removed. Then, the last element, the leftmost node in the last level of the heap, is removed and set as the root. This removal process retains the heap shape, but this new ordering may violate the proper ordering of the heap.
2624

2725
In a min heap, if either one of the new element's children are less than their parent, the new element is swapped with the smaller of the two children. This element keeps getting **bubbled down** in the tree until it either reaches the last level of the heap or it has been placed in the right position. The same process applies to max heaps as well, but the ordering is such that the children are both greater than the current node.
2826

29-
[Diagram of a swapping procedure]
27+
<img src="https://i.imgur.com/FGNU5Ks.png" width="700" height="681"/>
3028

3129
### Building a heap from a list
3230
One approach to building a heap from a list of N elements is starting with an empty heap and adding each item from a list, one at a time. This approach takes O(N log N) time because it performs N insertions, each of which takes log N time. However, this approach is suboptimal and the optimal approach of building a heap from N items only takes O(N) time!
@@ -41,14 +39,13 @@ With the guarantee of fullness and the binary tree property of the heap, we can
4139
* Left child: (current index * 2) + 1
4240
* Right child: (current index * 2) + 2
4341

44-
[insert diagram here]
42+
<img src="https://i.imgur.com/VHtiUsL.png" width="700" height="500"/>
4543

4644
These calculations enable it to easily implement the insertion and removal procedures within the array.
4745

4846
## Runtimes
4947
In the worst case scenario, the swapping procedure for insertions and deletions will move the element through the height of the heap. Because heaps are binary trees that are guaranteed to be as complete as possible, the number of levels in the heap will be log n.
5048

51-
5249
| Operation | Runtime |
5350
| ------------------------------------| -------- |
5451
| Reading largest or smallest element | O(1) |
@@ -57,7 +54,7 @@ In the worst case scenario, the swapping procedure for insertions and deletions
5754
| Creating a heap from a list | O(n) |
5855

5956
## Key takeaways
60-
* Heaps are especially useful when for getting the largest or smallest elements, and in situations where you donâ™t care about fast lookup, delete, or search.
57+
* Heaps are especially useful when for getting the largest or smallest elements, and in situations where you don't care about fast lookup, delete, or search.
6158
* Heaps are especially useful for questions that involve getting the x-largest or x-smallest elements of some data set.
6259
* Building a heap only takes O(n) time, so you can potentially optimize a solution by building a heap from a list instead of running insertion n times to create the heap.
6360

0 commit comments

Comments
 (0)