Je n'y connais rien mais en gros c'est ça un arbre binaire en python ?
arbre = {1: {2: {4:{}}, 3: {5:{}, 6:{}}}}
1 2 3 4 5 6 7 8 9
| def recursive(tree,pcd=''):
if isinstance(tree,dict):
if pcd:
pcd += ' contient '+str(list(tree.keys()))
else:
pcd = str(list(tree.keys()))
print(pcd)
for k in tree:
recursive(tree[k],pcd+' -> '+str(k)) |
1 2 3 4 5 6 7 8
| >>> recursive(arbre)
[1]
[1] -> 1 contient [2, 3]
[1] -> 1 contient [2, 3] -> 2 contient [4]
[1] -> 1 contient [2, 3] -> 2 contient [4] -> 4 contient []
[1] -> 1 contient [2, 3] -> 3 contient [5, 6]
[1] -> 1 contient [2, 3] -> 3 contient [5, 6] -> 5 contient []
[1] -> 1 contient [2, 3] -> 3 contient [5, 6] -> 6 contient [] |
??
Partager