Is there a balanced BST
structure that also keeps track of subtree size in each node?
In Java
, TreeMap
is a red-black tree, but doesn't provide subtree size in each node.
Previously, I did write some BST that could keep track subtree size of each node, but it's not balanced.
The questions are:
O(lg(n))
for basic operations)?Java
impl is great, but other languages (e.g c
, go
) would also be helpful.BTW:
Possible appliation:
The Weight Balanced Tree (also called the Adams Tree, or Bounded Balance tree) keeps the subtree size in each node.
This also makes it possible to find the Nth element, from the start or end, in log(n) time.
My implementation in Nim is on github. It has properties:
There are also implementations in Scheme and Haskell available.