From 0c410f519d3bf0447538d9dce717635552e40524 Mon Sep 17 00:00:00 2001 From: pranjalisingh1201 Date: Sun, 8 Oct 2023 18:40:34 +0530 Subject: [PATCH 01/10] Serialize and Deserialize #9611 --- .../serialize_and_deserialize_binary_tree.py | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 data_structures/binary_tree/serialize_and_deserialize_binary_tree.py diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py new file mode 100644 index 000000000000..dae53af3a62f --- /dev/null +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -0,0 +1,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() From 8f19f2ff563c52587a2e6d60c4f2ed42d30b0d4c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 16:07:07 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../serialize_and_deserialize_binary_tree.py | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index dae53af3a62f..eef245c5fa01 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -1,20 +1,24 @@ 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. - + + 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 : + The Tree has : 20 / \ @@ -32,15 +36,17 @@ def make_tree() -> Node | None: return root """ Serialize Function takes root as a parameter and returns a String. - - Agrs : - root : Takes root node as a parameter. - + + 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 = [] + result = [] + def depth_first_search(node): if not node: result.append("N") @@ -48,22 +54,25 @@ def depth_first_search(node): 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. - + + 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: + +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": @@ -74,14 +83,18 @@ def depth_first_search(): 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. + # 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) + yield from preorder(root.left) + yield from preorder(root.right) + def main() -> None: # Main function for testing. # Create binary tree. @@ -90,7 +103,9 @@ def main() -> None: # Main function for testing. print(f"Serialized string : {serialized_string} \n") deserialized_root = deserialize(serialized_string) print(f"The Deserialized Tree : {list(preorder(deserialized_root))}") - -if __name__ == '__main__': + + +if __name__ == "__main__": import doctest + doctest.testmod() From 843a0e5dcc6bb7053649b55b54d83ef2f7b2eeb5 Mon Sep 17 00:00:00 2001 From: pranjalisingh1201 Date: Sun, 8 Oct 2023 23:08:26 +0530 Subject: [PATCH 03/10] Serialize and Deserialize for a binary tree #9611 --- .../serialize_and_deserialize_binary_tree.py | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index dae53af3a62f..255afbe62e58 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -2,19 +2,19 @@ """ 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. + 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 + def __init__(self, data: int)-> None: + self.value = data self.left = None self.right = None def make_tree() -> Node | None: r""" - The Tree has : + The Tree has : 20 / \ @@ -33,15 +33,20 @@ def make_tree() -> Node | None: """ Serialize Function takes root as a parameter and returns a String. - Agrs : - root : Takes root node as a parameter. + 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. + Returns: A string of preorder traversal of nodes in tree + with null values of leaf nodes. """ def serialize(root: Node | None) -> str | None: - result = [] - def depth_first_search(node): + """ + >>> 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 @@ -52,19 +57,25 @@ def depth_first_search(node): return ",".join(result) """ - Deserialize Function takes String as a parameter and returns the root of the tree. - + 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. + 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: +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(): + def depth_first_search() -> Node | None: global index if node_values[index] == "N": index += 1 @@ -78,6 +89,12 @@ def 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) @@ -87,10 +104,10 @@ def main() -> None: # Main function for testing. # Create binary tree. root = make_tree() serialized_string = serialize(root) - print(f"Serialized string : {serialized_string} \n") + 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() From ecb04963fe4de0366b99006acfeeb419944527fb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:47:30 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../serialize_and_deserialize_binary_tree.py | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index 255afbe62e58..ed1658146610 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -1,17 +1,21 @@ 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: + def __init__(self, data: int) -> None: self.value = data self.left = None self.right = None + def make_tree() -> Node | None: r""" The Tree has : @@ -32,40 +36,44 @@ def make_tree() -> Node | None: 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: + + 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 : + 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") @@ -75,6 +83,7 @@ def deserialize(data: str | None) -> Node | None: global index index = 0 node_values = data.split(",") + def depth_first_search() -> Node | None: global index if node_values[index] == "N": @@ -85,9 +94,12 @@ def depth_first_search() -> Node | None: 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. + # 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())) @@ -97,8 +109,9 @@ def preorder(root: Node | None) -> Generator[int, None, None]: """ if root: yield root.value - yield from preorder(root.left) - yield from preorder(root.right) + yield from preorder(root.left) + yield from preorder(root.right) + def main() -> None: # Main function for testing. # Create binary tree. @@ -108,6 +121,8 @@ def main() -> None: # Main function for testing. deserialized_root = deserialize(serialized_string) print(f"The Deserialized Tree : {list(preorder(deserialized_root))}") -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() From 1a0723cf88d31649cda350707b6c95cfff23963d Mon Sep 17 00:00:00 2001 From: pranjalisingh1201 Date: Sun, 8 Oct 2023 23:30:05 +0530 Subject: [PATCH 05/10] Resolving Pre-commit --- .../binary_tree/serialize_and_deserialize_binary_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index 255afbe62e58..f2da76e562bb 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -12,7 +12,7 @@ def __init__(self, data: int)-> None: self.left = None self.right = None -def make_tree() -> Node | None: +def make_tree() -> Node: r""" The Tree has : @@ -46,7 +46,7 @@ def serialize(root: Node | None) -> str | None: '20,2,N,N,13,4,N,N,5,N,N' """ result = [] - def depth_first_search(node)-> Node | None: + def depth_first_search(node: Node)-> None: if not node: result.append("N") return From 4b7d8beebcb62ebba7ec206d9a248fad8533675f Mon Sep 17 00:00:00 2001 From: pranjalisingh1201 Date: Sun, 8 Oct 2023 23:33:43 +0530 Subject: [PATCH 06/10] Resolving pre-commit --- .../serialize_and_deserialize_binary_tree.py | 45 +++++-------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index 835cc532b3be..f2da76e562bb 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -1,26 +1,18 @@ 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: + def __init__(self, data: int)-> None: self.value = data self.left = None self.right = None -<<<<<<< HEAD def make_tree() -> Node: -======= - -def make_tree() -> Node | None: ->>>>>>> ecb04963fe4de0366b99006acfeeb419944527fb r""" The Tree has : @@ -40,48 +32,40 @@ def make_tree() -> Node | None: 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 = [] -<<<<<<< HEAD def depth_first_search(node: Node)-> None: -======= - - def depth_first_search(node) -> Node | None: ->>>>>>> ecb04963fe4de0366b99006acfeeb419944527fb 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 : + 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") @@ -91,7 +75,6 @@ def deserialize(data: str | None) -> Node | None: global index index = 0 node_values = data.split(",") - def depth_first_search() -> Node | None: global index if node_values[index] == "N": @@ -102,12 +85,9 @@ def depth_first_search() -> Node | None: 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. - - + #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())) @@ -117,9 +97,8 @@ def preorder(root: Node | None) -> Generator[int, None, None]: """ if root: yield root.value - yield from preorder(root.left) - yield from preorder(root.right) - + yield from preorder(root.left) + yield from preorder(root.right) def main() -> None: # Main function for testing. # Create binary tree. @@ -129,8 +108,6 @@ def main() -> None: # Main function for testing. deserialized_root = deserialize(serialized_string) print(f"The Deserialized Tree : {list(preorder(deserialized_root))}") - -if __name__ == "__main__": +if __name__ == '__main__': import doctest - doctest.testmod() From 64afa64ae936a63924a3047e68be4541d7765116 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 18:04:28 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../serialize_and_deserialize_binary_tree.py | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index f2da76e562bb..2d39dd4e3b7c 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -1,17 +1,21 @@ 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: + def __init__(self, data: int) -> None: self.value = data self.left = None self.right = None + def make_tree() -> Node: r""" The Tree has : @@ -32,40 +36,44 @@ def make_tree() -> Node: 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: + + 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 : + 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") @@ -75,6 +83,7 @@ def deserialize(data: str | None) -> Node | None: global index index = 0 node_values = data.split(",") + def depth_first_search() -> Node | None: global index if node_values[index] == "N": @@ -85,9 +94,12 @@ def depth_first_search() -> Node | None: 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. + # 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())) @@ -97,8 +109,9 @@ def preorder(root: Node | None) -> Generator[int, None, None]: """ if root: yield root.value - yield from preorder(root.left) - yield from preorder(root.right) + yield from preorder(root.left) + yield from preorder(root.right) + def main() -> None: # Main function for testing. # Create binary tree. @@ -108,6 +121,8 @@ def main() -> None: # Main function for testing. deserialized_root = deserialize(serialized_string) print(f"The Deserialized Tree : {list(preorder(deserialized_root))}") -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() From 9584b7c20a58f94b4e004b62a02100492f0ca74d Mon Sep 17 00:00:00 2001 From: pranjalisingh1201 Date: Mon, 9 Oct 2023 00:00:32 +0530 Subject: [PATCH 08/10] Resolving Pre-commit --- .../binary_tree/serialize_and_deserialize_binary_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index f2da76e562bb..d85a7c4c2d7e 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -9,8 +9,8 @@ class Node: def __init__(self, data: int)-> None: self.value = data - self.left = None - self.right = None + self.left : Node | None = None + self.right : Node | None = None def make_tree() -> Node: r""" From 93ee34b7ab77d1332abaee53f4276d7325ee1f07 Mon Sep 17 00:00:00 2001 From: pranjalisingh1201 Date: Mon, 9 Oct 2023 00:03:37 +0530 Subject: [PATCH 09/10] Resolving pre-commit --- .../serialize_and_deserialize_binary_tree.py | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index 75b3cbb290c6..d85a7c4c2d7e 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -1,21 +1,17 @@ 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: + 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 : @@ -36,44 +32,40 @@ def make_tree() -> Node: 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: + 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 : + 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") @@ -83,7 +75,6 @@ def deserialize(data: str | None) -> Node | None: global index index = 0 node_values = data.split(",") - def depth_first_search() -> Node | None: global index if node_values[index] == "N": @@ -94,12 +85,9 @@ def depth_first_search() -> Node | None: 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. - - + #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())) @@ -109,9 +97,8 @@ def preorder(root: Node | None) -> Generator[int, None, None]: """ if root: yield root.value - yield from preorder(root.left) - yield from preorder(root.right) - + yield from preorder(root.left) + yield from preorder(root.right) def main() -> None: # Main function for testing. # Create binary tree. @@ -121,8 +108,6 @@ def main() -> None: # Main function for testing. deserialized_root = deserialize(serialized_string) print(f"The Deserialized Tree : {list(preorder(deserialized_root))}") - -if __name__ == "__main__": +if __name__ == '__main__': import doctest - doctest.testmod() From f10e8fa46f37eaa4950e15ba632386d87f416e25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 18:34:34 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../serialize_and_deserialize_binary_tree.py | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py index d85a7c4c2d7e..ddd110e7ebfd 100644 --- a/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py +++ b/data_structures/binary_tree/serialize_and_deserialize_binary_tree.py @@ -1,16 +1,20 @@ 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: + def __init__(self, data: int) -> None: self.value = data - self.left : Node | None = None - self.right : Node | None = None + self.left: Node | None = None + self.right: Node | None = None + def make_tree() -> Node: r""" @@ -32,40 +36,44 @@ def make_tree() -> Node: 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: + + 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 : + 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") @@ -75,6 +83,7 @@ def deserialize(data: str | None) -> Node | None: global index index = 0 node_values = data.split(",") + def depth_first_search() -> Node | None: global index if node_values[index] == "N": @@ -85,9 +94,12 @@ def depth_first_search() -> Node | None: 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. + # 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())) @@ -97,8 +109,9 @@ def preorder(root: Node | None) -> Generator[int, None, None]: """ if root: yield root.value - yield from preorder(root.left) - yield from preorder(root.right) + yield from preorder(root.left) + yield from preorder(root.right) + def main() -> None: # Main function for testing. # Create binary tree. @@ -108,6 +121,8 @@ def main() -> None: # Main function for testing. deserialized_root = deserialize(serialized_string) print(f"The Deserialized Tree : {list(preorder(deserialized_root))}") -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod()