Skip to content

chore: convert functions to an ES2015 classes #1656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Conversions/RailwayTimeConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
8 changes: 3 additions & 5 deletions Data-Structures/Array/Reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]] // Swapping elements ES6 way

return arr
}
export { Reverse }
68 changes: 35 additions & 33 deletions Data-Structures/Stack/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,47 @@

// Creates a stack constructor
const Stack = (function () {
function Stack() {
// The top of the Stack
this.top = 0
// The array representation of the stack
this.stack = []
}

// Adds a value onto the end of the stack
Stack.prototype.push = function (value) {
this.stack[this.top] = value
this.top++
}
class Stack {
constructor() {
// The top of the Stack
this.top = 0
// The array representation of the stack
this.stack = []
}

// Removes and returns the value at the end of the stack
Stack.prototype.pop = function () {
if (this.top === 0) {
return 'Stack is Empty'
// Adds a value onto the end of the stack
push(value) {
this.stack[this.top] = value
this.top++
}

this.top--
const result = this.stack[this.top]
this.stack = this.stack.splice(0, this.top)
return result
}
// Removes and returns the value at the end of the stack
pop() {
if (this.top === 0) {
return 'Stack is Empty'
}

// Returns the size of the stack
Stack.prototype.size = function () {
return this.top
}
this.top--
const result = this.stack[this.top]
this.stack = this.stack.splice(0, this.top)
return result
}

// Returns the value at the end of the stack
Stack.prototype.peek = function () {
return this.stack[this.top - 1]
}
// Returns the size of the stack
size() {
return this.top
}

// Returns the value at the end of the stack
peek() {
return this.stack[this.top - 1]
}

// To see all the elements in the stack
Stack.prototype.view = function (output = (value) => console.log(value)) {
for (let i = 0; i < this.top; i++) {
output(this.stack[i])
// To see all the elements in the stack
view(output = (value) => console.log(value)) {
for (let i = 0; i < this.top; i++) {
output(this.stack[i])
}
}
}

Expand Down
99 changes: 52 additions & 47 deletions Data-Structures/Tree/AVLTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,62 @@ let utils
* If no argument is sent it uses utils.comparator
*/
const AVLTree = (function () {
function _avl(comp) {
/** @public comparator function */
this._comp = undefined
this._comp = comp !== undefined ? comp : utils.comparator()
class _avl {
constructor(comp) {
/** @public comparator function */
this._comp = undefined
this._comp = comp !== undefined ? comp : utils.comparator()

/** @public root of the AVL Tree */
this.root = null
/** @public number of elements in AVL Tree */
this.size = 0
/** @public root of the AVL Tree */
this.root = null
/** @public number of elements in AVL Tree */
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) {
this._val = val
this._left = null
this._right = null
this._height = 1
class Node {
constructor(val) {
this._val = val
this._left = null
this._right = null
this._height = 1
}
}

// get height of a node
Expand Down Expand Up @@ -199,40 +238,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
})()

Expand Down
Loading