How to search for an object in more efficient way?

53 Views Asked by At

I have a doubt how to check if structure contains an object.

Simple sketch of my structure:

Root - is a group which can contain groups, keys, values.

Group - can contain groups keys, values.

Key - can contain keys and values.

Value - just keeps value.


Lets say I need to check if root contains value.

Here some pseudo code:

for every item in root
    if it is group
        pass to group checking function
    if it is key
        pass to key checking function
    if it is value
        check

Also I can do it in reverse way:

for every item in root
    if it is value
        check
    if it is key
        pass to key checking function
    if it is group
        pass to group checking function

I think that the second one which goes from the smallest unit should perform better, but did some small (but not very small) test with random data and results was quite similar.

I would like to ask for some mathematician prover or something else what would let determine which way is faster.