Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. """
  2. Depth first search code for the Recurse Center!
  3. """
  4.  
  5. def search_tree(tree, node, target_node):
  6. """ Finds a node in a tree using depth first search """
  7. if node == target_node:
  8. return target_node
  9. for child_node in tree[node]:
  10. found_node = search_tree(tree, child_node, target_node)
  11. if found_node:
  12. return found_node
  13.  
  14.  
  15. if __name__ == '__main__':
  16. tree = {
  17. 'a': ['b', 'c'],
  18. 'b': ['d', 'e', 'f'],
  19. 'c': ['g'],
  20. 'd': [],
  21. 'e': [],
  22. 'f': [],
  23. 'g': ['h'],
  24. 'h': []
  25. }
  26.  
  27. output = search_tree(tree, 'a', 'g')
  28. print(output)
Add Comment
Please, Sign In to add comment