Skip to content

Commit cd57889

Browse files
committed
feat: Add comments of implementation details
1 parent 1a837f5 commit cd57889

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

Data-Structures/Tree/SegmentTree.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,78 @@ class SegmentTree {
1414
size
1515
tree
1616
constructor (arr) {
17+
// we define tree like this
18+
// tree[1] : root node of tree
19+
// tree[i] : i'th node
20+
// tree[i * 2] : i'th left child
21+
// tree[i * 2 + 1] : i'th right child
22+
// and we use bit, shift operation for index
1723
this.size = arr.length
1824
this.tree = new Array(2 * arr.length)
1925
this.tree.fill(0)
2026

2127
this.build(arr)
2228
}
2329

30+
// function to build the tree
2431
build (arr) {
2532
const { size, tree } = this
2633
// insert leaf nodes in tree
34+
// leaf nodes will start from index N
35+
// in this array and will go up to index (2 * N – 1)
2736
for (let i = 0; i < size; i++) {
2837
tree[size + i] = arr[i]
2938
}
3039

3140
// build the tree by calculating parents
41+
// tree's root node will contain all leaf node's sum
3242
for (let i = size - 1; i > 0; --i) {
33-
tree[i] = tree[i << 1] + tree[(i << 1) | 1]
43+
// current node's value is the sum of left child, right child
44+
// tree[i] = tree[i * 2] + tree[i * 2 + 1]
45+
tree[i] = tree[i * 2] + tree[i * 2 + 1]
3446
}
3547
}
3648

37-
update (p, value) {
49+
update (index, value) {
3850
const { size, tree } = this
3951

40-
// set value at position p
41-
tree[p + size] = value
42-
p += size
52+
// only update values in the parents of the given node being changed.
53+
// to get the parent move to parent node (index / 2)
54+
55+
// set value at position index
56+
index += size
57+
// tree[index] is leaf node and index's value of array
58+
tree[index] = value
4359

4460
// move upward and update parents
45-
for (let i = p; i > 1; i >>= 1) {
61+
for (let i = index; i > 1; i >>= 1) {
62+
// i ^ 1 turns (2 * i) to (2 * i + 1)
63+
// i ^ 1 is second child
4664
tree[i >> 1] = tree[i] + tree[i ^ 1]
4765
}
4866
}
4967

68+
// interval [L,R) with left index(L) included and right (R) excluded.
5069
query (left, right) {
5170
const { size, tree } = this
71+
// cause R is excluded, increase right for convenient
5272
right++
5373
let res = 0
5474

5575
// loop to find the sum in the range
5676
for (left += size, right += size; left < right; left >>= 1, right >>= 1) {
77+
// L is the left border of an query interval
78+
79+
// if L is odd it means that it is the right child of its parent and our interval includes only L and not the parent.
80+
// So we will simply include this node to sum and move to the parent of its next node by doing L = (L + 1) / 2.
81+
82+
// if L is even it is the left child of its parent
83+
// and the interval includes its parent also unless the right borders interfere.
5784
if ((left & 1) > 0) {
5885
res += tree[left++]
5986
}
6087

88+
// same in R (the right border of an query interval)
6189
if ((right & 1) > 0) {
6290
res += tree[--right]
6391
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy