Skip to content

Commit

Permalink
fix: trie key's ascii bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cemayan committed Jul 31, 2024
1 parent 02cf658 commit 0974e1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (t *trie) Insert(word string) {
func (t *trie) insert(word string, root *trieNode) {

insertCounter++
currentWord := word[0:insertCounter]
currentWord := string([]rune(word)[0:insertCounter])

// empty node
node := &trieNode{make(map[string]*trieNode), false}
Expand All @@ -65,7 +65,7 @@ func (t *trie) insert(word string, root *trieNode) {
// add new children to current node
root.children[currentWord] = node

if len(word) != insertCounter {
if len(word)-1 != insertCounter {
// recursion until reaching to last word
// example: tea || t -> te -> tea
t.insert(word, root.children[currentWord])
Expand Down Expand Up @@ -108,12 +108,12 @@ func (t *trie) traversalSearch(node *trieNode) {
func (t *trie) search(prefix string, root *trieNode, arr []string) []string {

searchCounter++
currentWord := prefix[0:searchCounter]
currentWord := string([]rune(prefix)[0:searchCounter])

n := root.children[currentWord]

//This condition for reach the actual node
if searchCounter != len(prefix) {
if (searchCounter != len(prefix)-1) && len(prefix) != 1 {
if n != nil {
arr = t.search(prefix, n, nil)
}
Expand Down
8 changes: 5 additions & 3 deletions trie/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ func (t *trie) ConvertForIndexing(data string) map[string]map[string]int {

lastMap := make(map[string]map[string]int)

for i := 1; i <= len(data); i++ {
for i := 1; i <= len(data)-1; i++ {
s := string([]rune(data)[0:i])

recordsMap := make(map[string]int)
array := t.SearchByPrefix(data[0:i])
array := t.SearchByPrefix(s)
for _, v := range array {
recordsMap[v] = 0
}

lastMap[data[0:i]] = recordsMap
lastMap[s] = recordsMap
}

return lastMap
Expand Down

0 comments on commit 0974e1e

Please sign in to comment.