Skip to content

Commit 843a0e5

Browse files
Serialize and Deserialize for a binary tree TheAlgorithms#9611
1 parent d34b9e1 commit 843a0e5

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

Diff for: data_structures/binary_tree/serialize_and_deserialize_binary_tree.py

+33-16
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
"""
33
The class Node is used to create nodes of the Binary Tree.
44
5-
Agrs : Takes value x to create an object of Node with value x.
5+
Agrs : Takes value x to create an object of Node with value x.
66
77
Returns : Creates new object of Node.
88
"""
99
class Node:
10-
def __init__(self, x: int):
11-
self.value = x
10+
def __init__(self, data: int)-> None:
11+
self.value = data
1212
self.left = None
1313
self.right = None
1414

1515
def make_tree() -> Node | None:
1616
r"""
17-
The Tree has :
17+
The Tree has :
1818
1919
20
2020
/ \
@@ -33,15 +33,20 @@ def make_tree() -> Node | None:
3333
"""
3434
Serialize Function takes root as a parameter and returns a String.
3535
36-
Agrs :
37-
root : Takes root node as a parameter.
36+
Agrs:
37+
root : Takes root node as a parameter.
3838
39-
Returns : A string of preorder traversal of nodes in a tree along with null values of leaf nodes.
39+
Returns: A string of preorder traversal of nodes in tree
40+
with null values of leaf nodes.
4041
"""
4142

4243
def serialize(root: Node | None) -> str | None:
43-
result = []
44-
def depth_first_search(node):
44+
"""
45+
>>> serialize(make_tree())
46+
'20,2,N,N,13,4,N,N,5,N,N'
47+
"""
48+
result = []
49+
def depth_first_search(node)-> Node | None:
4550
if not node:
4651
result.append("N")
4752
return
@@ -52,19 +57,25 @@ def depth_first_search(node):
5257
return ",".join(result)
5358

5459
"""
55-
Deserialize Function takes String as a parameter and returns the root of the tree.
56-
60+
Deserialize Function takes String as a parameter and returns root of tree.
61+
5762
Agrs :
58-
String : Takes string of all node values with null values of leaf nodes separated by comma as a parameter.
63+
String : Takes string of all node values with null values
64+
of leaf nodes separated by comma as a parameter.
5965
6066
Returns : Root of the tree created after deserialing the string.
6167
"""
6268

63-
def deserialize(data: str | None) -> Node | None:
69+
def deserialize(data: str | None) -> Node | None:
70+
"""
71+
>>> root = deserialize("20,2,N,N,13,4,N,N,5,N,N")
72+
>>> list(preorder(root))
73+
[20, 2, 13, 4, 5]
74+
"""
6475
global index
6576
index = 0
6677
node_values = data.split(",")
67-
def depth_first_search():
78+
def depth_first_search() -> Node | None:
6879
global index
6980
if node_values[index] == "N":
7081
index += 1
@@ -78,6 +89,12 @@ def depth_first_search():
7889

7990
#This method is written to traverse the tree created by deserialize method.
8091
def preorder(root: Node | None) -> Generator[int, None, None]:
92+
"""
93+
>>> list(preorder(make_tree()))
94+
[20, 2, 13, 4, 5]
95+
>>> list(preorder(None))
96+
[]
97+
"""
8198
if root:
8299
yield root.value
83100
yield from preorder(root.left)
@@ -87,10 +104,10 @@ def main() -> None: # Main function for testing.
87104
# Create binary tree.
88105
root = make_tree()
89106
serialized_string = serialize(root)
90-
print(f"Serialized string : {serialized_string} \n")
107+
print(f"Serialized string : {serialized_string}")
91108
deserialized_root = deserialize(serialized_string)
92109
print(f"The Deserialized Tree : {list(preorder(deserialized_root))}")
93-
110+
94111
if __name__ == '__main__':
95112
import doctest
96113
doctest.testmod()

0 commit comments

Comments
 (0)