patternMinor
Split in AVL tree with complexity $O(\log n)$
Viewed 0 times
logwithsplitavlcomplexitytree
Problem
Can the split operation be implemented for AVL trees with complexity $O(\log n)$? I'm interested in links to articles or any specific information about this subject.
The split operation divides the AVL tree into two derived AVL trees, based on key. One of the derived trees should contain all the vertices in which all keys less than the original key, and the second the rest.
I know this can be done in $O(\log^2 n)$ time. Here's a link to implementation with complexity $O(\log^2 n)$:
https://code.google.com/p/self-balancing-avl-tree/
I also know how to merge two AVL trees, such that the keys of one tree are all smaller than the keys of the other, in $O(\log n)$ time.
Here's an implementation with complexity $O(\log n)$:
The split operation divides the AVL tree into two derived AVL trees, based on key. One of the derived trees should contain all the vertices in which all keys less than the original key, and the second the rest.
I know this can be done in $O(\log^2 n)$ time. Here's a link to implementation with complexity $O(\log^2 n)$:
https://code.google.com/p/self-balancing-avl-tree/
I also know how to merge two AVL trees, such that the keys of one tree are all smaller than the keys of the other, in $O(\log n)$ time.
Here's an implementation with complexity $O(\log n)$:
def Merge(l, r) {
if (!l || !r)
return l ? l : r;
if (l->h h)
r->l = Merge(l, r->l), Rebalance(r);
else
l->r = Merge(l->r, r), Rebalance(l);
}Solution
Yes, this is possible.
You can read about it in Ramzi Fadel and Kim Vagn Jakobsen's "Data structures and algorithms in a two-level memory", section 3.1.6, (mirror) or in the OCaml standard library, at the "split" function.
One of the key insights is that the merge function you mention is, with more careful accounting, $O(h_1 - h_2)$, where $h_1$ is the height of the taller tree and $h_2$ is the height of the shorter tree. As such, merging a list of trees that have descending or ascending heights costs only $O(h_{\max} - h_{\min})$, since the sum telescopes.
You can read about it in Ramzi Fadel and Kim Vagn Jakobsen's "Data structures and algorithms in a two-level memory", section 3.1.6, (mirror) or in the OCaml standard library, at the "split" function.
One of the key insights is that the merge function you mention is, with more careful accounting, $O(h_1 - h_2)$, where $h_1$ is the height of the taller tree and $h_2$ is the height of the shorter tree. As such, merging a list of trees that have descending or ascending heights costs only $O(h_{\max} - h_{\min})$, since the sum telescopes.
Context
StackExchange Computer Science Q#43214, answer score: 7
Revisions (0)
No revisions yet.