예제 #1
0
 @Override
 public void setEdge(Node<Integer> v, Node<Integer> e) throws NodeNotFoundException {
   if (!this.adj(v, e)) {
     ArrayList<Node<Integer>> one = this.adjacencyList.get(v);
     one.add(e);
     if (e.value()
         != v.value()) { // Sonst würde bei einem "Selbstverweis" der Eintrag doppelt gesetzt
       // werden!
       ArrayList<Node<Integer>> two = this.adjacencyList.get(e);
       two.add(v);
     }
   }
 }
예제 #2
0
 @Override
 public Integer node(Node<Integer> v) throws NodeNotFoundException {
   if (this.adjacencyList.containsKey(v)) {
     return v.value(); // seehr grundlegende Implementierung, darum so einfach
   } else {
     throw new NodeNotFoundException();
   }
 }
예제 #3
0
 /** @return dfs wenn gefunden, ansonsten NULL */
 @Override
 public Graph<Integer> dfs(Node<Integer> start, Node<Integer> goal) {
   this.unvisitAllNodes();
   Graph<Integer> dfs = new ImplGraph();
   dfs.setNode(start.value());
   boolean found = this.dfs(start, goal, dfs);
   return found ? dfs : null;
 }
예제 #4
0
 /** Aufgabe b */
 @Override
 public void save(String fileName) {
   StringBuilder sb = new StringBuilder();
   if (this.adjacencyList.size() > 0) {
     Node<Integer>[] keys = this.adjacencyList.keySet().toArray(new Node[0]);
     Arrays.sort(keys);
     for (int i = 0; i < keys.length; i++) {
       Node<Integer> current = keys[i];
       ArrayList<Node<Integer>> adj = this.adjacencyList.get(current);
       for (Node<Integer> adjElement : adj) {
         if (adjElement.value() >= current.value()) {
           sb.append(current.value());
           sb.append("-");
           sb.append(adjElement.value());
           sb.append("\r\n");
         }
       }
     }
   }
   Helper.writeText(fileName, sb.toString());
 }
예제 #5
0
 public void add(String str) {
   Node n = root, t;
   char arr[] = str.toCharArray();
   for (int i = 0; i < arr.length; i++) {
     t = n.getChild(arr[i]);
     if (t == null) {
       t = n.addChild(arr[i]);
       if (i == arr.length - 1) {
         t.value = str;
       }
     }
     n = t;
   }
 }
예제 #6
0
 @Override
 public boolean adj(Node<Integer> v, Node<Integer> u) throws NodeNotFoundException {
   if (this.adjacencyList.containsKey(v) && this.adjacencyList.containsKey(u)) {
     ArrayList<Node<Integer>> nodes = this.adjacencyList.get(v);
     for (int i = 0; i < nodes.size(); i++) {
       if (nodes.get(i).value() == u.value()) {
         return true;
       }
     }
     return false;
   } else {
     throw new NodeNotFoundException();
   }
 }
예제 #7
0
 private boolean dfs(
     final Node<Integer> node, final Node<Integer> goal, Graph<Integer> accumulator) {
   try {
     this.visitNode(node);
     if (node.value() == goal.value()) {
       return true;
     }
     ArrayList<Node<Integer>> stack = this.adjacencyList.get(node);
     for (Node<Integer> next : stack) {
       if (!this.isVisited(next) && next.value() != node.value()) {
         accumulator.setNode(next.value());
         accumulator.setEdge(next, node);
         if (dfs(next, goal, accumulator)) {
           return true;
         }
       }
     }
     return false;
   } catch (NodeNotFoundException e) {
     System.out.println("darf net passiern damn!");
     return false;
   }
 }
예제 #8
0
  /** Aufgabe b */
  @Override
  public void print() {
    String adjSymbol = "#";
    String notAdjSymbol = ".";
    System.out.println("=================================");
    System.out.println("Nodes are adjetant: " + adjSymbol);
    System.out.println("Nodes are not adjetant: " + notAdjSymbol);
    System.out.println("=================================");

    Node<Integer>[] keys = this.adjacencyList.keySet().toArray(new Node[0]);
    Arrays.sort(keys);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < keys.length; i++) {
      Node<Integer> current = keys[i];
      ArrayList<Node<Integer>> adj = this.adjacencyList.get(current);
      HashSet<Integer> adjPositions = this.getAdjPos(keys, adj);
      for (int j = 0; j < keys.length; j++) {
        sb.append("\t");
        if (j == i) {
          sb.append(current.value());
          if (adjPositions.contains(j)) { // Bei "Selbstverweis"
            sb.append(adjSymbol);
          }
        } else if (j > i) {
          if (adjPositions.contains(j)) {
            sb.append(adjSymbol);
          } else {
            sb.append(notAdjSymbol);
          }
        }
      }
      sb.append("\r\n");
    }

    System.out.println(sb.toString());
  }
예제 #9
0
 public void add(int index, int value) {
   Node it = new Node();
   it.value = value;
   it.prio = rnd.nextInt();
   root = add(root, it, index);
 }
예제 #10
0
 // Push/add an item to the stack.
 public void push(T item) {
   Node temp = new Node();
   temp.value = item;
   temp.next = first;
   first = temp;
 }