Skip to content

Commit e22a42a

Browse files
committed
Merge branch 'master' of github.com:axptwig/swift-algorithm-club
2 parents 006e74d + 8d0a20c commit e22a42a

File tree

2 files changed

+66
-51
lines changed

2 files changed

+66
-51
lines changed

Trie/trie

4.76 KB
Binary file not shown.

Trie/trie.swift

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class Node {
6262
return self.children
6363
}
6464

65+
6566
func printNode(var indent: String, leaf: Bool) -> Void {
6667

6768
print(indent, terminator: "")
@@ -96,17 +97,32 @@ public class Trie {
9697
self.wordList = []
9798
}
9899

99-
func find(key: String) -> (key: String, found: Bool) {
100+
init(wordList: Set<String>) {
101+
102+
self.root = Node(c: "", p: nil)
103+
self.wordList = []
104+
105+
for word in wordList {
106+
self.insert(word)
107+
}
108+
}
109+
110+
func merge(other: Trie) -> Trie{
111+
let newWordList = Set(self.getWords() + other.getWords())
112+
return Trie(wordList: newWordList)
113+
}
114+
115+
func find(key: String) -> (node: Node?, found: Bool) {
100116
var currentNode = self.root
101117

102118
for c in key.characters {
103119
if currentNode.children[String(c)] == nil {
104-
return(key, false)
120+
return(nil, false)
105121
}
106122
currentNode = currentNode.children[String(c)]!
107123
}
108124

109-
return(key, currentNode.isValidWord())
125+
return(currentNode, currentNode.isValidWord())
110126
}
111127

112128
func isEmpty() -> Bool {
@@ -125,21 +141,20 @@ public class Trie {
125141
return find(w.lowercaseString).found
126142
}
127143

128-
func isPrefix(w: String) -> (node: Node?, found: Bool) {
129-
var currentNode = self.root
144+
func isPrefix(p: String) -> (node: Node?, found: Bool) {
145+
let prefixP = p.lowercaseString
130146

131-
let word = w.lowercaseString
132-
if !self.contains(w) {
133-
return (nil,false)
134-
}
147+
var currentNode = self.root
135148

136-
for c in word.characters {
137-
if let child = currentNode.children[String(c)] {
138-
currentNode = child
149+
for c in prefixP.characters {
150+
if currentNode.children[String(c)] == nil{
151+
return (nil, false)
139152
}
153+
154+
currentNode = currentNode.children[String(c)]!
140155
}
141156

142-
if currentNode.getChildren().count > 0 {
157+
if currentNode.numChildren() > 0 {
143158
return (currentNode, true)
144159
}
145160

@@ -167,7 +182,6 @@ public class Trie {
167182
}
168183

169184
let remainingChars = String(word.characters.suffix(length))
170-
print(remainingChars)
171185
for c in remainingChars.characters {
172186
currentNode.children[String(c)] = Node(c: String(c), p: currentNode)
173187
currentNode = currentNode.children[String(c)]!
@@ -180,7 +194,6 @@ public class Trie {
180194
}
181195

182196
func remove(w: String) -> (word: String, removed: Bool){
183-
184197
let word = w.lowercaseString
185198

186199
if(!self.contains(w)) {
@@ -196,13 +209,13 @@ public class Trie {
196209
currentNode.isNotWord()
197210
} else {
198211
var character = currentNode.char()
199-
while(currentNode.numChildren() < 1) {
212+
while(currentNode.numChildren() == 0 && !currentNode.isRoot()) {
213+
print(currentNode.getParent().char())
200214
currentNode = currentNode.getParent()
201215
currentNode.children[character]!.setParent(nil)
202216
currentNode.children[character]!.update(nil)
203217
currentNode.children[character] = nil
204218
character = currentNode.char()
205-
206219
}
207220
}
208221

@@ -221,46 +234,46 @@ public class Trie {
221234

222235
private func getChildrenWithPrefix(node: Node, var word: String, var words: [String]) -> [String] {
223236

224-
if node.isLeaf() {
225-
word += node.char()
237+
print(word)
226238

239+
if node.isLeaf() && node.isValidWord() {
227240
words.append(word)
241+
print(words)
228242

229-
}
243+
} else {
230244

231-
for (child, n) in node.getChildren() {
232-
word += child
233-
getChildrenWithPrefix(n, word: word, words: words)
245+
for (child, n) in node.getChildren(){
246+
print(child)
247+
word += child
248+
getChildrenWithPrefix(n, word: word, words: words)
249+
}
234250
}
235251

236252
return words
237253
}
238254

239255
func findPrefix(p: String) -> [String] {
256+
print("Entered")
257+
258+
259+
//var (node, pFound: Bool) = self.isPrefix(p)
240260
if self.isPrefix(p).found {
241-
print("here")
242-
return getChildrenWithPrefix(self.isPrefix(p).node!, word: "", words: [])
261+
print("I found the prefix!")
262+
return getChildrenWithPrefix(self.isPrefix(p).node!, word: p.lowercaseString, words: [])
243263
}
244264

245-
return []
265+
return ["HE"]
246266
}
247267

248268

249-
private func removeAllHelper(node: Node, w: String) {
250-
251-
if(node.getChildren().count == 0) {
252-
269+
func removeAll() -> Void {
270+
for word in wordList {
271+
self.remove(word)
253272
}
254-
255-
256-
273+
self.root.update(nil)
257274
}
258275

259276

260-
func removeAll(w: String) {
261-
262-
}
263-
264277
func printTrie() {
265278
self.root.printNode("", leaf: true)
266279
}
@@ -277,6 +290,19 @@ print(x.isValidWord())*/
277290

278291

279292
var T: Trie = Trie()
293+
T.insert("Hello")
294+
T.insert("Hi")
295+
T.insert("Hey")
296+
T.insert("Hallo")
297+
T.insert("Henry")
298+
var U: Trie = Trie(wordList: Set(["Hey", "HO", "hello", "yolo"]))
299+
var V: Trie = T.merge(U)
300+
//T.printTrie()
301+
//U.printTrie()
302+
//V.printTrie()
303+
print(V.getWords())
304+
V.removeAll()
305+
//V.printTrie()
280306
/*T.insert("Hello")
281307
T.insert("Hey")
282308
T.insert("YOLO")
@@ -290,17 +316,6 @@ assert(T.count() == 3)
290316
assert(T.contains("Him") == false, "Test failed")
291317
assert(T.wordList.count == 3)*/
292318

293-
294-
//T.insert("Hello")
295-
T.insert("Hi")
296-
T.insert("Hey")
297-
//T.insert("Hallo")
298-
T.insert("Henry")
299-
T.printTrie()
300-
assert(T.contains("Henry") == true)
301-
T.remove("Henry")
302-
assert(T.contains("Henry") == false)
303-
//assert(T.count() == 4)
304-
assert(T.isPrefix("Hen").found == false)
305-
T.printTrie()
306-
print(T.findPrefix("H"))
319+
//T.printTrie()
320+
//print(T.find(""))
321+
//print(T.findPrefix("H"))

0 commit comments

Comments
 (0)