Changeset 32725 in osm for applications/editors/josm/plugins/utilsplugin2/src/edu
- Timestamp:
- 2016-07-27T03:10:06+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/utilsplugin2/src/edu/princeton/cs/algs4
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/utilsplugin2/src/edu/princeton/cs/algs4/Bag.java
r28116 r32725 14 14 15 15 /** 16 * The <tt>Bag</tt> class represents a bag (or multiset) of 17 * generic items. It supports insertion and iterating over the 16 * The <tt>Bag</tt> class represents a bag (or multiset) of 17 * generic items. It supports insertion and iterating over the 18 18 * items in arbitrary order. 19 19 * <p> 20 * The <em>add</em>, <em>isEmpty</em>, and <em>size</em> operation 20 * The <em>add</em>, <em>isEmpty</em>, and <em>size</em> operation 21 21 * take constant time. Iteration takes time proportional to the number of items. 22 22 * <p> … … 34 34 } 35 35 36 /**36 /** 37 37 * Create an empty stack. 38 38 */ … … 43 43 } 44 44 45 /**45 /** 46 46 * Is the BAG empty? 47 47 */ … … 50 50 } 51 51 52 /**52 /** 53 53 * Return the number of items in the bag. 54 54 */ … … 57 57 } 58 58 59 /**59 /** 60 60 * Add the item to the bag. 61 61 */ … … 90 90 91 91 return true; 92 } 92 } 93 93 94 95 /** 94 /** 96 95 * Return an iterator that iterates over the items in the bag. 97 96 */ 97 @Override 98 98 public Iterator<Item> iterator() { 99 return new ListIterator(); 99 return new ListIterator(); 100 100 } 101 101 … … 104 104 private Node current = first; 105 105 106 @Override 106 107 public boolean hasNext() { return current != null; } 108 @Override 107 109 public void remove() { throw new UnsupportedOperationException(); } 108 110 111 @Override 109 112 public Item next() { 110 113 if (!hasNext()) throw new NoSuchElementException(); 111 114 Item item = current.item; 112 current = current.next; 115 current = current.next; 113 116 return item; 114 117 } 115 118 } 116 117 119 } -
applications/editors/josm/plugins/utilsplugin2/src/edu/princeton/cs/algs4/DirectedEdge.java
r28116 r32725 17 17 */ 18 18 19 public class DirectedEdge { 19 public class DirectedEdge { 20 20 private final int v; 21 21 private final int w; 22 22 private final double weight; 23 23 24 /**24 /** 25 25 * Create a directed edge from v to w with given weight. 26 26 */ … … 31 31 } 32 32 33 /**33 /** 34 34 * Return the vertex where this edge begins. 35 35 */ … … 38 38 } 39 39 40 /**40 /** 41 41 * Return the vertex where this edge ends. 42 42 */ … … 45 45 } 46 46 47 /**47 /** 48 48 * Return the weight of this edge. 49 49 */ 50 50 public double weight() { return weight; } 51 51 52 /**52 /** 53 53 * Return a string representation of this edge. 54 54 */ 55 @Override 55 56 public String toString() { 56 57 return v + "->" + w + " " + String.format("%5.2f", weight); 57 58 } 58 59 /**60 * Test client.61 */62 // public static void main(String[] args) {63 // DirectedEdge e = new DirectedEdge(12, 23, 3.14);64 // StdOut.println(e);65 // }66 59 } -
applications/editors/josm/plugins/utilsplugin2/src/edu/princeton/cs/algs4/EdgeWeightedDigraph.java
r30737 r32725 25 25 private int E; 26 26 private Bag<DirectedEdge>[] adj; 27 27 28 28 /** 29 29 * Create an empty edge-weighted digraph with V vertices. … … 34 34 this.V = V; 35 35 this.E = 0; 36 adj = (Bag<DirectedEdge>[])new Bag[V];36 adj = new Bag[V]; 37 37 for (int v = 0; v < V; v++) 38 38 adj[v] = new Bag<>(); 39 39 } 40 40 41 /**41 /** 42 42 * Create a edge-weighted digraph with V vertices and E edges. 43 43 */ … … 57 57 * Create an edge-weighted digraph from input stream. 58 58 */ 59 // public EdgeWeightedDigraph(In in) {60 // this(in.readInt());61 // int E = in.readInt();62 // for (int i = 0; i < E; i++) {63 // int v = in.readInt();64 // int w = in.readInt();65 // double weight = in.readDouble();66 // addEdge(new DirectedEdge(v, w, weight));67 // }68 // }59 // public EdgeWeightedDigraph(In in) { 60 // this(in.readInt()); 61 // int E = in.readInt(); 62 // for (int i = 0; i < E; i++) { 63 // int v = in.readInt(); 64 // int w = in.readInt(); 65 // double weight = in.readDouble(); 66 // addEdge(new DirectedEdge(v, w, weight)); 67 // } 68 // } 69 69 70 /**70 /** 71 71 * Copy constructor. 72 72 */ … … 86 86 } 87 87 88 /**88 /** 89 89 * Return the number of vertices in this digraph. 90 90 */ … … 93 93 } 94 94 95 /**95 /** 96 96 * Return the number of edges in this digraph. 97 97 */ … … 100 100 } 101 101 102 /**102 /** 103 103 * Add the edge e to this digraph. 104 104 */ … … 109 109 } 110 110 111 /**111 /** 112 112 * Return the edges leaving vertex v as an Iterable. 113 113 * To iterate over the edges leaving vertex v, use foreach notation: … … 118 118 } 119 119 120 /**120 /** 121 121 * Return all edges in this graph as an Iterable. 122 122 * To iterate over the edges, use foreach notation: … … 131 131 } 132 132 return list; 133 } 133 } 134 134 135 /**135 /** 136 136 * Return number of edges leaving v. 137 137 */ … … 140 140 } 141 141 142 /**142 /** 143 143 * Return a string representation of this graph. 144 144 */ 145 @Override 145 146 public String toString() { 146 147 String NEWLINE = System.getProperty("line.separator"); -
applications/editors/josm/plugins/utilsplugin2/src/edu/princeton/cs/algs4/IndexMinPQ.java
r30737 r32725 51 51 52 52 // return the index associated with a minimal key 53 public int minIndex() { 53 public int minIndex() { 54 54 if (N == 0) throw new NoSuchElementException("Priority queue underflow"); 55 return pq[1]; 55 return pq[1]; 56 56 } 57 57 58 58 // return a minimal key 59 public Key minKey() { 59 public Key minKey() { 60 60 if (N == 0) throw new NoSuchElementException("Priority queue underflow"); 61 return keys[pq[1]]; 61 return keys[pq[1]]; 62 62 } 63 63 64 64 // delete a minimal key and returns its associated index 65 public int delMin() { 65 public int delMin() { 66 66 if (N == 0) throw new NoSuchElementException("Priority queue underflow"); 67 int min = pq[1]; 68 exch(1, N--); 67 int min = pq[1]; 68 exch(1, N--); 69 69 sink(1); 70 70 qp[min] = -1; // delete 71 71 keys[pq[N+1]] = null; // to help with garbage collection 72 72 pq[N+1] = -1; // not needed 73 return min; 73 return min; 74 74 } 75 75 … … 121 121 122 122 123 /**************************************************************124 * General helper functions125 **************************************************************/123 /************************************************************** 124 * General helper functions 125 **************************************************************/ 126 126 private boolean greater(int i, int j) { 127 127 return keys[pq[i]].compareTo(keys[pq[j]]) > 0; … … 134 134 135 135 136 /**************************************************************137 * Heap helper functions138 **************************************************************/136 /************************************************************** 137 * Heap helper functions 138 **************************************************************/ 139 139 private void swim(int k) { 140 140 while (k > 1 && greater(k/2, k)) { … … 155 155 156 156 157 /***********************************************************************158 * Iterators159 **********************************************************************/157 /*********************************************************************** 158 * Iterators 159 **********************************************************************/ 160 160 161 /**161 /** 162 162 * Return an iterator that iterates over all of the elements on the 163 163 * priority queue in ascending order. … … 165 165 * The iterator doesn't implement <tt>remove()</tt> since it's optional. 166 166 */ 167 @Override 167 168 public Iterator<Integer> iterator() { return new HeapIterator(); } 168 169 … … 179 180 } 180 181 182 @Override 181 183 public boolean hasNext() { return !copy.isEmpty(); } 184 @Override 182 185 public void remove() { throw new UnsupportedOperationException(); } 183 186 187 @Override 184 188 public Integer next() { 185 189 if (!hasNext()) throw new NoSuchElementException(); -
applications/editors/josm/plugins/utilsplugin2/src/edu/princeton/cs/algs4/Stack.java
r28116 r32725 8 8 * A generic stack, implemented using a linked list. Each stack 9 9 * element is of type Item. 10 * 11 * % more tobe.txt 10 * 11 * % more tobe.txt 12 12 * to be or not to - be - - that - - - is 13 13 * … … 42 42 } 43 43 44 /**44 /** 45 45 * Create an empty stack. 46 46 */ … … 51 51 } 52 52 53 /**53 /** 54 54 * Is the stack empty? 55 55 */ … … 58 58 } 59 59 60 /**60 /** 61 61 * Return the number of items in the stack. 62 62 */ … … 65 65 } 66 66 67 /**67 /** 68 68 * Add the item to the stack. 69 69 */ … … 77 77 } 78 78 79 /**79 /** 80 80 * Delete and return the item most recently added to the stack. 81 81 * Throw an exception if no such item exists because the stack is empty. … … 91 91 92 92 93 /**93 /** 94 94 * Return the item most recently added to the stack. 95 95 * Throw an exception if no such item exists because the stack is empty. … … 100 100 } 101 101 102 /**102 /** 103 103 * Return string representation. 104 104 */ 105 @Override 105 106 public String toString() { 106 107 StringBuilder s = new StringBuilder(); … … 109 110 return s.toString(); 110 111 } 111 112 112 113 113 114 // check internal invariants … … 132 133 133 134 return true; 134 } 135 } 135 136 136 137 137 /**138 /** 138 139 * Return an iterator to the stack that iterates through the items in LIFO order. 139 140 */ 141 @Override 140 142 public Iterator<Item> iterator() { return new ListIterator(); } 141 143 … … 143 145 private class ListIterator implements Iterator<Item> { 144 146 private Node current = first; 147 @Override 145 148 public boolean hasNext() { return current != null; } 149 @Override 146 150 public void remove() { throw new UnsupportedOperationException(); } 147 151 152 @Override 148 153 public Item next() { 149 154 if (!hasNext()) throw new NoSuchElementException(); 150 155 Item item = current.item; 151 current = current.next; 156 current = current.next; 152 157 return item; 153 158 } … … 155 160 156 161 157 /**162 /** 158 163 * A test client. 159 164 */ 160 // public static void main(String[] args) {161 // Stack<String> s = new Stack<String>();162 // while (!StdIn.isEmpty()) {163 // String item = StdIn.readString();164 // if (!item.equals("-")) s.push(item);165 // else if (!s.isEmpty()) StdOut.print(s.pop() + " ");166 // }167 // StdOut.println("(" + s.size() + " left on stack)");168 // }165 // public static void main(String[] args) { 166 // Stack<String> s = new Stack<String>(); 167 // while (!StdIn.isEmpty()) { 168 // String item = StdIn.readString(); 169 // if (!item.equals("-")) s.push(item); 170 // else if (!s.isEmpty()) StdOut.print(s.pop() + " "); 171 // } 172 // StdOut.println("(" + s.size() + " left on stack)"); 173 // } 169 174 } 170 175
Note:
See TracChangeset
for help on using the changeset viewer.