Merge pull request #7471 from Doha2012/master

move code
This commit is contained in:
Loredana Crusoveanu 2019-08-03 13:48:18 +03:00 committed by GitHub
commit 255ae3b234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 31 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.graph;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
@ -29,7 +30,7 @@ public class Graph {
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
int current = stack.pop(); int current = stack.pop();
isVisited[current] = true; isVisited[current] = true;
System.out.print(" " + current); visit(current);
for (int dest : adjVertices.get(current)) { for (int dest : adjVertices.get(current)) {
if (!isVisited[dest]) if (!isVisited[dest])
stack.push(dest); stack.push(dest);
@ -44,29 +45,30 @@ public class Graph {
private void dfsRecursive(int current, boolean[] isVisited) { private void dfsRecursive(int current, boolean[] isVisited) {
isVisited[current] = true; isVisited[current] = true;
System.out.print(" " + current); visit(current);
for (int dest : adjVertices.get(current)) { for (int dest : adjVertices.get(current)) {
if (!isVisited[dest]) if (!isVisited[dest])
dfsRecursive(dest, isVisited); dfsRecursive(dest, isVisited);
} }
} }
public void topologicalSort(int start) { public List<Integer> topologicalSort(int start) {
Stack<Integer> result = new Stack<Integer>(); LinkedList<Integer> result = new LinkedList<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()]; boolean[] isVisited = new boolean[adjVertices.size()];
topologicalSortRecursive(start, isVisited, result); topologicalSortRecursive(start, isVisited, result);
while (!result.isEmpty()) { return result;
System.out.print(" " + result.pop());
}
} }
private void topologicalSortRecursive(int current, boolean[] isVisited, Stack<Integer> result) { private void topologicalSortRecursive(int current, boolean[] isVisited, LinkedList<Integer> result) {
isVisited[current] = true; isVisited[current] = true;
for (int dest : adjVertices.get(current)) { for (int dest : adjVertices.get(current)) {
if (!isVisited[dest]) if (!isVisited[dest])
topologicalSortRecursive(dest, isVisited, result); topologicalSortRecursive(dest, isVisited, result);
} }
result.push(current); result.addFirst(current);
} }
private void visit(int value) {
System.out.print(" " + value);
}
} }

View File

@ -103,14 +103,14 @@ public class BinaryTree {
public void traverseInOrder(Node node) { public void traverseInOrder(Node node) {
if (node != null) { if (node != null) {
traverseInOrder(node.left); traverseInOrder(node.left);
System.out.print(" " + node.value); visit(node.value);
traverseInOrder(node.right); traverseInOrder(node.right);
} }
} }
public void traversePreOrder(Node node) { public void traversePreOrder(Node node) {
if (node != null) { if (node != null) {
System.out.print(" " + node.value); visit(node.value);
traversePreOrder(node.left); traversePreOrder(node.left);
traversePreOrder(node.right); traversePreOrder(node.right);
} }
@ -120,7 +120,7 @@ public class BinaryTree {
if (node != null) { if (node != null) {
traversePostOrder(node.left); traversePostOrder(node.left);
traversePostOrder(node.right); traversePostOrder(node.right);
System.out.print(" " + node.value); visit(node.value);
} }
} }
@ -159,7 +159,7 @@ public class BinaryTree {
stack.push(current); stack.push(current);
} }
current = stack.pop(); current = stack.pop();
System.out.print(" " + current.value); visit(current.value);
if(current.right != null) { if(current.right != null) {
current = current.right; current = current.right;
stack.push(current); stack.push(current);
@ -173,7 +173,7 @@ public class BinaryTree {
stack.push(root); stack.push(root);
while(! stack.isEmpty()) { while(! stack.isEmpty()) {
current = stack.pop(); current = stack.pop();
System.out.print(" " + current.value); visit(current.value);
if(current.right != null) if(current.right != null)
stack.push(current.right); stack.push(current.right);
@ -196,7 +196,7 @@ public class BinaryTree {
if (!hasChild || isPrevLastChild) { if (!hasChild || isPrevLastChild) {
current = stack.pop(); current = stack.pop();
System.out.print(" " + current.value); visit(current.value);
prev = current; prev = current;
} else { } else {
if (current.right != null) { if (current.right != null) {
@ -209,6 +209,9 @@ public class BinaryTree {
} }
} }
private void visit(int value) {
System.out.print(" " + value);
}
class Node { class Node {
int value; int value;

View File

@ -1,5 +1,7 @@
package com.baeldung.graph; package com.baeldung.graph;
import java.util.List;
import org.junit.Test; import org.junit.Test;
public class GraphUnitTest { public class GraphUnitTest {
@ -15,7 +17,8 @@ public class GraphUnitTest {
@Test @Test
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() { public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
Graph graph = createDirectedGraph(); Graph graph = createDirectedGraph();
graph.topologicalSort(0); List<Integer> list = graph.topologicalSort(0);
System.out.println(list);
} }
private Graph createDirectedGraph() { private Graph createDirectedGraph() {

View File

@ -1,8 +1,8 @@
package com.baeldung.mongodb.daos; package com.baeldung.mongodb.file.daos;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import com.baeldung.mongodb.models.Photo; import com.baeldung.mongodb.file.models.Photo;
public interface PhotoRepository extends MongoRepository<Photo, String> { public interface PhotoRepository extends MongoRepository<Photo, String> {

View File

@ -1,4 +1,4 @@
package com.baeldung.mongodb.models; package com.baeldung.mongodb.file.models;
import org.bson.types.Binary; import org.bson.types.Binary;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;

View File

@ -1,4 +1,4 @@
package com.baeldung.mongodb.models; package com.baeldung.mongodb.file.models;
import java.io.InputStream; import java.io.InputStream;

View File

@ -1,4 +1,4 @@
package com.baeldung.mongodb.services; package com.baeldung.mongodb.file.services;
import java.io.IOException; import java.io.IOException;
@ -8,8 +8,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.daos.PhotoRepository; import com.baeldung.mongodb.file.daos.PhotoRepository;
import com.baeldung.mongodb.models.Photo; import com.baeldung.mongodb.file.models.Photo;
@Service @Service
public class PhotoService { public class PhotoService {

View File

@ -1,4 +1,4 @@
package com.baeldung.mongodb.services; package com.baeldung.mongodb.file.services;
import java.io.IOException; import java.io.IOException;
@ -11,7 +11,7 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.models.Video; import com.baeldung.mongodb.file.models.Video;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.client.gridfs.model.GridFSFile; import com.mongodb.client.gridfs.model.GridFSFile;

View File

@ -1,4 +1,4 @@
package com.baeldung.mongodb.web; package com.baeldung.mongodb.file.web;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
@ -12,8 +12,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.models.Photo; import com.baeldung.mongodb.file.models.Photo;
import com.baeldung.mongodb.services.PhotoService; import com.baeldung.mongodb.file.services.PhotoService;
@Controller @Controller
public class PhotoController { public class PhotoController {

View File

@ -1,4 +1,4 @@
package com.baeldung.mongodb.web; package com.baeldung.mongodb.file.web;
import java.io.IOException; import java.io.IOException;
@ -14,8 +14,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.models.Video; import com.baeldung.mongodb.file.models.Video;
import com.baeldung.mongodb.services.VideoService; import com.baeldung.mongodb.file.services.VideoService;
@Controller @Controller
public class VideoController { public class VideoController {