You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Once again, you create a reference to the root node. You'll move this reference down a chain of nodes.
99
-
2. Keep track of the word you want to insert.
100
-
3. Begin walking through your word letter by letter
101
-
4. Sometimes, the required node to insert already exists. That is the case for two words inside the `Trie` that shares letters (i.e "Apple", "App"). If a letter already exists, you'll reuse it, and simply traverse deeper down the chain. Otherwise, you'll create a new node representing the letter.
102
-
5. Once you get to the end, you mark `isTerminating` to true to mark that specific node as the end of a word.
96
+
2. Begin walking through your word letter by letter
97
+
3. Sometimes, the required node to insert already exists. That is the case for two words inside the `Trie` that shares letters (i.e "Apple", "App"). If a letter already exists, you'll reuse it, and simply traverse deeper down the chain. Otherwise, you'll create a new node representing the letter.
98
+
4. Once you get to the end, you mark `isTerminating` to true to mark that specific node as the end of a word.
103
99
104
100
### Removal
105
101
@@ -109,41 +105,27 @@ If you'd like to remove "Apple", you'll need to take care to leave the "App" cha
109
105
110
106
```swift
111
107
funcremove(word: String) {
112
-
guard!word.isEmptyelse { return }
108
+
guard!word.isEmptyelse {
109
+
return
110
+
}
113
111
114
-
// 1
115
-
var currentNode = root
116
-
117
-
// 2
118
-
var characters =Array(word.lowercased().characters)
while currentNode.children.count==0, let parent = currentNode.parent, !parent.isTerminating {
135
-
currentNode = parent
136
-
currentNode.children[character!] =nil
137
-
character = currentNode.value
138
-
}
121
+
terminalNode.isTerminating=false
139
122
}
123
+
wordCount -=1
140
124
}
141
125
```
142
126
143
-
1. Once again, you create a reference to the root node.
144
-
2. Keep track of the word you want to remove.
145
-
3. Attempt to walk to the terminating node of the word. The `guard` statement will return if it can't find one of the letters; It's possible to call `remove` on a non-existant entry.
146
-
4. If you reach the node representing the last letter of the word you want to remove, you'll have 2 cases to deal with. Either it's a leaf node, or it has more children. If it has more children, it means the node is used for other words. In that case, you'll just mark `isTerminating` to false. In the other case, you'll delete the nodes.
127
+
1.`findTerminalNodeOf` traverses through the Trie to find the last node that represents the `word`. If it is unable to traverse through the chain of characters, it returns `nil`.
128
+
2.`deleteNodesForWordEndingWith` traverse backwords, deleting the nodes represented by the `word`.
0 commit comments