You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: heaps/heaps.md
+6-9Lines changed: 6 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,13 @@
1
-
# Heaps
2
-
3
1
## Introduction
4
2
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:
5
3
6
4
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.
7
5
* 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.
8
6
* 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.
9
7
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.
11
9
12
-
[Insert diagram here of min heap/ max heap]
10
+
<imgsrc="https://i.imgur.com/1mghTRv.png"/>
13
11
14
12
## Heap Operations
15
13
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
19
17
20
18
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.
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.
26
24
27
25
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.
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
These calculations enable it to easily implement the insertion and removal procedures within the array.
47
45
48
46
## Runtimes
49
47
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.
@@ -57,7 +54,7 @@ In the worst case scenario, the swapping procedure for insertions and deletions
57
54
| Creating a heap from a list | O(n) |
58
55
59
56
## 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.
61
58
* Heaps are especially useful for questions that involve getting the x-largest or x-smallest elements of some data set.
62
59
* 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.
0 commit comments