0

Here's the question:

a Two-dimensional array relation[n][2] represent the relation between the nodes, for exemple relation[0] equals {2,4}, so there is an adjancency relation between node 2 and node 4, and contains no cyclic relation.

I wanna save the tree structure in a hashmap, so I tried to write my code like below:

Map<Integer, LinkedList<Integer>> graph = new HashMap<Integer, LinkedList<Integer>>();
    for (int i = 0; i < n; i++) {
        int A = relation[i][0];
        int B = relation[i][1];
        if (graph.get(A) == null) {
            List<Integer> tempList = new LinkedList();
            tempList.add(B);
            graph.put(A, tempList);
        } else {
            graph.get(A).add(B);
        }
        if (graph.get(B) == null) {
            List<Integer> tempList = new LinkedList();
            tempList.add(A);
            graph.put(B, tempList);
        } else {
            graph.get(B).add(A);
        }
    }

appearently it doesn't work, but I dont know how to fix it, can somebody help me pls? thanks!

Derlin
  • 9,572
  • 2
  • 32
  • 53
Kimi
  • 3
  • 1
  • 2
  • Can you give an example of an input and expected and actual output? – Jure Kolenko Apr 11 '17 at 12:47
  • 1
    Technically, you could just do `graph.put(0, tree);` and it'd be stored in a HashMap. Although I'm certain that's not what you want, haha. – byxor Apr 11 '17 at 12:48
  • Can you explain the use-case? Maybe there is a better solution than storing a tree in a hash-map. For example maybe you need a tree structure to be used as it is but at the same time the nodes have some IDs which needs to be kept in another structure (eg: HashMap) for rapid retrieval of nodes. – andreim Apr 11 '17 at 13:11

2 Answers2

1

The code code works (I tested) except that there is a small typing error.

Map<Integer, LinkedList<Integer>>

Declared as-is, the values in your map are supposed to be some LinkedList.

But here, you put some List :

List<Integer> tempList = //[...];
[...]
//Compiler will complain that tempList is a List but a LinkedList is expected.
graph.put(A, tempList);

So either create some LinkedList like this :

LinkedList<Integer> tempList = new LinkedList<>();

or declare that your Map takes somes List as values :

Map<Integer, List<Integer>>

Note : from Java 8, you can use Map.computeIfAbsent like this :

Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
for (int i = 0; i < n; i++) {
  int A = relation[i][0];
  int B = relation[i][1];
  graph.computeIfAbsent(A, k-> new LinkedList<>()).add(B);
  graph.computeIfAbsent(B, k-> new LinkedList<>()).add(A);
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

Try it this way. Handles even cases when the array does define edges multiple times (which could be possible):

//          1
//        / | \
//       2  3  4
//      /  / \  \
//     5  6   7  8
//
public static void main(String[] args) {
    int[][] tree = new int[][] {
            {1, 2}, {1, 3}, {1, 4},
        {2, 5}, {3, 6}, {3, 7}, {4, 8} };

    Map<Integer, List<Integer>> map = new HashMap<>();
    for (int[] node : tree) {
        addEdge(map, node[0], node[1]);
        addEdge(map, node[1], node[0]);
    }

    System.out.println(map);
}

private static void addEdge(Map<Integer, List<Integer>> map, int key, int value) {
    List<Integer> edges = map.computeIfAbsent(key, k -> new LinkedList<>());
    if (! edges.contains(value)) edges.add(value);
}

Output

{1=[2, 3, 4], 2=[1, 5], 3=[1, 6, 7], 4=[1, 8], 5=[2], 6=[3], 7=[3], 8=[4]}
Harmlezz
  • 7,972
  • 27
  • 35