|
7 | 7 | """
|
8 | 8 |
|
9 | 9 |
|
10 |
| -def binaryTreeMirrorDict(root: int, binary_tree: dict = {}, binary_tree_mirror: dict = {}): |
11 |
| - if root is None or root not in binary_tree.keys(): |
| 10 | +def binary_tree_mirror_dict(root: int, binary_tree_mirror_dictionary: dict): |
| 11 | + if not root or root not in binary_tree_mirror_dictionary: |
12 | 12 | return
|
13 |
| - left_child = binary_tree[root][0] |
14 |
| - right_child = binary_tree[root][1] |
15 |
| - binary_tree_mirror[root] = [right_child, left_child] |
16 |
| - binaryTreeMirrorDict(left_child, binary_tree, binary_tree_mirror) |
17 |
| - binaryTreeMirrorDict(right_child, binary_tree, binary_tree_mirror) |
| 13 | + left_child = binary_tree_mirror_dictionary[root][0] |
| 14 | + right_child = binary_tree_mirror_dictionary[root][1] |
| 15 | + binary_tree_mirror_dictionary[root] = [right_child, left_child] |
| 16 | + binary_tree_mirror_dict(left_child, binary_tree_mirror_dictionary) |
| 17 | + binary_tree_mirror_dict(right_child, binary_tree_mirror_dictionary) |
18 | 18 |
|
19 |
| -def binaryTreeMirror(root: int, binary_tree: dict = {}) -> dict: |
| 19 | + |
| 20 | +def binary_tree_mirror(binary_tree: dict = {}, root: int = 1) -> dict: |
20 | 21 | """
|
21 |
| - >>> binaryTreeMirror(1, { 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}) |
| 22 | + >>> binary_tree_mirror(1, { 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}) |
22 | 23 | {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]}
|
23 |
| - >>> binaryTreeMirror(5, { 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}) |
| 24 | + >>> binary_tree_mirror(5, { 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}) |
24 | 25 | {}
|
25 |
| - >>> binaryTreeMirror\ |
26 |
| - (1, { 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11], 5: [12,13],7: [8,9]}) |
27 |
| - {1: [3, 2], 2: [5, 4], 4: [11, 10], 5: [13, 12], 3: [7, 6], 7: [9, 8]} |
| 26 | + >>> binary_tree_mirror(1, { 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}) |
| 27 | + {1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]} |
28 | 28 | """
|
29 |
| - binary_tree_mirror = {} |
30 |
| - binaryTreeMirrorDict(root, binary_tree, binary_tree_mirror) |
31 |
| - return binary_tree_mirror |
| 29 | + if not binary_tree: |
| 30 | + raise ValueError("binary tree cannot be empty") |
| 31 | + if root not in binary_tree: |
| 32 | + raise ValueError("root is present in the binary_tree") |
| 33 | + binary_tree_mirror_dictionary = dict(binary_tree) |
| 34 | + binary_tree_mirror_dict(root, binary_tree_mirror_dictionary) |
| 35 | + return binary_tree_mirror_dictionary |
32 | 36 |
|
33 | 37 |
|
34 | 38 | if __name__ == "__main__":
|
35 |
| - binary_tree = { 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11], 5: [12,13],7: [8,9]} |
36 |
| - root = 1 |
37 |
| - print("Binary tree:", sep=" ") |
38 |
| - print(binary_tree) |
39 |
| - binary_tree_mirror = binaryTreeMirror(root, binary_tree) |
40 |
| - print(" binary tree mirror:", sep=" ") |
41 |
| - print(binary_tree_mirror) |
| 39 | + binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]} |
| 40 | + print("Binary tree: ", binary_tree) |
| 41 | + binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 1) |
| 42 | + print("Binary tree mirror: ", binary_tree_mirror_dictionary) |
0 commit comments