Skip to content

Commit

Permalink
Added description for the NewSegmentTree function (#691)
Browse files Browse the repository at this point in the history
* Added description for the NewSegmentTree function

* Fixed spelling errors and formatting issues

* modified comments to comply with go docs

* fixed spelling errors

* fixed grammatical errors in comments

---------

Co-authored-by: Rak Laptudirm <[email protected]>
  • Loading branch information
11-aryan and raklaptudirm authored Nov 11, 2023
1 parent 75c4951 commit 5f887c5
Showing 1 changed file with 20 additions and 19 deletions.
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
}

// 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

0 comments on commit 5f887c5

Please sign in to comment.