![見出し画像](https://assets.st-note.com/production/uploads/images/96772961/rectangle_large_type_2_c79dbad01c74849aa5de5262ef3cae64.png?width=1200)
深さ優先行きがけ順のプログラム
勉強の記録として残します。
グラフの図は下記になります。
![](https://assets.st-note.com/production/uploads/images/96772881/picture_pc_2c4f2f110184f0f86688f544f05a4133.png?width=1200)
行きがけ順の実際のコードは下記になります。
def depth_search():
tree = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14],
[], [], [], [], [], [], [], []]
data = []
def search(pos):
print(pos, end= ' ')
data.append(pos)
for i in tree[pos]:
print()
print(tree[pos])
print(f'pos -> {i}')
print('-------------')
search(i)
search(0)
print()
print(* data)
depth_search()
実行結果
![](https://assets.st-note.com/img/1675000257626-dhKIA1SCqC.png)