The insert method ends with the following two lines:
if currentIndex == characters.count {
currentNode.isTerminating = true
}
It seems that because of the condition on the while loop, this if will always be true and that the assignment alone is sufficient to complete this method. Are there any instances where this test makes a difference?
You mention that count, words and isEmpty are O(1) operations. isEmpty is fairly obvious. It seems that count would be O(1) if you introduce a new property count that gets increased every time isTerminating gets set to true and decreased every time it gets set to false. A count would then return the value of this property. Is there a better way to do this?
The words method would return a list of all of the words in the trie. I don’t see how this could be an O(1) operation. Wouldn’t it be a recursive traversal?
I would like to see code for serializing a trie using NSCoding and saving it to a file. I’ve looked around for examples that use NSCoding to serialize a recursive structure, but haven’t found any. Does anyone have ideas on how to do this? The only downside of adding this (that I can see) is that the trie nodes can no longer be generic. A class that implements NSCoding cannot be generic. This doesn’t seem like a big problem to me though. The Trie class is based on the fact that nodes contain characters.
Hmm, I’m not sure. I suppose you want to avoid reconstructing the tree during deserialization? One way could be extracting all the words of the Trie, serializing that into an array. When you deserialize, you can reassemble it into a Trie.
We should get more eyes on this question by posting it on the repo. Interesting question
This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]