TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
probability::windowed_median::WindowedMedian Class Reference

A class to calculate the median of a leading sliding window at the back of a stream of integer values. More...

Collaboration diagram for probability::windowed_median::WindowedMedian:
[legend]

Public Member Functions

 WindowedMedian (size_type windowSize)
 Constructs a WindowedMedian object.
 
void insert (int value)
 Insert a new value to the stream.
 
float getMedian () const
 Gets the median of the values in the sliding window.
 
float getMedianNaive () const
 A naive and inefficient method to obtain the median of the sliding window. Used for testing!
 

Private Member Functions

void insertToSorted (int value)
 Inserts a value to a sorted multi-value BST.
 
void eraseFromSorted (int value)
 Erases a value from a sorted multi-value BST.
 

Private Attributes

const size_type _windowSize
 sliding window size
 
Window _window
 a sliding window of values along the stream
 
std::multiset< int > _sortedValues
 
std::multiset< int >::const_iterator _itMedian
 

Detailed Description

A class to calculate the median of a leading sliding window at the back of a stream of integer values.

Definition at line 56 of file windowed_median.cpp.

Constructor & Destructor Documentation

◆ WindowedMedian()

probability::windowed_median::WindowedMedian::WindowedMedian ( size_type windowSize)
inlineexplicit

Constructs a WindowedMedian object.

Parameters
windowSizeSliding window size

Definition at line 123 of file windowed_median.cpp.

123: _windowSize(windowSize){};
const size_type _windowSize
sliding window size

Member Function Documentation

◆ eraseFromSorted()

void probability::windowed_median::WindowedMedian::eraseFromSorted ( int value)
inlineprivate

Erases a value from a sorted multi-value BST.

Parameters
valueValue to insert

If the erased value is on the left branch or the median itself and the number of elements is even, the new median will be the right child of the current one

O(1) - traversing one step to the right child

However, if the erased value is on the right branch or the median itself, and the number of elements is odd, the new median will be the left child of the current one

Find the (first) position of the value we want to erase, and erase it

Definition at line 95 of file windowed_median.cpp.

95 {
96 const auto sz = _sortedValues.size();
97
101 if (value <= *_itMedian && sz % 2 == 0) {
102 ++_itMedian;
103 }
104
108 else if (value >= *_itMedian && sz % 2 != 0) {
109 --_itMedian; // O(1) - traversing one step to the left child
110 }
111
114 const auto it = _sortedValues.find(value); // O(logN)
115 _sortedValues.erase(it); // O(logN)
116 }
std::multiset< int >::const_iterator _itMedian

◆ getMedian()

float probability::windowed_median::WindowedMedian::getMedian ( ) const
inline

Gets the median of the values in the sliding window.

Returns
Median of sliding window. For even window size return the average between the two values in the middle

O(1)

Definition at line 148 of file windowed_median.cpp.

148 {
149 if (_sortedValues.size() % 2 != 0) {
150 return *_itMedian; // O(1)
151 }
152 return 0.5f * *_itMedian + 0.5f * *next(_itMedian);
153 }

◆ getMedianNaive()

float probability::windowed_median::WindowedMedian::getMedianNaive ( ) const
inline

A naive and inefficient method to obtain the median of the sliding window. Used for testing!

Returns
Median of sliding window. For even window size return the average between the two values in the middle

Sort window - O(NlogN)

Find value in the middle - O(N)

O(N)

Definition at line 161 of file windowed_median.cpp.

161 {
162 auto window = _window;
163 window.sort();
164 auto median =
165 *next(window.begin(),
166 window.size() / 2);
167 if (window.size() % 2 != 0) {
168 return median;
169 }
170 return 0.5f * median +
171 0.5f * *next(window.begin(), window.size() / 2 - 1);
172 }
Window _window
a sliding window of values along the stream

◆ insert()

void probability::windowed_median::WindowedMedian::insert ( int value)
inline

Insert a new value to the stream.

Parameters
valueNew value to insert

Push new value to the back of the sliding window - O(1)

If exceeding size of window, pop from its left side

Erase from the multi-value BST the window left side value

Pop the left side value from the window - O(1)

Definition at line 129 of file windowed_median.cpp.

129 {
131 _window.push_back(value);
132 insertToSorted(value); // Insert value to the multi-value BST - O(logN)
133 if (_window.size() > _windowSize) {
136 _window.front());
138 _window.pop_front();
140 }
141 }
void insertToSorted(int value)
Inserts a value to a sorted multi-value BST.
void eraseFromSorted(int value)
Erases a value from a sorted multi-value BST.

◆ insertToSorted()

void probability::windowed_median::WindowedMedian::insertToSorted ( int value)
inlineprivate

Inserts a value to a sorted multi-value BST.

Parameters
valueValue to insert

Insert value to BST - O(logN)

For the first value, set median iterator to BST root

If new value goes to left tree branch, and number of elements is even, the new median in the balanced tree is the left child of the median before the insertion

However, if the new value goes to the right branch, the previous median's right child is the new median in the balanced tree

O(1) - traversing one step to the right child

Definition at line 69 of file windowed_median.cpp.

69 {
70 _sortedValues.insert(value);
71 const auto sz = _sortedValues.size();
72 if (sz == 1) {
73 _itMedian = _sortedValues.begin();
74 return;
75 }
76
80 if (value < *_itMedian && sz % 2 == 0) {
81 --_itMedian; // O(1) - traversing one step to the left child
82 }
83
86 else if (value >= *_itMedian && sz % 2 != 0) {
87 ++_itMedian;
88 }
89 }

Member Data Documentation

◆ _itMedian

std::multiset<int>::const_iterator probability::windowed_median::WindowedMedian::_itMedian
private

an iterator that points to the root of the multi-value BST

Definition at line 62 of file windowed_median.cpp.

◆ _sortedValues

std::multiset<int> probability::windowed_median::WindowedMedian::_sortedValues
private

a DS to represent a balanced multi-value binary search tree (BST)

Definition at line 59 of file windowed_median.cpp.

◆ _window

Window probability::windowed_median::WindowedMedian::_window
private

a sliding window of values along the stream

Definition at line 58 of file windowed_median.cpp.

◆ _windowSize

const size_type probability::windowed_median::WindowedMedian::_windowSize
private

sliding window size

Definition at line 57 of file windowed_median.cpp.


The documentation for this class was generated from the following file: