Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active August 29, 2015 14:02
Show Gist options
  • Save jesuscast/cf710d89fddba7599f32 to your computer and use it in GitHub Desktop.
Save jesuscast/cf710d89fddba7599f32 to your computer and use it in GitHub Desktop.
Tree branches. Defines a tree as a tuple and then access the branches with 0 based index, the nesting is defined as another tuple.
import math
def tree_ref(tree, index):
if(isinstance(index,(list,tuple)) and (len(index)>1)):
#print tree
#print index
#print "--1---"
newTree = tree[index[0]]
#print newTree
newIndex = tuple([index[k] for k in range(1,len(index))])
#print newIndex
#print "-----2------"
return tree_ref(newTree, newIndex)
else:
if(isinstance(index,(list,tuple))):
return tree[index[0]]
else:
return tree[index]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment