Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Rope (data structure)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Collect leaves === : ''Definition:'' Create a stack ''S'' and a list ''L''. Traverse down the left-most spine of the tree until you reach a leaf l', adding each node ''n'' to ''S''. Add l' to ''L''. The parent of l' (''p'') is at the top of the stack. Repeat the procedure for p's right subtree. <syntaxhighlight lang="java"> final class InOrderRopeIterator implements Iterator<RopeLike> { private final Deque<RopeLike> stack; InOrderRopeIterator(@NonNull RopeLike root) { stack = new ArrayDeque<>(); var c = root; while (c != null) { stack.push(c); c = c.getLeft(); } } @Override public boolean hasNext() { return stack.size() > 0; } @Override public RopeLike next() { val result = stack.pop(); if (!stack.isEmpty()) { var parent = stack.pop(); var right = parent.getRight(); if (right != null) { stack.push(right); var cleft = right.getLeft(); while (cleft != null) { stack.push(cleft); cleft = cleft.getLeft(); } } } return result; } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)