John Mavrick's Garden

Search IconIcon to open search

Last updated April 10, 2022

Status: Tags: Links: Binary Tree Traversal


Breadth First Search

16-48

Using Stack

Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
PreOrder(root):
	s = create stack of nodes
	s.push(root)
	While s is not empty:
		node = s.pop()
		printf(node→value)
		if (node→right != NULL)
			s.push(node→right)
		if (node→left != NULL)
			s.push(node→left)

Traversal

On graph: Image from Gyazo Image from Gyazo

Using Queue

Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
BreadthFirstSearch(root):
	q = create queue of nodes
	q.enqueue(root)
	while q is not empty:
		node = q.dequeue()
		printf(node→value)
		if (node→left != NULL)
			q.enqueue(node→left)
		if (node→right != NULL)
			 q.enqueue(node→right)

Traversal

On graph: Image from Gyazo ? Image from Gyazo


Backlinks

1
list from Breadth First Search AND !outgoing(Breadth First Search)

References:

Created:: 2021-11-10 15:02


Interactive Graph