MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / NewSegmentTree

Function NewSegmentTree

structure/segmenttree/segmenttree.go:117–137  ·  view source on GitHub ↗

NewSegmentTree returns a new instance of a SegmentTree. It takes an input array of integers representing Array, initializes and builds the SegmentTree.

(Array []int)

Source from the content-addressed store, hash-verified

115// NewSegmentTree returns a new instance of a SegmentTree. It takes an input
116// array of integers representing Array, initializes and builds the SegmentTree.
117func NewSegmentTree(Array []int) *SegmentTree {
118 if len(Array) == 0 {
119 return nil
120 }
121
122 segTree := SegmentTree{
123 Array: Array,
124 SegmentTree: make([]int, 4*len(Array)),
125 LazyTree: make([]int, 4*len(Array)),
126 }
127
128 for i := range segTree.LazyTree {
129 //fill LazyTree with empty lazy nodes
130 segTree.LazyTree[i] = emptyLazyNode
131 }
132
133 //starts with node 1 and interval [0, len(arr)-1] inclusive
134 segTree.Build(1, 0, len(Array)-1)
135
136 return &segTree
137}

Callers 1

TestSegmentTreeFunction · 0.85

Calls 1

BuildMethod · 0.95

Tested by 1

TestSegmentTreeFunction · 0.68