8000 Refactor in progress · jerrytdev/swift-algorithm-club@f5c74b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5c74b9

Browse files
kelvinlauKLremlostime
authored andcommitted
Refactor in progress
1 parent adf6065 commit f5c74b9

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

Encode and Decode Tree/readme.md

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
# Encode and Decode Binary Tree
22

3-
We need to design an algorithm to encode and decode a binary tree.
4-
* **Encode**: Convert a tree into a string that can be stored in the disk.
5-
* **Decode**: Given the encoded string, you need to convert it into a Tree.
3+
> **Note**: The prerequisite for this article is an understanding of how [binary trees](https://github.com/raywenderlich/swift-algorithm-club/tree/master/Binary%20Tree) work.
64
7-
For example, you may serialize the following tree
5+
Trees are complex structures. Unlike linear collections such as arrays or linked lists, trees are *non-linear* and each element in a tree has positional information such as the *parent-child* relationship between nodes. When you want to send a tree structure to your backend, you need to send the data of each node, and a way to represent the parent-child relationship for each node.
86

7+
Your strategy in how you choose to represent this information is called your **encoding** strategy. The opposite of that - changing your encoded data back to its original form - is your **decoding** strategy.
98

10-
as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
11-
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
9+
There are many ways to encode a tree and decode a tree. The important thing to keep in mind is that encoding and decoding strategies are closely related. The way you choose to encode a tree directly affects how you might decode a tree.
1210

13-
## Solution
14-
For example, given a tree like this
15-
> a
16-
> / \\
17-
>
18-
> b c
19-
>
20-
> ​ / \\
21-
> ​ d e
11+
Encoding and decoding are synonyms to *serializing* and *deserializing* trees.
2212

23-
We can use inorder traversal to convert the tree into the string like this `a b # # c d # # e # #`
13+
As a reference, the following code represents the typical `Node` type of a binary tree:
2414

25-
So, the idea is for the empty node, we use `#` to represent.
15+
```swift
16+
class BinaryNode<Element: Comparable> {
17+
var data: Element
18+
var leftChild: BinaryNode?
19+
var rightChild: BinaryNode?
20+
21+
// ... (rest of the implementation)
22+
}
23+
```
24+
25+
## Encoding
26+
27+
As mentioned before, there are different ways to do encoding. For no particular reason, you'll opt for the following rules:
28+
29+
1. The result of the encoding will be a `String` object.
30+
2. You'll encode using *pre-order* traversal.
31+
32+
Here's an example of this operation in code:
33+
34+
```swift
35+
extension BinaryNode {
36+
var encodedString: String {
37+
var str = ""
38+
preOrderTraversal { str.append($0) }
39+
return str
40+
}
41+
42+
func preOrderTraversal(visit: (T) -> ()) {
43+
visit(data)
44+
leftChild?.preOrderTraversal(visit: visit)
45+
rightChild?.preOrderTraversal(visit: visit)
46+
}
47+
}
48+
```
2649

27-
For the decode process, we can still use inorder to convert the string back to a tree.

0 commit comments

Comments
 (0)
0