alyce brady cs 470: data structures cs 510: computer algorithms
DESCRIPTION
Breadth-First Binary Tree Traversal Algorithm. Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms. Reminder: Breadth-First Traversal. A. B. C. D. E. F. G. A B C D E F G. breadth-first-traversal put root node onto a queue while the queue is not empty - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/1.jpg)
Alyce BradyAlyce Brady
CS 470: Data StructuresCS 470: Data Structures
CS 510: Computer Algorithms CS 510: Computer Algorithms
Breadth-FirstBinary Tree
Traversal Algorithm
![Page 2: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/2.jpg)
Reminder:Breadth-First Traversal
A
B C
D E F G
A B C D E F GA B C D E F G
![Page 3: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/3.jpg)
Pseudo-Code forBreadth-First Traversal
breadth-first-traversalbreadth-first-traversalput root node onto a queueput root node onto a queuewhile the queue is not emptywhile the queue is not empty
dequeue the next nodedequeue the next nodevisit the nodevisit the node e.g., print valuee.g., print value
enqueue the left child nodeenqueue the left child nodeenqueue the right child nodeenqueue the right child node
![Page 4: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/4.jpg)
Breadth-First Search
A
B C
D E F G
A B C D E F GA B C D E F G
Queue:
Current:
![Page 5: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/5.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:
A
![Page 6: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/6.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:
A
A
![Page 7: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/7.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:A
AA
![Page 8: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/8.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:
B
A
AA
![Page 9: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/9.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:
CB
A
AA
![Page 10: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/10.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:B
AA
CB
![Page 11: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/11.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:B
C
A BA B
![Page 12: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/12.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:
DC
B
A BA B
![Page 13: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/13.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:
EDC
B
A BA B
![Page 14: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/14.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
EDC
A BA B
![Page 15: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/15.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
A B CA B C
ED
![Page 16: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/16.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
A B CA B C
FED
![Page 17: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/17.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
A B CA B C
GFED
![Page 18: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/18.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:D
A B CA B C
GFED
![Page 19: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/19.jpg)
A B C DA B C D
Breadth-First Search
A
B C
D E F G
Queue:
Current:D
GFE
![Page 20: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/20.jpg)
Breadth-First Search
A
B C
D E F G
A B C DA B C D
Queue:
Current:E
GFE
![Page 21: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/21.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:E
GF
A B C D EA B C D E
![Page 22: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/22.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:F
GF
A B C D EA B C D E
![Page 23: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/23.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:F
G
A B C D E FA B C D E F
![Page 24: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/24.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:G
G
A B C D E FA B C D E F
![Page 25: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/25.jpg)
Breadth-First Search
A
B C
D E F G
Queue:
Current:G
A B C D E F GA B C D E F G
![Page 26: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/26.jpg)
Breadth-First Search
A
B C
D E F G
A B C D E F GA B C D E F G
![Page 27: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/27.jpg)
Time and Space Complexityfor Breadth-First Search Alg.
Time ComplexityTime Complexity– Consider each node twiceConsider each node twice O(n)O(n)
when put on queuewhen put on queue when taken from queuewhen taken from queue
![Page 28: Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081519/56813b0e550346895da3b7cc/html5/thumbnails/28.jpg)
Space ComplexitySpace Complexity– Queue to handle unexplored nodesQueue to handle unexplored nodes
Queue length = width of lowest levelQueue length = width of lowest level (n/2)(n/2) O(n)O(n)
Time and Space Complexityfor Breadth-First Search Alg.