Skip to content

Commit 1650272

Browse files
committed
adding fix #1342 fixing docs for FenWickTree and DepthFirstSearch
1 parent bd34e9f commit 1650272

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

Trees/DepthFirstSearch.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* DFS Algorithm for traversing or searching graph data structures.
55
*/
66

7-
// traverses a give tree from specified root's value
7+
/**
8+
* Traverses a given tree using Depth-First Search (DFS) algorithm from the specified root's value.
9+
* @param {Array} tree The tree data structure represented as an array of nodes.
10+
* @param {number} rootValue The value of the root node from which traversal starts.
11+
* @returns {Array} An array containing the values of nodes traversed in DFS order.
12+
*/
813
function traverseDFS(tree, rootValue) {
914
const stack = []
1015
const res = []
@@ -23,7 +28,12 @@ function traverseDFS(tree, rootValue) {
2328
}
2429
return res.reverse()
2530
}
26-
31+
/**
32+
* Searches for a node with the specified value in the given tree using Depth-First Search (DFS) algorithm.
33+
* @param {Array} tree The tree data structure represented as an array of nodes.
34+
* @param {number} value The value to search for in the tree nodes.
35+
* @returns {Object|null} The node object if found, or null if the value is not found in the tree.
36+
*/
2737
function searchDFS(tree, value) {
2838
const stack = []
2939
stack.push(tree[0])

Trees/FenwickTree.js

+37-24
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,45 @@
55
*/
66

77
class FenwickTree {
8-
constructor(feneickArray, array, n) {
9-
for (let i = 1; i <= n; i++) {
10-
feneickArray[i] = 0
11-
}
12-
for (let i = 0; i < n; i++) {
13-
this.update(feneickArray, n, i, array[i])
14-
}
15-
}
8+
/**
9+
* Constructs a Fenwick Tree.
10+
* @param {Array} fenwickArray The Fenwick Tree array to be initialized.
11+
* @param {Array} array The input array whose prefix sum is to be calculated.
12+
* @param {number} n The size of the input array.
13+
*/
14+
constructor(feneickArray, array, n) {
15+
for (let i = 1; i <= n; i++) {
16+
feneickArray[i] = 0
17+
}
18+
for (let i = 0; i < n; i++) {
19+
this.update(feneickArray, n, i, array[i])
20+
}
21+
}
1622

17-
update(feneickArray, n, index, value) {
18-
index = index + 1
19-
while (index <= n) {
20-
feneickArray[index] += value
21-
index += index & -index
22-
}
23-
}
23+
/**
24+
* Updates the Fenwick Tree with a new value at the specified index.
25+
* @param {Array} fenwickArray The Fenwick Tree array.
26+
* @param {number} n The size of the Fenwick Tree array.
27+
* @param {number} index The index at which the value is updated.
28+
* @param {number} value The new value to be added at the index.
29+
*/
30+
update(feneickArray, n, index, value) {
31+
index = index + 1
32+
while (index <= n) {
33+
feneickArray[index] += value
34+
index += index & -index
35+
}
36+
}
2437

25-
getPrefixSum(feneickArray, index) {
26-
let currSum = 0
27-
index = index + 1
28-
while (index > 0) {
29-
currSum += feneickArray[index]
30-
index -= index & -index
31-
}
38+
getPrefixSum(feneickArray, index) {
39+
let currSum = 0
40+
index = index + 1
41+
while (index > 0) {
42+
currSum += feneickArray[index]
43+
index -= index & -index
44+
}
3245

33-
return currSum
34-
}
46+
return currSum
47+
}
3548
}
3649
export { FenwickTree }

0 commit comments

Comments
 (0)