You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An algorithm that traverses through a tree (or graph) level-by-level, starting at the root.
Breadth-First Traversal is iterative and uses a queue to keep track of unvisited nodes.
In a tree, the bottom-right node is evaluated last (i.e. the node that is deepest and is farthest right).
Binary Tree BFT
// Breadth-First Traversal of a Binary TreevoidBFTraversal(BTNoderoot) {
Queue<BTNode> queue = newLinkedList<BTNode>();
queue.add(root);
while (!queue.isEmpty()) {
BTNodenode = queue.poll(); // poll() removes the present headSystem.out.println(node.data);
// Enqueue left childif (node.left != null) queue.add(node.left);
// Enqueue right childif (node.right != null) queue.add(node.right);
}
}
Graph BFS
// Breadth-First Search of a GraphvoidBFS(Noderoot) {
Queue<Node> queue = newLinkedList<Node>();
root.visited = true;
queue.add(root);
while(!queue.isEmpty()) {
Noder = queue.poll();
visit(r);
for (inti = 0; i < root.adjacent.length; i++) {
if (root.adjacent[i].visited == false) {
root.adjacent[i].visited = true;
queue.add(root.adjacent[i]);
}
}
}
}
Time Complexity
O(|V| + |E|)
Depth-First Traversal
An algorithm that traverses through a tree (or graph) by traversing the depth of the tree first, starting at the root.
Depth-First Traversal is usually recursive and uses a stack to keep track of unvisited nodes.
In pre-order & in-order traversal, the right-most node is evaluated last (the node that is right of all its ancestors).
In post-order traversal, the root node is evaluated last.
Pre-Order Traversal
Process the current node.
Visit the left child subtree.
Visit the right child subtree.
In-Order Traversal
Visit the left child subtree.
Process the current node.
Visit the right child subtree.
Post-Order Traversal
Visit the left child subtree.
Visit the right child subtree.
Process the current node.
Binary Tree DFT (Recursive)
// Pre-Order Depth-First Traversal of a Binary TreevoidDFTraversal(BTNodenode) {
if (node == null) return;
System.out.println(node.data); // visit nodeDFTraversal(node.left); // left subtreeDFTraversal(node.right); // right subtree
}
Binary Tree DFT (Iterative)
// Iterative Pre-Order DFT of a Binary TreevoidIterativePreOrderDFT(BTNodenode) {
if (node == null) return;
// The stack will keep track of the order to visit the nodes, from top to bottom.Stack<BTNode> stack = newStack<>();
stack.push(root);
// Traverse the tree.while (!stack.isEmpty()) {
BTNodenode = stack.pop();
System.out.print(node.data + " ");
// Push right then left child to stack so left child is visited first.if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
}
// Iterative In-Order DFT of a Binary TreevoidIterativeInOrderDFT(BTNodenode) {
if (root == null) return;
// The stack will keep track of the order to visit the nodes, from top to bottom.Stack<BTNode> stack = newStack<>();
BTNodenode = root;
// Set the top of the stack to the leftmost node.while (node != null) {
stack.push(node);
node = node.left;
}
// Traverse the tree.while (!stack.isEmpty()) {
node = stack.pop();
System.out.print(node.data + " ");
if (node.right != null) {
node = node.right;
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
}
// Iterative Post-Order DFT of a Binary TreevoidIterativePostOrderDFT(BTNodenode) {
if (root == null) return;
// The stack will keep track of the order to visit the nodes, from top to bottom.Stack<BTNode> stack = newStack<>();
stack.push(root);
BTNodeprev = null;
while (!stack.isEmpty()) {
BTNodecurr = stack.peek();
// Go down the tree. If current node is leaf, process it and pop stack,// otherwise, keep going down.if (prev == null || prev.left == curr || prev.right == curr) {
if (curr.left != null) {
stack.push(curr.left);
} elseif (curr.right != null) {
stack.push(curr.right);
} else {
System.out.println(curr.data);
stack.pop();
}
// Go up the tree from the left node. If there is a right child, push it// onto stack. Else, process parent node and pop stack.
} elseif (curr.left == prev) {
if (curr.right != null) {
stack.push(curr.right);
} else {
System.out.println(curr.data);
stack.pop();
}
// Go up the tree from the right node. Process parent node and pop stack.
} elseif (curr.right == prev) {
System.out.println(curr.data);
stack.pop();
}
prev = curr;
}
}
Graph DFS
// Depth-First Search of a GraphvoidDFS(Noderoot) {
if (root == null) return;
visit(root);
root.visited = true;
for (inti = 0; i < root.adjacent.length; i++) {
if (root.adjacent[i].visited == false) DFS(root.adjacent[i]);
}
}
Time Complexity
O(|V| + |E|)
BFS vs. DFS & Other Search Algorithms
Breadth-first search is guaranteed to find a shortest possible path between two vertices in a graph. Depth-first search is not (and usually does not).
Iterative deepening depth-first search is a graph search strategy in which a depth-limited version of depth-first search is run repeatedly with increasing depth limits until the goal is found. This is equivalent to breadth-first search but uses much less memory since on each iteration, it visits the nodes in the search tree in the same order as depth-first search but the cumulative order in which nodes are visited is effectively breadth-first.
Bidirectional search is used to find the shortest path between a source and a destination node. It operates by essentially running two simultaneous breadth-first searches, one from each node. When the searches collide, the path is found.