Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added description for the NewSegmentTree function #691

Merged
Merged
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
39 changes: 20 additions & 19 deletions structure/segmenttree/segmenttree.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//Segment Tree Data Structure for Range Queries
//Build: O(n*log(n))
//Query: O(log(n))
//Update: O(log(n))
//reference: https://cp-algorithms.com/data_structures/segment_tree.html

// Segment Tree Data Structure for efficient range queries on an array of integers.
// It can query the sum and update the elements to a new value of any range of the array.
// Build: O(n*log(n))
// Query: O(log(n))
// Update: O(log(n))
// reference: https://cp-algorithms.com/data_structures/segment_tree.html
package segmenttree

import (
Expand All @@ -13,14 +13,14 @@ import (

const emptyLazyNode = 0

// SegmentTree with original Array and the Segment Tree Array
// SegmentTree represents the data structure of a segment tree with lazy propagation
type SegmentTree struct {
Array []int
SegmentTree []int
LazyTree []int
Array []int // The original array
SegmentTree []int // Stores the sum of different ranges
LazyTree []int // Stores the values of lazy propagation
raklaptudirm marked this conversation as resolved.
Show resolved Hide resolved
}

// Propagate lazy tree node values
// Propagate propagates the lazy updates to the child nodes
func (s *SegmentTree) Propagate(node int, leftNode int, rightNode int) {
if s.LazyTree[node] != emptyLazyNode {
//add lazy node value multiplied by (right-left+1), which represents all interval
Expand All @@ -42,8 +42,8 @@ func (s *SegmentTree) Propagate(node int, leftNode int, rightNode int) {
}
}

// Query on interval [firstIndex, leftIndex]
// node, leftNode and rightNode always should start with 1, 0 and len(Array)-1
// Query returns the sum of elements of the array in the interval [firstIndex, leftIndex].
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Query(node int, leftNode int, rightNode int, firstIndex int, lastIndex int) int {
if (firstIndex > lastIndex) || (leftNode > rightNode) {
//outside the interval
Expand All @@ -67,10 +67,9 @@ func (s *SegmentTree) Query(node int, leftNode int, rightNode int, firstIndex in
return leftNodeSum + rightNodeSum
}

// Update Segment Tree
// node, leftNode and rightNode always should start with 1, 0 and len(Array)-1
// index is the Array index that you want to update
// value is the value that you want to override
// Update updates the elements of the array in the range [firstIndex, lastIndex]
// with the new value provided and recomputes the sum of different ranges.
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Update(node int, leftNode int, rightNode int, firstIndex int, lastIndex int, value int) {
//propagate lazy tree
s.Propagate(node, leftNode, rightNode)
Expand All @@ -96,8 +95,8 @@ func (s *SegmentTree) Update(node int, leftNode int, rightNode int, firstIndex i
}
}

// Build Segment Tree
// node, leftNode and rightNode always should start with 1, 0 and len(Array)-1
// Build builds the SegmentTree by computing the sum of different ranges.
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Build(node int, left int, right int) {
if left == right {
//leaf node
Expand All @@ -113,6 +112,8 @@ func (s *SegmentTree) Build(node int, left int, right int) {
}
}

// NewSegmentTree returns a new instance of a SegmentTree. It takes an input
// array of integers representing Array, initializes and builds the SegmentTree.
func NewSegmentTree(Array []int) *SegmentTree {
if len(Array) == 0 {
return nil
Expand Down