-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Feature: traverse backwards with $dfsIterator #7094
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
size-limit report 📦
|
I’m not sure it makes sense for the direction to be determined by the two nodes alone here, could be a bit confusing. I have been working on a much more robust traversal API that’s not quite ready that does offer both directions in #7046 |
@etrepum thanks for taking a look at my PR, I appreciate it! I took a look at #7046, but I don't fully understand it. It is a new concept, so it might take me some time to get the idea fully. Naively, I don't understand how that PR will change the $dfsIterator API. You mentioned that it could be confusing for the direction to be determined by the two nodes alone, but the API of $dfsIterator is the same on your branch - it still just accepts the two nodes. Or is there going to be some other API entirely for a backwards depth-first search, using the |
Perhaps I should have marked this PR as a bug fix. It seems wrong to me that you can supply an |
That’s right, the PR doesn’t change the existing API at all, it provides new more powerful abstractions. $dfsIterator is always strictly depth first left to right, there aren’t any existing full traversal APIs that go right to left ($getNextRightPreorderNode is as close as it gets IIRC). |
Description
The
$dfsIterator
allows you to pass in a startNode and an endNode, and then will progress from the start to the end.However, if you pass in an endNode that exists in the tree BEFORE the startNode, the iterator will progress forward from the startNode until it traverses the entire remainder of the tree and never hit the endNode.
This change allows the iterator to detect this condition and traverse the tree backwards.
The use-case for this is to traverse backwards from a starting point, towards the beginning of the editor, until you meet some condition. For example:
The alternative is to call
$dfs().reverse()
, but that is slow for editors with many nodes (lots of overhead).