@@ -43,54 +43,53 @@ class TrieNode<T: Hashable> {
43
43
/// A trie data structure containing words. Each node is a single
44
44
/// character of a word.
45
45
class Trie : NSObject , NSCoding {
46
- typealias Node = TrieNode < Character >
47
- /// The number of words in the trie
48
- public var count : Int {
49
- return wordCount
50
- }
51
- /// Is the trie empty?
52
- public var isEmpty : Bool {
53
- return wordCount == 0
54
- }
55
- /// All words currently in the trie
56
- public var words : [ String ] {
57
- return wordsInSubtrie ( rootNode: root, partialWord: " " )
58
- }
59
- fileprivate let root : Node
60
- fileprivate var wordCount : Int
61
-
62
- /// Creates an empty trie.
63
- override init ( ) {
64
- root = Node ( )
65
- wordCount = 0
66
- super. init ( )
67
- }
46
+ typealias Node = TrieNode < Character >
47
+ /// The number of words in the trie
48
+ public var count : Int {
49
+ return wordCount
50
+ }
51
+ /// Is the trie empty?
52
+ public var isEmpty : Bool {
53
+ return wordCount == 0
54
+ }
55
+ /// All words currently in the trie
56
+ public var words : [ String ] {
57
+ return wordsInSubtrie ( rootNode: root, partialWord: " " )
58
+ }
59
+ fileprivate let root : Node
60
+ fileprivate var wordCount : Int
61
+
62
+ /// Creates an empty trie.
63
+ override init ( ) {
64
+ root = Node ( )
65
+ wordCount = 0
66
+ super. init ( )
67
+ }
68
68
69
- // MARK: NSCoding
69
+ // MARK: NSCoding
70
70
71
- /// Initializes the trie with words from an archive
72
- ///
73
- /// - Parameter decoder: Decodes the archive
74
- required convenience init ? ( coder decoder: NSCoder ) {
75
- self . init ( )
76
- let words = decoder. decodeObject ( forKey: " words " ) as? [ String ]
77
- for word in words! {
78
- self . insert ( word: word)
79
- }
71
+ /// Initializes the trie with words from an archive
72
+ ///
73
+ /// - Parameter decoder: Decodes the archive
74
+ required convenience init ? ( coder decoder: NSCoder ) {
75
+ self . init ( )
76
+ let words = decoder. decodeObject ( forKey: " words " ) as? [ String ]
77
+ for word in words! {
78
+ self . insert ( word: word)
80
79
}
80
+ }
81
81
82
- /// Encodes the words in the trie by putting them in an array then encoding
83
- /// the array.
84
- ///
85
- /// - Parameter coder: The object that will encode the array
86
- func encode( with coder: NSCoder ) {
87
- coder. encode ( self . words, forKey: " words " )
88
- }
82
+ /// Encodes the words in the trie by putting them in an array then encoding
83
+ /// the array.
84
+ ///
85
+ /// - Parameter coder: The object that will encode the array
86
+ func encode( with coder: NSCoder ) {
87
+ coder. encode ( self . words, forKey: " words " )
88
+ }
89
89
}
90
90
91
91
// MARK: - Adds methods: insert, remove, contains
92
92
extension Trie {
93
-
94
93
/// Inserts a word into the trie. If the word is already present,
95
94
/// there is no change.
96
95
///
@@ -142,14 +141,14 @@ extension Trie {
142
141
/// - Returns: the node where the search ended, nil if the
143
142
/// search failed.
144
143
private func findLastNodeOf( word: String ) -> Node ? {
145
- var currentNode = root
146
- for character in word. lowercased ( ) {
147
- guard let childNode = currentNode. children [ character] else {
148
- return nil
149
- }
150
- currentNode = childNode
144
+ var currentNode = root
145
+ for character in word. lowercased ( ) {
146
+ guard let childNode = currentNode. children [ character] else {
147
+ return nil
151
148
}
152
- return currentNode
149
+ currentNode = childNode
150
+ }
151
+ return currentNode
153
152
}
154
153
155
154
/// Attempts to walk to the terminating node of a word. The
@@ -160,10 +159,9 @@ extension Trie {
160
159
/// search failed.
161
160
private func findTerminalNodeOf( word: String ) -> Node ? {
162
161
if let lastNode = findLastNodeOf ( word: word) {
163
- return lastNode. isTerminating ? lastNode : nil
162
+ return lastNode. isTerminating ? lastNode : nil
164
163
}
165
164
return nil
166
-
167
165
}
168
166
169
167
/// Deletes a word from the trie by starting with the last letter
@@ -237,17 +235,17 @@ extension Trie {
237
235
/// - prefix: the letters for word prefix
238
236
/// - Returns: the words in the subtrie that start with prefix
239
237
func findWordsWithPrefix( prefix: String ) -> [ String ] {
240
- var words = [ String] ( )
241
- let prefixLowerCased = prefix. lowercased ( )
242
- if let lastNode = findLastNodeOf ( word: prefixLowerCased) {
243
- if lastNode. isTerminating {
244
- words. append ( prefixLowerCased)
245
- }
246
- for childNode in lastNode. children. values {
247
- let childWords = wordsInSubtrie ( rootNode: childNode, partialWord: prefixLowerCased)
248
- words += childWords
249
- }
238
+ var words = [ String] ( )
239
+ let prefixLowerCased = prefix. lowercased ( )
240
+ if let lastNode = findLastNodeOf ( word: prefixLowerCased) {
241
+ if lastNode. isTerminating {
242
+ words. append ( prefixLowerCased)
243
+ }
244
+ for childNode in lastNode. children. values {
245
+ let childWords = wordsInSubtrie ( rootNode: childNode, partialWord: prefixLowerCased)
246
+ words += childWords
250
247
}
251
- return words
248
+ }
249
+ return words
252
250
}
253
251
}
0 commit comments