Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3622f23

Browse files
Merge pull request #361 from MoigeMatino/add-serialise-deserialise-tree
feat: add serialise deserialise tree
2 parents 0c0d268 + 80a4f72 commit 3622f23

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## **Problem Statement**
2+
3+
Serialize a given binary tree to a file and deserialize it back to a tree. Make sure that the original and the deserialized trees are identical.
4+
5+
Serialize: Write the tree to a file.
6+
7+
Deserialize: Read from a file and reconstruct the tree in memory.
8+
9+
Serialize the tree into a list of integers, and then, deserialize it back from the list to a tree. For simplicity’s sake, there’s no need to write the list to the files.
10+
11+
12+
### Constraints
13+
14+
> The number of nodes in the tree is in the range [0,500].
15+
16+
> −1000 ≤ Node.value ≤ 1000

‎binary_tree/serialise_deserialise_tree/__init__.py

Whitespace-only changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class Node:
2+
def __init__(self, val):
3+
self.val = val
4+
self.right = None
5+
self.left = None
6+
7+
def serialise(root: Node) -> list:
8+
"""
9+
Serialise a binary tree into a list using a pre-order traversal approach.
10+
The function uses a stack to traverse the tree iteratively and appends node
11+
values to a list. If a node is a leaf, it appends "None" to indicate the end
12+
of a branch.
13+
14+
Parameters:
15+
root (Node): The root node of the binary tree.
16+
17+
Returns:
18+
list: A list representing the serialised binary tree.
19+
"""
20+
if not root:
21+
return None
22+
23+
stack = [root]
24+
stream = []
25+
while stack:
26+
current = stack.pop()
27+
stream.append(current.val)
28+
29+
# Append "None" to indicate the end of a branch
30+
if not current.right and not current.left:
31+
stream.append("None")
32+
33+
# Push right child first so that left child is processed first
34+
if current.right:
35+
stack.append(current.right)
36+
37+
if current.left:
38+
stack.append(current.left)
39+
return stream
40+
41+
def deserialise(stream: list) -> Node:
42+
"""
43+
Deserialise a list back into a binary tree. The function reverses the list
44+
and uses a helper function to recursively reconstruct the tree.
45+
46+
Parameters:
47+
stream (list): The list representing the serialised binary tree.
48+
49+
Returns:
50+
Node: The root node of the deserialised binary tree.
51+
"""
52+
stream.reverse()
53+
root = deserialise_helper(stream)
54+
return root
55+
56+
def deserialise_helper(stream: list) -> Node:
57+
"""
58+
Helper function to recursively deserialise the list into a binary tree.
59+
60+
Parameters:
61+
stream (list): The list representing the serialised binary tree.
62+
63+
Returns:
64+
Node: The current node being reconstructed.
65+
"""
66+
node_val = stream.pop()
67+
if isinstance(node_val, str) and node_val == "None":
68+
return None
69+
70+
node = Node(node_val)
71+
node.left = deserialise_helper(stream)
72+
node.right = deserialise_helper(stream)
73+
74+
return node
75+
76+
# Approach and Reasoning
77+
# ----------------------
78+
# The serialisation function uses an iterative pre-order traversal to convert the binary tree into a list.
79+
# It uses a stack to manage the traversal order, ensuring that the left child is processed before the right child.
80+
# "None" is appended to the list to indicate the end of a branch, which helps in reconstructing the tree during deserialisation.
81+
82+
# The deserialisation function reverses the list and uses a recursive helper function to reconstruct the tree.
83+
# The helper function pops elements from the list, creating nodes and recursively setting their left and right children.
84+
# The use of "None" in the list helps identify when a branch ends, allowing the function to return None for leaf children.
85+
86+
# Complexity Analysis
87+
# -------------------
88+
# Time Complexity:
89+
# - Serialisation: O(n), where n is the number of nodes in the tree. Each node is visited once.
90+
# - Deserialisation: O(n), where n is the number of nodes in the tree. Each node is reconstructed once.
91+
92+
# Space Complexity:
93+
# - Serialisation: O(n), where n is the number of nodes in the tree. The list stores each node's value and "None" for leaves.
94+
# - Deserialisation: O(h), where h is the height of the tree. This is due to the recursive call stack during reconstruction.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /