forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_and_deserialize_binary_tree.py
113 lines (99 loc) · 3.18 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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, data: int)-> None:
self.value = data
self.left : Node | None = None
self.right : Node | None = None
def make_tree() -> Node:
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 tree
with null values of leaf nodes.
"""
def serialize(root: Node | None) -> str | None:
"""
>>> serialize(make_tree())
'20,2,N,N,13,4,N,N,5,N,N'
"""
result = []
def depth_first_search(node: Node)-> None:
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 root of 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:
"""
>>> root = deserialize("20,2,N,N,13,4,N,N,5,N,N")
>>> list(preorder(root))
[20, 2, 13, 4, 5]
"""
global index
index = 0
node_values = data.split(",")
def depth_first_search() -> Node | None:
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]:
"""
>>> list(preorder(make_tree()))
[20, 2, 13, 4, 5]
>>> list(preorder(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}")
deserialized_root = deserialize(serialized_string)
print(f"The Deserialized Tree : {list(preorder(deserialized_root))}")
if __name__ == '__main__':
import doctest
doctest.testmod()