Skip to content

Commit ab57cf9

Browse files
committed
Revamp heap files
1 parent c712040 commit ab57cf9

File tree

3 files changed

+28
-186
lines changed

3 files changed

+28
-186
lines changed

src/main/java/dataStructures/heap/MaxHeap.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Map;
77

88
/**
9-
* Implementation of an array-based max heap structure derived from a binary heap.
9+
* Implementation of an (0-indexed) array-based max heap structure derived from a binary heap.
1010
*
1111
* Callable methods are:
1212
* size() - O(1)
@@ -20,14 +20,6 @@
2020
* heapify(T ...seq) - O(n)
2121
* toString()
2222
*
23-
* Notes:
24-
* 1. Array is essentially filled in level-order
25-
* 2. 0-indexed implementation
26-
* 3. Actually it suffices to compare index of left child of a node and size of heap to check if it's a leaf node
27-
* 4. Heapify deals with bubbling down all elements starting from the back,
28-
* what about bubbling-up all elements starting from the front instead?
29-
* No issue with correctness, problem lies with efficiency of operation.
30-
*
3123
* @param <T> generic type for objects to be stored and queried
3224
*/
3325
public class MaxHeap<T extends Comparable<T>> {
@@ -255,6 +247,7 @@ private void bubbleUp(int i) {
255247
* @return boolean value that determines is leaf or not
256248
*/
257249
private boolean isLeaf(int i) {
250+
// actually, suffice to compare index of left child of a node and size of heap
258251
return this.getRightIndex(i) >= this.size() && this.getLeftIndex(i) >= this.size();
259252
}
260253

src/main/java/dataStructures/heap/MinHeap.java

Lines changed: 0 additions & 169 deletions
This file was deleted.

src/main/java/dataStructures/heap/README.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Heap
22

3+
## Background
34
### Binary Heap
45
A binary heap is often used to introduce the concept of heaps. It is a tree-based data structure that satisfies the
56
following properties:
67
1. Complete binary tree - every level, except possibly the last, is completely filled
7-
2. Max (min) heap property - the value of every vertex in the binary tree is >= (<=) than that of its child vertices
8+
2. Max (min) heap property - the value of every vertex in the binary tree is >= (<=) than that of its child nodes
89

910
This makes it a powerful data structure that provides efficient access to the highest (or lowest) priority element,
1011
making it suitable as an underlying implementation of the ADT, priority queue.
@@ -28,16 +29,31 @@ heaps in Python and Java is primarily due to design choices and considerations o
2829
Further, this operation, without augmentation, would take O(n) due to having to search for the object to begin with
2930
(see under Notes).
3031

31-
Still, one can circumvent the lack of such operations by simply removing and re-inserting (still O(n)).
32+
One can circumvent the lack of such operations by simply removing and re-inserting (albeit, still O(n)).
3233

33-
This is worth a mention. In cases like Dijkstra where the concern is having to maintain V nodes in the priority queue
34-
by constantly updating rather than insertion of all edges, it is not really a big issue. After all, the log factor
35-
in the order of growth will turn log(E) = log(V^2) in the worst case of a complete graph, to 2log(V) = O(log(V)).
34+
**This is worth a mention:** <br>
35+
In cases like Dijkstra algorithm, there is no need to strictly maintain V nodes in the priority queue for O(nlogn).
36+
One can just insert all edges rather than constantly updating (hence, no need for updateKey operations). <br>
37+
After all, the log factor in the order of growth will turn log(E) = log(V^2) in the worst case of a complete graph,
38+
to 2log(V) = O(log(V)).
39+
40+
### Heapify - Choice between bubbleUp and bubbleDown
41+
Heapify deals with bubbling down (for max heap) all elements starting from the back. <br>
42+
What about bubbling-up all elements starting from the front instead? <br>
43+
**No issue with correctness, problem lies with efficiency of operation.**
44+
45+
The number of operations required for bubbleUp and bubbleDown (to maintain heap property), is proportional to the
46+
distance the node have to move. bubbleDown starts from the bottom level whereas bubbleUp starts from the top level.
47+
Only 1 node is at the top whereas (approx) half the nodes is at the bottom level. It therefore makes sense to use
48+
bubbleDown.
3649

3750
## Complexity Analysis
38-
Time: O(log(n)) in general for most native operations,
39-
except heapify (building a heap from a sequence of elements) that takes o(n) <br>
40-
Space: O(n) where n is the number of elements (whatever the structure, it must store at least n nodes) <br>
51+
**Time**: O(log(n)) in general for most native operations,
52+
except heapify (building a heap from a sequence of elements) that takes o(n)
53+
54+
**Space**: O(n)
55+
56+
where n is the number of elements (whatever the structure, it must store at least n nodes)
4157

4258
## Notes
4359
1. Heaps are often presented as max-heaps (eg. in textbooks), hence the implementation follows a max-heap structure
@@ -50,3 +66,5 @@ Space: O(n) where n is the number of elements (whatever the structure, it must s
5066
- The trade-off would be that the heap does not support insertion of duplicate objects else the Map would not work
5167
as intended.
5268
3. Rather than using Java arrays, where size must be declared upon initializing, we use list here in the implementation.
69+
4. [Good read](https://stackoverflow.com/questions/9755721/how-can-building-a-heap-be-on-time-complexity?) on the
70+
time complexity of heapify and making the correct choice between bubbleUp and bubbleDown.

0 commit comments

Comments
 (0)