fix error with 'helpExists' function; the error was that we recursed in 'helpExists' function by calling keyPos + 1 (nextKeyPos) which is the wrong way to search when the trie key has a string size larger than 1

This commit is contained in:
2024-09-04 13:32:45 +01:00
parent 6696480a39
commit 65eff83c0b

View File

@@ -79,17 +79,19 @@ struct
SOME idx => SOME idx =>
let let
val trieKey = Vector.sub (keys, idx) val trieKey = Vector.sub (keys, idx)
val nextKeyPos = keyPos + 1
in in
(case searchKeyMatch (searchKey, trieKey, nextKeyPos) of (case searchKeyMatch (searchKey, trieKey, keyPos + 1) of
NO_SEARCH_MATCH => false NO_SEARCH_MATCH => false
| FULL_SEARCH_MATCH => | FULL_SEARCH_MATCH =>
let val trieChild = Vector.sub (children, idx) let val trieChild = Vector.sub (children, idx)
in isFoundNode trieChild in isFoundNode trieChild
end end
| SEARCH_KEY_CONTAINS_TRIE_KEY => | SEARCH_KEY_CONTAINS_TRIE_KEY =>
let val trieChild = Vector.sub (children, idx) let
in helpExists (searchKey, nextKeyPos, trieChild) val trieChild = Vector.sub (children, idx)
in
helpExists
(searchKey, keyPos + String.size trieKey, trieChild)
end end
| TRIE_KEY_CONTAINS_SEARCH_KEY => false) | TRIE_KEY_CONTAINS_SEARCH_KEY => false)
end end
@@ -103,17 +105,19 @@ struct
SOME idx => SOME idx =>
let let
val trieKey = Vector.sub (keys, idx) val trieKey = Vector.sub (keys, idx)
val nextKeyPos = keyPos + 1
in in
(case searchKeyMatch (searchKey, trieKey, nextKeyPos) of (case searchKeyMatch (searchKey, trieKey, keyPos + 1) of
NO_SEARCH_MATCH => false NO_SEARCH_MATCH => false
| FULL_SEARCH_MATCH => | FULL_SEARCH_MATCH =>
let val trieChild = Vector.sub (children, idx) let val trieChild = Vector.sub (children, idx)
in isFoundNode trieChild in isFoundNode trieChild
end end
| SEARCH_KEY_CONTAINS_TRIE_KEY => | SEARCH_KEY_CONTAINS_TRIE_KEY =>
let val trieChild = Vector.sub (children, idx) let
in helpExists (searchKey, nextKeyPos, trieChild) val trieChild = Vector.sub (children, idx)
in
helpExists
(searchKey, keyPos + String.size trieKey, trieChild)
end end
| TRIE_KEY_CONTAINS_SEARCH_KEY => false) | TRIE_KEY_CONTAINS_SEARCH_KEY => false)
end end
@@ -272,10 +276,7 @@ struct
val splitTrieKeyStart = String.substring (trieKey, 0, diffIdx) val splitTrieKeyStart = String.substring (trieKey, 0, diffIdx)
val trieChild = Vector.sub (children, insIdx) val trieChild = Vector.sub (children, insIdx)
in in
if if String.sub (trieKey, diffIdx) > String.sub (insKey, diffIdx) then
String.sub (trieKey, diffIdx)
> String.sub (insKey, diffIdx)
then
(* place insKey before trieKey *) (* place insKey before trieKey *)
let let
val childKeys = Vector.fromList [insKey, trieKey] val childKeys = Vector.fromList [insKey, trieKey]
@@ -427,16 +428,8 @@ struct
end end
| fromList ([]) = raise Empty | fromList ([]) = raise Empty
(* test trie *)
val trie = fromList
["hello", "hi", "bye", "below", "tree", "try", "why", "what"]
(* (*
* todo: * todo:
* Insertion function works (and seems to work exactly as intended), * Implement prefix searching, returning all keys that match a given string prefix.
* but there seems to be a problem with the 'exists' function,
* because some strings ("try" and "why") are correctly stored
* in the trie, as shown by the REPL,
* but 'exists' for them returns false.
*) *)
end end