Skip to content

Commit f27c4aa

Browse files
committed
Removes old implementation. Updated README to reflect the new code changes.
1 parent f14c133 commit f27c4aa

File tree

8 files changed

+201
-873
lines changed

8 files changed

+201
-873
lines changed

Trie/Old Implementation/trie.swift

Lines changed: 0 additions & 523 deletions
This file was deleted.

Trie/ReadMe.md

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,38 @@ Insertion into a `Trie` requires you to walk over the nodes until you either hal
6464

6565
```swift
6666
func insert(word: String) {
67-
guard !word.isEmpty else { return }
67+
guard !word.isEmpty else {
68+
return
69+
}
6870

6971
// 1
7072
var currentNode = root
71-
72-
// 2
73-
var characters = Array(word.lowercased().characters)
74-
var currentIndex = 0
75-
76-
// 3
77-
while currentIndex < characters.count {
78-
let character = characters[currentIndex]
7973

80-
// 4
81-
if let child = currentNode.children[character] {
82-
currentNode = child
74+
// 2
75+
for character in word.lowercased().characters {
76+
// 3
77+
if let childNode = currentNode.children[character] {
78+
currentNode = childNode
8379
} else {
84-
currentNode.add(child: character)
80+
currentNode.add(value: character)
8581
currentNode = currentNode.children[character]!
8682
}
87-
88-
currentIndex += 1
89-
90-
// 5
91-
if currentIndex == characters.count {
92-
currentNode.isTerminating = true
93-
}
9483
}
84+
// Word already present?
85+
guard !currentNode.isTerminating else {
86+
return
87+
}
88+
89+
// 4
90+
wordCount += 1
91+
currentNode.isTerminating = true
9592
}
9693
```
9794

9895
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.
10399

104100
### Removal
105101

@@ -109,41 +105,27 @@ If you'd like to remove "Apple", you'll need to take care to leave the "App" cha
109105

110106
```swift
111107
func remove(word: String) {
112-
guard !word.isEmpty else { return }
108+
guard !word.isEmpty else {
109+
return
110+
}
113111

114-
// 1
115-
var currentNode = root
116-
117-
// 2
118-
var characters = Array(word.lowercased().characters)
119-
var currentIndex = 0
120-
121-
// 3
122-
while currentIndex < characters.count {
123-
let character = characters[currentIndex]
124-
guard let child = currentNode.children[character] else { return }
125-
currentNode = child
126-
currentIndex += 1
112+
// 1
113+
guard let terminalNode = findTerminalNodeOf(word: word) else {
114+
return
127115
}
128-
129-
// 4
130-
if currentNode.children.count > 0 {
131-
currentNode.isTerminating = false
116+
117+
// 2
118+
if terminalNode.isLeaf {
119+
deleteNodesForWordEndingWith(terminalNode: terminalNode)
132120
} else {
133-
var character = currentNode.value
134-
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
139122
}
123+
wordCount -= 1
140124
}
141125
```
142126

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`.
147129

148130
### Time Complexity
149131

Trie/Trie.playground/Contents.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

Trie/Trie.playground/Sources/Node.swift

Lines changed: 0 additions & 21 deletions
This file was deleted.

Trie/Trie.playground/Sources/Trie.swift

Lines changed: 0 additions & 83 deletions
This file was deleted.

Trie/Trie.playground/contents.xcplayground

Lines changed: 0 additions & 4 deletions
This file was deleted.

Trie/Trie.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)