mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
style: Fixed most styles (according to standardjs)
This commit is contained in:
@ -11,7 +11,7 @@
|
||||
*/
|
||||
|
||||
// class Node
|
||||
var Node = (function () {
|
||||
const Node = (function () {
|
||||
// Node in the tree
|
||||
function Node (val) {
|
||||
this.value = val
|
||||
@ -67,7 +67,7 @@ var Node = (function () {
|
||||
}())
|
||||
|
||||
// class Tree
|
||||
var Tree = (function () {
|
||||
const Tree = (function () {
|
||||
function Tree () {
|
||||
// Just store the root
|
||||
this.root = null
|
||||
@ -103,7 +103,7 @@ var Tree = (function () {
|
||||
}())
|
||||
|
||||
// Implementation of BST
|
||||
var bst = new Tree()
|
||||
const bst = new Tree()
|
||||
bst.addValue(6)
|
||||
bst.addValue(3)
|
||||
bst.addValue(9)
|
||||
|
@ -1,4 +1,4 @@
|
||||
var TrieNode = function TrieNode (key, parent) {
|
||||
const TrieNode = function TrieNode (key, parent) {
|
||||
this.key = key
|
||||
this.count = 0
|
||||
this.children = Object.create(null)
|
||||
@ -20,7 +20,7 @@ Trie.findAllWords = function (root, word, output) {
|
||||
if (root.count > 0) {
|
||||
if (typeof output === 'object') { output.push({ word: word, count: root.count }) }
|
||||
}
|
||||
var key
|
||||
let key
|
||||
for (key in root.children) {
|
||||
word += key
|
||||
this.findAllWords(root.children[key], word, output)
|
||||
@ -34,9 +34,9 @@ Trie.prototype.insert = function (word) {
|
||||
this.root.count += 1
|
||||
return
|
||||
}
|
||||
var node = this.root
|
||||
var len = word.length
|
||||
var i
|
||||
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)]
|
||||
@ -46,9 +46,9 @@ Trie.prototype.insert = function (word) {
|
||||
|
||||
Trie.prototype.findPrefix = function (word) {
|
||||
if (typeof word !== 'string') return null
|
||||
var node = this.root
|
||||
var len = word.length
|
||||
var i
|
||||
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
|
||||
@ -69,9 +69,9 @@ Trie.prototype.remove = function (word, count) {
|
||||
return
|
||||
}
|
||||
|
||||
var child = this.root
|
||||
var len = word.length
|
||||
var i, key
|
||||
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)
|
||||
@ -93,9 +93,9 @@ Trie.prototype.remove = function (word, count) {
|
||||
}
|
||||
|
||||
Trie.prototype.findAllWords = function (prefix) {
|
||||
var output = []
|
||||
const output = []
|
||||
// find the node with provided prefix
|
||||
var node = this.findPrefix(prefix)
|
||||
const node = this.findPrefix(prefix)
|
||||
// No such prefix exists
|
||||
if (node === null) return output
|
||||
Trie.findAllWords(node, prefix, output)
|
||||
@ -104,7 +104,7 @@ Trie.prototype.findAllWords = function (prefix) {
|
||||
|
||||
Trie.prototype.contains = function (word) {
|
||||
// find the node with given prefix
|
||||
var node = this.findPrefix(word)
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
if (node === null || node.count === 0) return false
|
||||
return true
|
||||
@ -112,7 +112,7 @@ Trie.prototype.contains = function (word) {
|
||||
|
||||
Trie.prototype.findOccurences = function (word) {
|
||||
// find the node with given prefix
|
||||
var node = this.findPrefix(word)
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
if (node === null) return 0
|
||||
return node.count
|
||||
@ -120,7 +120,7 @@ Trie.prototype.findOccurences = function (word) {
|
||||
|
||||
// To test
|
||||
(function demo () {
|
||||
var x = new Trie()
|
||||
const x = new Trie()
|
||||
x.insert('sheldon')
|
||||
x.insert('hello')
|
||||
x.insert('anyword')
|
||||
|
Reference in New Issue
Block a user