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(':')
// split second and shift value.
const [second, shift] = [
secondWithShift.substr(0, 2),
secondWithShift.substr(2)
secondWithShift.substring(0, 2),
secondWithShift.substring(2)
]
// convert shifted time to not-shift time(Railway time) by using the above explanation.
if (shift === 'PM') {

View File

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

View File

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

View File

@ -31,8 +31,8 @@ let utils
* @argument comp - A function used by AVL Tree For Comparison
* If no argument is sent it uses utils.comparator
*/
const AVLTree = (function () {
function _avl(comp) {
class AVLTree {
constructor(comp) {
/** @public comparator function */
this._comp = undefined
this._comp = comp !== undefined ? comp : utils.comparator()
@ -43,59 +43,99 @@ const AVLTree = (function () {
this.size = 0
}
// creates new Node Object
const Node = function (val) {
/* 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
*/
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._left = null
this._right = null
this._height = 1
}
}
// get height of a node
const getHeight = function (node) {
// get height of a node
const getHeight = function (node) {
if (node == null) {
return 0
}
return node._height
}
}
// height difference or balance factor of a node
const getHeightDifference = function (node) {
// height difference or balance factor of a node
const getHeightDifference = function (node) {
return node == null ? 0 : getHeight(node._left) - getHeight(node._right)
}
}
// update height of a node based on children's heights
const updateHeight = function (node) {
// update height of a node based on children's heights
const updateHeight = function (node) {
if (node == null) {
return
}
node._height = Math.max(getHeight(node._left), getHeight(node._right)) + 1
}
}
// Helper: To check if the balanceFactor is valid
const isValidBalanceFactor = (balanceFactor) =>
// Helper: To check if the balanceFactor is valid
const isValidBalanceFactor = (balanceFactor) =>
[0, 1, -1].includes(balanceFactor)
// rotations of AVL Tree
const leftRotate = function (node) {
// rotations of AVL Tree
const leftRotate = function (node) {
const temp = node._right
node._right = temp._left
temp._left = node
updateHeight(node)
updateHeight(temp)
return temp
}
const rightRotate = function (node) {
}
const rightRotate = function (node) {
const temp = node._left
node._left = temp._right
temp._right = node
updateHeight(node)
updateHeight(temp)
return temp
}
}
// check if tree is balanced else balance it for insertion
const insertBalance = function (node, _val, balanceFactor, tree) {
// check if tree is balanced else balance it for insertion
const insertBalance = function (node, _val, balanceFactor, tree) {
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
return rightRotate(node) // Left Left Case
}
@ -108,10 +148,10 @@ const AVLTree = (function () {
}
node._right = rightRotate(node._right)
return leftRotate(node)
}
}
// check if tree is balanced after deletion
const delBalance = function (node) {
// check if tree is balanced after deletion
const delBalance = function (node) {
const balanceFactor1 = getHeightDifference(node)
if (isValidBalanceFactor(balanceFactor1)) {
return node
@ -128,10 +168,10 @@ const AVLTree = (function () {
return leftRotate(node) // Right Left
}
return leftRotate(node) // Right Right
}
}
// implement avl tree insertion
const insert = function (root, val, tree) {
// implement avl tree insertion
const insert = function (root, val, tree) {
if (root == null) {
tree.size++
return new Node(val)
@ -148,10 +188,10 @@ const AVLTree = (function () {
return isValidBalanceFactor(balanceFactor)
? root
: insertBalance(root, val, balanceFactor, tree)
}
}
// delete am element
const deleteElement = function (root, _val, tree) {
// delete am element
const deleteElement = function (root, _val, tree) {
if (root == null) {
return root
}
@ -184,9 +224,9 @@ const AVLTree = (function () {
updateHeight(root)
root = delBalance(root)
return root
}
// search tree for a element
const searchAVLTree = function (root, val, tree) {
}
// search tree for a element
const searchAVLTree = function (root, val, tree) {
if (root == null) {
return null
}
@ -197,44 +237,7 @@ const AVLTree = (function () {
return searchAVLTree(root._right, 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

View File

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

View File

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