Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
* [KthElementFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/KthElementFinder.java)
* [LeftistHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java)
* [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java)
* [MedianFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MedianFinder.java)
* [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java)
* [MinPriorityQueue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java)
* lists
Expand Down Expand Up @@ -851,6 +852,7 @@
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
* [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
* lists
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.thealgorithms.datastructures.heaps;

import java.util.PriorityQueue;

/**
* This class maintains the median of a dynamically changing data stream using
* two heaps: a max-heap and a min-heap. The max-heap stores the smaller half
* of the numbers, and the min-heap stores the larger half.
* This data structure ensures that retrieving the median is efficient.
*
* Time Complexity:
* - Adding a number: O(log n) due to heap insertion.
* - Finding the median: O(1).
*
* Space Complexity: O(n), where n is the total number of elements added.
*
* @author Hardvan
*/
public final class MedianFinder {
MedianFinder() {
}

private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
private PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);

/**
* Adds a new number to the data stream. The number is placed in the appropriate
* heap to maintain the balance between the two heaps.
*
* @param num the number to be added to the data stream
*/
public void addNum(int num) {
if (maxHeap.isEmpty() || num <= maxHeap.peek()) {
maxHeap.offer(num);
} else {
minHeap.offer(num);
}

if (maxHeap.size() > minHeap.size() + 1) {
minHeap.offer(maxHeap.poll());
} else if (minHeap.size() > maxHeap.size()) {
maxHeap.offer(minHeap.poll());
}
}

/**
* Finds the median of the numbers added so far. If the total number of elements
* is even, the median is the average of the two middle elements. If odd, the
* median is the middle element from the max-heap.
*
* @return the median of the numbers in the data stream
*/
public double findMedian() {
if (maxHeap.size() == minHeap.size()) {
return (maxHeap.peek() + minHeap.peek()) / 2.0;
}
return maxHeap.peek();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.thealgorithms.datastructures.heaps;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MedianFinderTest {
@Test
public void testMedianMaintenance() {
MedianFinder mf = new MedianFinder();
mf.addNum(1);
mf.addNum(2);
assertEquals(1.5, mf.findMedian());
mf.addNum(3);
assertEquals(2.0, mf.findMedian());
}
}