13
13
@dataclass
14
14
class Node :
15
15
"""
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
17
31
"""
18
32
19
33
data : int
@@ -24,12 +38,25 @@ class Node:
24
38
def make_symmetric_tree () -> Node :
25
39
r"""
26
40
Create a symmetric tree for testing.
41
+
27
42
The tree looks like this:
28
43
1
29
44
/ \
30
45
2 2
31
46
/ \ / \
32
47
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
33
60
"""
34
61
root = Node (1 )
35
62
root .left = Node (2 )
@@ -43,13 +70,26 @@ def make_symmetric_tree() -> Node:
43
70
44
71
def make_asymmetric_tree () -> Node :
45
72
r"""
46
- Create a asymmetric tree for testing.
73
+ Create an asymmetric tree for testing.
74
+
47
75
The tree looks like this:
48
76
1
49
77
/ \
50
78
2 2
51
79
/ \ / \
52
80
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
53
93
"""
54
94
root = Node (1 )
55
95
root .left = Node (2 )
@@ -63,7 +103,15 @@ def make_asymmetric_tree() -> Node:
63
103
64
104
def is_symmetric_tree (tree : Node ) -> bool :
65
105
"""
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:
67
115
>>> is_symmetric_tree(make_symmetric_tree())
68
116
True
69
117
>>> is_symmetric_tree(make_asymmetric_tree())
@@ -76,8 +124,17 @@ def is_symmetric_tree(tree: Node) -> bool:
76
124
77
125
def is_mirror (left : Node | None , right : Node | None ) -> bool :
78
126
"""
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:
79
137
>>> tree1 = make_symmetric_tree()
80
- >>> tree1.right.right = Node(3)
81
138
>>> is_mirror(tree1.left, tree1.right)
82
139
True
83
140
>>> tree2 = make_asymmetric_tree()
@@ -91,7 +148,7 @@ def is_mirror(left: Node | None, right: Node | None) -> bool:
91
148
# One side is empty while the other is not, which is not symmetric.
92
149
return False
93
150
if left .data == right .data :
94
- # The values match, so check the subtree
151
+ # The values match, so check the subtrees recursively.
95
152
return is_mirror (left .left , right .right ) and is_mirror (left .right , right .left )
96
153
return False
97
154
0 commit comments