Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Breadth-First Binary Tree Traversal Algorithm
Reminder: Breadth-First Traversal A BC DEFG A B C D E F G
Pseudo-Code for Breadth-First Traversal breadth-first-traversal put root node onto a queue while the queue is not empty dequeue the next node visit the node e.g., print value enqueue the left child node enqueue the right child node
Breadth-First Search A BC DEFG A B C D E F G Queue: Current:
Breadth-First Search A BC DEFG Queue: Current: A
Breadth-First Search A BC DEFG Queue: Current: A A
Breadth-First Search A BC DEFG Queue: Current: A A A
Breadth-First Search A BC DEFG Queue: Current: B A A A
Breadth-First Search A BC DEFG Queue: Current: CBCB A A A
Breadth-First Search A BC DEFG Queue: Current: B A A CBCB
Breadth-First Search A BC DEFG Queue: Current: B C A B
Breadth-First Search A BC DEFG Queue: Current: DCDC B A B
Breadth-First Search A BC DEFG Queue: Current: EDCEDC B A B
Breadth-First Search A BC DEFG Queue: Current: C EDCEDC A B
Breadth-First Search A BC DEFG Queue: Current: C A B C EDED
Breadth-First Search A BC DEFG Queue: Current: C A B C FEDFED
Breadth-First Search A BC DEFG Queue: Current: C A B C GFEDGFED
Breadth-First Search A BC DEFG Queue: Current: D A B C GFEDGFED
A B C D Breadth-First Search A BC DEFG Queue: Current: D GFEGFE
Breadth-First Search A BC DEFG A B C D Queue: Current: E GFEGFE
Breadth-First Search A BC DEFG Queue: Current: E GFGF A B C D E
Breadth-First Search A BC DEFG Queue: Current: F GFGF A B C D E
Breadth-First Search A BC DEFG Queue: Current: F G A B C D E F
Breadth-First Search A BC DEFG Queue: Current: G G A B C D E F
Breadth-First Search A BC DEFG Queue: Current: G A B C D E F G
Breadth-First Search A BC DEFG A B C D E F G
Time and Space Complexity for Breadth-First Search Alg. Time Complexity Time Complexity –Consider each node twiceO(n) when put on queue when put on queue when taken from queue when taken from queue
Space Complexity Space Complexity –Queue to handle unexplored nodes Queue length = width of lowest level(n/2) Queue length = width of lowest level(n/2) O(n) O(n) Time and Space Complexity for Breadth-First Search Alg.