/** The comparator that should be used to order the elements in this heap */
  public static void main(String[] args) throws IOException {

    BinaryHeap<Integer> heeep = new BinaryHeap<Integer>();
    System.out.println(heeep.toString());

    java.util.List<Integer> stuff =
        Arrays.asList(
            15, 51, 93, 27, 68, 72, 70, 1, 86, 43, 99, null, 19, 96, 52, 73, 4, 44, 65, 64, 14, 66,
            25, 92, 90, 71, 69, 77, 67, 45);
    // null = 102400

    for (Integer in : stuff) {
      //
      //
      //  System.in.read();
      System.out.print(in + ": ");
      heeep.add(in);
      //	System.in.read();

    }

    while (!heeep.isEmpty()) {
      // System.in.read();
      System.out.print(heeep.size - 1 + ": ");
      heeep.remove();
      // System.in.read();
      stuff = stuff;
    }
  }
Exemple #2
0
  // Test program
  public static void main(String[] args) {
    int numItems = 10000;
    BinaryHeap<Integer> h = new BinaryHeap<>();
    int i = 37;

    for (i = 37; i != 0; i = (i + 37) % numItems) h.insert(i);
    for (i = 1; i < numItems; i++) if (h.deleteMin() != i) System.out.println("Oops! " + i);
  }
    public static int[] sort(int[] arr) {
      int[] temp = new int[arr.length];
      BinaryHeap heap = new BinaryHeap();
      for (int i : arr) {
        heap.add(i);
      }

      for (int i = 0; i < arr.length; i++) {
        temp[arr.length - i - 1] = heap.swapLowestWithRootAndRemove();
      }

      return temp;
    }
 public static void main(String[] args) {
   BinaryHeap bh = new BinaryHeap();
   Scanner sc = new Scanner(System.in);
   while (true) {
     System.out.print("Your input : ");
     Object a = sc.nextLine();
     if (a.equals("-")) {
       bh.deQueue();
     } else {
       bh.enQueue(Integer.parseInt((String) a));
     }
     System.out.println(bh);
   }
 }
  void UpdateDilation(String womanName, int increaseDilation) { // UPDATE ITEM

    Woman mother = new Woman();
    mother.name = womanName;
    mother.dilation = increaseDilation;
    bh.Update(mother);
  }
 @Test
 public void testAddElement() {
   int[] arr_expected = new int[] {0, 3, 4, 5, 6, 7, 8};
   BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
   heap.add(0);
   heap.add(5);
   heap.add(7);
   heap.add(3);
   heap.add(6);
   heap.add(4);
   heap.add(8);
   Integer[] arr_actual = heap.toArray(new Integer[0]);
   assertTrue(isSameArray(arr_actual, arr_expected));
 }
  // method to construct the minimum spanning tree and put all chosen edges into an ArrayList
  public ArrayList<Edge> findPath() {
    // each single vertex in a set
    DisjSets ds = new DisjSets(numV);

    ArrayList<Edge> mst = new ArrayList<Edge>();

    while (mst.size() != numV - 1) {
      // find the smallest remaining edge
      Edge min = bh.deleteMin();

      int p1 = ds.find(vertices.indexOf(min.u));
      int p2 = ds.find(vertices.indexOf(min.v));

      if (p1 != p2) {
        mst.add(min);
        ds.union(p1, p2);
      }
    }
    return mst;
  }
 public TreeNode findClosest(TreeNode root, Object target) throws UnderflowException {
   root = this.root;
   BinaryHeap heap = new BinaryHeap();
   heap.insert(root);
   TreeNode q = new TreeNode();
   q = (TreeNode) heap.findMin();
   while (!heap.isEmpty()) {
     while (q != null && q.getElement() != target) {
       q.getLeft().distance += q.distance;
       q.getRight().distance += q.distance;
       heap.insert(q.getLeft());
       heap.insert(q.getRight());
     }
   }
   return q;
 }
  // aFile the file containing coordinates of all cities
  public Kruskal(File aFile) throws FileNotFoundException {

    Scanner in = new Scanner(aFile);

    // put all vertices in the ArrayList
    numV = 0;
    x_max = 0;
    y_max = 0;
    while (in.hasNextLine()) {
      String aLine = in.nextLine();
      String[] info = aLine.split("\\s+");
      String name = info[0];

      int xaxis = Integer.parseInt(info[1]);
      int yaxis = Integer.parseInt(info[2]);
      Vertex v = new Vertex(name, xaxis, yaxis);

      if (xaxis > x_max) {
        x_max = xaxis;
      }
      if (yaxis > y_max) {
        y_max = yaxis;
      }
      vertices.add(v);
      numV++;
    }
    // put all edges to the BinaryHeap
    bh = new BinaryHeap<Edge>(numV * numV / 2 - numV / 2);
    for (int j = 0; j < vertices.size() - 1; j++) {
      for (int k = j + 1; k < (vertices.size()); k++) {
        Vertex u = vertices.get(j), v = vertices.get(k);
        double weight = Math.sqrt(Math.pow(u.x - v.x, 2) + Math.pow(u.y - v.y, 2));
        Edge e = new Edge(u, v, weight);
        bh.insert(e);
      }
    }
  }
 @Test
 public void testPollElement() {
   BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
   heap.add(0);
   heap.add(5);
   heap.add(7);
   heap.add(3);
   heap.add(6);
   heap.add(4);
   heap.add(8);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {0, 3, 4, 5, 6, 7, 8}));
   assertTrue(heap.poll() == 0);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {3, 5, 4, 8, 6, 7}));
   assertTrue(heap.poll() == 3);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {4, 5, 7, 8, 6}));
   assertTrue(heap.poll() == 4);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {5, 6, 7, 8}));
   assertTrue(heap.poll() == 5);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {6, 8, 7}));
   assertTrue(heap.poll() == 6);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {7, 8}));
   assertTrue(heap.poll() == 7);
   assertTrue(isSameArray(heap.toArray(new Integer[0]), new int[] {8}));
 }
Exemple #11
0
  public void run() {

    if (!star) {
      System.out.println(
          "Run PCC de "
              + zoneOrigine
              + ":"
              + origine
              + " vers "
              + zoneDestination
              + ":"
              + destination);
    }
    if (star) {
      System.out.println(
          "Run PCC-Star de "
              + zoneOrigine
              + ":"
              + origine
              + " vers "
              + zoneDestination
              + ":"
              + destination);
    }

    // decision temps ou distance. Si on est en mode covoiturage, ce n'est pas nécessaire car on est
    // forcément en temps
    if (covoit == 0) {
      Scanner sc = new Scanner(System.in);
      System.out.println("En temps(0) ou distance(1)?");
      if (sc.nextInt() == 1) {
        this.setdist(true);
      }
    }
    // chronometrage
    long start_time = System.currentTimeMillis();

    Label l = new Label(false, 0.0, null, this.graphe.sommets.get(origine));
    chem.add(l);
    tas.insert(l);
    maplabel.put(this.graphe.sommets.get(origine), l);

    // affichage de l'origine et de la destination
    this.graphe.getDessin().setColor(Color.BLUE);
    this.graphe
        .getDessin()
        .drawPoint(
            this.graphe.sommets.get(origine).getlon(),
            this.graphe.sommets.get(origine).getlat(),
            10);
    this.graphe
        .getDessin()
        .drawPoint(
            this.graphe.sommets.get(destination).getlon(),
            this.graphe.sommets.get(destination).getlat(),
            10);

    // compteur d'elements explores
    int nb_explor = 0;
    int tas_max = 0;

    while (!(tas.isEmpty())) {
      // extraction du minimum
      Label pere = tas.findMin();
      tas.deleteMin();

      // on arrete le programme une fois la destination trouvee, sauf si l'on ne souhaite pas
      // arreter ---> covoiturage
      if (pere.getCourant().getNum() == destination && (covoit == 0)) {
        System.out.println("Algorithme termine!");
        break;
      }

      pere.setMarquage(true);
      chem.add(pere);

      for (Route route : pere.getCourant().routes) {
        Sommet suiv = route.getArrivee();
        // affichage du parcours en temps reel. On ne le fait pas dans l'optimisation de covoiturage
        // car cela surcharge trop le dessin.
        if (this.getcovoit() == 0) {
          this.graphe.getDessin().setColor(Color.gray);
          this.graphe
              .getDessin()
              .drawLine(
                  suiv.getlon(),
                  suiv.getlat(),
                  pere.getCourant().getlon(),
                  pere.getCourant().getlat());
        }
        // declarations des variables de cout, cout secondaire et estimation
        double new_cout = 0.0;

        double estim = 0.0; // ESTIMATION VERY IMPORTANT

        double cout_sec = 0.0;

        if (this.dist_temps) // Cout n distance
        {
          double vitesse = ((double) (route.getDesc().vitesseMax()));
          new_cout = pere.getCout() + ((double) (route.getDist()));

          // cout secondaire, ici en temps
          cout_sec = pere.getCoutSec() + (60.0 * ((double) (route.getDist())) / (1000.0 * vitesse));
          if (this.star) {
            estim = suiv.dist_vol_oiseau(this.graphe.sommets.get(destination));
          }
        } else {
          // Cout en temps
          double vitesse = ((double) (route.getDesc().vitesseMax()));
          // cas du pieton
          if (this.getcovoit() == 1 && (vitesse == 130.0 || vitesse == 110.0)) {

            // ici, le pieton ne peut pas acceder a cette route,
            // elle est reservee aux voitures, le cout est donc le plus grand possible
            new_cout = Double.MAX_VALUE;
          } else {
            if (this.getcovoit() == 1) {
              vitesse = 4.0;
            }
            new_cout = pere.getCout() + (60.0 * ((double) (route.getDist())) / (1000.0 * vitesse));

            // cout secondaire, ici en distance
            cout_sec = pere.getCoutSec() + ((double) (route.getDist()));
          }

          // Ici on modifie le calcul de l'estimation dans le cas d'un calcul Astar.  VERY IMPORTANT
          if (this.star) {
            estim =
                suiv.cout_vol_oiseau(this.graphe.sommets.get(destination), this.graphe.getvitmax());
          }
        }

        // on regarde si le sommet a deja un label associe avec un cout different de l'infini)
        // pour ce faire, on verifie juste qu'il est integre a la hashmap ou pas
        if (maplabel.containsKey(suiv)) {
          maplabel.get(suiv).setEstim(estim);

          // maj du label, et de la hashmap
          // on ne verifie pas si le sommet est marque : en effet, s'il l'est, son cout
          // est deja minimal et la condition suivante sera toujours fausse.
          if (maplabel.get(suiv).getCout() > new_cout) {
            maplabel.get(suiv).setCout(new_cout);
            maplabel.get(suiv).setPere(pere.getCourant());
            maplabel.get(suiv).setCoutSec(cout_sec);
            tas.update(maplabel.get(suiv));
          }
        } else {
          // le sommet a un cout infini : on cree un label, et on l'integre a la hashmap
          Label lab = new Label(false, new_cout, pere.getCourant(), suiv);
          lab.setEstim(estim);
          lab.setCoutSec(cout_sec);
          tas.insert(lab);
          maplabel.put(suiv, lab);

          // compteurs de performance
          nb_explor++;
          if (tas.size() > tas_max) {
            tas_max = tas.size();
          }
        }
      }
    }
    // on affiche le chemin
    this.print_chemin(this.graphe.sommets.get(destination));

    // fin du chronometre
    long end_time = System.currentTimeMillis();
    long difference = end_time - start_time;

    // on affiche les performances
    System.out.println("Temps écoulé en millisecondes : " + difference);
    System.out.println("Elements explorés : " + nb_explor);
    System.out.println("Taille max du tas : " + tas_max);
  }
  public static void main(String[] args) {
    Integer[] array = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};

    BinaryHeap heap = new BinaryHeap(100);
    for (int i : array) {
      heap.insert(i);
    }

    System.out.println(heap.toString());
    int heapSize = heap.size();
    for (int i = 0; i < heapSize; i++) {
      System.out.print(heap.extractMax() + " ");
    }
    System.out.println();

    for (int i : array) {
      heap.insert(i);
    }
    heap.insert(15);
    heap.insert(100);
    heap.insert(17);
    heap.insert(42);

    heapSize = heap.size();
    for (int i = 0; i < heapSize; i++) {
      System.out.print(heap.extractMax() + " ");
    }
    System.out.println();
  }
  String Query() { // RETURN HEAD OF QUEUE
    String ans;
    ans = bh.Query();

    return ans;
  }
 void GiveBirth(String womanName) { // DELETE ITEM
   // bh.ExtractMax(); // for Subset B
   bh.Delete(womanName);
 }
 void ArriveAtHospital(String womanName, int dilation) { // INSERT ITEM
   Woman mother = new Woman();
   mother.name = womanName;
   mother.dilation = dilation;
   bh.Insert(mother);
 }