MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / main

Function main

data_structures/binary_tree/mirror_binary_tree.py:114–153  ·  view source on GitHub ↗

r""" Mirror binary trees with the given root and returns the root >>> tree = make_tree_nine() >>> tuple(tree) (7, 4, 8, 2, 5, 9, 1, 3, 6) >>> tuple(tree.mirror()) (6, 3, 1, 9, 5, 2, 8, 4, 7) nine_tree:: 1 / \ 2 3 / \ \

()

Source from the content-addressed store, hash-verified

112
113
114def main() -> None:
115 r"""
116 Mirror binary trees with the given root and returns the root
117
118 >>> tree = make_tree_nine()
119 >>> tuple(tree)
120 (7, 4, 8, 2, 5, 9, 1, 3, 6)
121 >>> tuple(tree.mirror())
122 (6, 3, 1, 9, 5, 2, 8, 4, 7)
123
124 nine_tree::
125
126 1
127 / \
128 2 3
129 / \ \
130 4 5 6
131 / \ \
132 7 8 9
133
134 The mirrored tree looks like this::
135
136 1
137 / \
138 3 2
139 / / \
140 6 5 4
141 / / \
142 9 8 7
143 """
144 trees = {"zero": Node(0), "seven": make_tree_seven(), "nine": make_tree_nine()}
145 for name, tree in trees.items():
146 print(f" The {name} tree: {tuple(tree)}")
147 # (0,)
148 # (4, 2, 5, 1, 6, 3, 7)
149 # (7, 4, 8, 2, 5, 9, 1, 3, 6)
150 print(f"Mirror of {name} tree: {tuple(tree.mirror())}")
151 # (0,)
152 # (7, 3, 6, 1, 5, 2, 4)
153 # (6, 3, 1, 9, 5, 2, 8, 4, 7)
154
155
156if __name__ == "__main__":

Callers 1

Calls 4

make_tree_sevenFunction · 0.85
make_tree_nineFunction · 0.85
mirrorMethod · 0.80
NodeClass · 0.70

Tested by

no test coverage detected