mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
chore: convert functions to an ES2015 classes (#1656)
* chore: convert functions to an ES2015 classes * remove unnecessary functions
This commit is contained in:
@ -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') {
|
||||||
|
@ -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 }
|
||||||
|
@ -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 }
|
||||||
|
@ -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,13 +43,53 @@ const AVLTree = (function () {
|
|||||||
this.size = 0
|
this.size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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
|
// creates new Node Object
|
||||||
const Node = function (val) {
|
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) {
|
||||||
@ -199,43 +239,6 @@ const AVLTree = (function () {
|
|||||||
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
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
@ -8,14 +9,16 @@ const TrieNode = function TrieNode(key, parent) {
|
|||||||
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') {
|
||||||
@ -30,7 +33,7 @@ Trie.findAllWords = function (root, word, output) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@ -48,7 +51,7 @@ Trie.prototype.insert = function (word) {
|
|||||||
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
|
||||||
@ -61,7 +64,7 @@ Trie.prototype.findPrefix = function (word) {
|
|||||||
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
|
||||||
@ -100,7 +103,7 @@ Trie.prototype.remove = function (word, count) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
@ -110,20 +113,20 @@ Trie.prototype.findAllWords = function (prefix) {
|
|||||||
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 }
|
||||||
|
Reference in New Issue
Block a user