Skip to content

Instantly share code, notes, and snippets.

@mondwan
Created December 22, 2015 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mondwan/50ec2d6cfd0c86e31f11 to your computer and use it in GitHub Desktop.
Save mondwan/50ec2d6cfd0c86e31f11 to your computer and use it in GitHub Desktop.
Demo how to use dfs in python
from treelib import Tree
tree = Tree()
# Create a tree for demo purpose
tree.create_node('A', 'a')
tree.create_node('B', 'b', parent='a')
tree.create_node('C', 'c', parent='a')
tree.create_node('D', 'd', parent='b')
tree.create_node('E', 'e', parent='b')
tree.create_node('F', 'f', parent='c')
tree.create_node('G', 'g', parent='c')
tree.create_node('H', 'h', parent='e')
# DFS
for n in tree.expand_tree('a', Tree.DEPTH):
print(tree[n].tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment