Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6fe21d2

Browse files
authoredApr 13, 2024
chore: convert functions to an ES2015 classes (TheAlgorithms#1656)
* chore: convert functions to an ES2015 classes * remove unnecessary functions
1 parent 314144f commit 6fe21d2

File tree

8 files changed

+384
-378
lines changed

8 files changed

+384
-378
lines changed
 

‎Conversions/RailwayTimeConversion.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const RailwayTimeConversion = (timeString) => {
2424
const [hour, minute, secondWithShift] = timeString.split(':')
2525
// split second and shift value.
2626
const [second, shift] = [
27-
secondWithShift.substr(0, 2),
28-
secondWithShift.substr(2)
27+
secondWithShift.substring(0, 2),
28+
secondWithShift.substring(2)
2929
]
3030
// convert shifted time to not-shift time(Railway time) by using the above explanation.
3131
if (shift === 'PM') {

‎Data-Structures/Array/Reverse.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
const Reverse = (arr) => {
99
// limit specifies the amount of Reverse actions
10-
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--) {
11-
const temp = arr[i]
12-
arr[i] = arr[j]
13-
arr[j] = temp
14-
}
10+
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--)
11+
[arr[i], arr[j]] = [arr[j], arr[i]]
12+
1513
return arr
1614
}
1715
export { Reverse }

‎Data-Structures/Stack/Stack.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
// Functions: push, pop, peek, view, length
99

1010
// Creates a stack constructor
11-
const Stack = (function () {
12-
function Stack() {
11+
class Stack {
12+
constructor() {
1313
// The top of the Stack
1414
this.top = 0
1515
// The array representation of the stack
1616
this.stack = []
1717
}
1818

1919
// Adds a value onto the end of the stack
20-
Stack.prototype.push = function (value) {
20+
push(value) {
2121
this.stack[this.top] = value
2222
this.top++
2323
}
2424

2525
// Removes and returns the value at the end of the stack
26-
Stack.prototype.pop = function () {
26+
pop() {
2727
if (this.top === 0) {
2828
return 'Stack is Empty'
2929
}
@@ -35,23 +35,21 @@ const Stack = (function () {
3535
}
3636

3737
// Returns the size of the stack
38-
Stack.prototype.size = function () {
38+
size() {
3939
return this.top
4040
}
4141

4242
// Returns the value at the end of the stack
43-
Stack.prototype.peek = function () {
43+
peek() {
4444
return this.stack[this.top - 1]
4545
}
4646

4747
// To see all the elements in the stack
48-
Stack.prototype.view = function (output = (value) => console.log(value)) {
48+
view(output = (value) => console.log(value)) {
4949
for (let i = 0; i < this.top; i++) {
5050
output(this.stack[i])
5151
}
5252
}
53-
54-
return Stack
55-
})()
53+
}
5654

5755
export { Stack }

‎Data-Structures/Tree/AVLTree.js

Lines changed: 166 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ let utils
3131
* @argument comp - A function used by AVL Tree For Comparison
3232
* If no argument is sent it uses utils.comparator
3333
*/
34-
const AVLTree = (function () {
35-
function _avl(comp) {
34+
class AVLTree {
35+
constructor(comp) {
3636
/** @public comparator function */
3737
this._comp = undefined
3838
this._comp = comp !== undefined ? comp : utils.comparator()
@@ -43,162 +43,6 @@ const AVLTree = (function () {
4343
this.size = 0
4444
}
4545

46-
// creates new Node Object
47-
const Node = function (val) {
48-
this._val = val
49-
this._left = null
50-
this._right = null
51-
this._height = 1
52-
}
53-
54-
// get height of a node
55-
const getHeight = function (node) {
56-
if (node == null) {
57-
return 0
58-
}
59-
return node._height
60-
}
61-
62-
// height difference or balance factor of a node
63-
const getHeightDifference = function (node) {
64-
return node == null ? 0 : getHeight(node._left) - getHeight(node._right)
65-
}
66-
67-
// update height of a node based on children's heights
68-
const updateHeight = function (node) {
69-
if (node == null) {
70-
return
71-
}
72-
node._height = Math.max(getHeight(node._left), getHeight(node._right)) + 1
73-
}
74-
75-
// Helper: To check if the balanceFactor is valid
76-
const isValidBalanceFactor = (balanceFactor) =>
77-
[0, 1, -1].includes(balanceFactor)
78-
79-
// rotations of AVL Tree
80-
const leftRotate = function (node) {
81-
const temp = node._right
82-
node._right = temp._left
83-
temp._left = node
84-
updateHeight(node)
85-
updateHeight(temp)
86-
return temp
87-
}
88-
const rightRotate = function (node) {
89-
const temp = node._left
90-
node._left = temp._right
91-
temp._right = node
92-
updateHeight(node)
93-
updateHeight(temp)
94-
return temp
95-
}
96-
97-
// check if tree is balanced else balance it for insertion
98-
const insertBalance = function (node, _val, balanceFactor, tree) {
99-
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
100-
return rightRotate(node) // Left Left Case
101-
}
102-
if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) {
103-
return leftRotate(node) // Right Right Case
104-
}
105-
if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) {
106-
node._left = leftRotate(node._left) // Left Right Case
107-
return rightRotate(node)
108-
}
109-
node._right = rightRotate(node._right)
110-
return leftRotate(node)
111-
}
112-
113-
// check if tree is balanced after deletion
114-
const delBalance = function (node) {
115-
const balanceFactor1 = getHeightDifference(node)
116-
if (isValidBalanceFactor(balanceFactor1)) {
117-
return node
118-
}
119-
if (balanceFactor1 > 1) {
120-
if (getHeightDifference(node._left) >= 0) {
121-
return rightRotate(node) // Left Left
122-
}
123-
node._left = leftRotate(node._left)
124-
return rightRotate(node) // Left Right
125-
}
126-
if (getHeightDifference(node._right) > 0) {
127-
node._right = rightRotate(node._right)
128-
return leftRotate(node) // Right Left
129-
}
130-
return leftRotate(node) // Right Right
131-
}
132-
133-
// implement avl tree insertion
134-
const insert = function (root, val, tree) {
135-
if (root == null) {
136-
tree.size++
137-
return new Node(val)
138-
}
139-
if (tree._comp(root._val, val) < 0) {
140-
root._right = insert(root._right, val, tree)
141-
} else if (tree._comp(root._val, val) > 0) {
142-
root._left = insert(root._left, val, tree)
143-
} else {
144-
return root
145-
}
146-
updateHeight(root)
147-
const balanceFactor = getHeightDifference(root)
148-
return isValidBalanceFactor(balanceFactor)
149-
? root
150-
: insertBalance(root, val, balanceFactor, tree)
151-
}
152-
153-
// delete am element
154-
const deleteElement = function (root, _val, tree) {
155-
if (root == null) {
156-
return root
157-
}
158-
if (tree._comp(root._val, _val) === 0) {
159-
// key found case
160-
if (root._left === null && root._right === null) {
161-
root = null
162-
tree.size--
163-
} else if (root._left === null) {
164-
root = root._right
165-
tree.size--
166-
} else if (root._right === null) {
167-
root = root._left
168-
tree.size--
169-
} else {
170-
let temp = root._right
171-
while (temp._left != null) {
172-
temp = temp._left
173-
}
174-
root._val = temp._val
175-
root._right = deleteElement(root._right, temp._val, tree)
176-
}
177-
} else {
178-
if (tree._comp(root._val, _val) < 0) {
179-
root._right = deleteElement(root._right, _val, tree)
180-
} else {
181-
root._left = deleteElement(root._left, _val, tree)
182-
}
183-
}
184-
updateHeight(root)
185-
root = delBalance(root)
186-
return root
187-
}
188-
// search tree for a element
189-
const searchAVLTree = function (root, val, tree) {
190-
if (root == null) {
191-
return null
192-
}
193-
if (tree._comp(root._val, val) === 0) {
194-
return root
195-
}
196-
if (tree._comp(root._val, val) < 0) {
197-
return searchAVLTree(root._right, val, tree)
198-
}
199-
return searchAVLTree(root._left, val, tree)
200-
}
201-
20246
/* Public Functions */
20347
/**
20448
* For Adding Elements to AVL Tree
@@ -207,34 +51,193 @@ const AVLTree = (function () {
20751
* if a element exists it return false
20852
* @returns {Boolean} element added or not
20953
*/
210-
_avl.prototype.add = function (_val) {
54+
add(_val) {
21155
const prevSize = this.size
21256
this.root = insert(this.root, _val, this)
21357
return this.size !== prevSize
21458
}
59+
21560
/**
21661
* TO check is a particular element exists or not
21762
* @param {any} _val
21863
* @returns {Boolean} exists or not
21964
*/
220-
_avl.prototype.find = function (_val) {
65+
find(_val) {
22166
const temp = searchAVLTree(this.root, _val, this)
22267
return temp != null
22368
}
69+
22470
/**
22571
*
22672
* @param {any} _val
22773
* It is possible that element doesn't exists in tree
22874
* in that case it return false
22975
* @returns {Boolean} if element was found and deleted
23076
*/
231-
_avl.prototype.remove = function (_val) {
77+
remove(_val) {
23278
const prevSize = this.size
23379
this.root = deleteElement(this.root, _val, this)
23480
return prevSize !== this.size
23581
}
236-
return _avl
237-
})()
82+
}
83+
84+
// creates new Node Object
85+
class Node {
86+
constructor(val) {
87+
this._val = val
88+
this._left = null
89+
this._right = null
90+
this._height = 1
91+
}
92+
}
93+
94+
// get height of a node
95+
const getHeight = function (node) {
96+
if (node == null) {
97+
return 0
98+
}
99+
return node._height
100+
}
101+
102+
// height difference or balance factor of a node
103+
const getHeightDifference = function (node) {
104+
return node == null ? 0 : getHeight(node._left) - getHeight(node._right)
105+
}
106+
107+
// update height of a node based on children's heights
108+
const updateHeight = function (node) {
109+
if (node == null) {
110+
return
111+
}
112+
node._height = Math.max(getHeight(node._left), getHeight(node._right)) + 1
113+
}
114+
115+
// Helper: To check if the balanceFactor is valid
116+
const isValidBalanceFactor = (balanceFactor) =>
117+
[0, 1, -1].includes(balanceFactor)
118+
119+
// rotations of AVL Tree
120+
const leftRotate = function (node) {
121+
const temp = node._right
122+
node._right = temp._left
123+
temp._left = node
124+
updateHeight(node)
125+
updateHeight(temp)
126+
return temp
127+
}
128+
const rightRotate = function (node) {
129+
const temp = node._left
130+
node._left = temp._right
131+
temp._right = node
132+
updateHeight(node)
133+
updateHeight(temp)
134+
return temp
135+
}
136+
137+
// check if tree is balanced else balance it for insertion
138+
const insertBalance = function (node, _val, balanceFactor, tree) {
139+
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
140+
return rightRotate(node) // Left Left Case
141+
}
142+
if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) {
143+
return leftRotate(node) // Right Right Case
144+
}
145+
if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) {
146+
node._left = leftRotate(node._left) // Left Right Case
147+
return rightRotate(node)
148+
}
149+
node._right = rightRotate(node._right)
150+
return leftRotate(node)
151+
}
152+
153+
// check if tree is balanced after deletion
154+
const delBalance = function (node) {
155+
const balanceFactor1 = getHeightDifference(node)
156+
if (isValidBalanceFactor(balanceFactor1)) {
157+
return node
158+
}
159+
if (balanceFactor1 > 1) {
160+
if (getHeightDifference(node._left) >= 0) {
161+
return rightRotate(node) // Left Left
162+
}
163+
node._left = leftRotate(node._left)
164+
return rightRotate(node) // Left Right
165+
}
166+
if (getHeightDifference(node._right) > 0) {
167+
node._right = rightRotate(node._right)
168+
return leftRotate(node) // Right Left
169+
}
170+
return leftRotate(node) // Right Right
171+
}
172+
173+
// implement avl tree insertion
174+
const insert = function (root, val, tree) {
175+
if (root == null) {
176+
tree.size++
177+
return new Node(val)
178+
}
179+
if (tree._comp(root._val, val) < 0) {
180+
root._right = insert(root._right, val, tree)
181+
} else if (tree._comp(root._val, val) > 0) {
182+
root._left = insert(root._left, val, tree)
183+
} else {
184+
return root
185+
}
186+
updateHeight(root)
187+
const balanceFactor = getHeightDifference(root)
188+
return isValidBalanceFactor(balanceFactor)
189+
? root
190+
: insertBalance(root, val, balanceFactor, tree)
191+
}
192+
193+
// delete am element
194+
const deleteElement = function (root, _val, tree) {
195+
if (root == null) {
196+
return root
197+
}
198+
if (tree._comp(root._val, _val) === 0) {
199+
// key found case
200+
if (root._left === null && root._right === null) {
201+
root = null
202+
tree.size--
203+
} else if (root._left === null) {
204+
root = root._right
205+
tree.size--
206+
} else if (root._right === null) {
207+
root = root._left
208+
tree.size--
209+
} else {
210+
let temp = root._right
211+
while (temp._left != null) {
212+
temp = temp._left
213+
}
214+
root._val = temp._val
215+
root._right = deleteElement(root._right, temp._val, tree)
216+
}
217+
} else {
218+
if (tree._comp(root._val, _val) < 0) {
219+
root._right = deleteElement(root._right, _val, tree)
220+
} else {
221+
root._left = deleteElement(root._left, _val, tree)
222+
}
223+
}
224+
updateHeight(root)
225+
root = delBalance(root)
226+
return root
227+
}
228+
// search tree for a element
229+
const searchAVLTree = function (root, val, tree) {
230+
if (root == null) {
231+
return null
232+
}
233+
if (tree._comp(root._val, val) === 0) {
234+
return root
235+
}
236+
if (tree._comp(root._val, val) < 0) {
237+
return searchAVLTree(root._right, val, tree)
238+
}
239+
return searchAVLTree(root._left, val, tree)
240+
}
238241

239242
/**
240243
* A Code for Testing the AVLTree

‎Data-Structures/Tree/BinarySearchTree.js

Lines changed: 94 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,79 @@
1313
// class Node
1414
const Node = (function Node() {
1515
// Node in the tree
16-
function Node(val) {
17-
this.value = val
18-
this.left = null
19-
this.right = null
20-
}
21-
22-
// Search the tree for a value
23-
Node.prototype.search = function (val) {
24-
if (this.value === val) {
25-
return this
26-
} else if (val < this.value && this.left !== null) {
27-
return this.left.search(val)
28-
} else if (val > this.value && this.right !== null) {
29-
return this.right.search(val)
16+
class Node {
17+
constructor(val) {
18+
this.value = val
19+
this.left = null
20+
this.right = null
3021
}
31-
return null
32-
}
3322

34-
// Visit a node
35-
Node.prototype.visit = function (output = (value) => console.log(value)) {
36-
// Recursively go left
37-
if (this.left !== null) {
38-
this.left.visit()
39-
}
40-
// Print out value
41-
output(this.value)
42-
// Recursively go right
43-
if (this.right !== null) {
44-
this.right.visit()
23+
// Search the tree for a value
24+
search(val) {
25+
if (this.value === val) {
26+
return this
27+
} else if (val < this.value && this.left !== null) {
28+
return this.left.search(val)
29+
} else if (val > this.value && this.right !== null) {
30+
return this.right.search(val)
31+
}
32+
return null
4533
}
46-
}
4734

48-
// Add a node
49-
Node.prototype.addNode = function (n) {
50-
if (n.value < this.value) {
51-
if (this.left === null) {
52-
this.left = n
53-
} else {
54-
this.left.addNode(n)
35+
// Visit a node
36+
visit(output = (value) => console.log(value)) {
37+
// Recursively go left
38+
if (this.left !== null) {
39+
this.left.visit()
5540
}
56-
} else if (n.value > this.value) {
57-
if (this.right === null) {
58-
this.right = n
59-
} else {
60-
this.right.addNode(n)
41+
// Print out value
42+
output(this.value)
43+
// Recursively go right
44+
if (this.right !== null) {
45+
this.right.visit()
6146
}
6247
}
63-
}
6448

65-
// remove a node
66-
Node.prototype.removeNode = function (val) {
67-
if (val === this.value) {
68-
if (!this.left && !this.right) {
69-
return null
70-
} else {
71-
if (this.left) {
72-
const leftMax = maxVal(this.left)
73-
this.value = leftMax
74-
this.left = this.left.removeNode(leftMax)
49+
// Add a node
50+
addNode(n) {
51+
if (n.value < this.value) {
52+
if (this.left === null) {
53+
this.left = n
7554
} else {
76-
const rightMin = minVal(this.right)
77-
this.value = rightMin
78-
this.right = this.right.removeNode(rightMin)
55+
this.left.addNode(n)
56+
}
57+
} else if (n.value > this.value) {
58+
if (this.right === null) {
59+
this.right = n
60+
} else {
61+
this.right.addNode(n)
7962
}
8063
}
81-
} else if (val < this.value) {
82-
this.left = this.left && this.left.removeNode(val)
83-
} else if (val > this.value) {
84-
this.right = this.right && this.right.removeNode(val)
8564
}
86-
return this
65+
66+
// remove a node
67+
removeNode(val) {
68+
if (val === this.value) {
69+
if (!this.left && !this.right) {
70+
return null
71+
} else {
72+
if (this.left) {
73+
const leftMax = maxVal(this.left)
74+
this.value = leftMax
75+
this.left = this.left.removeNode(leftMax)
76+
} else {
77+
const rightMin = minVal(this.right)
78+
this.value = rightMin
79+
this.right = this.right.removeNode(rightMin)
80+
}
81+
}
82+
} else if (val < this.value) {
83+
this.left = this.left && this.left.removeNode(val)
84+
} else if (val > this.value) {
85+
this.right = this.right && this.right.removeNode(val)
86+
}
87+
return this
88+
}
8789
}
8890

8991
// find maximum value in the tree
@@ -107,44 +109,46 @@ const Node = (function Node() {
107109

108110
// class Tree
109111
const Tree = (function () {
110-
function Tree() {
111-
// Just store the root
112-
this.root = null
113-
}
112+
class Tree {
113+
constructor() {
114+
// Just store the root
115+
this.root = null
116+
}
114117

115-
// Inorder traversal
116-
Tree.prototype.traverse = function () {
117-
if (!this.root) {
118-
// No nodes are there in the tree till now
119-
return
118+
// Inorder traversal
119+
traverse() {
120+
if (!this.root) {
121+
// No nodes are there in the tree till now
122+
return
123+
}
124+
this.root.visit()
120125
}
121-
this.root.visit()
122-
}
123126

124-
// Start by searching the root
125-
Tree.prototype.search = function (val) {
126-
const found = this.root.search(val)
127-
if (found !== null) {
128-
return found.value
127+
// Start by searching the root
128+
search(val) {
129+
const found = this.root.search(val)
130+
if (found !== null) {
131+
return found.value
132+
}
133+
// not found
134+
return null
129135
}
130-
// not found
131-
return null
132-
}
133136

134-
// Add a new value to the tree
135-
Tree.prototype.addValue = function (val) {
136-
const n = new Node(val)
137-
if (this.root === null) {
138-
this.root = n
139-
} else {
140-
this.root.addNode(n)
137+
// Add a new value to the tree
138+
addValue(val) {
139+
const n = new Node(val)
140+
if (this.root === null) {
141+
this.root = n
142+
} else {
143+
this.root.addNode(n)
144+
}
141145
}
142-
}
143146

144-
// remove a value from the tree
145-
Tree.prototype.removeValue = function (val) {
146-
// remove something if root exists
147-
this.root = this.root && this.root.removeNode(val)
147+
// remove a value from the tree
148+
removeValue(val) {
149+
// remove something if root exists
150+
this.root = this.root && this.root.removeNode(val)
151+
}
148152
}
149153

150154
// returns the constructor

‎Data-Structures/Tree/Trie.js

Lines changed: 109 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,132 @@
1-
const TrieNode = function TrieNode(key, parent) {
2-
this.key = key
3-
this.count = 0
4-
this.children = Object.create(null)
5-
if (parent === undefined) {
6-
this.parent = null
7-
} else {
8-
this.parent = parent
1+
class TrieNode {
2+
constructor(key, parent) {
3+
this.key = key
4+
this.count = 0
5+
this.children = Object.create(null)
6+
if (parent === undefined) {
7+
this.parent = null
8+
} else {
9+
this.parent = parent
10+
}
911
}
1012
}
1113

12-
function Trie() {
13-
// create only root with null key and parent
14-
this.root = new TrieNode(null, null)
15-
}
14+
class Trie {
15+
constructor() {
16+
// create only root with null key and parent
17+
this.root = new TrieNode(null, null)
18+
}
1619

17-
// Recursively finds the occurrence of all words in a given node
18-
Trie.findAllWords = function (root, word, output) {
19-
if (root === null) return
20-
if (root.count > 0) {
21-
if (typeof output === 'object') {
22-
output.push({ word, count: root.count })
20+
// Recursively finds the occurrence of all words in a given node
21+
static findAllWords(root, word, output) {
22+
if (root === null) return
23+
if (root.count > 0) {
24+
if (typeof output === 'object') {
25+
output.push({ word, count: root.count })
26+
}
27+
}
28+
let key
29+
for (key in root.children) {
30+
word += key
31+
this.findAllWords(root.children[key], word, output)
32+
word = word.slice(0, -1)
2333
}
2434
}
25-
let key
26-
for (key in root.children) {
27-
word += key
28-
this.findAllWords(root.children[key], word, output)
29-
word = word.slice(0, -1)
30-
}
31-
}
3235

33-
Trie.prototype.insert = function (word) {
34-
if (typeof word !== 'string') return
35-
if (word === '') {
36-
this.root.count += 1
37-
return
38-
}
39-
let node = this.root
40-
const len = word.length
41-
let i
42-
for (i = 0; i < len; i++) {
43-
if (node.children[word.charAt(i)] === undefined) {
44-
node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node)
36+
insert(word) {
37+
if (typeof word !== 'string') return
38+
if (word === '') {
39+
this.root.count += 1
40+
return
41+
}
42+
let node = this.root
43+
const len = word.length
44+
let i
45+
for (i = 0; i < len; i++) {
46+
if (node.children[word.charAt(i)] === undefined) {
47+
node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node)
48+
}
49+
node = node.children[word.charAt(i)]
4550
}
46-
node = node.children[word.charAt(i)]
51+
node.count += 1
4752
}
48-
node.count += 1
49-
}
5053

51-
Trie.prototype.findPrefix = function (word) {
52-
if (typeof word !== 'string') return null
53-
let node = this.root
54-
const len = word.length
55-
let i
56-
// After end of this loop node will be at desired prefix
57-
for (i = 0; i < len; i++) {
58-
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
59-
node = node.children[word.charAt(i)]
54+
findPrefix(word) {
55+
if (typeof word !== 'string') return null
56+
let node = this.root
57+
const len = word.length
58+
let i
59+
// After end of this loop node will be at desired prefix
60+
for (i = 0; i < len; i++) {
61+
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
62+
node = node.children[word.charAt(i)]
63+
}
64+
return node
6065
}
61-
return node
62-
}
6366

64-
Trie.prototype.remove = function (word, count) {
65-
if (typeof word !== 'string') return
66-
if (typeof count !== 'number') count = 1
67-
else if (count <= 0) return
67+
remove(word, count) {
68+
if (typeof word !== 'string') return
69+
if (typeof count !== 'number') count = 1
70+
else if (count <= 0) return
6871

69-
// for empty string just delete count of root
70-
if (word === '') {
71-
if (this.root.count >= count) this.root.count -= count
72-
else this.root.count = 0
73-
return
74-
}
72+
// for empty string just delete count of root
73+
if (word === '') {
74+
if (this.root.count >= count) this.root.count -= count
75+
else this.root.count = 0
76+
return
77+
}
7578

76-
let child = this.root
77-
const len = word.length
78-
let i, key
79-
// child: node which is to be deleted
80-
for (i = 0; i < len; i++) {
81-
key = word.charAt(i)
82-
if (child.children[key] === undefined) return
83-
child = child.children[key]
84-
}
79+
let child = this.root
80+
const len = word.length
81+
let i, key
82+
// child: node which is to be deleted
83+
for (i = 0; i < len; i++) {
84+
key = word.charAt(i)
85+
if (child.children[key] === undefined) return
86+
child = child.children[key]
87+
}
8588

86-
// Delete no of occurrences specified
87-
if (child.count >= count) child.count -= count
88-
else child.count = 0
89+
// Delete no of occurrences specified
90+
if (child.count >= count) child.count -= count
91+
else child.count = 0
8992

90-
// If some occurrences are left we don't delete it or else
91-
// if the object forms some other objects prefix we don't delete it
92-
// For checking an empty object
93-
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
94-
if (
95-
child.count <= 0 &&
96-
Object.keys(child.children).length &&
97-
child.children.constructor === Object
98-
) {
99-
child.parent.children[child.key] = undefined
93+
// If some occurrences are left we don't delete it or else
94+
// if the object forms some other objects prefix we don't delete it
95+
// For checking an empty object
96+
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
97+
if (
98+
child.count <= 0 &&
99+
Object.keys(child.children).length &&
100+
child.children.constructor === Object
101+
) {
102+
child.parent.children[child.key] = undefined
103+
}
100104
}
101-
}
102105

103-
Trie.prototype.findAllWords = function (prefix) {
104-
const output = []
105-
// find the node with provided prefix
106-
const node = this.findPrefix(prefix)
107-
// No such prefix exists
108-
if (node === null) return output
109-
Trie.findAllWords(node, prefix, output)
110-
return output
111-
}
112-
113-
Trie.prototype.contains = function (word) {
114-
// find the node with given prefix
115-
const node = this.findPrefix(word)
116-
// No such word exists
106+
findAllWords(prefix) {
107+
const output = []
108+
// find the node with provided prefix
109+
const node = this.findPrefix(prefix)
110+
// No such prefix exists
111+
if (node === null) return output
112+
Trie.findAllWords(node, prefix, output)
113+
return output
114+
}
117115

118-
return node !== null && node.count !== 0
119-
}
116+
contains(word) {
117+
// find the node with given prefix
118+
const node = this.findPrefix(word)
119+
// No such word exists
120+
return node !== null && node.count !== 0
121+
}
120122

121-
Trie.prototype.findOccurrences = function (word) {
122-
// find the node with given prefix
123-
const node = this.findPrefix(word)
124-
// No such word exists
125-
if (node === null) return 0
126-
return node.count
123+
findOccurrences(word) {
124+
// find the node with given prefix
125+
const node = this.findPrefix(word)
126+
// No such word exists
127+
if (node === null) return 0
128+
return node.count
129+
}
127130
}
128131

129132
export { Trie }

‎Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ determine the total number of the subset with sum
44
equal to the given sum.
55
*/
66
/*
7-
Given solution is O(n*sum) Time complexity and O(sum) Space complexity
7+
Given solution is O(n*sum) Time complexity and O(sum) Space complexity
88
*/
99
function NumberOfSubsetSum(array, sum) {
1010
const dp = [] // create an dp array where dp[i] denote number of subset with sum equal to i

‎Project-Euler/Problem004.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://projecteuler.net/problem=4
22
/* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
3-
Find the largest palindrome made from the product of two 3-digit numbers.
3+
Find the largest palindrome made from the product of two 3-digit numbers.
44
*/
55
export const largestPalindromic = (digits) => {
66
let i

0 commit comments

Comments
 (0)
Please sign in to comment.