notes38 - Breadth-rst traversal Look at the siblings before...

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

View Full Document Right Arrow Icon
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)))))) Why would we use this more complicated technique? For example, in some situations the same value might
Image of page 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern