Skip to content

Commit b772141

Browse files
kelvinlauKLremlostime
authored andcommitted
More progress
1 parent f5c74b9 commit b772141

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

Encode and Decode Tree/readme.md

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,86 @@ Here's an example of this operation in code:
3333

3434
```swift
3535
extension BinaryNode {
36+
// 1
37+
private var splitter: String { return "," }
38+
private var nilNode: String { return "nil" }
39+
40+
// 2
3641
var encodedString: String {
3742
var str = ""
38-
preOrderTraversal { str.append($0) }
43+
preOrderTraversal { data in
44+
if let data = data {
45+
let string = String(describing: data)
46+
str.append(string)
47+
} else {
48+
str.append(nilNode)
49+
}
50+
str.append(splitter)
51+
}
3952
return str
4053
}
4154

42-
func preOrderTraversal(visit: (T) -> ()) {
55+
// 3
56+
func preOrderTraversal(visit: (T?) -> ()) {
4357
visit(data)
44-
leftChild?.preOrderTraversal(visit: visit)
45-
rightChild?.preOrderTraversal(visit: visit)
58+
59+
if let leftChild = leftChild {
60+
leftChild.preOrderTraversal(visit: visit)
61+
} else {
62+
visit(nil)
63+
}
64+
65+
if let rightChild = rightChild {
66+
rightChild.preOrderTraversal(visit: visit)
67+
} else {
68+
visit(nil)
69+
}
4670
}
4771
}
4872
```
4973

74+
Here's a high level overview of the above code:
75+
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.
77+
78+
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.
79+
80+
3. It is interesting to note that this pre-order traversal implementation also emits `nil` values in place of absent children.
81+
82+
## Decoding
83+
84+
Your decoding strategy is the exact opposite of your encoding strategy. You'll take an encoded string, and turn it back into your binary tree.
85+
86+
Your encoding strategy followed the following rules:
87+
88+
1. The result of the encoding will be a `String` object.
89+
2. You'll encode using *pre-order* traversal.
90+
91+
The implementation also added a few important details:
92+
93+
* node values are separated by `,`
94+
* `nil` children are denoted by the `nil` string
95+
96+
These details will shape your `decode` operation. Here's a possible implementation:
97+
98+
```swift
99+
extension BinaryTree {
100+
101+
static func decode<Element>(from string: String) -> BinaryNode<Element>? {
102+
   let array = string.split(separator: ",")
103+
let deque: Deque<String> = array
104+
return decode(from: deque)
105+
}
106+
107+
static func decode<Elements: RangeReplaceableCollection>(from deque: RangeReplaceableCollection)
108+
-> BinaryNode<Elements.Element>? {
109+
110+
}
111+
}
112+
```
113+
114+
115+
116+
117+
118+

0 commit comments

Comments
 (0)