depth first search
This commit is contained in:
parent
ba9d52a1ec
commit
5b5733239c
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Graph {
|
||||
|
||||
private List<List<Integer>> neighbours;
|
||||
private int n;
|
||||
|
||||
public Graph(int n) {
|
||||
this.n = n;
|
||||
this.neighbours = new ArrayList<List<Integer>>(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
this.neighbours.add(new ArrayList<Integer>());
|
||||
}
|
||||
}
|
||||
|
||||
public void addEdge(int src, int dest) {
|
||||
this.neighbours.get(src)
|
||||
.add(dest);
|
||||
}
|
||||
|
||||
public void dfsWithoutRecursion(int start) {
|
||||
Stack<Integer> stack = new Stack<Integer>();
|
||||
boolean[] isVisited = new boolean[n];
|
||||
stack.push(start);
|
||||
while (!stack.isEmpty()) {
|
||||
int current = stack.pop();
|
||||
isVisited[current] = true;
|
||||
System.out.print(" " + current);
|
||||
for (int dest : neighbours.get(current)) {
|
||||
if (!isVisited[dest])
|
||||
stack.push(dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dfs(int start) {
|
||||
boolean[] isVisited = new boolean[n];
|
||||
dfsRecursive(start, isVisited);
|
||||
}
|
||||
|
||||
private void dfsRecursive(int current, boolean[] isVisited) {
|
||||
isVisited[current] = true;
|
||||
System.out.print(" " + current);
|
||||
for (int dest : neighbours.get(current)) {
|
||||
if (!isVisited[dest])
|
||||
dfsRecursive(dest, isVisited);
|
||||
}
|
||||
}
|
||||
|
||||
public void topologicalSort(int start) {
|
||||
Stack<Integer> result = new Stack<Integer>();
|
||||
boolean[] isVisited = new boolean[n];
|
||||
topologicalSortRecursive(start, isVisited, result);
|
||||
while (!result.isEmpty()) {
|
||||
System.out.print(" " + result.pop());
|
||||
}
|
||||
}
|
||||
|
||||
private void topologicalSortRecursive(int current, boolean[] isVisited, Stack<Integer> result) {
|
||||
isVisited[current] = true;
|
||||
for (int dest : neighbours.get(current)) {
|
||||
if (!isVisited[dest])
|
||||
topologicalSortRecursive(dest, isVisited, result);
|
||||
}
|
||||
result.push(current);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.baeldung.tree;
|
|||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class BinaryTree {
|
||||
|
||||
|
@ -147,6 +148,68 @@ public class BinaryTree {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void traverseInOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
while(! stack.isEmpty()) {
|
||||
while(current.left != null) {
|
||||
current = current.left;
|
||||
stack.push(current);
|
||||
}
|
||||
current = stack.pop();
|
||||
System.out.print(" " + current.value);
|
||||
if(current.right != null) {
|
||||
current = current.right;
|
||||
stack.push(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void traversePreOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
while(! stack.isEmpty()) {
|
||||
current = stack.pop();
|
||||
System.out.print(" " + current.value);
|
||||
|
||||
if(current.right != null)
|
||||
stack.push(current.right);
|
||||
|
||||
if(current.left != null)
|
||||
stack.push(current.left);
|
||||
}
|
||||
}
|
||||
|
||||
public void traversePostOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node prev = root;
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
current = stack.peek();
|
||||
boolean hasChild = (current.left != null || current.right != null);
|
||||
boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));
|
||||
|
||||
if (!hasChild || isPrevLastChild) {
|
||||
current = stack.pop();
|
||||
System.out.print(" " + current.value);
|
||||
prev = current;
|
||||
} else {
|
||||
if (current.right != null) {
|
||||
stack.push(current.right);
|
||||
}
|
||||
if (current.left != null) {
|
||||
stack.push(current.left);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Node {
|
||||
int value;
|
||||
Node left;
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GraphUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenDFS_thenPrintAllValues() {
|
||||
Graph graph = createDirectedGraph();
|
||||
graph.dfs(0);
|
||||
System.out.println();
|
||||
graph.dfsWithoutRecursion(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
|
||||
Graph graph = createDirectedGraph();
|
||||
graph.topologicalSort(0);
|
||||
}
|
||||
|
||||
private Graph createDirectedGraph() {
|
||||
Graph graph = new Graph(6);
|
||||
graph.addEdge(0, 1);
|
||||
graph.addEdge(0, 2);
|
||||
graph.addEdge(1, 3);
|
||||
graph.addEdge(2, 3);
|
||||
graph.addEdge(3, 4);
|
||||
graph.addEdge(4, 5);
|
||||
return graph;
|
||||
}
|
||||
}
|
|
@ -87,6 +87,8 @@ public class BinaryTreeUnitTest {
|
|||
BinaryTree bt = createBinaryTree();
|
||||
|
||||
bt.traverseInOrder(bt.root);
|
||||
System.out.println();
|
||||
bt.traverseInOrderWithoutRecursion();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -95,6 +97,8 @@ public class BinaryTreeUnitTest {
|
|||
BinaryTree bt = createBinaryTree();
|
||||
|
||||
bt.traversePreOrder(bt.root);
|
||||
System.out.println();
|
||||
bt.traversePreOrderWithoutRecursion();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -103,6 +107,8 @@ public class BinaryTreeUnitTest {
|
|||
BinaryTree bt = createBinaryTree();
|
||||
|
||||
bt.traversePostOrder(bt.root);
|
||||
System.out.println();
|
||||
bt.traversePostOrderWithoutRecursion();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue