Skip to content

Commit

Permalink
fix bug where certain phrases infinitely loop on adding a node to the…
Browse files Browse the repository at this point in the history
… tree
  • Loading branch information
vuldin committed Apr 4, 2018
1 parent 6d48af5 commit 0ea72a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
33 changes: 5 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ function Node(d) {
.slice(1)
.reverse()
.join('')
//result += this.children.size

result += `}`
}
return result
Expand All @@ -53,11 +51,10 @@ export default function Trie() {
let phrase = [this.phraseCount, isEop]
// create node
let node = new Node({ stem, phrase })

// add node to parent
// add node to parents
const padd = (n, itr) => {
for (let c of itr.values()) {
if (c.phrases.has(phrase[0])) {
if (c.phrases.has(phrase[0]) && c.stem !== n.stem) {
padd(n, c.children)
c.children.set(stem, n)
}
Expand All @@ -67,8 +64,9 @@ export default function Trie() {

// push node to root
let rCopy = this.root.children.get(stem)
if (!rCopy) this.root.children.set(stem, node)
else {
if (!rCopy) {
this.root.children.set(stem, node)
} else {
rCopy.phrases.set(phrase[0], phrase[1])
node = rCopy
}
Expand Down Expand Up @@ -120,25 +118,4 @@ export default function Trie() {
}
return result
}

this.toString = () => {
/* adding each phrase:
* - have you ever been to mars?
* - have you ever been to amsterdam?
* - have you been to the store?
* - have you eaten?
* - have you eaten yet?
* - i don't know what to do with my hands
* creates the following trie:
* 1 1,2:ever/3:been/4*,5:eaten/6:i
* 2 1,2:been/3*:store/5*:yet/6:don't
* 3 1*:mars/2*:amsterdam
* 4 6*:hands
*/
let str = ''
for (let val of this.root.children.values()) {
str += `|${val.word}`
}
return str
}
}
14 changes: 8 additions & 6 deletions tests/index-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import expect from 'expect'
import Trie from 'src/index'

describe('Trie tests', () => {
describe('@vuldin/trie', () => {
const trie = new Trie()
const children = trie.root.children
const phrase = 'this is a longer sentence'
Expand Down Expand Up @@ -41,15 +41,14 @@ describe('Trie tests', () => {
})
it('should handle any phrase', () => {
trie.add(phrase)
/*
for (let node of children.get('is').children.values()) {
console.log(node.toString())
}
*/
expect(children.size).toEqual(7)
expect(children.get('is').children.size).toEqual(2)
expect(children.get('is').children.get('longer')).toNotBe(undefined)
})
it('should handle phrases that previously caused infinite loop', () => {
const phrase = 'this is a yet another test'
expect(trie.add(phrase) instanceof Trie).toEqual(true)
})
it('finds an exact matching word', () => {
const result = trie.find('word')
expect(result.count).toEqual(1)
Expand All @@ -60,4 +59,7 @@ describe('Trie tests', () => {
expect(result.count).toEqual(3)
expect(result.exact).toEqual(true)
})
it('will not find a non-existent phrase', () => {
expect(trie.find(`this phrase doesn't exist`)).toEqual(false)
})
})

0 comments on commit 0ea72a6

Please sign in to comment.