As the question say, how may I create a server- client application using DatagramPacket and DatagramSocket ?
See the following solution:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class DFSTree {
public static ArrayList<Integer> listeIDsOrder = new ArrayList<>();
public static void main (String[] args){
Map<Integer, Integer> map = new HashMap<>();
map.put (1, null); //root
map.put (10, 1);
map.put (11, 10);
map.put (12, 10);
map.put (13, 10);
map.put (20, 1);
map.put (21, 20);
map.put (22, 20);
map.put (30, 1);
map.put (31, 30);
getOrder (1, map);
System.out.println (listeIDsOrder);
}
public static void getOrder (int root, Map<Integer, Integer> graph){
LinkedList<Integer> stack = new LinkedList<>(); //use stack for correct order of processing
stack.add(root); //add root
getOrder(graph, stack);
}
public static void getOrder (Map<Integer, Integer> graph, LinkedList<Integer> stack ){
while(!stack.isEmpty()){
int node = stack.pollFirst(); //for bfs order. for dfs order use stack.pollLast();
listeIDsOrder.add(node);
List<Integer> keys = new ArrayList<>(graph.keySet());
Collections.sort(keys);
for(int key : keys){
if(graph.get(key) != null && graph.get(key) == node) {
stack.add(key);
}
}
}
}
}
Output: [1, 10, 20, 30, 11, 12, 13, 21, 22, 31]