fix: remove unnecesary throw to fix #704

- Fix #704
- Thanks @lprone
This commit is contained in:
yanglbme 2019-02-06 10:13:55 +08:00
parent bb670a2ceb
commit 8b92c3fdbe
9 changed files with 368 additions and 363 deletions

View File

@ -1,12 +1,11 @@
/**
*
*/
package heaps;
package Heaps;
/**
* @author Nicolas Renard
* Exception to be thrown if the getElement method is used on an empty heap.
*
*/
@SuppressWarnings("serial")
public class EmptyHeapException extends Exception {

View File

@ -1,4 +1,4 @@
package heaps;
package Heaps;
/**
* Interface common to heap data structures.<br>
@ -10,32 +10,31 @@ package heaps;
* max-heap).</p>
* <p>All heap-related operations (inserting or deleting an element, extracting the min or max) are performed in
* O(log n) time.</p>
*
* @author Nicolas Renard
*
*
*/
public interface Heap {
/**
*
* @return the top element in the heap, the one with lowest key for min-heap or with
* the highest key for max-heap
* @throws Exception if heap is empty
* @throws EmptyHeapException if heap is empty
*/
public abstract HeapElement getElement() throws EmptyHeapException;
HeapElement getElement() throws EmptyHeapException;
/**
* Inserts an element in the heap. Adds it to then end and toggle it until it finds its
* right position.
*
* @param element an instance of the HeapElement class.
*/
public abstract void insertElement(HeapElement element);
void insertElement(HeapElement element);
/**
* Delete an element in the heap.
*
* @param elementIndex int containing the position in the heap of the element to be deleted.
*/
public abstract void deleteElement(int elementIndex);
void deleteElement(int elementIndex);
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package heaps;
package Heaps;
import java.lang.Double;
import java.lang.Object;
@ -12,8 +12,8 @@ import java.lang.Object;
* or double, either primitive type or object) and any kind of IMMUTABLE object the user sees fit
* to carry any information he/she likes. Be aware that the use of a mutable object might
* jeopardize the integrity of this information. </p>
* @author Nicolas Renard
*
* @author Nicolas Renard
*/
public class HeapElement {
private final double key;
@ -22,7 +22,6 @@ public class HeapElement {
// Constructors
/**
*
* @param key : a number of primitive type 'double'
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
* additional information of use for the user
@ -33,7 +32,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of primitive type 'int'
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
* additional information of use for the user
@ -44,7 +42,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of object type 'Integer'
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
* additional information of use for the user
@ -55,7 +52,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of object type 'Double'
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
* additional information of use for the user
@ -66,7 +62,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of primitive type 'double'
*/
public HeapElement(double key) {
@ -75,7 +70,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of primitive type 'int'
*/
public HeapElement(int key) {
@ -84,7 +78,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of object type 'Integer'
*/
public HeapElement(Integer key) {
@ -93,7 +86,6 @@ public class HeapElement {
}
/**
*
* @param key : a number of object type 'Double'
*/
public HeapElement(Double key) {
@ -102,12 +94,14 @@ public class HeapElement {
}
// Getters
/**
* @return the object containing the additional info provided by the user.
*/
public Object getInfo() {
return additionalInfo;
}
/**
* @return the key value of the element
*/
@ -120,8 +114,8 @@ public class HeapElement {
public String toString() {
return "Key: " + key + " - " + additionalInfo.toString();
}
/**
*
* @param otherHeapElement
* @return true if the keys on both elements are identical and the additional info objects
* are identical.

View File

@ -1,4 +1,4 @@
package heaps;
package Heaps;
import java.util.ArrayList;
import java.util.List;
@ -6,15 +6,15 @@ import java.util.List;
/**
* Heap tree where a node's key is higher than or equal to its parent's and lower than or equal
* to its children's.
* @author Nicolas Renard
*
* @author Nicolas Renard
*/
public class MaxHeap implements Heap {
private final List<HeapElement> maxHeap;
public MaxHeap(List<HeapElement> listElements) throws Exception {
maxHeap = new ArrayList<HeapElement>();
public MaxHeap(List<HeapElement> listElements) {
maxHeap = new ArrayList<>();
for (HeapElement heapElement : listElements) {
if (heapElement != null) insertElement(heapElement);
else System.out.println("Null element. Not added to heap");
@ -22,9 +22,15 @@ public class MaxHeap implements Heap {
if (maxHeap.size() == 0) System.out.println("No element has been added, empty heap.");
}
// Get the element at a given index. The key for the list is equal to index value - 1
/**
* Get the element at a given index. The key for the list is equal to index value - 1
*
* @param elementIndex index
* @return heapElement
*/
public HeapElement getElement(int elementIndex) {
if ((elementIndex <= 0) && (elementIndex > maxHeap.size())) throw new IndexOutOfBoundsException("Index out of heap range");
if ((elementIndex <= 0) || (elementIndex > maxHeap.size()))
throw new IndexOutOfBoundsException("Index out of heap range");
return maxHeap.get(elementIndex - 1);
}
@ -59,8 +65,7 @@ public class MaxHeap implements Heap {
if ((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex * 2 + 1) > getElementKey(elementIndex * 2))) {
swap(elementIndex, 2 * elementIndex + 1);
elementIndex = 2 * elementIndex + 1;
}
else {
} else {
swap(elementIndex, 2 * elementIndex);
elementIndex = 2 * elementIndex;
}
@ -90,7 +95,8 @@ public class MaxHeap implements Heap {
} catch (EmptyHeapException e) {
e.printStackTrace();
}
if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
if ((elementIndex > maxHeap.size()) || (elementIndex <= 0))
throw new IndexOutOfBoundsException("Index out of heap range");
// The last element in heap replaces the one to be deleted
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
maxHeap.remove(maxHeap.size());
@ -98,7 +104,8 @@ public class MaxHeap implements Heap {
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
// ... or down ?
else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) ||
((2*elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex*2)))) toggleDown(elementIndex);
((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))))
toggleDown(elementIndex);
}
@Override
@ -109,7 +116,4 @@ public class MaxHeap implements Heap {
throw new EmptyHeapException("Heap is empty. Error retrieving element");
}
}
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package heaps;
package Heaps;
import java.util.ArrayList;
import java.util.List;
@ -9,15 +9,15 @@ import java.util.List;
/**
* Heap tree where a node's key is higher than or equal to its parent's and lower than or equal
* to its children's.
* @author Nicolas Renard
*
* @author Nicolas Renard
*/
public class MinHeap implements Heap {
private final List<HeapElement> minHeap;
public MinHeap(List<HeapElement> listElements) throws Exception {
minHeap = new ArrayList<HeapElement>();
public MinHeap(List<HeapElement> listElements) {
minHeap = new ArrayList<>();
for (HeapElement heapElement : listElements) {
if (heapElement != null) insertElement(heapElement);
else System.out.println("Null element. Not added to heap");
@ -27,7 +27,8 @@ public class MinHeap implements Heap {
// Get the element at a given index. The key for the list is equal to index value - 1
public HeapElement getElement(int elementIndex) {
if ((elementIndex <= 0) && (elementIndex > minHeap.size())) throw new IndexOutOfBoundsException("Index out of heap range");
if ((elementIndex <= 0) || (elementIndex > minHeap.size()))
throw new IndexOutOfBoundsException("Index out of heap range");
return minHeap.get(elementIndex - 1);
}
@ -62,8 +63,7 @@ public class MinHeap implements Heap {
if ((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex * 2 + 1) < getElementKey(elementIndex * 2))) {
swap(elementIndex, 2 * elementIndex + 1);
elementIndex = 2 * elementIndex + 1;
}
else {
} else {
swap(elementIndex, 2 * elementIndex);
elementIndex = 2 * elementIndex;
}
@ -93,7 +93,8 @@ public class MinHeap implements Heap {
} catch (EmptyHeapException e) {
e.printStackTrace();
}
if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
if ((elementIndex > minHeap.size()) || (elementIndex <= 0))
throw new IndexOutOfBoundsException("Index out of heap range");
// The last element in heap replaces the one to be deleted
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
minHeap.remove(minHeap.size());
@ -101,7 +102,8 @@ public class MinHeap implements Heap {
if (getElementKey(elementIndex) < getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
// ... or down ?
else if (((2 * elementIndex <= minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))) ||
((2*elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex*2)))) toggleDown(elementIndex);
((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))))
toggleDown(elementIndex);
}
@Override

View File

@ -1,4 +1,4 @@
package Heaps;
/* Minimum Priority Queue
* It is a part of heap data structure
* A heap is a specific tree based data structure

View File

@ -1,12 +1,11 @@
import java.util.Scanner;
/**
*
* @author Afrizal Fikri (https://github.com/icalF)
*
* @author Libin Yang (https://github.com/yanglbme)
*/
public class LongestIncreasingSubsequence {
public static void main(String[] args) throws Exception {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
@ -37,7 +36,9 @@ public class LongestIncreasingSubsequence {
return 0;
int[] tail = new int[N];
int length = 1; // always points empty slot in tail
// always points empty slot in tail
int length = 1;
tail[0] = array[0];
for (int i = 1; i < N; i++) {

View File

@ -1,16 +1,14 @@
/*
@author : Mayank K Jha
/**
* @author Mayank K Jha
*/
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
public class Dijkshtra {
public static void main(String[] args) throws IOException {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// n = Number of nodes or vertices
@ -19,11 +17,11 @@ public class Dijkshtra {
int m = in.nextInt();
// Adjacency Matrix
long w[][] = new long [n+1][n+1];
long[][] w = new long[n + 1][n + 1];
// Initializing Matrix with Certain Maximum Value for path b/w any two vertices
for (long[] row : w) {
Arrays.fill(row, 1000000l);
Arrays.fill(row, 1000000L);
}
/* From above,we Have assumed that,initially path b/w any two Pair of vertices is Infinite such that Infinite = 1000000l
@ -36,12 +34,13 @@ public class Dijkshtra {
// Comparing previous edge value with current value - Cycle Case
if (w[x][y] > cmp) {
w[x][y] = cmp; w[y][x] = cmp;
w[x][y] = cmp;
w[y][x] = cmp;
}
}
// Implementing Dijkshtra's Algorithm
Stack<Integer> t = new Stack<Integer>();
Stack<Integer> t = new Stack<>();
int src = in.nextInt();
for (int i = 1; i <= n; i++) {
@ -50,7 +49,7 @@ public class Dijkshtra {
}
}
Stack <Integer> p = new Stack<Integer>();
Stack<Integer> p = new Stack<>();
p.push(src);
w[src][src] = 0;
@ -71,7 +70,7 @@ public class Dijkshtra {
// Printing shortest path from the given source src
for (int i = 1; i <= n; i++) {
if(i != src && w[src][i] != 1000000l) {
if (i != src && w[src][i] != 1000000L) {
System.out.print(w[src][i] + " ");
}
// Printing -1 if there is no path b/w given pair of edges

View File

@ -5,19 +5,22 @@ package Others;
* Dijkstra's algorithm,is a graph search algorithm that solves the single-source
* shortest path problem for a graph with nonnegative edge path costs, producing
* a shortest path tree.
*
* <p>
* NOTE: The inputs to Dijkstra's algorithm are a directed and weighted graph consisting
* of 2 or more nodes, generally represented by an adjacency matrix or list, and a start node.
*
* <p>
* Original source of code: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java
* Also most of the comments are from RosettaCode.
*
*/
//import java.io.*;
import java.util.*;
public class Dijkstra {
private static final Graph.Edge[] GRAPH = {
new Graph.Edge("a", "b", 7), //Distance from node "a" to node "b" is 7. In the current Graph there is no way to move the other way (e,g, from "b" to "a"), a new edge would be needed for that
// Distance from node "a" to node "b" is 7.
// In the current Graph there is no way to move the other way (e,g, from "b" to "a"),
// a new edge would be needed for that
new Graph.Edge("a", "b", 7),
new Graph.Edge("a", "c", 9),
new Graph.Edge("a", "f", 14),
new Graph.Edge("b", "c", 10),
@ -43,12 +46,14 @@ public class Dijkstra {
}
class Graph {
private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges
// mapping of vertex names to Vertex objects, built from a set of Edges
private final Map<String, Vertex> graph;
/** One edge of the graph (only used by Graph constructor) */
public static class Edge {
public final String v1, v2;
public final int dist;
public Edge(String v1, String v2, int dist) {
this.v1 = v1;
this.v2 = v2;
@ -59,7 +64,8 @@ class Graph {
/** One vertex of the graph, complete with mappings to neighbouring vertices */
public static class Vertex implements Comparable<Vertex> {
public final String name;
public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
// MAX_VALUE assumed to be infinity
public int dist = Integer.MAX_VALUE;
public Vertex previous = null;
public final Map<Vertex, Integer> neighbours = new HashMap<>();
@ -70,11 +76,9 @@ class Graph {
private void printPath() {
if (this == this.previous) {
System.out.printf("%s", this.name);
}
else if (this.previous == null) {
} else if (this.previous == null) {
System.out.printf("%s(unreached)", this.name);
}
else {
} else {
this.previous.printPath();
System.out.printf(" -> %s(%d)", this.name, this.dist);
}
@ -87,7 +91,8 @@ class Graph {
return Integer.compare(dist, other.dist);
}
@Override public String toString() {
@Override
public String toString() {
return "(" + name + ", " + dist + ")";
}
}
@ -132,9 +137,10 @@ class Graph {
private void dijkstra(final NavigableSet<Vertex> q) {
Vertex u, v;
while (!q.isEmpty()) {
u = q.pollFirst(); // vertex with shortest distance (first iteration will return source)
if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable
// vertex with shortest distance (first iteration will return source)
u = q.pollFirst();
if (u.dist == Integer.MAX_VALUE)
break; // we can ignore u (and any other remaining vertices) since they are unreachable
// look at distances to each neighbour
for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
@ -161,6 +167,7 @@ class Graph {
graph.get(endName).printPath();
System.out.println();
}
/** Prints the path from the source to every vertex (output order is not guaranteed) */
public void printAllPaths() {
for (Vertex v : graph.values()) {