forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored implementation of Trie. Leaving the old implementation in …
…a folder for future reference.
- Loading branch information
Kelvin Lau
authored and
Kelvin Lau
committed
Sep 12, 2016
1 parent
8a679ff
commit 1da1118
Showing
5 changed files
with
80 additions
and
267 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
let trie = Trie() | ||
|
||
trie.insert(word: "apple") | ||
trie.insert(word: "ap") | ||
trie.insert(word: "a") | ||
|
||
trie.contains(word: "apple") | ||
trie.contains(word: "ap") | ||
trie.contains(word: "a") | ||
|
||
trie.remove(word: "apple") | ||
trie.contains(word: "a") | ||
trie.contains(word: "apple") | ||
|
||
trie.insert(word: "apple") | ||
trie.contains(word: "apple") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,21 @@ | ||
/// A Trie (prefix tree) | ||
/// | ||
/// Some of the functionality of the trie makes use of the Queue implementation for this project | ||
/// | ||
/// Every node in the Trie stores a bit of information pertaining to what it references: | ||
/// -Character (letter of an inserted word) | ||
/// -Parent (Letter that comes before the current letter in some word) | ||
/// -Children (Words that have more letters than available in the prefix) | ||
/// -isAWord (Does the current letter mark the end of a known inserted word?) | ||
/// -visited (Mainly for the findPrefix() function) | ||
public class Node { | ||
public var character: String | ||
public var parent: Node? | ||
public var children: [String: Node] | ||
public var isAWord: Bool | ||
public var visited: Bool // only for findPrefix | ||
public final class TrieNode<T: Hashable> { | ||
public var value: T? | ||
public weak var parent: TrieNode? | ||
public var children: [T: TrieNode] = [:] | ||
public var isTerminating = false | ||
|
||
init(character: String, parent: Node?) { | ||
self.character = character | ||
self.children = [:] | ||
self.isAWord = false | ||
self.parent = parent | ||
self.visited = false | ||
} | ||
|
||
/// Returns `true` if the node is a leaf node, false otherwise. | ||
var isLeaf: Bool { | ||
return children.count == 0 | ||
} | ||
|
||
/// Changes the parent of the current node to the passed in node. | ||
/// | ||
/// - parameter node: A `Node` object. | ||
func setParent(node: Node) { | ||
parent = node | ||
} | ||
init() {} | ||
|
||
/// Returns the child node that holds the specific passed letter. | ||
/// | ||
/// - parameter character: A `String` | ||
/// | ||
/// - returns: The `Node` object that contains the `character`. | ||
func getChildAt(character: String) -> Node { | ||
return children[character]! | ||
} | ||
|
||
/// Returns whether or not the current node marks the end of a valid word. | ||
var isValidWord: Bool { | ||
return isAWord | ||
} | ||
|
||
|
||
/// Returns whether or not the current node is the root of the trie. | ||
var isRoot: Bool { | ||
return character == "" | ||
init(value: T, parent: TrieNode? = nil) { | ||
self.value = value | ||
self.parent = parent | ||
} | ||
} | ||
|
||
// MARK: - CustomStringConvertible | ||
extension Node: CustomStringConvertible { | ||
public var description: String { | ||
return "" | ||
// MARK: - Insertion | ||
public extension TrieNode { | ||
func add(child: T) { | ||
guard children[child] == nil else { return } | ||
children[child] = TrieNode(value: child, parent: self) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters