diff --git a/basic_data_structure/__init__.py b/basic_data_structure/__init__.py index 21290f6..117eb6c 100644 --- a/basic_data_structure/__init__.py +++ b/basic_data_structure/__init__.py @@ -73,6 +73,7 @@ def main(): node = lst[4] # get node print('Saved node:', node.value) + # Output: Saved node: 3 lst.insert(4, 4) # insert new value into list lst.append(2) # append a new value @@ -83,17 +84,18 @@ def main(): lst.reverse() # reverse list print('List length:', len(lst)) # length of the list + # Output: List length: 9 + # iteration over values for val in lst.values(): - # iteration over values print(val, end=' ') # Output: 1 2 3 4 5 6 7 8 9 print('') + # iteration over nodes for node in lst: - # iteration over nodes print(node.value, end=' ') # Output: 1 2 3 4 5 6 7 8 9 @@ -157,13 +159,9 @@ def generate_tree() -> TreeNode: def dfs(root: Optional[TreeNode]) -> Generator[int, None, None]: \"""Depth-first search.\""" if root: - if root.left: - yield from dfs(root.left) - + yield from dfs(root.left) yield root.value - - if root.right: - yield from dfs(root.right) + yield from dfs(root.right) def main(): diff --git a/basic_data_structure/linked_list.py b/basic_data_structure/linked_list.py index c34e16f..a339f97 100644 --- a/basic_data_structure/linked_list.py +++ b/basic_data_structure/linked_list.py @@ -17,6 +17,7 @@ def main(): node = lst[4] # get node print('Saved node:', node.value) + # Output: Saved node: 3 lst.insert(4, 4) # insert new value into list lst.append(2) # append a new value @@ -27,17 +28,18 @@ def main(): lst.reverse() # reverse list print('List length:', len(lst)) # length of the list + # Output: List length: 9 + # iteration over values for val in lst.values(): - # iteration over values print(val, end=' ') # Output: 1 2 3 4 5 6 7 8 9 print('') + # iteration over nodes for node in lst: - # iteration over nodes print(node.value, end=' ') # Output: 1 2 3 4 5 6 7 8 9 diff --git a/basic_data_structure/nodes/tree_node.py b/basic_data_structure/nodes/tree_node.py index 79431a9..8d124b4 100644 --- a/basic_data_structure/nodes/tree_node.py +++ b/basic_data_structure/nodes/tree_node.py @@ -65,13 +65,9 @@ def generate_tree() -> TreeNode: def dfs(root: Optional[TreeNode]) -> Generator[int, None, None]: \"""Depth-first search.\""" if root: - if root.left: - yield from dfs(root.left) - + yield from dfs(root.left) yield root.value - - if root.right: - yield from dfs(root.right) + yield from dfs(root.right) def main():