Skip to content

Commit 434a22b

Browse files
author
Alexander Belov
committed
Fixes
1 parent 0e5ceb0 commit 434a22b

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

src/lib/segment-tree/segment-tree.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
export class SegmentTree {}
1+
import { isPowerOfTwo, nearestHighestPowerOfTwoResult } from '@algo-js/core';
2+
3+
export class SegmentTree {
4+
/**
5+
* Input array
6+
*
7+
* @type {number[]}
8+
* @private
9+
*/
10+
private input: number[] = [];
11+
12+
/**
13+
* Operation function
14+
*
15+
* @type {function}
16+
* @private
17+
*/
18+
private operation: () => {};
19+
20+
/**
21+
* Fallback value for non-intersect intervals
22+
*
23+
* @type {function}
24+
* @private
25+
*/
26+
private fallbackValue: number = 0;
27+
28+
/**
29+
* Tree instance
30+
*
31+
* @type {number[]}
32+
*/
33+
private tree: number[] = null;
34+
35+
/**
36+
* @param {number[]} input
37+
* @param {function} operation
38+
* @param {number} fallbackValue
39+
*/
40+
constructor(input: number[], operation: () => {}, fallbackValue: number = 0) {
41+
this.input = input;
42+
this.operation = operation;
43+
this.fallbackValue = fallbackValue;
44+
45+
this.tree = this.createTree();
46+
}
47+
48+
private createTree(): number[] {
49+
let segmentTreeArrayLength;
50+
const inputArrayLength = this.input.length;
51+
52+
let treeArrayLength = inputArrayLength;
53+
54+
if (!isPowerOfTwo(inputArrayLength)) {
55+
// If original array length is not a power of two then we need to find
56+
// next number that is a power of two and use it to calculate
57+
// tree array size. This is happens because we need to fill empty children
58+
// in perfect binary tree with nulls.And those nulls need extra space.
59+
treeArrayLength = nearestHighestPowerOfTwoResult(inputArrayLength + 1);
60+
}
61+
62+
segmentTreeArrayLength = 2 * treeArrayLength - 1;
63+
64+
return new Array(segmentTreeArrayLength).fill(null);
65+
}
66+
}

tslint.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@
99
/* tslint-immutable rules */
1010
// Recommended built-in rules
1111
"no-var-keyword": true,
12-
"no-parameter-reassignment": true,
12+
"no-parameter-reassignment": false,
1313
"typedef": [true, "call-signature"],
1414

1515
// Immutability rules
16-
"readonly-keyword": true,
16+
"readonly-keyword": false,
1717
"readonly-array": false,
1818
"no-let": false,
19-
"no-object-mutation": true,
19+
"no-object-mutation": false,
2020
"no-delete": false,
21-
"no-method-signature": true,
21+
"no-method-signature": false,
2222

2323
// Functional style rules
2424
"no-this": false,
2525
"no-class": false,
26-
"no-mixed-interface": true,
26+
"no-mixed-interface": false,
2727
"no-expression-statement": [
28-
true,
28+
false,
2929
{ "ignore-prefix": ["console.", "process.exit"] }
3030
],
31-
"no-if-statement": true,
31+
"no-if-statement": false,
3232
"no-bitwise": false,
3333
"no-unused-variable": false,
3434
"no-unused-expression": false,

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# yarn lockfile v1
33

44

5+
"@algo-js/core@^1.0.21":
6+
version "1.0.21"
7+
resolved "https://registry.yarnpkg.com/@algo-js/core/-/core-1.0.21.tgz#6aa375a0768ad6159be33e44b88644ee8cae1bab"
8+
integrity sha512-a2nv+LawY1hJHr+RhThVvjQfaeCen0oACvAM0EDzNuCTwgTYhwje9WvELmRUo8T61O+lXDIs0WXlV7TkmjM66g==
9+
510
"@ava/babel-plugin-throws-helper@^3.0.0":
611
version "3.0.0"
712
resolved "https://registry.yarnpkg.com/@ava/babel-plugin-throws-helper/-/babel-plugin-throws-helper-3.0.0.tgz#2c933ec22da0c4ce1fc5369f2b95452c70420586"

0 commit comments

Comments
 (0)