Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data structure advice on c++

I am looking for data structure in c++ and I need an advice.

I have nodes, every node has unique_id and group_id:

1 1.1.1.1
2 1.1.1.2
3 1.1.1.3

4 1.1.2.1
5 1.1.2.2
6 1.1.2.3

7 2.1.1.1
8 2.1.1.2

I need a data structure to answer those questions:

  1. what is the group_id of node 4
  2. give me list (probably vector) of unique_id's that belong to group 1.1.1
  3. give me list (probably vector) of unique_id's that belong to group 1.1
  4. give me list (probably vector) of unique_id's that belong to group 1

Is there a data structure that can answer those questions (what is the complexity time of inserting and answering)? or should I implement it?

I would appreciate an example.

EDIT:

at the beginning, I need to build this data structure. most of the action is reading by group id. insertion will happen but less then reading.

the time complexity is more important than memory space

like image 496
user1673206 Avatar asked Apr 30 '15 09:04

user1673206


1 Answers

To me, hierarchical data like the group ID calls for a tree structure. (I assume that for 500 elements this is not really necessary, but it seems natural and scales well.)

Each element in the first two levels of the tree would just hold vectors (if they come ordered) or maps (if they come un-ordered) of sub-IDs.

The third level in the tree hierarchy would hold pointers to leaves, again in a vector or map, which contain the fourth group ID part and the unique ID.

Questions 2-4 are easily and quickly answered by navigating the tree.

For question 1 one needs an additional map from unique IDs to leaves in the tree; each element inserted into the tree also has a pointer to it inserted into the map.

like image 141
Peter - Reinstate Monica Avatar answered Sep 26 '22 13:09

Peter - Reinstate Monica