diff --git a/range_queries/persistent_segment_tree_with_lazy_propagation.cpp b/range_queries/persistent_segment_tree_with_lazy_propagation.cpp index 200a810f0..b81671e8a 100644 --- a/range_queries/persistent_segment_tree_with_lazy_propagation.cpp +++ b/range_queries/persistent_segment_tree_with_lazy_propagation.cpp @@ -1,29 +1,49 @@ +/** + * @file + * @brief [Persistent segment tree with range updates (lazy propagation)](https://en.wikipedia.org/wiki/Persistent_data_structure - https://www.geeksforgeeks.org/lazy-propagation-in-segment-tree/) + * + * @details + * A normal segment tree facilitates making point updates and range queries in logarithmic time. + * Lazy propagation preserves the logarithmic time with range updates. + * So, a segment tree with lazy propagation enables doing range updates and range queries in logarithmic time, but it doesn't save any information about itself before the last update. + * A persistent data structure always preserves the previous version of itself when it is modified. + * That is, a new version of the segment tree is generated after every update. + * It saves all previous versions of itself (before every update) to facilitate doing range queries in any version. + * More memory is used ,but the logarithmic time is preserved because the new version points to the same nodes, that the previous version points to, that are not affected by the update. + * That is, only the nodes that are affected by the update and their ancestors are copied. The rest is copied using lazy propagation in the next queries. + * Thus preserving the logarithmic time because the number of nodes copied after any update is logarithmic. + * + * @author [Magdy Sedra](https://github.com/MSedra) + */ #include #include -#include +#include /// to manage dynamic memory -// query -> range sum +/// Range query here is range sum ,but the code can be modified to make different queries like range max or min class perSegTree { private: class Node { public: - std::shared_ptr left; - std::shared_ptr right; - int64_t val, prop; - int version; + std::shared_ptr left; /// pointer to the left node + std::shared_ptr right; /// pointer to the right node + int64_t val, prop; /// val is the value of the node (here equals to the sum of the leaf nodes children of that node), prop is the value to be propagated/added to all the leaf nodes children of that node Node() { - val = prop = version = 0; + val = prop = 0; left = right = nullptr; } }; - int n; // number of elements in the segment tree - std::vector> ptrs; // ptrs[i] holds a root pointer to the segment tree after the ith update - std::vector vec; + int n; /// number of elements/leaf nodes in the segment tree + std::vector> ptrs; /// ptrs[i] holds a root pointer to the segment tree after the ith update. ptrs[0] holds a root pointer to the segment tree before any updates + std::vector vec; /// values of the leaf nodes that the segment tree will be constructed with + /** + * @brief Creating a new node with the same values of curr node + * @param curr node that would be copied + * @returns the new node + */ std::shared_ptr newKid(std::shared_ptr const &curr) { - //std::shared_ptr newNode(new Node()); auto newNode = std::make_shared(Node()); newNode->left = curr->left; newNode->right = curr->right; @@ -32,6 +52,13 @@ private: return newNode; } + /** + * @brief If there is some value to be propagated to the passed node, value is added to the node and the children of the node, if exist, are copied and the propagated value is also added to them + * @param i the left index of the range that the passed node holds its sum + * @param j the right index of the range that the passed node holds its sum + * @param curr pointer to the node to be propagated + * @returns void + */ void lazy(int i, int j, std::shared_ptr const &curr) { if (!curr->prop) { return; @@ -42,11 +69,16 @@ private: curr->right = newKid(curr->right); curr->left->prop += curr->prop; curr->right->prop += curr->prop; - curr->left->version = curr->right->version = curr->version; } curr->prop = 0; } + /** + * @brief Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum of the given range, set its pointers to the children, and set its value to the sum of the children's values + * @param i the left index of the range that the created node holds its sum + * @param j the right index of the range that the created node holds its sum + * @returns pointer to the newly created node + */ std::shared_ptr construct(int i, int j) { auto newNode = std::make_shared(Node()); if (i == j) { @@ -62,12 +94,21 @@ private: return newNode; } + /** + * @brief Doing range update, checking at every node if it has some value to be propagated. All nodes affected by the update are copied and propagation value is added to the leaf of them + * @param i the left index of the range that the passed node holds its sum + * @param j the right index of the range that the passed node holds its sum + * @param l the left index of the range to be updated + * @param r the right index of the range to be updated + * @param value the value to be added to every element whose index x satisfies l<=x<=r + * @param curr pointer to the current node, which has value = the sum of elements whose index x satisfies i<=x<=j + * @return + */ std::shared_ptr update(int i, int j, int l, int r, int value, std::shared_ptr const &curr) { lazy(i, j, curr); if (i >= l && j <= r) { std::shared_ptr newNode = newKid(curr); newNode->prop += value; - newNode->version = ptrs.size(); lazy(i, j, newNode); return newNode; } @@ -79,10 +120,18 @@ private: newNode->left = update(i, mid, l, r, value, curr->left); newNode->right = update(mid + 1, j, l, r, value, curr->right); newNode->val = newNode->left->val + newNode->right->val; - newNode->version = ptrs.size(); return newNode; } + /** + * @brief Querying the range from index l to index r, checking at every node if it has some value to be propagated. Current node's value is returned if its range is completely inside the wanted range, else 0 is returned + * @param i the left index of the range that the passed node holds its sum + * @param j the right index of the range that the passed node holds its sum + * @param l the left index of the range whose sum should be returned as a result + * @param r the right index of the range whose sum should be returned as a result + * @param curr pointer to the current node, which has value = the sum of elements whose index x satisfies i<=x<=j + * @return sum of elements whose index x satisfies l<=x<=r + */ int64_t query(int i, int j, int l, int r, std::shared_ptr const &curr) { lazy(i, j, curr); if (j < l || r < i) { @@ -94,12 +143,18 @@ private: int mid = i + (j - i) / 2; return query(i, mid, l, r, curr->left) + query(mid + 1, j, l, r, curr->right); } - +/** + * public methods that can be used directly from outside the class. They call the private functions that do all the work + */ public: perSegTree() { n = 0; } + /** + * @brief Constructing the segment tree with the values in the passed vector. Returned root pointer is pushed in the pointers vector to have access to the original version if the segment tree is updated + * @param vec vector whose values will be used to build the segment tree + */ void construct(const std::vector &vec) // the segment tree will be built from the values in "vec", "vec" is 0 indexed { if (vec.empty()) { @@ -111,25 +166,40 @@ public: ptrs.push_back(root); } + /** + * @brief Doing range update by passing the left and right indices of the range as well as the value to be added. + * @param l the left index of the range to be updated + * @param r the right index of the range to be updated + * @param value the value to be added to every element whose index x satisfies l<=x<=r + */ void update(int l, int r, int value) // all elements from index "l" to index "r" would by updated by "value", "l" and "r" are 0 indexed { - ptrs.push_back(update(0, n - 1, l, r, value, - ptrs[ptrs.size() - 1])); // saving the root pointer to the new segment tree + ptrs.push_back(update(0, n - 1, l, r, value,ptrs[ptrs.size() - 1])); // saving the root pointer to the new segment tree } + /** + * @brief Querying the range from index l to index r, getting the sum of the elements whose index x satisfies l<=x<=r + * @param l the left index of the range whose sum should be returned as a result + * @param r the right index of the range whose sum should be returned as a result + * @param version the version to query on. If equals to 0, the original segment tree will be queried + * @return sum of elements whose index x satisfies l<=x<=r + */ int64_t query(int l, int r, int version) // querying the range from "l" to "r" in a segment tree after "version" updates, "l" and "r" are 0 indexed { return query(0, n - 1, l, r, ptrs[version]); } + /** + * @brief Getting the number of versions after updates so far which is equal to the size of the pointers vector + * @return the number of versions + */ int size() // returns the number of segment trees (versions) , the number of updates done so far = returned value - 1 ,because one of the trees is the original segment tree { return ptrs.size(); } }; -int main() { - +void test() { std::vector arr = {-5, 2, 3, 11, -2, 7, 0, 1}; perSegTree tree; std::cout << "Elements before any updates are {"; @@ -165,5 +235,9 @@ int main() { std::cout << "Number of segment trees (versions) now = " << tree.size() << '\n'; std::cout << "Querying range sum on version 0 from index 3 to 5 = 11-2+7 = " << tree.query(3, 5, 0) << '\n'; std::cout << "Querying range sum on version 1 from index 3 to 5 = 4-9+0 = " << tree.query(3, 5, 1) << '\n'; +} + +int main() { + test(); return 0; } \ No newline at end of file