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(); }
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; }
// 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); }
// 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); } } }
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); }