Skip to content

Commit 4ce9edc

Browse files
Merge branch 'TheAlgorithms#9611-Serialize_and_Deserialize' of https://github.com/pranjalisingh1201/hactoberfest-Python into TheAlgorithms#9611-Serialize_and_Deserialize
2 parents 9584b7c + 64afa64 commit 4ce9edc

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

Diff for: data_structures/binary_tree/serialize_and_deserialize_binary_tree.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
from collections.abc import Generator
2+
23
"""
34
The class Node is used to create nodes of the Binary Tree.
4-
5+
56
Agrs : Takes value x to create an object of Node with value x.
6-
7+
78
Returns : Creates new object of Node.
89
"""
10+
11+
912
class Node:
10-
def __init__(self, data: int)-> None:
13+
def __init__(self, data: int) -> None:
1114
self.value = data
1215
self.left : Node | None = None
1316
self.right : Node | None = None
1417

18+
1519
def make_tree() -> Node:
1620
r"""
1721
The Tree has :
@@ -32,40 +36,44 @@ def make_tree() -> Node:
3236
return root
3337
"""
3438
Serialize Function takes root as a parameter and returns a String.
35-
39+
3640
Agrs:
3741
root : Takes root node as a parameter.
38-
42+
3943
Returns: A string of preorder traversal of nodes in tree
4044
with null values of leaf nodes.
4145
"""
4246

47+
4348
def serialize(root: Node | None) -> str | None:
4449
"""
4550
>>> serialize(make_tree())
4651
'20,2,N,N,13,4,N,N,5,N,N'
4752
"""
4853
result = []
49-
def depth_first_search(node: Node)-> None:
54+
55+
def depth_first_search(node: Node) -> None:
5056
if not node:
5157
result.append("N")
5258
return
5359
result.append(str(node.value))
5460
depth_first_search(node.left)
5561
depth_first_search(node.right)
62+
5663
depth_first_search(root)
5764
return ",".join(result)
5865

5966
"""
6067
Deserialize Function takes String as a parameter and returns root of tree.
6168
62-
Agrs :
69+
Agrs :
6370
String : Takes string of all node values with null values
6471
of leaf nodes separated by comma as a parameter.
65-
72+
6673
Returns : Root of the tree created after deserialing the string.
6774
"""
6875

76+
6977
def deserialize(data: str | None) -> Node | None:
7078
"""
7179
>>> root = deserialize("20,2,N,N,13,4,N,N,5,N,N")
@@ -75,6 +83,7 @@ def deserialize(data: str | None) -> Node | None:
7583
global index
7684
index = 0
7785
node_values = data.split(",")
86+
7887
def depth_first_search() -> Node | None:
7988
global index
8089
if node_values[index] == "N":
@@ -85,9 +94,12 @@ def depth_first_search() -> Node | None:
8594
root.left = depth_first_search()
8695
root.right = depth_first_search()
8796
return root
97+
8898
return depth_first_search()
8999

90-
#This method is written to traverse the tree created by deserialize method.
100+
# This method is written to traverse the tree created by deserialize method.
101+
102+
91103
def preorder(root: Node | None) -> Generator[int, None, None]:
92104
"""
93105
>>> list(preorder(make_tree()))
@@ -97,8 +109,9 @@ def preorder(root: Node | None) -> Generator[int, None, None]:
97109
"""
98110
if root:
99111
yield root.value
100-
yield from preorder(root.left)
101-
yield from preorder(root.right)
112+
yield from preorder(root.left)
113+
yield from preorder(root.right)
114+
102115

103116
def main() -> None: # Main function for testing.
104117
# Create binary tree.
@@ -108,6 +121,8 @@ def main() -> None: # Main function for testing.
108121
deserialized_root = deserialize(serialized_string)
109122
print(f"The Deserialized Tree : {list(preorder(deserialized_root))}")
110123

111-
if __name__ == '__main__':
124+
125+
if __name__ == "__main__":
112126
import doctest
127+
113128
doctest.testmod()

0 commit comments

Comments
 (0)