I need to present a tree structure in what is basically a list view with support for data virtualization. To do this I need to be able to query for a range of items in the tree based on their index, or position in the visible tree.
Because each node in the tree may contain millions of children, I load children only when a parent node is expanded. On the back-end side I want to cache the loaded children for each expanded node in a file. The front-end then needs to be able to query for the visible range of items as if it was a flat list however.
So for example the tree:
- Root
- A
- A1
- A2
- B
- B1
- C
- D
- A
Would need to be presented as a list like [A, A1, A2, B, B1, C, D].
Collapsing a node, for example A would instead yield the result [A, B, B1, C, D].
To do this some kind of index is needed to keep track of expanded nodes and where the cached data resides.
To do this one idea was to have some sort of range map, where we map indices (or positions) to data files. So for the tree above we could have something like:
[0, 0] => (Root, 0)
[1, 2] => (A, 0)
[3, 3] => (Root, 1)
[4, 4] => (B, 0)
[5, 6] => (Root, 2)
Such an index could allow for fast querying of a range, for example to retrieve all items between index 2 and 5.
However, what would be a good data structure to store such an index? Since expanding or collapsing nodes requires changing the intervals of all successors to the node being expanded or collapsed.
Or is there a better way to structure such an index?