I have the following hierarchy:
- A
- X : [1, 2, 3]
- Y : [4, 5]
- Z : [10, 11]
- B
- X : [6, 7]
- Y : [8]
And what I want is to have following queries give me following results:
get(A) ==> [1,2,3,4,5,10,11]
get(A,Y) ==> [4,5]
get(B) ==> [6,7,8]
get(B,X) ==> [6,7]
So far, it seems easy. I can accomplish this by having a Dictionary> which can be a defaultdict(lambda : defaultdict(list)) in Python. However, what if I need to make it more generic and have another level, or another 2 levels? Something like :
- A
- X
- i : [1]
- ii : [2,3]
- Y
- i : [4, 5]
- Z
- ii : [10, 11]
- B
- X
- ii : [6]
- iii : [7]
- Y
- i : [8]
In this example, the first hierarchy is a "projection" of the second hierarchy where the last level is merged into the parent. So, all queries for the first hierarchy should give the same results.
Some sample queries for new level:
get(B, X, ii) ==> [6]
get(B,X) ==> [6,7] (same query and result as before)
Please note that, data is only in leaf nodes. So, for insertion, whole path must be given:
insert(A, X, i, 20)
That also means, we can give the depth of the tree in constructor of the data structure.
EDIT: I realized that I need validation of depth:
- Insert operation : whole path must be given and the len(path) must be equal to depth
- Get operation : a path "deeper" than the depth of the structure is not allowed