簡體   English   中英

python 中的 BFS 到 DFS 八叉樹轉換

[英]BFS to DFS octree conversion in python

我正在使用由第三方軟件構建的八叉樹。 這個軟件報告了如何以廣度優先的方式遍歷樹,但我想以深度優先的方式遍歷樹。

對於我的應用程序(為天體物理學應用程序從粒子數據構建網格),我傾向於根據它們的“精煉”列表來考慮八叉樹,即對於不精煉的單元格,我們將有:

False

而對於細化為 Oct 的單個單元格,我們將有:

True
False
False
False
False
False
False
False
False

我的目標是將這種以廣度優先方式生成的refined列表轉換為深度優先方式(在 python 中),但不知道從哪里開始。

例如這段代碼:

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()

將為深度優先遍歷創建refined列表。

所以你正在與hyperion合作? 如前所述,樹本質上不是深度優先或廣度優先的。 然而,hyperion 處理它們的方式實際上是為深度優先遍歷而設置的。 以列表格式考慮這個八叉樹:

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
             ]

深度優先遍歷是1、2、3、4、10、11、12……或者圖中的綠線: 八叉樹遍歷

為此,您只需要:

for node in refine3:
    print(node)

廣度優先(橙色線)也可以,但會更復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM