@@ -12,12 +12,9 @@ class TreeNode:
12
12
13
13
Attributes:
14
14
-----------
15
- value : int
16
- The value stored at the node.
17
- left : TreeNode
18
- Pointer to the left child node (default is None).
19
- right : TreeNode
20
- Pointer to the right child node (default is None).
15
+ value : The value stored at the node.
16
+ left : Pointer to the left child node (default is None).
17
+ right : Pointer to the right child node (default is None).
21
18
"""
22
19
23
20
def __init__ (self , value : int ) -> None :
@@ -30,15 +27,6 @@ class BinaryTree:
30
27
"""
31
28
Class representing a binary tree.
32
29
33
- Methods:
34
- --------
35
- insert(value: int) -> None:
36
- Insert a value into the binary tree following binary search tree (BST) rules.
37
-
38
- morris_inorder_traversal() -> List[int]:
39
- Perform inorder traversal and return list of node values.
40
-
41
-
42
30
>>> bt = BinaryTree()
43
31
>>> bt.insert(9)
44
32
>>> bt.insert(6)
@@ -63,8 +51,7 @@ def insert(self, value: int) -> None:
63
51
64
52
Parameters:
65
53
-----------
66
- value : int
67
- The value to be inserted into the binary tree.
54
+ value : The value to be inserted into the binary tree.
68
55
"""
69
56
if self .root is None :
70
57
self .root = TreeNode (value )
@@ -77,10 +64,8 @@ def _insert_recursive(self, node: TreeNode, value: int) -> None:
77
64
78
65
Parameters:
79
66
-----------
80
- node : TreeNode
81
- The current node in the binary tree.
82
- value : int
83
- The value to be inserted.
67
+ node : The current node in the binary tree.
68
+ value : The value to be inserted.
84
69
"""
85
70
if value < node .value :
86
71
if node .left is None :
@@ -98,13 +83,11 @@ def _predecessor(self, node: TreeNode) -> TreeNode:
98
83
99
84
Parameters:
100
85
-----------
101
- node : TreeNode
102
- A node in the binary tree.
86
+ node : A node in the binary tree.
103
87
104
88
Returns:
105
89
--------
106
- TreeNode:
107
- The predecessor of the node passed in the parameter
90
+ The predecessor of the node passed in the parameter
108
91
"""
109
92
temp_node = node .left
110
93
while temp_node and temp_node .right and temp_node .right != node :
@@ -137,8 +120,7 @@ def morris_inorder_traversal(self) -> list[int]:
137
120
138
121
Returns:
139
122
--------
140
- List[int]:
141
- A list of integers representing the inorder traversal.
123
+ A list of integers representing the inorder traversal.
142
124
143
125
144
126
0 commit comments