How to Traverse an NLTK Tree object?

Maybe I’m overlooking things, but is this what you’re after? import nltk s=”(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))” tree = nltk.tree.Tree.fromstring(s) def traverse_tree(tree): # print(“tree:”, tree) for subtree in tree: if type(subtree) == nltk.tree.Tree: traverse_tree(subtree) traverse_tree(tree) It traverses your tree depth-first.

What are the practical factors to consider when choosing between Depth-First Search (DFS) and Breadth-First Search (BFS)? [closed]

That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items). If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth first search … Read more