mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +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:
@ -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,162 +43,6 @@ const AVLTree = (function () {
|
||||
this.size = 0
|
||||
}
|
||||
|
||||
// creates new Node Object
|
||||
const Node = function (val) {
|
||||
this._val = val
|
||||
this._left = null
|
||||
this._right = null
|
||||
this._height = 1
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return node == null ? 0 : getHeight(node._left) - getHeight(node._right)
|
||||
}
|
||||
|
||||
// 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) =>
|
||||
[0, 1, -1].includes(balanceFactor)
|
||||
|
||||
// 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 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) {
|
||||
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
|
||||
return rightRotate(node) // Left Left Case
|
||||
}
|
||||
if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) {
|
||||
return leftRotate(node) // Right Right Case
|
||||
}
|
||||
if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) {
|
||||
node._left = leftRotate(node._left) // Left Right Case
|
||||
return rightRotate(node)
|
||||
}
|
||||
node._right = rightRotate(node._right)
|
||||
return leftRotate(node)
|
||||
}
|
||||
|
||||
// check if tree is balanced after deletion
|
||||
const delBalance = function (node) {
|
||||
const balanceFactor1 = getHeightDifference(node)
|
||||
if (isValidBalanceFactor(balanceFactor1)) {
|
||||
return node
|
||||
}
|
||||
if (balanceFactor1 > 1) {
|
||||
if (getHeightDifference(node._left) >= 0) {
|
||||
return rightRotate(node) // Left Left
|
||||
}
|
||||
node._left = leftRotate(node._left)
|
||||
return rightRotate(node) // Left Right
|
||||
}
|
||||
if (getHeightDifference(node._right) > 0) {
|
||||
node._right = rightRotate(node._right)
|
||||
return leftRotate(node) // Right Left
|
||||
}
|
||||
return leftRotate(node) // Right Right
|
||||
}
|
||||
|
||||
// implement avl tree insertion
|
||||
const insert = function (root, val, tree) {
|
||||
if (root == null) {
|
||||
tree.size++
|
||||
return new Node(val)
|
||||
}
|
||||
if (tree._comp(root._val, val) < 0) {
|
||||
root._right = insert(root._right, val, tree)
|
||||
} else if (tree._comp(root._val, val) > 0) {
|
||||
root._left = insert(root._left, val, tree)
|
||||
} else {
|
||||
return root
|
||||
}
|
||||
updateHeight(root)
|
||||
const balanceFactor = getHeightDifference(root)
|
||||
return isValidBalanceFactor(balanceFactor)
|
||||
? root
|
||||
: insertBalance(root, val, balanceFactor, tree)
|
||||
}
|
||||
|
||||
// delete am element
|
||||
const deleteElement = function (root, _val, tree) {
|
||||
if (root == null) {
|
||||
return root
|
||||
}
|
||||
if (tree._comp(root._val, _val) === 0) {
|
||||
// key found case
|
||||
if (root._left === null && root._right === null) {
|
||||
root = null
|
||||
tree.size--
|
||||
} else if (root._left === null) {
|
||||
root = root._right
|
||||
tree.size--
|
||||
} else if (root._right === null) {
|
||||
root = root._left
|
||||
tree.size--
|
||||
} else {
|
||||
let temp = root._right
|
||||
while (temp._left != null) {
|
||||
temp = temp._left
|
||||
}
|
||||
root._val = temp._val
|
||||
root._right = deleteElement(root._right, temp._val, tree)
|
||||
}
|
||||
} else {
|
||||
if (tree._comp(root._val, _val) < 0) {
|
||||
root._right = deleteElement(root._right, _val, tree)
|
||||
} else {
|
||||
root._left = deleteElement(root._left, _val, tree)
|
||||
}
|
||||
}
|
||||
updateHeight(root)
|
||||
root = delBalance(root)
|
||||
return root
|
||||
}
|
||||
// search tree for a element
|
||||
const searchAVLTree = function (root, val, tree) {
|
||||
if (root == null) {
|
||||
return null
|
||||
}
|
||||
if (tree._comp(root._val, val) === 0) {
|
||||
return root
|
||||
}
|
||||
if (tree._comp(root._val, val) < 0) {
|
||||
return searchAVLTree(root._right, val, tree)
|
||||
}
|
||||
return searchAVLTree(root._left, val, tree)
|
||||
}
|
||||
|
||||
/* Public Functions */
|
||||
/**
|
||||
* For Adding Elements to AVL Tree
|
||||
@ -207,20 +51,22 @@ const AVLTree = (function () {
|
||||
* if a element exists it return false
|
||||
* @returns {Boolean} element added or not
|
||||
*/
|
||||
_avl.prototype.add = function (_val) {
|
||||
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
|
||||
*/
|
||||
_avl.prototype.find = function (_val) {
|
||||
find(_val) {
|
||||
const temp = searchAVLTree(this.root, _val, this)
|
||||
return temp != null
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {any} _val
|
||||
@ -228,13 +74,170 @@ const AVLTree = (function () {
|
||||
* in that case it return false
|
||||
* @returns {Boolean} if element was found and deleted
|
||||
*/
|
||||
_avl.prototype.remove = function (_val) {
|
||||
remove(_val) {
|
||||
const prevSize = this.size
|
||||
this.root = deleteElement(this.root, _val, this)
|
||||
return prevSize !== this.size
|
||||
}
|
||||
return _avl
|
||||
})()
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if (node == null) {
|
||||
return 0
|
||||
}
|
||||
return node._height
|
||||
}
|
||||
|
||||
// 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) {
|
||||
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) =>
|
||||
[0, 1, -1].includes(balanceFactor)
|
||||
|
||||
// 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 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) {
|
||||
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
|
||||
return rightRotate(node) // Left Left Case
|
||||
}
|
||||
if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) {
|
||||
return leftRotate(node) // Right Right Case
|
||||
}
|
||||
if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) {
|
||||
node._left = leftRotate(node._left) // Left Right Case
|
||||
return rightRotate(node)
|
||||
}
|
||||
node._right = rightRotate(node._right)
|
||||
return leftRotate(node)
|
||||
}
|
||||
|
||||
// check if tree is balanced after deletion
|
||||
const delBalance = function (node) {
|
||||
const balanceFactor1 = getHeightDifference(node)
|
||||
if (isValidBalanceFactor(balanceFactor1)) {
|
||||
return node
|
||||
}
|
||||
if (balanceFactor1 > 1) {
|
||||
if (getHeightDifference(node._left) >= 0) {
|
||||
return rightRotate(node) // Left Left
|
||||
}
|
||||
node._left = leftRotate(node._left)
|
||||
return rightRotate(node) // Left Right
|
||||
}
|
||||
if (getHeightDifference(node._right) > 0) {
|
||||
node._right = rightRotate(node._right)
|
||||
return leftRotate(node) // Right Left
|
||||
}
|
||||
return leftRotate(node) // Right Right
|
||||
}
|
||||
|
||||
// implement avl tree insertion
|
||||
const insert = function (root, val, tree) {
|
||||
if (root == null) {
|
||||
tree.size++
|
||||
return new Node(val)
|
||||
}
|
||||
if (tree._comp(root._val, val) < 0) {
|
||||
root._right = insert(root._right, val, tree)
|
||||
} else if (tree._comp(root._val, val) > 0) {
|
||||
root._left = insert(root._left, val, tree)
|
||||
} else {
|
||||
return root
|
||||
}
|
||||
updateHeight(root)
|
||||
const balanceFactor = getHeightDifference(root)
|
||||
return isValidBalanceFactor(balanceFactor)
|
||||
? root
|
||||
: insertBalance(root, val, balanceFactor, tree)
|
||||
}
|
||||
|
||||
// delete am element
|
||||
const deleteElement = function (root, _val, tree) {
|
||||
if (root == null) {
|
||||
return root
|
||||
}
|
||||
if (tree._comp(root._val, _val) === 0) {
|
||||
// key found case
|
||||
if (root._left === null && root._right === null) {
|
||||
root = null
|
||||
tree.size--
|
||||
} else if (root._left === null) {
|
||||
root = root._right
|
||||
tree.size--
|
||||
} else if (root._right === null) {
|
||||
root = root._left
|
||||
tree.size--
|
||||
} else {
|
||||
let temp = root._right
|
||||
while (temp._left != null) {
|
||||
temp = temp._left
|
||||
}
|
||||
root._val = temp._val
|
||||
root._right = deleteElement(root._right, temp._val, tree)
|
||||
}
|
||||
} else {
|
||||
if (tree._comp(root._val, _val) < 0) {
|
||||
root._right = deleteElement(root._right, _val, tree)
|
||||
} else {
|
||||
root._left = deleteElement(root._left, _val, tree)
|
||||
}
|
||||
}
|
||||
updateHeight(root)
|
||||
root = delBalance(root)
|
||||
return root
|
||||
}
|
||||
// search tree for a element
|
||||
const searchAVLTree = function (root, val, tree) {
|
||||
if (root == null) {
|
||||
return null
|
||||
}
|
||||
if (tree._comp(root._val, val) === 0) {
|
||||
return root
|
||||
}
|
||||
if (tree._comp(root._val, val) < 0) {
|
||||
return searchAVLTree(root._right, val, tree)
|
||||
}
|
||||
return searchAVLTree(root._left, val, tree)
|
||||
}
|
||||
|
||||
/**
|
||||
* A Code for Testing the AVLTree
|
||||
|
@ -13,77 +13,79 @@
|
||||
// class Node
|
||||
const Node = (function Node() {
|
||||
// Node in the tree
|
||||
function Node(val) {
|
||||
this.value = val
|
||||
this.left = null
|
||||
this.right = null
|
||||
}
|
||||
|
||||
// Search the tree for a value
|
||||
Node.prototype.search = function (val) {
|
||||
if (this.value === val) {
|
||||
return this
|
||||
} else if (val < this.value && this.left !== null) {
|
||||
return this.left.search(val)
|
||||
} else if (val > this.value && this.right !== null) {
|
||||
return this.right.search(val)
|
||||
class Node {
|
||||
constructor(val) {
|
||||
this.value = val
|
||||
this.left = null
|
||||
this.right = null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Visit a node
|
||||
Node.prototype.visit = function (output = (value) => console.log(value)) {
|
||||
// Recursively go left
|
||||
if (this.left !== null) {
|
||||
this.left.visit()
|
||||
}
|
||||
// Print out value
|
||||
output(this.value)
|
||||
// Recursively go right
|
||||
if (this.right !== null) {
|
||||
this.right.visit()
|
||||
}
|
||||
}
|
||||
|
||||
// Add a node
|
||||
Node.prototype.addNode = function (n) {
|
||||
if (n.value < this.value) {
|
||||
if (this.left === null) {
|
||||
this.left = n
|
||||
} else {
|
||||
this.left.addNode(n)
|
||||
// Search the tree for a value
|
||||
search(val) {
|
||||
if (this.value === val) {
|
||||
return this
|
||||
} else if (val < this.value && this.left !== null) {
|
||||
return this.left.search(val)
|
||||
} else if (val > this.value && this.right !== null) {
|
||||
return this.right.search(val)
|
||||
}
|
||||
} else if (n.value > this.value) {
|
||||
if (this.right === null) {
|
||||
this.right = n
|
||||
} else {
|
||||
this.right.addNode(n)
|
||||
return null
|
||||
}
|
||||
|
||||
// Visit a node
|
||||
visit(output = (value) => console.log(value)) {
|
||||
// Recursively go left
|
||||
if (this.left !== null) {
|
||||
this.left.visit()
|
||||
}
|
||||
// Print out value
|
||||
output(this.value)
|
||||
// Recursively go right
|
||||
if (this.right !== null) {
|
||||
this.right.visit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove a node
|
||||
Node.prototype.removeNode = function (val) {
|
||||
if (val === this.value) {
|
||||
if (!this.left && !this.right) {
|
||||
return null
|
||||
} else {
|
||||
if (this.left) {
|
||||
const leftMax = maxVal(this.left)
|
||||
this.value = leftMax
|
||||
this.left = this.left.removeNode(leftMax)
|
||||
// Add a node
|
||||
addNode(n) {
|
||||
if (n.value < this.value) {
|
||||
if (this.left === null) {
|
||||
this.left = n
|
||||
} else {
|
||||
const rightMin = minVal(this.right)
|
||||
this.value = rightMin
|
||||
this.right = this.right.removeNode(rightMin)
|
||||
this.left.addNode(n)
|
||||
}
|
||||
} else if (n.value > this.value) {
|
||||
if (this.right === null) {
|
||||
this.right = n
|
||||
} else {
|
||||
this.right.addNode(n)
|
||||
}
|
||||
}
|
||||
} else if (val < this.value) {
|
||||
this.left = this.left && this.left.removeNode(val)
|
||||
} else if (val > this.value) {
|
||||
this.right = this.right && this.right.removeNode(val)
|
||||
}
|
||||
return this
|
||||
|
||||
// remove a node
|
||||
removeNode(val) {
|
||||
if (val === this.value) {
|
||||
if (!this.left && !this.right) {
|
||||
return null
|
||||
} else {
|
||||
if (this.left) {
|
||||
const leftMax = maxVal(this.left)
|
||||
this.value = leftMax
|
||||
this.left = this.left.removeNode(leftMax)
|
||||
} else {
|
||||
const rightMin = minVal(this.right)
|
||||
this.value = rightMin
|
||||
this.right = this.right.removeNode(rightMin)
|
||||
}
|
||||
}
|
||||
} else if (val < this.value) {
|
||||
this.left = this.left && this.left.removeNode(val)
|
||||
} else if (val > this.value) {
|
||||
this.right = this.right && this.right.removeNode(val)
|
||||
}
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
// find maximum value in the tree
|
||||
@ -107,44 +109,46 @@ const Node = (function Node() {
|
||||
|
||||
// class Tree
|
||||
const Tree = (function () {
|
||||
function Tree() {
|
||||
// Just store the root
|
||||
this.root = null
|
||||
}
|
||||
|
||||
// Inorder traversal
|
||||
Tree.prototype.traverse = function () {
|
||||
if (!this.root) {
|
||||
// No nodes are there in the tree till now
|
||||
return
|
||||
class Tree {
|
||||
constructor() {
|
||||
// Just store the root
|
||||
this.root = null
|
||||
}
|
||||
this.root.visit()
|
||||
}
|
||||
|
||||
// Start by searching the root
|
||||
Tree.prototype.search = function (val) {
|
||||
const found = this.root.search(val)
|
||||
if (found !== null) {
|
||||
return found.value
|
||||
// Inorder traversal
|
||||
traverse() {
|
||||
if (!this.root) {
|
||||
// No nodes are there in the tree till now
|
||||
return
|
||||
}
|
||||
this.root.visit()
|
||||
}
|
||||
// not found
|
||||
return null
|
||||
}
|
||||
|
||||
// Add a new value to the tree
|
||||
Tree.prototype.addValue = function (val) {
|
||||
const n = new Node(val)
|
||||
if (this.root === null) {
|
||||
this.root = n
|
||||
} else {
|
||||
this.root.addNode(n)
|
||||
// Start by searching the root
|
||||
search(val) {
|
||||
const found = this.root.search(val)
|
||||
if (found !== null) {
|
||||
return found.value
|
||||
}
|
||||
// not found
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// remove a value from the tree
|
||||
Tree.prototype.removeValue = function (val) {
|
||||
// remove something if root exists
|
||||
this.root = this.root && this.root.removeNode(val)
|
||||
// Add a new value to the tree
|
||||
addValue(val) {
|
||||
const n = new Node(val)
|
||||
if (this.root === null) {
|
||||
this.root = n
|
||||
} else {
|
||||
this.root.addNode(n)
|
||||
}
|
||||
}
|
||||
|
||||
// remove a value from the tree
|
||||
removeValue(val) {
|
||||
// remove something if root exists
|
||||
this.root = this.root && this.root.removeNode(val)
|
||||
}
|
||||
}
|
||||
|
||||
// returns the constructor
|
||||
|
@ -1,129 +1,132 @@
|
||||
const TrieNode = function TrieNode(key, parent) {
|
||||
this.key = key
|
||||
this.count = 0
|
||||
this.children = Object.create(null)
|
||||
if (parent === undefined) {
|
||||
this.parent = null
|
||||
} else {
|
||||
this.parent = parent
|
||||
}
|
||||
}
|
||||
|
||||
function Trie() {
|
||||
// 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) {
|
||||
if (root === null) return
|
||||
if (root.count > 0) {
|
||||
if (typeof output === 'object') {
|
||||
output.push({ word, count: root.count })
|
||||
class TrieNode {
|
||||
constructor(key, parent) {
|
||||
this.key = key
|
||||
this.count = 0
|
||||
this.children = Object.create(null)
|
||||
if (parent === undefined) {
|
||||
this.parent = null
|
||||
} else {
|
||||
this.parent = parent
|
||||
}
|
||||
}
|
||||
let key
|
||||
for (key in root.children) {
|
||||
word += key
|
||||
this.findAllWords(root.children[key], word, output)
|
||||
word = word.slice(0, -1)
|
||||
}
|
||||
}
|
||||
|
||||
Trie.prototype.insert = function (word) {
|
||||
if (typeof word !== 'string') return
|
||||
if (word === '') {
|
||||
this.root.count += 1
|
||||
return
|
||||
class Trie {
|
||||
constructor() {
|
||||
// create only root with null key and parent
|
||||
this.root = new TrieNode(null, null)
|
||||
}
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
let i
|
||||
for (i = 0; i < len; i++) {
|
||||
if (node.children[word.charAt(i)] === undefined) {
|
||||
node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node)
|
||||
|
||||
// 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') {
|
||||
output.push({ word, count: root.count })
|
||||
}
|
||||
}
|
||||
let key
|
||||
for (key in root.children) {
|
||||
word += key
|
||||
this.findAllWords(root.children[key], word, output)
|
||||
word = word.slice(0, -1)
|
||||
}
|
||||
node = node.children[word.charAt(i)]
|
||||
}
|
||||
node.count += 1
|
||||
}
|
||||
|
||||
Trie.prototype.findPrefix = function (word) {
|
||||
if (typeof word !== 'string') return null
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
let i
|
||||
// After end of this loop node will be at desired prefix
|
||||
for (i = 0; i < len; i++) {
|
||||
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
|
||||
node = node.children[word.charAt(i)]
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
Trie.prototype.remove = function (word, count) {
|
||||
if (typeof word !== 'string') return
|
||||
if (typeof count !== 'number') count = 1
|
||||
else if (count <= 0) return
|
||||
|
||||
// for empty string just delete count of root
|
||||
if (word === '') {
|
||||
if (this.root.count >= count) this.root.count -= count
|
||||
else this.root.count = 0
|
||||
return
|
||||
}
|
||||
|
||||
let child = this.root
|
||||
const len = word.length
|
||||
let i, key
|
||||
// child: node which is to be deleted
|
||||
for (i = 0; i < len; i++) {
|
||||
key = word.charAt(i)
|
||||
if (child.children[key] === undefined) return
|
||||
child = child.children[key]
|
||||
insert(word) {
|
||||
if (typeof word !== 'string') return
|
||||
if (word === '') {
|
||||
this.root.count += 1
|
||||
return
|
||||
}
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
let i
|
||||
for (i = 0; i < len; i++) {
|
||||
if (node.children[word.charAt(i)] === undefined) {
|
||||
node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node)
|
||||
}
|
||||
node = node.children[word.charAt(i)]
|
||||
}
|
||||
node.count += 1
|
||||
}
|
||||
|
||||
// Delete no of occurrences specified
|
||||
if (child.count >= count) child.count -= count
|
||||
else child.count = 0
|
||||
|
||||
// If some occurrences are left we don't delete it or else
|
||||
// if the object forms some other objects prefix we don't delete it
|
||||
// For checking an empty object
|
||||
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
|
||||
if (
|
||||
child.count <= 0 &&
|
||||
Object.keys(child.children).length &&
|
||||
child.children.constructor === Object
|
||||
) {
|
||||
child.parent.children[child.key] = undefined
|
||||
findPrefix(word) {
|
||||
if (typeof word !== 'string') return null
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
let i
|
||||
// After end of this loop node will be at desired prefix
|
||||
for (i = 0; i < len; i++) {
|
||||
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
|
||||
node = node.children[word.charAt(i)]
|
||||
}
|
||||
return node
|
||||
}
|
||||
}
|
||||
|
||||
Trie.prototype.findAllWords = function (prefix) {
|
||||
const output = []
|
||||
// find the node with provided prefix
|
||||
const node = this.findPrefix(prefix)
|
||||
// No such prefix exists
|
||||
if (node === null) return output
|
||||
Trie.findAllWords(node, prefix, output)
|
||||
return output
|
||||
}
|
||||
remove(word, count) {
|
||||
if (typeof word !== 'string') return
|
||||
if (typeof count !== 'number') count = 1
|
||||
else if (count <= 0) return
|
||||
|
||||
Trie.prototype.contains = function (word) {
|
||||
// find the node with given prefix
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
// for empty string just delete count of root
|
||||
if (word === '') {
|
||||
if (this.root.count >= count) this.root.count -= count
|
||||
else this.root.count = 0
|
||||
return
|
||||
}
|
||||
|
||||
return node !== null && node.count !== 0
|
||||
}
|
||||
let child = this.root
|
||||
const len = word.length
|
||||
let i, key
|
||||
// child: node which is to be deleted
|
||||
for (i = 0; i < len; i++) {
|
||||
key = word.charAt(i)
|
||||
if (child.children[key] === undefined) return
|
||||
child = child.children[key]
|
||||
}
|
||||
|
||||
Trie.prototype.findOccurrences = function (word) {
|
||||
// find the node with given prefix
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
if (node === null) return 0
|
||||
return node.count
|
||||
// Delete no of occurrences specified
|
||||
if (child.count >= count) child.count -= count
|
||||
else child.count = 0
|
||||
|
||||
// If some occurrences are left we don't delete it or else
|
||||
// if the object forms some other objects prefix we don't delete it
|
||||
// For checking an empty object
|
||||
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
|
||||
if (
|
||||
child.count <= 0 &&
|
||||
Object.keys(child.children).length &&
|
||||
child.children.constructor === Object
|
||||
) {
|
||||
child.parent.children[child.key] = undefined
|
||||
}
|
||||
}
|
||||
|
||||
findAllWords(prefix) {
|
||||
const output = []
|
||||
// find the node with provided prefix
|
||||
const node = this.findPrefix(prefix)
|
||||
// No such prefix exists
|
||||
if (node === null) return output
|
||||
Trie.findAllWords(node, prefix, output)
|
||||
return output
|
||||
}
|
||||
|
||||
contains(word) {
|
||||
// find the node with given prefix
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
return node !== null && node.count !== 0
|
||||
}
|
||||
|
||||
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