I need a container that supports efficient random access and installing and uninstalling O (k)

I tried again to ask the same question , but in the end I asked another question without providing significant information about my problem.

I am implementing a data structure that is a tree. Each node of this tree has an array / vector / (random access structure) with all its children, which can be arbitrary. Inserting / deleting elements is easy as we always double / halve the number of elements in this array.

This is what insert / delete means O(k)in this context. We have items kand we will add kmore or remove k/2. The reconstruction of the entire data structure is still beautiful. A dynamic array(or a vector).

The operation that raises the question is as follows. Sometimes we need "split"a node to have nchildren, which means that we "divide" the children among different nodes. We dive into continuous groups. The most effective way for this separation is, in my opinion, to have one pointer for each new node in the place where its children are and how many there are (let each node accept kchildren). But then the number of these children can change and this should not affect his brothers and sisters (or, even worse, the whole tree), then the insertion time should be O(k), and not O(n). How do we do this?

An easy but ineffective job will be that every time we split the node, we replace the “large” array with childrenmany (as well as divided parts) “small” dynamic arrays.

Each of the following “boxes” represents a random access structure.

alt text

+3
source share
3 answers

From the description that you indicated in the tree structure that you are implementing, it is best to create a new data structure that mimics your tree. Especially if you are already tracking pointers between nodes.

, node node . node, , , node node.

:

N1->N2->(n3,n4,n5,n6,n7,n8) N2 : N1->(N2_1, N2_2) N2_1->(n3,n4,n5) N2_2->(n6,n7,n8)

(, , ...)

, , , log n. , .

:

, N1->N2->(n3,n4,n5,n6,n7,n8). N1 , N1 node: N1->(N2,N9)->(n3,n4,n5,n6,n7,n8)

node ( ):

class Node {
  vector<Node *> children;
  Node * parent;
};

, . node , children node. .

+1

hashmap?

O(1) , ; O(n) .

+3

, B + Tree...

B +, , (log n), .

, 1000, , 1000 , 1 000 000 ..

1 000 000 000 , 3 , .

python, , blist, list ( ++ vector) B +, . .

+1

Source: https://habr.com/ru/post/1766886/


All Articles