notes38 - Breadth-first traversal Look at the siblings...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Breadth-first 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 (breadth-first-search tree) (bfs-iter (list tree))) (define (bfs-iter queue) (if (null? queue) ’done (let ((task (car queue))) (print (datum task)) (bfs-iter (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.

Ask a homework question - tutors are online