Created
December 22, 2015 16:03
-
-
Save mondwan/50ec2d6cfd0c86e31f11 to your computer and use it in GitHub Desktop.
Demo how to use dfs in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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