forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_and_deserialize_binary_tree.py
96 lines (82 loc) · 2.84 KB
/
serialize_and_deserialize_binary_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from collections.abc import Generator
"""
The class Node is used to create nodes of the Binary Tree.
Agrs : Takes value x to create an object of Node with value x.
Returns : Creates new object of Node.
"""
class Node:
def __init__(self, x: int):
self.value = x
self.left = None
self.right = None
def make_tree() -> Node | None:
r"""
The Tree has :
20
/ \
2 13
/ \ / \
null - N N 4 5
/ \ / \
N N N N - null
"""
root = Node(20)
root.left = Node(2)
root.right = Node(13)
root.right.left = Node(4)
root.right.right = Node(5)
return root
"""
Serialize Function takes root as a parameter and returns a String.
Agrs :
root : Takes root node as a parameter.
Returns : A string of preorder traversal of nodes in a tree along with null values of leaf nodes.
"""
def serialize(root: Node | None) -> str | None:
result = []
def depth_first_search(node):
if not node:
result.append("N")
return
result.append(str(node.value))
depth_first_search(node.left)
depth_first_search(node.right)
depth_first_search(root)
return ",".join(result)
"""
Deserialize Function takes String as a parameter and returns the root of the tree.
Agrs :
String : Takes string of all node values with null values of leaf nodes separated by comma as a parameter.
Returns : Root of the tree created after deserialing the string.
"""
def deserialize(data: str | None) -> Node | None:
global index
index = 0
node_values = data.split(",")
def depth_first_search():
global index
if node_values[index] == "N":
index += 1
return None
root = Node(int(node_values[index]))
index += 1
root.left = depth_first_search()
root.right = depth_first_search()
return root
return depth_first_search()
#This method is written to traverse the tree created by deserialize method.
def preorder(root: Node | None) -> Generator[int, None, None]:
if root:
yield root.value
yield from preorder(root.left)
yield from preorder(root.right)
def main() -> None: # Main function for testing.
# Create binary tree.
root = make_tree()
serialized_string = serialize(root)
print(f"Serialized string : {serialized_string} \n")
deserialized_root = deserialize(serialized_string)
print(f"The Deserialized Tree : {list(preorder(deserialized_root))}")
if __name__ == '__main__':
import doctest
doctest.testmod()