Bonjour,
toute nouvelle dans Python .Comme exercice je cherche à faire un arbre
lexicographique. Cad un arbre des lettres d'une série de mots.
Pour un langage fini quelconque L construit son arbre lexicographique. On devra pouvoir afficher les arbres obtenus
L = { A, AN, ANE, ANES, ANI, BAT, BATE, BAS, BASSE, JE, JEU, JOUE }.
j'ai essayer d'abord avec un exemple mais j'ai un petit problème il m'affiche que le premier mot
Si quelqu'un a des idées, merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #!/usr/bin/python3.1 def append_word(tree, word): if len(word): char = word[0] if not tree.has_key(char): tree[char] = {} append_word(tree[char], word[1:]) def make_tree(tree, words): for w in words: append_word(tree, w) def dump_tree(tree, indent=0): for c in tree.keys(): print (" " * indent + c) dump_tree(tree[c], indent + 2) words = ['balle', 'ballon', 'bals', 'bas'] tree = {} make_tree(tree, words) dump_tree(tree)
Partager