|
| 1 | +var TrieNode = function TrieNode (key, parent) { |
| 2 | + this.key = key |
| 3 | + this.count = 0 |
| 4 | + this.children = Object.create(null) |
| 5 | + if (parent === undefined) { |
| 6 | + this.parent = null |
| 7 | + } else { |
| 8 | + this.parent = parent |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +function Trie () { |
| 13 | + // create only root with null key and parent |
| 14 | + this.root = new TrieNode(null, null) |
| 15 | +} |
| 16 | + |
| 17 | +// Recursively finds the occurence of all words in a given node |
| 18 | +Trie.findAllWords = function (root, word, output) { |
| 19 | + if (root === null) return |
| 20 | + if (root.count > 0) { |
| 21 | + if (typeof output === 'object') { output.push({ word: word, count: root.count }) } |
| 22 | + } |
| 23 | + var key |
| 24 | + for (key in root.children) { |
| 25 | + word += key |
| 26 | + this.findAllWords(root.children[key], word, output) |
| 27 | + word = word.slice(0, -1) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +Trie.prototype.insert = function (word) { |
| 32 | + if (typeof word !== 'string') return |
| 33 | + if (word === '') { |
| 34 | + this.root.count += 1 |
| 35 | + return |
| 36 | + } |
| 37 | + var node = this.root |
| 38 | + var len = word.length |
| 39 | + var i |
| 40 | + for (i = 0; i < len; i++) { |
| 41 | + if (node.children[word.charAt(i)] === undefined) { node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node) } |
| 42 | + node = node.children[word.charAt(i)] |
| 43 | + } |
| 44 | + node.count += 1 |
| 45 | +} |
| 46 | + |
| 47 | +Trie.prototype.findPrefix = function (word) { |
| 48 | + if (typeof word !== 'string') return null |
| 49 | + var node = this.root |
| 50 | + var len = word.length |
| 51 | + var i |
| 52 | + // After end of this loop node will be at desired prefix |
| 53 | + for (i = 0; i < len; i++) { |
| 54 | + if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists |
| 55 | + node = node.children[word.charAt(i)] |
| 56 | + } |
| 57 | + return node |
| 58 | +} |
| 59 | + |
| 60 | +Trie.prototype.remove = function (word, count) { |
| 61 | + if (typeof word !== 'string') return |
| 62 | + if (typeof count !== 'number') count = 1 |
| 63 | + else if (count <= 0) return |
| 64 | + |
| 65 | + // for empty string just delete count of root |
| 66 | + if (word === '') { |
| 67 | + if (this.root.count >= count) this.root.count -= count |
| 68 | + else this.root.count = 0 |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + var child = this.root |
| 73 | + var len = word.length |
| 74 | + var i, key |
| 75 | + // child: node which is to be deleted |
| 76 | + for (i = 0; i < len; i++) { |
| 77 | + key = word.charAt(i) |
| 78 | + if (child.children[key] === undefined) return |
| 79 | + child = child.children[key] |
| 80 | + } |
| 81 | + |
| 82 | + // Delete no of occurences specified |
| 83 | + if (child.count >= count) child.count -= count |
| 84 | + else child.count = 0 |
| 85 | + |
| 86 | + // If some occurences are left we dont delete it or else |
| 87 | + // if the object forms some other objects prefix we dont delete it |
| 88 | + // For checking an empty object |
| 89 | + // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object |
| 90 | + if (child.count <= 0 && (Object.keys(child.children).length && child.childre.constructor === Object)) { |
| 91 | + child.parent.children[child.key] = undefined |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +Trie.prototype.findAllWords = function (prefix) { |
| 96 | + var output = [] |
| 97 | + // find the node with provided prefix |
| 98 | + var node = this.findPrefix(prefix) |
| 99 | + // No such prefix exists |
| 100 | + if (node === null) return output |
| 101 | + Trie.findAllWords(node, prefix, output) |
| 102 | + return output |
| 103 | +} |
| 104 | + |
| 105 | +Trie.prototype.contains = function (word) { |
| 106 | + // find the node with given prefix |
| 107 | + var node = this.findPrefix(word) |
| 108 | + // No such word exists |
| 109 | + if (node === null || node.count === 0) return false |
| 110 | + return true |
| 111 | +} |
| 112 | + |
| 113 | +Trie.prototype.findOccurences = function (word) { |
| 114 | + // find the node with given prefix |
| 115 | + var node = this.findPrefix(word) |
| 116 | + // No such word exists |
| 117 | + if (node === null) return 0 |
| 118 | + return node.count |
| 119 | +}; |
| 120 | + |
| 121 | +// To test |
| 122 | +(function demo () { |
| 123 | + var x = new Trie() |
| 124 | + x.insert('sheldon') |
| 125 | + x.insert('hello') |
| 126 | + x.insert('anyword') |
| 127 | + x.insert('sheldoncooper') |
| 128 | + console.log(x.findOccurences('sheldon')) |
| 129 | + x.remove('anything') |
| 130 | + x.insert('sheldon') |
| 131 | + console.log(x.findOccurences('sheldon')) |
| 132 | + console.log(x.findAllWords('sheldon')) |
| 133 | + x.insert('anything') |
| 134 | + x.remove('sheldoncooper') |
| 135 | + console.log(x.contains('sheldoncooper')) |
| 136 | + console.log(x.findAllWords('sheldon')) |
| 137 | +})() |
0 commit comments