Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making binary tree traversals lazy. #8725

Closed
amirsoroush opened this issue May 12, 2023 · 4 comments · Fixed by #9208 or #9237
Closed

Making binary tree traversals lazy. #8725

amirsoroush opened this issue May 12, 2023 · 4 comments · Fixed by #9208 or #9237
Labels
enhancement This PR modified some existing files

Comments

@amirsoroush
Copy link
Contributor

Feature description

Hi,
In binary_tree_traversals.py file, there are different kinds of traversals such as preorder, inorder, postorder and etc.

Although the implementations are pretty clean one-liner like:

# preorder
return [root.data, *preorder(root.left), *preorder(root.right)] if root else []

It isn't memory friendly. We can use generators instead not to load all the nodes into the memory:

# preorder
    if not root:
        return []
    yield root.data
    yield from preorder(root.left)
    yield from preorder(root.right)

Shall we go ahead and change them?

@amirsoroush amirsoroush added the enhancement This PR modified some existing files label May 12, 2023
@spam-1008
Copy link

+1

@rohan472000
Copy link
Contributor

Absolutely, it's a great idea to switch from list-based traversals to generator-based traversals in the binary_tree_traversals.py file. Using generators instead of lists will indeed make the traversals more memory-friendly, especially for large trees.

@dev-soni-07

This comment was marked as off-topic.

@tianyizheng02
Copy link
Contributor

@digital-dev-07 Read the contributing guidelines.

If you are interested in resolving an open issue, simply make a pull request with your proposed fix. We do not assign issues in this repo so please do not ask for permission to work on an issue.

tianyizheng02 pushed a commit that referenced this issue Oct 1, 2023
…ixes #8725 completely. (#9237)

* Made binary tree memory-friendly using generators based travels. Fixes
#8725

* Made binary tree memory-friendly using generators based travels. Fixes
#8725

* Fixed pre-commit errors
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this issue Sep 15, 2024
…ixes TheAlgorithms#8725 completely. (TheAlgorithms#9237)

* Made binary tree memory-friendly using generators based travels. Fixes
TheAlgorithms#8725

* Made binary tree memory-friendly using generators based travels. Fixes
TheAlgorithms#8725

* Fixed pre-commit errors
@isidroas isidroas mentioned this issue Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
6 participants
@tianyizheng02 @rohan472000 @dev-soni-07 @amirsoroush @spam-1008 and others