Skip to content

Commit 3683281

Browse files
svediresvedire
authored andcommitted
Minor changes
1 parent 3bf3c6e commit 3683281

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

data_structures/binary_tree/binary_tree_mirror.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
"""
88

99

10-
def binaryTreeMirror(root: int, binary_tree: dict = {}, binary_tree_mirror: dict = {}):
10+
def binaryTreeMirrorDict(root: int, binary_tree: dict = {}, binary_tree_mirror: dict = {}):
11+
if root is None or root not in binary_tree.keys():
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)
18+
19+
def binaryTreeMirror(root: int, binary_tree: dict = {}) -> dict:
1120
"""
1221
>>> binaryTreeMirror(1, { 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]})
1322
{1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]}
@@ -17,21 +26,16 @@ def binaryTreeMirror(root: int, binary_tree: dict = {}, binary_tree_mirror: dict
1726
(1, { 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11], 5: [12,13],7: [8,9]})
1827
{1: [3, 2], 2: [5, 4], 4: [11, 10], 5: [13, 12], 3: [7, 6], 7: [9, 8]}
1928
"""
20-
if root is None or root not in binary_tree.keys():
21-
return
22-
left_child = binary_tree[root][0]
23-
right_child = binary_tree[root][1]
24-
binary_tree_mirror[root] = [right_child, left_child]
25-
binaryTreeMirror(left_child, binary_tree, binary_tree_mirror)
26-
binaryTreeMirror(right_child, binary_tree, binary_tree_mirror)
29+
binary_tree_mirror = {}
30+
binaryTreeMirrorDict(root, binary_tree, binary_tree_mirror)
31+
return binary_tree_mirror
2732

2833

2934
if __name__ == "__main__":
30-
binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]}
35+
binary_tree = { 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11], 5: [12,13],7: [8,9]}
3136
root = 1
3237
print("Binary tree:", sep=" ")
3338
print(binary_tree)
34-
binary_tree_mirror = {}
35-
binaryTreeMirror(root, binary_tree, binary_tree_mirror)
39+
binary_tree_mirror = binaryTreeMirror(root, binary_tree)
3640
print(" binary tree mirror:", sep=" ")
3741
print(binary_tree_mirror)

0 commit comments

Comments
 (0)