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

Method mirror

data_structures/binary_tree/mirror_binary_tree.py:33–53  ·  view source on GitHub ↗

Mirror the binary tree rooted at this node by swapping left and right children. >>> tree = Node(0) >>> list(tree) [0] >>> list(tree.mirror()) [0] >>> tree = Node(1, Node(0), Node(3, Node(2), Node(4, None, Node(5)))) >>> tuple(tree)

(self)

Source from the content-addressed store, hash-verified

31 return sum(1 for _ in self)
32
33 def mirror(self) -> Node:
34 """
35 Mirror the binary tree rooted at this node by swapping left and right children.
36
37 >>> tree = Node(0)
38 >>> list(tree)
39 [0]
40 >>> list(tree.mirror())
41 [0]
42 >>> tree = Node(1, Node(0), Node(3, Node(2), Node(4, None, Node(5))))
43 >>> tuple(tree)
44 (0, 1, 2, 3, 4, 5)
45 >>> tuple(tree.mirror())
46 (5, 4, 3, 2, 1, 0)
47 """
48 self.left, self.right = self.right, self.left
49 if self.left:
50 self.left.mirror()
51 if self.right:
52 self.right.mirror()
53 return self
54
55
56def make_tree_seven() -> Node:

Callers 1

mainFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected