b First Bin Tree Trav Alg
-
Upload
joe-manley -
Category
Documents
-
view
19 -
download
2
description
Transcript of b First Bin Tree Trav Alg
Alyce BradyAlyce BradyCS 470: Data StructuresCS 470: Data Structures
CS 510: Computer Algorithms CS 510: Computer Algorithms
Breadth-FirstBinary Tree
Traversal Algorithm
Reminder:Breadth-First Traversal
A
B C
D E F G
A B C D E F G
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
Breadth-First Search
A
B C
D E F G
A B C D E F G
Queue:
Current:
Breadth-First Search
A
B C
D E F G
Queue:
Current:
A
Breadth-First Search
A
B C
D E F G
Queue:
Current:
A
A
Breadth-First Search
A
B C
D E F G
Queue:
Current:A
A
Breadth-First Search
A
B C
D E F G
Queue:
Current:
B
A
A
Breadth-First Search
A
B C
D E F G
Queue:
Current:
CB
A
A
Breadth-First Search
A
B C
D E F G
Queue:
Current:B
A
CB
Breadth-First Search
A
B C
D E F G
Queue:
Current:B
C
A B
Breadth-First Search
A
B C
D E F G
Queue:
Current:
DC
B
A B
Breadth-First Search
A
B C
D E F G
Queue:
Current:
EDC
B
A B
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
EDC
A B
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
A B C
ED
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
A B C
FED
Breadth-First Search
A
B C
D E F G
Queue:
Current:C
A B C
GFED
Breadth-First Search
A
B C
D E F G
Queue:
Current:D
A B C
GFED
A B C D
Breadth-First Search
A
B C
D E F G
Queue:
Current:D
GFE
Breadth-First Search
A
B C
D E F G
A B C D
Queue:
Current:E
GFE
Breadth-First Search
A
B C
D E F G
Queue:
Current:E
GF
A B C D E
Breadth-First Search
A
B C
D E F G
Queue:
Current:F
GF
A B C D E
Breadth-First Search
A
B C
D E F G
Queue:
Current:F
G
A B C D E F
Breadth-First Search
A
B C
D E F G
Queue:
Current:G
G
A B C D E F
Breadth-First Search
A
B C
D E F G
Queue:
Current:G
A B C D E F G
Breadth-First Search
A
B C
D E F G
A B C D E F G
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
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.