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 isn’t the order in which they’re 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
This note was uploaded on 02/17/2010 for the course COMPUTER S 26275 taught by Professor Harvey,b during the Spring '10 term at Berkeley.
 Spring '10
 Harvey,B

Click to edit the document details