public void methodsOfPQ() {
    PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); // Default Constructor:
    //		Queue<Integer> pq = new PriorityQueue<Integer>();
    pq.add(10);
    pq.add(30);
    pq.add(50);
    pq.add(40);
    pq.add(20);
    System.out.println(pq); // [10, 20, 50, 40, 30]

    pq.offer(75); // add element randomly anywhere
    System.out.println(pq); // [10, 20, 50, 40, 30, 75]

    System.out.println(pq.peek()); // 10 | return head of queue and don't touch pq
    System.out.println(pq); // [10, 20, 50, 40, 30, 75]

    System.out.println(pq.poll()); // 10 | return head and also removes head from pq
    System.out.println(pq); // [20, 50, 40, 30, 75]

    System.out.println(pq.element()); // 20 | same as peek but throw exception if pq is empty
    System.out.println(pq); // [20, 50, 40, 30, 75]

    pq.remove(); // 20 | same as poll but throw exception when queue is empty
    System.out.println(pq); // [50, 40, 30, 75]

    List<Integer> ls = new ArrayList<Integer>();
    ls.add(1000);
    ls.add(2000);

    pq.addAll(ls);
    System.out.println(pq); // [30, 40, 50, 75, 1000, 2000]

    pq.removeAll(ls);
    System.out.println(pq); // [30, 40, 50, 75]

    ls.clear(); // empty ls
    ls.add(101);
    ls.add(102);
    pq.addAll(ls);
    pq.retainAll(ls); // all element will be removed except ls
    System.out.println(pq); // [101, 102]

    System.out.println(pq.containsAll(ls)); // true
    System.out.println("********************************************");

    //		Queue<String> q=new PriorityQueue<String>();
  }
Esempio n. 2
0
  /**
   * Recursively search for the k nearest neighbors to target.
   *
   * @param target target point
   * @param maxDistance maximum distance
   * @param k number of neighbors to find
   */
  private PriorityQueue<HeapItem> search(final double[] target, double maxDistance, final int k) {
    PriorityQueue<HeapItem> heap = new PriorityQueue<HeapItem>();
    if (root == null) {
      return heap;
    }

    double tau = maxDistance;
    final FastQueue<Node> nodes = new FastQueue<Node>(20, Node.class, false);
    nodes.add(root);

    while (nodes.size() > 0) {
      final Node node = nodes.getTail();
      nodes.removeTail();
      final double dist = distance(items[node.index], target);

      if (dist <= tau) {
        if (heap.size() == k) {
          heap.poll();
        }
        heap.add(new HeapItem(node.index, dist));
        if (heap.size() == k) {
          tau = heap.element().dist;
        }
      }

      if (node.left != null && dist - tau <= node.threshold) {
        nodes.add(node.left);
      }

      if (node.right != null && dist + tau >= node.threshold) {
        nodes.add(node.right);
      }
    }

    return heap;
  }
Esempio n. 3
0
 @Override
 public T element() {
   pruneToDuration();
   return queue.element().getWrapped();
 }