如图所示,爬去网站的url时,深度优先按照这个顺序爬取 利用递归实现树的广度优先遍历
A B D E I C F G H(递归实现)
def depth-tree(tree-code);
if tree-node is not None;
print (tree-note.-data)
if tree-node.-left is not None;
return depth-tree(tree-node.-left)
if tree-node.-right is not None;
return depth-tree(tree-node.-right)
如图所示,爬去网站的url时, 广度优先按照这个顺序爬取 利用队列实现树的广度优先遍历
A B C D E F G H I (队列实现)
def level-queue(root);
if root is None;
return
my-queue = [ ]
node=root
my-queue.append(node)
while my-queue:
node = my-queue.pop(0)
print(node.elem)
if node.lchild is not none:
my-queue.append(node.lchild)
if node.rchild is not none:
my-queue.append(node.rchild)