Esempio n. 1
0
  /**
   * Returns the keys in the BST in level order (for debugging).
   *
   * @return the keys in the BST in level order traversal
   */
  private Iterable<Point2D> levelOrder() {

    Queue<Point2D> keys = new Queue<Point2D>();
    Queue<Node> queue = new Queue<Node>();
    queue.enqueue(root);
    while (!queue.isEmpty()) {
      Node x = queue.dequeue();
      if (x == null) {
        continue;
      }
      keys.enqueue(x.key);
      queue.enqueue(x.left);
      queue.enqueue(x.right);
    }
    return keys;
  }
Esempio n. 2
0
  private ShortestPath findShortestPath(int v, int w) {
    if (!isValid(v)) {
      throw new java.lang.NullPointerException("Invalid v parameter");
    }

    if (!isValid(w)) {
      throw new java.lang.NullPointerException("Ivalid w parameter");
    }

    Queue<Integer> q = new Queue<Integer>();
    boolean[] marked = new boolean[G.V()];
    int[] distTo = new int[G.V()];

    q.enqueue(v);
    q.enqueue(w);

    marked[v] = true;
    marked[w] = true;
    distTo[v] = 0;
    distTo[w] = 0;

    while (!q.isEmpty()) {
      int a = q.dequeue();
      for (int b : G.adj(a)) {
        if (marked[b]) {
          int length = distTo[b] + distTo[a] + 1;
          ShortestPath sp = new ShortestPath();

          sp.ancestor = b;
          sp.length = length;

          return sp;
        }

        distTo[b] = distTo[a] + 1;
        marked[b] = true;
        q.enqueue(b);
      }
    }

    return null;
  }