Skip to content

Commit f410669

Browse files
svediresvedire
authored andcommitted
Resolved comments
1 parent 3683281 commit f410669

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

data_structures/binary_tree/binary_tree_mirror.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,36 @@
77
"""
88

99

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:
1212
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)
1818

19-
def binaryTreeMirror(root: int, binary_tree: dict = {}) -> dict:
19+
20+
def binary_tree_mirror(binary_tree: dict = {}, root: int = 1) -> dict:
2021
"""
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]})
2223
{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]})
2425
{}
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]}
2828
"""
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
3236

3337

3438
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

Comments
 (0)