28
Alyce Brady Alyce Brady CS 470: Data Structures CS 470: Data Structures CS 510: Computer CS 510: Computer Algorithms Algorithms Breadth-First Binary Tree Traversal Algorithm

Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Breadth-First Binary Tree Traversal Algorithm

Embed Size (px)

Citation preview

Alyce BradyAlyce Brady

CS 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 GA 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 GA 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

AA

Breadth-First Search

A

B C

D E F G

Queue:

Current:

B

A

AA

Breadth-First Search

A

B C

D E F G

Queue:

Current:

CB

A

AA

Breadth-First Search

A

B C

D E F G

Queue:

Current:B

AA

CB

Breadth-First Search

A

B C

D E F G

Queue:

Current:B

C

A BA B

Breadth-First Search

A

B C

D E F G

Queue:

Current:

DC

B

A BA B

Breadth-First Search

A

B C

D E F G

Queue:

Current:

EDC

B

A BA B

Breadth-First Search

A

B C

D E F G

Queue:

Current:C

EDC

A BA B

Breadth-First Search

A

B C

D E F G

Queue:

Current:C

A B CA B C

ED

Breadth-First Search

A

B C

D E F G

Queue:

Current:C

A B CA B C

FED

Breadth-First Search

A

B C

D E F G

Queue:

Current:C

A B CA B C

GFED

Breadth-First Search

A

B C

D E F G

Queue:

Current:D

A B CA B C

GFED

A B C DA 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 DA 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 EA B C D E

Breadth-First Search

A

B C

D E F G

Queue:

Current:F

GF

A B C D EA B C D E

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

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

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

Breadth-First Search

A

B C

D E F G

A B C D E F GA 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.