Skip to content

Commit

Permalink
Allow for 'slow', 'slower', 'slowly'
Browse files Browse the repository at this point in the history
Add some stacked Insert & Find tests for better integration testing

Allow for unexpected edge case where 'slow' can be an entry but NOT a leaf

Add 'IsEntry' method to allow for non-terminal entries (such as 'slow')

Add additional tests for 'test', 'toaster', 'toasting'
  • Loading branch information
mramshaw committed May 14, 2018
1 parent 38f074a commit 0d4152b
Show file tree
Hide file tree
Showing 4 changed files with 747 additions and 213 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ than for system testing.

- [ ] Investigate applications of Patricia tries
- [x] Refactor tests to avoid some of the duplicated code
- [ ] Find out the idiom for stacking __Insert__ and __Find__ tests (avoiding mocks)
- [x] Add code and tests to allow for entries such as "slow", "slower", "slowly"
- [x] Find out the idiom for stacking __Insert__ and __Find__ tests (avoiding mocks)
- [ ] Investigate whether byte-based __and__ rune-based options are viable
- [ ] Find more examples of tries in use - specifically Rune-based CJKV (Chinese, Japanese, Korean, Vietnamese)
- [ ] Find out whether the usual practice is to sort trie entries (the Wikipedia example __is__ sorted)
Expand Down
17 changes: 13 additions & 4 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ type Node struct {
value string
children []*Node
childCount int
entry bool
}

// IsEntry may be called to determine if the current node is
// terminal for an entry. Note that this node may or may not
// also be a leaf (in the case of 'slow' and 'slowly', both
// are entries but only 'slowly' is a leaf).
func (n *Node) IsEntry() bool {
return n.entry
}

// IsLeaf may be called to determine if the current node is a leaf.
func (n *Node) IsLeaf() bool {
return n.childCount == 0
}

func (n *Node) makeChildNode(s string) *Node {
func (n *Node) makeChildNode(s string, entry bool) *Node {
//fmt.Printf("makingChildNode: %s\n", s)
child := makeNode(s)
child := makeNode(s, entry)
n.childCount++
if n.children == nil {
n.children = []*Node{&child}
Expand All @@ -35,7 +44,7 @@ func (n *Node) setChildNode(newNode *Node) bool {
return true
}

func makeNode(s string) Node {
func makeNode(s string, isEntry bool) Node {
//fmt.Printf("makingNode: %s\n", s)
return Node{value: s, childCount: 0}
return Node{value: s, childCount: 0, entry: isEntry}
}
15 changes: 8 additions & 7 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,35 @@ func (t *Trie) insertRuneNode(parent *Node, n *Node, s string) bool {
lenC := len(c.value)
if index > 0 {
if index == lenC {
return t.insertRuneNode(parent, c, s[index:])
return t.insertRuneNode(c, c, s[index:])
}
if index < lenC {
child := c.makeChildNode(c.value[index:])
child := c.makeChildNode(c.value[index:], true)
child.children = c.children
child.childCount = c.childCount
child.childCount = 0
c.setChildNode(child)
//fmt.Printf("c.value: %s\n", c.value)
c.value = c.value[:index]
//fmt.Printf("c.value: %s\n", c.value)
c.entry = false
}
//fmt.Printf("making child node: %s\n", s[index:])
c.makeChildNode(s[index:])
c.makeChildNode(s[index:], true)
t.count++
return true
}
}

// No match in the children so attach to the parent node
//fmt.Printf("parented.value2: %s\n", s)
parent.makeChildNode(s)
parent.makeChildNode(s, true)
t.count++
return true
}

func (t *Trie) makeRuneNode(s string) {
rootRune := makeNode(s[:1])
rootChild := makeNode(s[1:])
rootRune := makeNode(s[:1], false)
rootChild := makeNode(s[1:], true)
rootRune.children = []*Node{&rootChild}
rootRune.childCount = 1
t.child = []*Node{&rootRune}
Expand Down
Loading

0 comments on commit 0d4152b

Please sign in to comment.