Skip to content
Snippets Groups Projects
Select Git revision
  • 2e4943999a1e7c6e9a9860435f3424104e4d806a
  • master default protected
2 results

viz.py

Blame
  • Forked from Vuillemot Romain / INF-TC1
    Source project has a limited visibility.
    arbre-parcours-largeur.py 1.05 KiB
    class Noeud:
        def __init__(self, v, c = []):
            self.v = v
            self.c = c
    
    def parcours(n):
        stack = []
        stack.append(n)
        r = []
        while len(stack) > 0:
            current = stack.pop(0)
            r.append(current.v)
            for v in current.c:
                stack.append(v)
        return r
    
    if __name__=="__main__": 
    
        print(parcours(Noeud(1,                         \
                        [                               \
                            Noeud(2, [                  \
                                Noeud(4), Noeud(5)      \
                            ]),                         \
                            Noeud(3)                    \
                        ]                               \
                    )                                   \
        ))
    
        # [1, 2, 3, 4, 5]
        print(parcours(Noeud(5, [Noeud(4, [Noeud(2), Noeud(1)]), Noeud(3)])))
        # [5, 4, 3, 1, 2]
        print(parcours(Noeud(5, [Noeud(1), Noeud(2), Noeud(3), Noeud(4)])))
        # [5, 1, 2, 3, 4]
        print(parcours(Noeud(1, [Noeud(5), Noeud(4), Noeud(3), Noeud(2)])))
        # [1, 5, 4, 3, 2]