Skip to content

Commit dc06958

Browse files
susmith98svediregithub-actions
authored
Added binary tree mirror algorithm (TheAlgorithms#3159)
* Added binary tree mirror algorithm * Minor changes * Resolved comments * Minor Changes * resolved comments and updated doctests * updated doctests * updating DIRECTORY.md Co-authored-by: svedire <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent f0aa63f commit dc06958

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
* [Basic Binary Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/basic_binary_tree.py)
106106
* [Binary Search Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_search_tree.py)
107107
* [Binary Search Tree Recursive](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_search_tree_recursive.py)
108+
* [Binary Tree Mirror](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_tree_mirror.py)
108109
* [Fenwick Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/fenwick_tree.py)
109110
* [Lazy Segment Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lazy_segment_tree.py)
110111
* [Lowest Common Ancestor](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lowest_common_ancestor.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Problem Description:
3+
Given a binary tree, return it's mirror.
4+
"""
5+
6+
7+
def binary_tree_mirror_dict(binary_tree_mirror_dictionary: dict, root: int):
8+
if not root or root not in binary_tree_mirror_dictionary:
9+
return
10+
left_child, right_child = binary_tree_mirror_dictionary[root][:2]
11+
binary_tree_mirror_dictionary[root] = [right_child, left_child]
12+
binary_tree_mirror_dict(binary_tree_mirror_dictionary, left_child)
13+
binary_tree_mirror_dict(binary_tree_mirror_dictionary, right_child)
14+
15+
16+
def binary_tree_mirror(binary_tree: dict, root: int = 1) -> dict:
17+
"""
18+
>>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1)
19+
{1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]}
20+
>>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 1)
21+
{1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]}
22+
>>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 5)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: root 5 is not present in the binary_tree
26+
>>> binary_tree_mirror({}, 5)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: binary tree cannot be empty
30+
"""
31+
if not binary_tree:
32+
raise ValueError("binary tree cannot be empty")
33+
if root not in binary_tree:
34+
raise ValueError(f"root {root} is not present in the binary_tree")
35+
binary_tree_mirror_dictionary = dict(binary_tree)
36+
binary_tree_mirror_dict(binary_tree_mirror_dictionary, root)
37+
return binary_tree_mirror_dictionary
38+
39+
40+
if __name__ == "__main__":
41+
binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]}
42+
print(f"Binary tree: {binary_tree}")
43+
binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 5)
44+
print(f"Binary tree mirror: {binary_tree_mirror_dictionary}")

0 commit comments

Comments
 (0)