forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize_deserialize_binary_tree.py
More file actions
Latest commit
140 lines (110 loc) · 3.48 KB
/
Copy pathserialize_deserialize_binary_tree.py
File metadata and controls
140 lines (110 loc) · 3.48 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from __future__ importannotations
fromcollections.abcimportIterator
fromdataclassesimportdataclass
@dataclass
classTreeNode:
"""
A binary tree node has a value, left child, and right child.
Props:
value: The value of the node.
left: The left child of the node.
right: The right child of the node.
"""
value: int=0
left: TreeNode|None=None
right: TreeNode|None=None
def__post_init__(self):
ifnotisinstance(self.value, int):
raiseTypeError("Value must be an integer.")
def__iter__(self) ->Iterator[TreeNode]:
"""
Iterate through the tree in preorder.
Returns:
An iterator of the tree nodes.
>>> list(TreeNode(1))
[1,null,null]
>>> tuple(TreeNode(1, TreeNode(2), TreeNode(3)))
(1,2,null,null,3,null,null, 2,null,null, 3,null,null)
"""
yieldself
yieldfromself.leftor ()
yieldfromself.rightor ()
def__len__(self) ->int:
"""
Count the number of nodes in the tree.
Returns:
The number of nodes in the tree.
>>> len(TreeNode(1))
1
>>> len(TreeNode(1, TreeNode(2), TreeNode(3)))
3
"""
returnsum(1for_inself)
def__repr__(self) ->str:
"""
Represent the tree as a string.
Returns:
A string representation of the tree.
>>> repr(TreeNode(1))
'1,null,null'
>>> repr(TreeNode(1, TreeNode(2), TreeNode(3)))
'1,2,null,null,3,null,null'
>>> repr(TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5))))
'1,2,null,null,3,4,null,null,5,null,null'
"""
returnf"{self.value},{self.left!r},{self.right!r}".replace("None", "null")
@classmethod
deffive_tree(cls) ->TreeNode:
"""
>>> repr(TreeNode.five_tree())
'1,2,null,null,3,4,null,null,5,null,null'
"""
root=TreeNode(1)
root.left=TreeNode(2)
root.right=TreeNode(3)
root.right.left=TreeNode(4)
root.right.right=TreeNode(5)
returnroot
defdeserialize(data: str) ->TreeNode|None:
"""
Deserialize a string to a binary tree.
Args:
data(str): The serialized string.
Returns:
The root of the binary tree.
>>> root = TreeNode.five_tree()
>>> serialzed_data = repr(root)
>>> deserialized = deserialize(serialzed_data)
>>> root == deserialized
True
>>> root is deserialized # two separate trees
False
>>> root.right.right.value = 6
>>> root == deserialized
False
>>> serialzed_data = repr(root)
>>> deserialized = deserialize(serialzed_data)
>>> root == deserialized
True
>>> deserialize("")
Traceback (most recent call last):
...
ValueError: Data cannot be empty.
"""
ifnotdata:
raiseValueError("Data cannot be empty.")
# Split the serialized string by a comma to get node values
nodes=data.split(",")
defbuild_tree() ->TreeNode|None:
# Get the next value from the list
value=nodes.pop(0)
ifvalue=="null":
returnNone
node=TreeNode(int(value))
node.left=build_tree() # Recursively build left subtree
node.right=build_tree() # Recursively build right subtree
returnnode
returnbuild_tree()
if__name__=="__main__":
importdoctest
doctest.testmod()