Created
January 26, 2015 03:56
-
-
Save caoxudong/45406c3287796c88a80a to your computer and use it in GitHub Desktop.
树深度优先搜索
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
#!/usr/bin/env python | |
__author__ = 'caoxudong' | |
from datastructure.common_datastructure import node | |
def visit_node(node): | |
print(node.data) | |
def depth_first_search(root=node.Node): | |
if root: | |
stack = [] | |
stack.append(root) | |
while len(stack): | |
temp_node = stack.pop() | |
visit_node(temp_node) | |
if temp_node.children and len(temp_node.children) > 0: | |
for e in temp_node.children: | |
stack.append(e) | |
if __name__ == '__main__': | |
root = node.Node | |
depth_first_search(root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment