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!
=== Rebalance === : ''Definition:'' Collect the set of leaves ''L'' and rebuild the tree from the bottom-up. <syntaxhighlight lang="java"> static boolean isBalanced(RopeLike r) { val depth = r.depth(); if (depth >= FIBONACCI_SEQUENCE.length - 2) { return false; } return FIBONACCI_SEQUENCE[depth + 2] <= r.weight(); } static RopeLike rebalance(RopeLike r) { if (!isBalanced(r)) { val leaves = Ropes.collectLeaves(r); return merge(leaves, 0, leaves.size()); } return r; } static RopeLike merge(List<RopeLike> leaves) { return merge(leaves, 0, leaves.size()); } static RopeLike merge(List<RopeLike> leaves, int start, int end) { int range = end - start; if (range == 1) { return leaves.get(start); } if (range == 2) { return new RopeLikeTree(leaves.get(start), leaves.get(start + 1)); } int mid = start + (range / 2); return new RopeLikeTree(merge(leaves, start, mid), merge(leaves, mid, end)); } </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)