From a7a0a0f9a1ab1bc72e67357e3b406ea2ce41647b Mon Sep 17 00:00:00 2001 From: jindal2309 Date: Thu, 14 Jan 2021 15:51:03 -0800 Subject: [PATCH 1/3] Added code to merge two trees --- .../binary_tree/merge_two_binary_trees.py | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 data_structures/binary_tree/merge_two_binary_trees.py diff --git a/data_structures/binary_tree/merge_two_binary_trees.py b/data_structures/binary_tree/merge_two_binary_trees.py new file mode 100644 index 000000000000..cba8294a1b8d --- /dev/null +++ b/data_structures/binary_tree/merge_two_binary_trees.py @@ -0,0 +1,80 @@ +""" +Problem Description: Given two binary tree, return the merged tree. +The rule for merging is that if two nodes overlap, then put the value sum of +both nodes to the new value of merged node. Otherwise, the NOT null node +will be used as the node of new tree. +""" + + +class Node: + """ + A binary node has value variable and pointers to its left and right node. + """ + + def __init__(self, value=0, left=None, right=None): + self.value = value + self.left = left + self.right = right + + +def merge_two_binary_trees(tree1: Node, tree2: Node) -> Node: + """ + Returns root node of the merged tree. + """ + if tree1 is None: + return tree2 + if tree2 is None: + return tree1 + + tree1.value = tree1.value + tree2.value + tree1.left = merge_two_binary_trees(tree1.left, tree2.left) + tree1.right = merge_two_binary_trees(tree1.right, tree2.right) + return tree1 + + +def print_preorder(root: Node) -> None: + """ + Print pre-order traversal of the tree. + + >>> root = Node(1) + >>> root.left = Node(2) + >>> root.right = Node(3) + >>> print_preorder(root) + 1 + 2 + 3 + >>> print_preorder(root.right) + 3 + """ + if root: + print(root.value) + print_preorder(root.left) + print_preorder(root.right) + + +def main() -> None: + """ + Main function for testing. + """ + tree1 = Node(1) + tree1.left = Node(2) + tree1.right = Node(3) + tree1.left.left = Node(4) + + tree2 = Node(2) + tree2.left = Node(4) + tree2.right = Node(6) + tree2.left.right = Node(9) + tree2.right.right = Node(5) + + print("Tree1 is: ") + print_preorder(tree1) + print("Tree2 is: ") + print_preorder(tree2) + merged_tree = merge_two_binary_trees(tree1, tree2) + print("Merged Tree is: ") + print_preorder(merged_tree) + + +if __name__ == "__main__": + main() From 2383a666911d94ce62fb21d95ffb2db0dc289c28 Mon Sep 17 00:00:00 2001 From: jindal2309 Date: Sun, 17 Jan 2021 21:17:26 -0800 Subject: [PATCH 2/3] Added doctest and type hints --- .../binary_tree/merge_two_binary_trees.py | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/data_structures/binary_tree/merge_two_binary_trees.py b/data_structures/binary_tree/merge_two_binary_trees.py index cba8294a1b8d..93d684ed4c9e 100644 --- a/data_structures/binary_tree/merge_two_binary_trees.py +++ b/data_structures/binary_tree/merge_two_binary_trees.py @@ -4,22 +4,41 @@ both nodes to the new value of merged node. Otherwise, the NOT null node will be used as the node of new tree. """ +from typing import Optional class Node: """ A binary node has value variable and pointers to its left and right node. """ - - def __init__(self, value=0, left=None, right=None): + def __init__(self, value: int=0) -> None: self.value = value - self.left = left - self.right = right + self.left: Optional[Node] = None + self.right: Optional[Node] = None -def merge_two_binary_trees(tree1: Node, tree2: Node) -> Node: +def merge_two_binary_trees(tree1: Optional[Node], tree2: Optional[Node]) -> Node: """ Returns root node of the merged tree. + + >>> tree1 = Node(5) + >>> tree1.left = Node(6) + >>> tree1.right = Node(7) + >>> tree1.left.left = Node(2) + >>> tree2 = Node(4) + >>> tree2.left = Node(5) + >>> tree2.right = Node(8) + >>> tree2.left.right = Node(1) + >>> tree2.right.right = Node(4) + + >>> merged_tree = merge_two_binary_trees(tree1, tree2) + >>> print_preorder(merged_tree) + 3 + 6 + 4 + 9 + 9 + 5 """ if tree1 is None: return tree2 @@ -32,7 +51,7 @@ def merge_two_binary_trees(tree1: Node, tree2: Node) -> Node: return tree1 -def print_preorder(root: Node) -> None: +def print_preorder(root: Optional[Node]) -> None: """ Print pre-order traversal of the tree. @@ -77,4 +96,4 @@ def main() -> None: if __name__ == "__main__": - main() + main() \ No newline at end of file From 93de894d51b542584640d9cc3cc0a54ee988b5cf Mon Sep 17 00:00:00 2001 From: jindal2309 Date: Sun, 17 Jan 2021 22:20:32 -0800 Subject: [PATCH 3/3] Added pre-commit --- .../binary_tree/merge_two_binary_trees.py | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/data_structures/binary_tree/merge_two_binary_trees.py b/data_structures/binary_tree/merge_two_binary_trees.py index 93d684ed4c9e..6b202adb3cf5 100644 --- a/data_structures/binary_tree/merge_two_binary_trees.py +++ b/data_structures/binary_tree/merge_two_binary_trees.py @@ -1,7 +1,8 @@ +#!/usr/local/bin/python3 """ Problem Description: Given two binary tree, return the merged tree. The rule for merging is that if two nodes overlap, then put the value sum of -both nodes to the new value of merged node. Otherwise, the NOT null node +both nodes to the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. """ from typing import Optional @@ -11,7 +12,8 @@ class Node: """ A binary node has value variable and pointers to its left and right node. """ - def __init__(self, value: int=0) -> None: + + def __init__(self, value: int = 0) -> None: self.value = value self.left: Optional[Node] = None self.right: Optional[Node] = None @@ -30,15 +32,14 @@ def merge_two_binary_trees(tree1: Optional[Node], tree2: Optional[Node]) -> Node >>> tree2.right = Node(8) >>> tree2.left.right = Node(1) >>> tree2.right.right = Node(4) - >>> merged_tree = merge_two_binary_trees(tree1, tree2) >>> print_preorder(merged_tree) - 3 - 6 - 4 9 - 9 - 5 + 11 + 2 + 1 + 15 + 4 """ if tree1 is None: return tree2 @@ -71,10 +72,7 @@ def print_preorder(root: Optional[Node]) -> None: print_preorder(root.right) -def main() -> None: - """ - Main function for testing. - """ +if __name__ == "__main__": tree1 = Node(1) tree1.left = Node(2) tree1.right = Node(3) @@ -93,7 +91,3 @@ def main() -> None: merged_tree = merge_two_binary_trees(tree1, tree2) print("Merged Tree is: ") print_preorder(merged_tree) - - -if __name__ == "__main__": - main() \ No newline at end of file