8000 More progress · datengx/swift-algorithm-club@e159902 · GitHub
[go: up one dir, main page]

Skip to content

Commit e159902

Browse files
kelvinlauKLremlostime
authored andcommitted
More progress
1 parent 959aaaf commit e159902

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

Encode and Decode Tree/readme.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Here's an example of this operation in code:
3434
```swift
3535
extension BinaryNode {
3636
// 1
37-
fileprivate var splitter: String { return "," }
38-
fileprivate var nilNode: String { return "nil" }
37+
fileprivate var separator: String { return "," }
38+
fileprivate var nilNode: String { return "X" }
3939

4040
// 2
4141
var encodedString: String {
@@ -47,33 +47,33 @@ extension BinaryNode {
4747
} else {
4848
str.append(nilNode)
4949
}
50-
str.append(splitter)
50+
str.append(separator)
5151
}
5252
return str
5353
}
5454

5555
// 3
56-
func preOrderTraversal(visit: (T?) -> ()) {
57-
visit(data)
56+
func preOrderTraversal(visit: (Element?) throws -> ()) rethrows {
57+
try visit(data)
5858

5959
if let leftChild = leftChild {
60-
leftChild.preOrderTraversal(visit: visit)
60+
try leftChild.preOrderTraversal(visit: visit)
6161
} else {
62-
visit(nil)
62+
try visit(nil)
6363
}
6464

6565
if let rightChild = rightChild {
66-
rightChild.preOrderTraversal(visit: visit)
66+
try rightChild.preOrderTraversal(visit: visit)
6767
} else {
68-
visit(nil)
68+
try visit(nil)
6969
}
7070
}
7171
}
7272
```
7373

7474
Here's a high level overview of the above code:
7575

76-
1. `splitter` is a way to distinguish the nodes in a string. To illustrate its importance, consider the following encoded string "banana". How did the tree structure look like before encoding? Without the `splitter`, you can't tell.
76+
1. `separator` is a way to distinguish the nodes in a string. To illustrate its importance, consider the following encoded string "banana". How did the tree structure look like before encoding? Without the `separator`, you can't tell.
7777

7878
2. `encodedString` is the result of the encoding process. Returns a string representation of the tree. For example: "ba,nana,nil" represents a tree with two nodes - "ba" and "nana" - in pre-order format.
7979

@@ -96,34 +96,36 @@ The implementation also added a few important details:
9696
These details will shape your `decode` operation. Here's a possible implementation:
9797

9898
```swift
99-
extension BinaryTree {
100-
99+
extension BinaryNode {
100+
101101
// 1
102-
static func decode<Element>(from string: String) -> BinaryNode<Element>? {
103-
   let array = string.split(separator: ",")
104-
return decode(from: array)
102+
public static func decode(from str: String) -> AVLNode<String>? {
103+
let components = encoded.lazy.split(separator: separator).reversed().map(String.init)
104+
return decode(from: components)
105105
}
106-
107-
// 2
108-
static func decode<Elements: Sequence>(from sequence: Sequence)
109-
-> BinaryNode<Elements.Element>? {
106+
107+
public static func decode(from array: inout [String])
108+
-> AVLNode<String>? {
110109

111-
guard let value = sequence.first else { return nil }
112-
if value == nilNode {
113-
return nil
114-
} else {
115-
let node = BinaryNode<Elements.Element>(value: value)
116-
node.leftChild = decode(from: AnySequence<Elements.Element>(sequence.dropFirst()))
117-
node.rightChild = decode(from: AnySequence<Elements.Element>(sequence.dropFirst()))
118-
}
110+
guard !array.isEmpty else { return nil }
111+
let value = array.removeLast()
112+
guard value != "\(nilNode)" else { return nil }
113+
114+
let node = AVLNode<String>(value: value)
115+
node.leftChild = decode(from: &array)
116+
node.rightChild = decode(from: &array)
117+
return node
119118
}
120119
}
121120
```
122121

123122
Here's a high level overview of the above code:
124123

125-
1. Takes a `String`, and uses `split` to partition the contents of `string` into an array based on the `","` character.
126-
2. Takes any `Sequence` type and recursively creates a binary tree based on the rules declared in the encoding operation.
124+
1. Takes a `String`, and uses `split` to partition the contents of `string` into an array based on the `separator` defined in the encoding step. The result is first `reversed`, and then mapped to a `String`. The `reverse` step is an optimization for the next function, allowing us to use `array.removeLast()` instead of `array.removeFirst()`.
125+
126+
2. Using an array as a stack, you recursively decode each node. The array keeps track of sequence of nodes and progress.
127+
128+
127129

128130

129131

0 commit comments

Comments
 (0)
0