Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions basic_data_structure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down
6 changes: 4 additions & 2 deletions basic_data_structure/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions basic_data_structure/nodes/tree_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down