0

I'm working with an octree that has been built by third party software. This software reports how to traverse the tree in a breadth first manner, but I want to traverse the tree in a depth first manner.

For my application (building a grid out of particle data for astrophysics applications) I tend to think of octrees in terms of their 'refined' lists, i.e. for a cell that doesn't refine, we would have:

False

Whereas for a single cell that refines into an Oct, we would have:

True
False
False
False
False
False
False
False
False

My goal is to convert such a refined list that is generated in a Breadth First manner into a Depth First manner (in python), but am at a loss as to where to start.

For an example this code:

def construct_octree(refined=[True]):

    # Loop over subcells
    for subcell in range(8):

        # Insert criterion for whether cell should be sub-divided. Here we
        # just use a random number to demonstrate.
        divide = random.random() < 0.5

        # Append boolean to overall list
        refined.append(divide)

        # If the cell is sub-divided, recursively divide it further
        if divide:
            construct_octree(refined)

    return refined

oct = construct_octree()

would create the refined list for a depth first traversal.

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
nop.bot
  • 11
  • 1
  • 1
    Your question isn't very clear to me. Depth-first and breadth-first are ways to traverse a tree, they are not features of the tree itself. I don't really know what you mean by refined lists. From a cursory search, it looks as though each node in an Octree usually represents a volume in space. Breadth first traversal would visit the 8 largest volumes first and then visit the sub-volumes. Depth first would do the opposite. Perhaps you could provide some code for your tree and how you currently traverse it. – FiddleStix Nov 21 '19 at 17:46
  • thanks - I edited the question above to hopefully be more clear. Basically - I have a tree built by 3d party software that reports such a `refined` list in breadth first format, and I want to figure out how to convert this into depth first. – nop.bot Nov 21 '19 at 18:29

1 Answers1

0

So you're working with hyperion? As mentioned, trees aren't inherently depth-first or breadth-first. However, the way hyperion treats them is actually ideally set up for depth first traversal. Consider this octree in list format:

refine3 = [True,       # 1
             False,    # 2
             False,    # 3
             True,     # 4
               False,  # 10
               False,  # 11
               False,  # ..
               False,  # ..
               False,  # 14
               False,  # 15
               False,  # 16
               False,  # 17
             False,    # 5
             False,    # 6
             False,    # 7
             True,     # 8
               False,  # 18
               False,  # 19
               False,  # 20
               False,  # ..
               False,  # ..
               False,  # ..
               False,  # ..
               False,  # 25
             False,    # 9
             ]

Depth-first traversal is 1, 2, 3, 4, 10, 11, 12... or the green line on the diagram: octree traversal

To do that, all you need is:

for node in refine3:
    print(node)

Breadth-first (the orange line) can be done as well, but it would be more complicated.

FiddleStix
  • 3,016
  • 20
  • 21
  • I am using hyperion! Thanks - this is a useful way to think about this and I appreciate the nice schematic! My issue is: I want to convert a 'refined' list from an octree generated by a different package (yt) that is generated in breadth-first format into a depth first format for hyperion. What I'm wondering is if there's a standard algorithm/pre-packaged code in python that does this sort of thing. (Otherwise I can roll my own...but trying not to reinvent the wheel). – nop.bot Nov 22 '19 at 15:42
  • If you can give us a link to the yt source or an example of a yt-generated refined list, I'm sure someone could point you in the right direction. – FiddleStix Nov 22 '19 at 16:25