Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created April 19, 2010 14:07
Show Gist options
  • Save radiosilence/371079 to your computer and use it in GitHub Desktop.
Save radiosilence/371079 to your computer and use it in GitHub Desktop.
t = ( "F",
( "B",
( "A" ),
( "D",
( "C",
( "Z" ),
( "M" )
),
( "E" )
)
),
( "G",
(),
( "I",
( "H" ),
()
)
)
)
def Traverse( t, o ):
if isinstance( o, basestring ):
order = {
'pre': [ 0, 1, 2 ],
'in': [ 1, 0, 2 ],
'post': [ 1, 2, 0 ],
}[o]
else:
order = o
nodes = []
for i in order:
if isinstance( t[ i ], str ):
nodes.append( t[ i ] )
elif len( t[ i ] ) > 1:
nodes.extend( Traverse( t[ i ], order ))
return nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment