Skip to content

Commit 00e9d86

Browse files
authored
Improve comments, add doctests in symmetric_tree.py (#11619)
1 parent 43a47e0 commit 00e9d86

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

Diff for: data_structures/binary_tree/symmetric_tree.py

+62-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@
1313
@dataclass
1414
class Node:
1515
"""
16-
A Node has data variable and pointers to Nodes to its left and right.
16+
A Node represents an element of a binary tree, which contains:
17+
18+
Attributes:
19+
data: The value stored in the node (int).
20+
left: Pointer to the left child node (Node or None).
21+
right: Pointer to the right child node (Node or None).
22+
23+
Example:
24+
>>> node = Node(1, Node(2), Node(3))
25+
>>> node.data
26+
1
27+
>>> node.left.data
28+
2
29+
>>> node.right.data
30+
3
1731
"""
1832

1933
data: int
@@ -24,12 +38,25 @@ class Node:
2438
def make_symmetric_tree() -> Node:
2539
r"""
2640
Create a symmetric tree for testing.
41+
2742
The tree looks like this:
2843
1
2944
/ \
3045
2 2
3146
/ \ / \
3247
3 4 4 3
48+
49+
Returns:
50+
Node: Root node of a symmetric tree.
51+
52+
Example:
53+
>>> tree = make_symmetric_tree()
54+
>>> tree.data
55+
1
56+
>>> tree.left.data == tree.right.data
57+
True
58+
>>> tree.left.left.data == tree.right.right.data
59+
True
3360
"""
3461
root = Node(1)
3562
root.left = Node(2)
@@ -43,13 +70,26 @@ def make_symmetric_tree() -> Node:
4370

4471
def make_asymmetric_tree() -> Node:
4572
r"""
46-
Create a asymmetric tree for testing.
73+
Create an asymmetric tree for testing.
74+
4775
The tree looks like this:
4876
1
4977
/ \
5078
2 2
5179
/ \ / \
5280
3 4 3 4
81+
82+
Returns:
83+
Node: Root node of an asymmetric tree.
84+
85+
Example:
86+
>>> tree = make_asymmetric_tree()
87+
>>> tree.data
88+
1
89+
>>> tree.left.data == tree.right.data
90+
True
91+
>>> tree.left.left.data == tree.right.right.data
92+
False
5393
"""
5494
root = Node(1)
5595
root.left = Node(2)
@@ -63,7 +103,15 @@ def make_asymmetric_tree() -> Node:
63103

64104
def is_symmetric_tree(tree: Node) -> bool:
65105
"""
66-
Test cases for is_symmetric_tree function
106+
Check if a binary tree is symmetric (i.e., a mirror of itself).
107+
108+
Parameters:
109+
tree: The root node of the binary tree.
110+
111+
Returns:
112+
bool: True if the tree is symmetric, False otherwise.
113+
114+
Example:
67115
>>> is_symmetric_tree(make_symmetric_tree())
68116
True
69117
>>> is_symmetric_tree(make_asymmetric_tree())
@@ -76,8 +124,17 @@ def is_symmetric_tree(tree: Node) -> bool:
76124

77125
def is_mirror(left: Node | None, right: Node | None) -> bool:
78126
"""
127+
Check if two subtrees are mirror images of each other.
128+
129+
Parameters:
130+
left: The root node of the left subtree.
131+
right: The root node of the right subtree.
132+
133+
Returns:
134+
bool: True if the two subtrees are mirrors of each other, False otherwise.
135+
136+
Example:
79137
>>> tree1 = make_symmetric_tree()
80-
>>> tree1.right.right = Node(3)
81138
>>> is_mirror(tree1.left, tree1.right)
82139
True
83140
>>> tree2 = make_asymmetric_tree()
@@ -91,7 +148,7 @@ def is_mirror(left: Node | None, right: Node | None) -> bool:
91148
# One side is empty while the other is not, which is not symmetric.
92149
return False
93150
if left.data == right.data:
94-
# The values match, so check the subtree
151+
# The values match, so check the subtrees recursively.
95152
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
96153
return False
97154

0 commit comments

Comments
 (0)