1
1
from collections .abc import Generator
2
+
2
3
"""
3
4
The class Node is used to create nodes of the Binary Tree.
4
-
5
+
5
6
Agrs : Takes value x to create an object of Node with value x.
6
-
7
+
7
8
Returns : Creates new object of Node.
8
9
"""
10
+
11
+
9
12
class Node :
10
- def __init__ (self , data : int )-> None :
13
+ def __init__ (self , data : int ) -> None :
11
14
self .value = data
12
15
self .left : Node | None = None
13
16
self .right : Node | None = None
14
17
18
+
15
19
def make_tree () -> Node :
16
20
r"""
17
21
The Tree has :
@@ -32,40 +36,44 @@ def make_tree() -> Node:
32
36
return root
33
37
"""
34
38
Serialize Function takes root as a parameter and returns a String.
35
-
39
+
36
40
Agrs:
37
41
root : Takes root node as a parameter.
38
-
42
+
39
43
Returns: A string of preorder traversal of nodes in tree
40
44
with null values of leaf nodes.
41
45
"""
42
46
47
+
43
48
def serialize (root : Node | None ) -> str | None :
44
49
"""
45
50
>>> serialize(make_tree())
46
51
'20,2,N,N,13,4,N,N,5,N,N'
47
52
"""
48
53
result = []
49
- def depth_first_search (node : Node )-> None :
54
+
55
+ def depth_first_search (node : Node ) -> None :
50
56
if not node :
51
57
result .append ("N" )
52
58
return
53
59
result .append (str (node .value ))
54
60
depth_first_search (node .left )
55
61
depth_first_search (node .right )
62
+
56
63
depth_first_search (root )
57
64
return "," .join (result )
58
65
59
66
"""
60
67
Deserialize Function takes String as a parameter and returns root of tree.
61
68
62
- Agrs :
69
+ Agrs :
63
70
String : Takes string of all node values with null values
64
71
of leaf nodes separated by comma as a parameter.
65
-
72
+
66
73
Returns : Root of the tree created after deserialing the string.
67
74
"""
68
75
76
+
69
77
def deserialize (data : str | None ) -> Node | None :
70
78
"""
71
79
>>> root = deserialize("20,2,N,N,13,4,N,N,5,N,N")
@@ -75,6 +83,7 @@ def deserialize(data: str | None) -> Node | None:
75
83
global index
76
84
index = 0
77
85
node_values = data .split ("," )
86
+
78
87
def depth_first_search () -> Node | None :
79
88
global index
80
89
if node_values [index ] == "N" :
@@ -85,9 +94,12 @@ def depth_first_search() -> Node | None:
85
94
root .left = depth_first_search ()
86
95
root .right = depth_first_search ()
87
96
return root
97
+
88
98
return depth_first_search ()
89
99
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
+
91
103
def preorder (root : Node | None ) -> Generator [int , None , None ]:
92
104
"""
93
105
>>> list(preorder(make_tree()))
@@ -97,8 +109,9 @@ def preorder(root: Node | None) -> Generator[int, None, None]:
97
109
"""
98
110
if root :
99
111
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
+
102
115
103
116
def main () -> None : # Main function for testing.
104
117
# Create binary tree.
@@ -108,6 +121,8 @@ def main() -> None: # Main function for testing.
108
121
deserialized_root = deserialize (serialized_string )
109
122
print (f"The Deserialized Tree : { list (preorder (deserialized_root ))} " )
110
123
111
- if __name__ == '__main__' :
124
+
125
+ if __name__ == "__main__" :
112
126
import doctest
127
+
113
128
doctest .testmod ()
0 commit comments