BFS and Queue

BFS (Bridth First Search) can be implemented by a queue.

BFS and Queue

Procedure is like this: (Q is Queue)

1, Put 1 in Q : ->1 (front)

2, Read the front of Q (which is 1) and put its children in Q: ->4->3->2

3, Repeat 2

....

Also, DFS (Depth First Search) can be implemented by a stack.

BFS and Queue

Procedure is like this: (S is Stack)

1, Put 1 in S: 1<- (bottom)

2, Read the top of S which is 1 and put its children into S: 2<-7<-8

3, Repeat 2

....

Think about it!

上一篇:POST模拟百度登录和自动发帖


下一篇:jquery 网页局部打印总结