Skip to content

Commit b964252

Browse files
committed
minor fix
1 parent 5fabe70 commit b964252

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

data-structures/src/main/java/com/baeldung/graph/Graph.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5+
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.Map;
78
import java.util.Stack;
@@ -29,7 +30,7 @@ public void dfsWithoutRecursion(int start) {
2930
while (!stack.isEmpty()) {
3031
int current = stack.pop();
3132
isVisited[current] = true;
32-
System.out.print(" " + current);
33+
visit(current);
3334
for (int dest : adjVertices.get(current)) {
3435
if (!isVisited[dest])
3536
stack.push(dest);
@@ -44,29 +45,30 @@ public void dfs(int start) {
4445

4546
private void dfsRecursive(int current, boolean[] isVisited) {
4647
isVisited[current] = true;
47-
System.out.print(" " + current);
48+
visit(current);
4849
for (int dest : adjVertices.get(current)) {
4950
if (!isVisited[dest])
5051
dfsRecursive(dest, isVisited);
5152
}
5253
}
5354

54-
public void topologicalSort(int start) {
55-
Stack<Integer> result = new Stack<Integer>();
55+
public List<Integer> topologicalSort(int start) {
56+
LinkedList<Integer> result = new LinkedList<Integer>();
5657
boolean[] isVisited = new boolean[adjVertices.size()];
5758
topologicalSortRecursive(start, isVisited, result);
58-
while (!result.isEmpty()) {
59-
System.out.print(" " + result.pop());
60-
}
59+
return result;
6160
}
6261

63-
private void topologicalSortRecursive(int current, boolean[] isVisited, Stack<Integer> result) {
62+
private void topologicalSortRecursive(int current, boolean[] isVisited, LinkedList<Integer> result) {
6463
isVisited[current] = true;
6564
for (int dest : adjVertices.get(current)) {
6665
if (!isVisited[dest])
6766
topologicalSortRecursive(dest, isVisited, result);
6867
}
69-
result.push(current);
68+
result.addFirst(current);
7069
}
7170

71+
private void visit(int value) {
72+
System.out.print(" " + value);
73+
}
7274
}

data-structures/src/main/java/com/baeldung/tree/BinaryTree.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ private int findSmallestValue(Node root) {
103103
public void traverseInOrder(Node node) {
104104
if (node != null) {
105105
traverseInOrder(node.left);
106-
System.out.print(" " + node.value);
106+
visit(node.value);
107107
traverseInOrder(node.right);
108108
}
109109
}
110110

111111
public void traversePreOrder(Node node) {
112112
if (node != null) {
113-
System.out.print(" " + node.value);
113+
visit(node.value);
114114
traversePreOrder(node.left);
115115
traversePreOrder(node.right);
116116
}
@@ -120,7 +120,7 @@ public void traversePostOrder(Node node) {
120120
if (node != null) {
121121
traversePostOrder(node.left);
122122
traversePostOrder(node.right);
123-
System.out.print(" " + node.value);
123+
visit(node.value);
124124
}
125125
}
126126

@@ -159,7 +159,7 @@ public void traverseInOrderWithoutRecursion() {
159159
stack.push(current);
160160
}
161161
current = stack.pop();
162-
System.out.print(" " + current.value);
162+
visit(current.value);
163163
if(current.right != null) {
164164
current = current.right;
165165
stack.push(current);
@@ -173,7 +173,7 @@ public void traversePreOrderWithoutRecursion() {
173173
stack.push(root);
174174
while(! stack.isEmpty()) {
175175
current = stack.pop();
176-
System.out.print(" " + current.value);
176+
visit(current.value);
177177

178178
if(current.right != null)
179179
stack.push(current.right);
@@ -196,7 +196,7 @@ public void traversePostOrderWithoutRecursion() {
196196

197197
if (!hasChild || isPrevLastChild) {
198198
current = stack.pop();
199-
System.out.print(" " + current.value);
199+
visit(current.value);
200200
prev = current;
201201
} else {
202202
if (current.right != null) {
@@ -209,6 +209,9 @@ public void traversePostOrderWithoutRecursion() {
209209
}
210210
}
211211

212+
private void visit(int value) {
213+
System.out.print(" " + value);
214+
}
212215

213216
class Node {
214217
int value;

data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.graph;
22

3+
import java.util.List;
4+
35
import org.junit.Test;
46

57
public class GraphUnitTest {
@@ -15,7 +17,8 @@ public void givenDirectedGraph_whenDFS_thenPrintAllValues() {
1517
@Test
1618
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
1719
Graph graph = createDirectedGraph();
18-
graph.topologicalSort(0);
20+
List<Integer> list = graph.topologicalSort(0);
21+
System.out.println(list);
1922
}
2023

2124
private Graph createDirectedGraph() {

0 commit comments

Comments
 (0)