This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Breadthfirst traversal: Look at the siblings before the children. What we want to do is take horizontal slices of the tree. First we look at the root node, then we look at the children of the root, then the grandchildren, and so on. The program is a little more complicated because the order in which we want to visit nodes isnt the order in which theyre connected together. To solve this, we use an extra data structure, called a queue , which is just an ordered list of tasks to be carried out. Each task is a node to visit, and a node is a tree, so a list of nodes is just a forest. The iterative helper procedure takes the first task in the queue (the car), visits that node, and adds its children at the end of the queue (using append ). ;;;;; In file cs61a/lectures/2.2/search.scm (define (breadthfirstsearch tree) (bfsiter (list tree))) (define (bfsiter queue) (if (null? queue) done (let ((task (car queue))) (print (datum task)) (bfsiter (append (cdr queue) (children task))))))...
View Full
Document
 Spring '10
 Harvey,B

Click to edit the document details