mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +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(':')
|
||||
// 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') {
|
||||
|
@ -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 }
|
||||
|
@ -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 }
|
||||
|
@ -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,13 +43,53 @@ const AVLTree = (function () {
|
||||
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
|
||||
const Node = function (val) {
|
||||
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) {
|
||||
@ -199,43 +239,6 @@ const AVLTree = (function () {
|
||||
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
|
||||
*/
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
@ -8,14 +9,16 @@ const TrieNode = function TrieNode(key, parent) {
|
||||
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) {
|
||||
static findAllWords(root, word, output) {
|
||||
if (root === null) return
|
||||
if (root.count > 0) {
|
||||
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 (word === '') {
|
||||
this.root.count += 1
|
||||
@ -48,7 +51,7 @@ Trie.prototype.insert = function (word) {
|
||||
node.count += 1
|
||||
}
|
||||
|
||||
Trie.prototype.findPrefix = function (word) {
|
||||
findPrefix(word) {
|
||||
if (typeof word !== 'string') return null
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
@ -61,7 +64,7 @@ Trie.prototype.findPrefix = function (word) {
|
||||
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
|
||||
@ -100,7 +103,7 @@ Trie.prototype.remove = function (word, count) {
|
||||
}
|
||||
}
|
||||
|
||||
Trie.prototype.findAllWords = function (prefix) {
|
||||
findAllWords(prefix) {
|
||||
const output = []
|
||||
// find the node with provided prefix
|
||||
const node = this.findPrefix(prefix)
|
||||
@ -110,20 +113,20 @@ Trie.prototype.findAllWords = function (prefix) {
|
||||
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 }
|
||||
|
Reference in New Issue
Block a user