Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Doha2012 committed Aug 2, 2019
1 parent 5fabe70 commit b964252
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
20 changes: 11 additions & 9 deletions data-structures/src/main/java/com/baeldung/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
Expand Down Expand Up @@ -29,7 +30,7 @@ public void dfsWithoutRecursion(int start) {
while (!stack.isEmpty()) {
int current = stack.pop();
isVisited[current] = true;
System.out.print(" " + current);
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
stack.push(dest);
Expand All @@ -44,29 +45,30 @@ public void dfs(int start) {

private void dfsRecursive(int current, boolean[] isVisited) {
isVisited[current] = true;
System.out.print(" " + current);
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
dfsRecursive(dest, isVisited);
}
}

public void topologicalSort(int start) {
Stack<Integer> result = new Stack<Integer>();
public List<Integer> topologicalSort(int start) {
LinkedList<Integer> result = new LinkedList<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()];
topologicalSortRecursive(start, isVisited, result);
while (!result.isEmpty()) {
System.out.print(" " + result.pop());
}
return result;
}

private void topologicalSortRecursive(int current, boolean[] isVisited, Stack<Integer> result) {
private void topologicalSortRecursive(int current, boolean[] isVisited, LinkedList<Integer> result) {
isVisited[current] = true;
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
topologicalSortRecursive(dest, isVisited, result);
}
result.push(current);
result.addFirst(current);
}

private void visit(int value) {
System.out.print(" " + value);
}
}
15 changes: 9 additions & 6 deletions data-structures/src/main/java/com/baeldung/tree/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ private int findSmallestValue(Node root) {
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(" " + node.value);
visit(node.value);
traverseInOrder(node.right);
}
}

public void traversePreOrder(Node node) {
if (node != null) {
System.out.print(" " + node.value);
visit(node.value);
traversePreOrder(node.left);
traversePreOrder(node.right);
}
Expand All @@ -120,7 +120,7 @@ public void traversePostOrder(Node node) {
if (node != null) {
traversePostOrder(node.left);
traversePostOrder(node.right);
System.out.print(" " + node.value);
visit(node.value);
}
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public void traverseInOrderWithoutRecursion() {
stack.push(current);
}
current = stack.pop();
System.out.print(" " + current.value);
visit(current.value);
if(current.right != null) {
current = current.right;
stack.push(current);
Expand All @@ -173,7 +173,7 @@ public void traversePreOrderWithoutRecursion() {
stack.push(root);
while(! stack.isEmpty()) {
current = stack.pop();
System.out.print(" " + current.value);
visit(current.value);

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

if (!hasChild || isPrevLastChild) {
current = stack.pop();
System.out.print(" " + current.value);
visit(current.value);
prev = current;
} else {
if (current.right != null) {
Expand All @@ -209,6 +209,9 @@ public void traversePostOrderWithoutRecursion() {
}
}

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

class Node {
int value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.baeldung.graph;

import java.util.List;

import org.junit.Test;

public class GraphUnitTest {
Expand All @@ -15,7 +17,8 @@ public void givenDirectedGraph_whenDFS_thenPrintAllValues() {
@Test
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
Graph graph = createDirectedGraph();
graph.topologicalSort(0);
List<Integer> list = graph.topologicalSort(0);
System.out.println(list);
}

private Graph createDirectedGraph() {
Expand Down

0 comments on commit b964252

Please sign in to comment.