chore: convert functions to an ES2015 classes (#1656)

* chore: convert functions to an ES2015 classes

* remove unnecessary functions
This commit is contained in:
Hasan Al-Kaf
2024-04-13 20:51:54 +03:00
committed by GitHub
parent 314144fae6
commit 6fe21d21e9
8 changed files with 392 additions and 386 deletions

View File

@ -24,8 +24,8 @@ const RailwayTimeConversion = (timeString) => {
const [hour, minute, secondWithShift] = timeString.split(':') const [hour, minute, secondWithShift] = timeString.split(':')
// split second and shift value. // split second and shift value.
const [second, shift] = [ const [second, shift] = [
secondWithShift.substr(0, 2), secondWithShift.substring(0, 2),
secondWithShift.substr(2) secondWithShift.substring(2)
] ]
// convert shifted time to not-shift time(Railway time) by using the above explanation. // convert shifted time to not-shift time(Railway time) by using the above explanation.
if (shift === 'PM') { if (shift === 'PM') {

View File

@ -7,11 +7,9 @@
const Reverse = (arr) => { const Reverse = (arr) => {
// limit specifies the amount of Reverse actions // limit specifies the amount of Reverse actions
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--) { for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--)
const temp = arr[i] [arr[i], arr[j]] = [arr[j], arr[i]]
arr[i] = arr[j]
arr[j] = temp
}
return arr return arr
} }
export { Reverse } export { Reverse }

View File

@ -8,8 +8,8 @@
// Functions: push, pop, peek, view, length // Functions: push, pop, peek, view, length
// Creates a stack constructor // Creates a stack constructor
const Stack = (function () { class Stack {
function Stack() { constructor() {
// The top of the Stack // The top of the Stack
this.top = 0 this.top = 0
// The array representation of the stack // The array representation of the stack
@ -17,13 +17,13 @@ const Stack = (function () {
} }
// Adds a value onto the end of the stack // Adds a value onto the end of the stack
Stack.prototype.push = function (value) { push(value) {
this.stack[this.top] = value this.stack[this.top] = value
this.top++ this.top++
} }
// Removes and returns the value at the end of the stack // Removes and returns the value at the end of the stack
Stack.prototype.pop = function () { pop() {
if (this.top === 0) { if (this.top === 0) {
return 'Stack is Empty' return 'Stack is Empty'
} }
@ -35,23 +35,21 @@ const Stack = (function () {
} }
// Returns the size of the stack // Returns the size of the stack
Stack.prototype.size = function () { size() {
return this.top return this.top
} }
// Returns the value at the end of the stack // Returns the value at the end of the stack
Stack.prototype.peek = function () { peek() {
return this.stack[this.top - 1] return this.stack[this.top - 1]
} }
// To see all the elements in the stack // To see all the elements in the stack
Stack.prototype.view = function (output = (value) => console.log(value)) { view(output = (value) => console.log(value)) {
for (let i = 0; i < this.top; i++) { for (let i = 0; i < this.top; i++) {
output(this.stack[i]) output(this.stack[i])
} }
} }
}
return Stack
})()
export { Stack } export { Stack }

View File

@ -31,8 +31,8 @@ let utils
* @argument comp - A function used by AVL Tree For Comparison * @argument comp - A function used by AVL Tree For Comparison
* If no argument is sent it uses utils.comparator * If no argument is sent it uses utils.comparator
*/ */
const AVLTree = (function () { class AVLTree {
function _avl(comp) { constructor(comp) {
/** @public comparator function */ /** @public comparator function */
this._comp = undefined this._comp = undefined
this._comp = comp !== undefined ? comp : utils.comparator() this._comp = comp !== undefined ? comp : utils.comparator()
@ -43,59 +43,99 @@ const AVLTree = (function () {
this.size = 0 this.size = 0
} }
// creates new Node Object /* Public Functions */
const Node = function (val) { /**
* For Adding Elements to AVL Tree
* @param {any} _val
* Since in AVL Tree an element can only occur once so
* if a element exists it return false
* @returns {Boolean} element added or not
*/
add(_val) {
const prevSize = this.size
this.root = insert(this.root, _val, this)
return this.size !== prevSize
}
/**
* TO check is a particular element exists or not
* @param {any} _val
* @returns {Boolean} exists or not
*/
find(_val) {
const temp = searchAVLTree(this.root, _val, this)
return temp != null
}
/**
*
* @param {any} _val
* It is possible that element doesn't exists in tree
* in that case it return false
* @returns {Boolean} if element was found and deleted
*/
remove(_val) {
const prevSize = this.size
this.root = deleteElement(this.root, _val, this)
return prevSize !== this.size
}
}
// creates new Node Object
class Node {
constructor(val) {
this._val = val this._val = val
this._left = null this._left = null
this._right = null this._right = null
this._height = 1 this._height = 1
} }
}
// get height of a node // get height of a node
const getHeight = function (node) { const getHeight = function (node) {
if (node == null) { if (node == null) {
return 0 return 0
} }
return node._height return node._height
} }
// height difference or balance factor of a node // height difference or balance factor of a node
const getHeightDifference = function (node) { const getHeightDifference = function (node) {
return node == null ? 0 : getHeight(node._left) - getHeight(node._right) return node == null ? 0 : getHeight(node._left) - getHeight(node._right)
} }
// update height of a node based on children's heights // update height of a node based on children's heights
const updateHeight = function (node) { const updateHeight = function (node) {
if (node == null) { if (node == null) {
return return
} }
node._height = Math.max(getHeight(node._left), getHeight(node._right)) + 1 node._height = Math.max(getHeight(node._left), getHeight(node._right)) + 1
} }
// Helper: To check if the balanceFactor is valid // Helper: To check if the balanceFactor is valid
const isValidBalanceFactor = (balanceFactor) => const isValidBalanceFactor = (balanceFactor) =>
[0, 1, -1].includes(balanceFactor) [0, 1, -1].includes(balanceFactor)
// rotations of AVL Tree // rotations of AVL Tree
const leftRotate = function (node) { const leftRotate = function (node) {
const temp = node._right const temp = node._right
node._right = temp._left node._right = temp._left
temp._left = node temp._left = node
updateHeight(node) updateHeight(node)
updateHeight(temp) updateHeight(temp)
return temp return temp
} }
const rightRotate = function (node) { const rightRotate = function (node) {
const temp = node._left const temp = node._left
node._left = temp._right node._left = temp._right
temp._right = node temp._right = node
updateHeight(node) updateHeight(node)
updateHeight(temp) updateHeight(temp)
return temp return temp
} }
// check if tree is balanced else balance it for insertion // check if tree is balanced else balance it for insertion
const insertBalance = function (node, _val, balanceFactor, tree) { const insertBalance = function (node, _val, balanceFactor, tree) {
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) { if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
return rightRotate(node) // Left Left Case return rightRotate(node) // Left Left Case
} }
@ -108,10 +148,10 @@ const AVLTree = (function () {
} }
node._right = rightRotate(node._right) node._right = rightRotate(node._right)
return leftRotate(node) return leftRotate(node)
} }
// check if tree is balanced after deletion // check if tree is balanced after deletion
const delBalance = function (node) { const delBalance = function (node) {
const balanceFactor1 = getHeightDifference(node) const balanceFactor1 = getHeightDifference(node)
if (isValidBalanceFactor(balanceFactor1)) { if (isValidBalanceFactor(balanceFactor1)) {
return node return node
@ -128,10 +168,10 @@ const AVLTree = (function () {
return leftRotate(node) // Right Left return leftRotate(node) // Right Left
} }
return leftRotate(node) // Right Right return leftRotate(node) // Right Right
} }
// implement avl tree insertion // implement avl tree insertion
const insert = function (root, val, tree) { const insert = function (root, val, tree) {
if (root == null) { if (root == null) {
tree.size++ tree.size++
return new Node(val) return new Node(val)
@ -148,10 +188,10 @@ const AVLTree = (function () {
return isValidBalanceFactor(balanceFactor) return isValidBalanceFactor(balanceFactor)
? root ? root
: insertBalance(root, val, balanceFactor, tree) : insertBalance(root, val, balanceFactor, tree)
} }
// delete am element // delete am element
const deleteElement = function (root, _val, tree) { const deleteElement = function (root, _val, tree) {
if (root == null) { if (root == null) {
return root return root
} }
@ -184,9 +224,9 @@ const AVLTree = (function () {
updateHeight(root) updateHeight(root)
root = delBalance(root) root = delBalance(root)
return root return root
} }
// search tree for a element // search tree for a element
const searchAVLTree = function (root, val, tree) { const searchAVLTree = function (root, val, tree) {
if (root == null) { if (root == null) {
return null return null
} }
@ -197,44 +237,7 @@ const AVLTree = (function () {
return searchAVLTree(root._right, val, tree) return searchAVLTree(root._right, val, tree)
} }
return searchAVLTree(root._left, val, tree) return searchAVLTree(root._left, val, tree)
} }
/* Public Functions */
/**
* For Adding Elements to AVL Tree
* @param {any} _val
* Since in AVL Tree an element can only occur once so
* if a element exists it return false
* @returns {Boolean} element added or not
*/
_avl.prototype.add = function (_val) {
const prevSize = this.size
this.root = insert(this.root, _val, this)
return this.size !== prevSize
}
/**
* TO check is a particular element exists or not
* @param {any} _val
* @returns {Boolean} exists or not
*/
_avl.prototype.find = function (_val) {
const temp = searchAVLTree(this.root, _val, this)
return temp != null
}
/**
*
* @param {any} _val
* It is possible that element doesn't exists in tree
* in that case it return false
* @returns {Boolean} if element was found and deleted
*/
_avl.prototype.remove = function (_val) {
const prevSize = this.size
this.root = deleteElement(this.root, _val, this)
return prevSize !== this.size
}
return _avl
})()
/** /**
* A Code for Testing the AVLTree * A Code for Testing the AVLTree

View File

@ -13,14 +13,15 @@
// class Node // class Node
const Node = (function Node() { const Node = (function Node() {
// Node in the tree // Node in the tree
function Node(val) { class Node {
constructor(val) {
this.value = val this.value = val
this.left = null this.left = null
this.right = null this.right = null
} }
// Search the tree for a value // Search the tree for a value
Node.prototype.search = function (val) { search(val) {
if (this.value === val) { if (this.value === val) {
return this return this
} else if (val < this.value && this.left !== null) { } else if (val < this.value && this.left !== null) {
@ -32,7 +33,7 @@ const Node = (function Node() {
} }
// Visit a node // Visit a node
Node.prototype.visit = function (output = (value) => console.log(value)) { visit(output = (value) => console.log(value)) {
// Recursively go left // Recursively go left
if (this.left !== null) { if (this.left !== null) {
this.left.visit() this.left.visit()
@ -46,7 +47,7 @@ const Node = (function Node() {
} }
// Add a node // Add a node
Node.prototype.addNode = function (n) { addNode(n) {
if (n.value < this.value) { if (n.value < this.value) {
if (this.left === null) { if (this.left === null) {
this.left = n this.left = n
@ -63,7 +64,7 @@ const Node = (function Node() {
} }
// remove a node // remove a node
Node.prototype.removeNode = function (val) { removeNode(val) {
if (val === this.value) { if (val === this.value) {
if (!this.left && !this.right) { if (!this.left && !this.right) {
return null return null
@ -85,6 +86,7 @@ const Node = (function Node() {
} }
return this return this
} }
}
// find maximum value in the tree // find maximum value in the tree
const maxVal = function (node) { const maxVal = function (node) {
@ -107,13 +109,14 @@ const Node = (function Node() {
// class Tree // class Tree
const Tree = (function () { const Tree = (function () {
function Tree() { class Tree {
constructor() {
// Just store the root // Just store the root
this.root = null this.root = null
} }
// Inorder traversal // Inorder traversal
Tree.prototype.traverse = function () { traverse() {
if (!this.root) { if (!this.root) {
// No nodes are there in the tree till now // No nodes are there in the tree till now
return return
@ -122,7 +125,7 @@ const Tree = (function () {
} }
// Start by searching the root // Start by searching the root
Tree.prototype.search = function (val) { search(val) {
const found = this.root.search(val) const found = this.root.search(val)
if (found !== null) { if (found !== null) {
return found.value return found.value
@ -132,7 +135,7 @@ const Tree = (function () {
} }
// Add a new value to the tree // Add a new value to the tree
Tree.prototype.addValue = function (val) { addValue(val) {
const n = new Node(val) const n = new Node(val)
if (this.root === null) { if (this.root === null) {
this.root = n this.root = n
@ -142,10 +145,11 @@ const Tree = (function () {
} }
// remove a value from the tree // remove a value from the tree
Tree.prototype.removeValue = function (val) { removeValue(val) {
// remove something if root exists // remove something if root exists
this.root = this.root && this.root.removeNode(val) this.root = this.root && this.root.removeNode(val)
} }
}
// returns the constructor // returns the constructor
return Tree return Tree

View File

@ -1,4 +1,5 @@
const TrieNode = function TrieNode(key, parent) { class TrieNode {
constructor(key, parent) {
this.key = key this.key = key
this.count = 0 this.count = 0
this.children = Object.create(null) this.children = Object.create(null)
@ -7,15 +8,17 @@ const TrieNode = function TrieNode(key, parent) {
} else { } else {
this.parent = parent this.parent = parent
} }
}
} }
function Trie() { class Trie {
constructor() {
// create only root with null key and parent // create only root with null key and parent
this.root = new TrieNode(null, null) this.root = new TrieNode(null, null)
} }
// Recursively finds the occurrence of all words in a given node // Recursively finds the occurrence of all words in a given node
Trie.findAllWords = function (root, word, output) { static findAllWords(root, word, output) {
if (root === null) return if (root === null) return
if (root.count > 0) { if (root.count > 0) {
if (typeof output === 'object') { if (typeof output === 'object') {
@ -28,9 +31,9 @@ Trie.findAllWords = function (root, word, output) {
this.findAllWords(root.children[key], word, output) this.findAllWords(root.children[key], word, output)
word = word.slice(0, -1) word = word.slice(0, -1)
} }
} }
Trie.prototype.insert = function (word) { insert(word) {
if (typeof word !== 'string') return if (typeof word !== 'string') return
if (word === '') { if (word === '') {
this.root.count += 1 this.root.count += 1
@ -46,9 +49,9 @@ Trie.prototype.insert = function (word) {
node = node.children[word.charAt(i)] node = node.children[word.charAt(i)]
} }
node.count += 1 node.count += 1
} }
Trie.prototype.findPrefix = function (word) { findPrefix(word) {
if (typeof word !== 'string') return null if (typeof word !== 'string') return null
let node = this.root let node = this.root
const len = word.length const len = word.length
@ -59,9 +62,9 @@ Trie.prototype.findPrefix = function (word) {
node = node.children[word.charAt(i)] node = node.children[word.charAt(i)]
} }
return node return node
} }
Trie.prototype.remove = function (word, count) { remove(word, count) {
if (typeof word !== 'string') return if (typeof word !== 'string') return
if (typeof count !== 'number') count = 1 if (typeof count !== 'number') count = 1
else if (count <= 0) return else if (count <= 0) return
@ -98,9 +101,9 @@ Trie.prototype.remove = function (word, count) {
) { ) {
child.parent.children[child.key] = undefined child.parent.children[child.key] = undefined
} }
} }
Trie.prototype.findAllWords = function (prefix) { findAllWords(prefix) {
const output = [] const output = []
// find the node with provided prefix // find the node with provided prefix
const node = this.findPrefix(prefix) const node = this.findPrefix(prefix)
@ -108,22 +111,22 @@ Trie.prototype.findAllWords = function (prefix) {
if (node === null) return output if (node === null) return output
Trie.findAllWords(node, prefix, output) Trie.findAllWords(node, prefix, output)
return output return output
} }
Trie.prototype.contains = function (word) { contains(word) {
// find the node with given prefix // find the node with given prefix
const node = this.findPrefix(word) const node = this.findPrefix(word)
// No such word exists // No such word exists
return node !== null && node.count !== 0 return node !== null && node.count !== 0
} }
Trie.prototype.findOccurrences = function (word) { findOccurrences(word) {
// find the node with given prefix // find the node with given prefix
const node = this.findPrefix(word) const node = this.findPrefix(word)
// No such word exists // No such word exists
if (node === null) return 0 if (node === null) return 0
return node.count return node.count
}
} }
export { Trie } export { Trie }