Skip to content

Commit 91ba558

Browse files
committed
[Bug fix]
1.Fix the bug of not checking the boundary of the array. 2.Add some functions in get iterable symbol table.
1 parent 5db546d commit 91ba558

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+73-20
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,17 @@
175175
return t;
176176
}
177177
public Boolean isEmpty() { return size.equals(0); }
178-
public Integer size() { return size; }
178+
public Integer size() { return size; }
179+
public Iterator<T> iterator() {
180+
return new ListFIFOQueueIterator<T>();
181+
}
182+
183+
private class ListFIFOQueueIterator<T> implements Iterator<T>{
184+
public boolean hasNext() { return size > 0; }
185+
public T next() { return (T) dequeue(); }
186+
public void remove() {
187+
}
188+
}
179189
}
180190

181191
11. 栈 LIFO队列:
@@ -821,26 +831,69 @@
821831

822832
二分法实现类:
823833
实现:ca.mcmaster.chapter.three.BinarySearchST<K, V>
824-
public class BinarySearchST<K extends Comparable<K>, V> extends SearchSTAbstract<K, V> {
825-
public BinarySearchST(int capacity) { super(capacity); }
826-
public int binarySearch(K k) {
827-
int lo = 0, hi = N - 1;
828-
while(lo <= hi){
829-
int mid = lo + (hi - lo) / 2;
830-
int cmp = keys[mid].compareTo(k);
831-
if(cmp < 0) lo = mid + 1;
832-
else if(cmp > 0) hi = mid - 1;
833-
else return mid;
834+
public abstract class SearchSTAbstract<K extends Comparable<K>, V> {
835+
protected K[] keys;
836+
protected V[] values;
837+
protected int N;
838+
public SearchSTAbstract(int capacity){
839+
keys = (K[]) new Comparable[capacity];
840+
values = (V[]) new Object[capacity];
841+
}
842+
public int size() { return N; }
843+
public V get(K k){
844+
if(isEmpty()) return null;
845+
int i = binarySearch(k);
846+
if(i < N && keys[i].compareTo(k) == 0) return values[i];
847+
else return null;
848+
}
849+
public void put(K k, V v){
850+
int i = binarySearch(k);
851+
if(i < N && keys[i].compareTo(k) == 0){
852+
values[i] = v; return;
834853
}
835-
return lo;
836-
}
837-
public Integer traditionalBinarySearch(K k, int lo, int hi){
838-
if(lo > hi) return lo;
839-
int mid = lo + (hi - lo) / 2;
840-
int cmp = keys[mid].compareTo(k);
841-
if(cmp > 0) return traditionalBinarySearch(k, lo, mid - 1);
842-
else if(cmp < 0) return traditionalBinarySearch(k, mid + 1, hi);
843-
else return mid;
854+
for(int j = N; j > i; j--){
855+
keys[j] = keys[j-1];
856+
values[j] = values[j-1];
857+
}
858+
keys[i] = k;
859+
values[i] = v;
860+
N++;
861+
}
862+
public Boolean isEmpty() { return N == 0; }
863+
public abstract int binarySearch(K k);
864+
public void delete(K k){
865+
int i = binarySearch(k);
866+
if(i < N && keys[i].compareTo(k) == 0){
867+
for(int j = i; j < N; j++){
868+
keys[j] = keys[j+1];
869+
values[j] = values[j+1];
870+
}
871+
keys[N] = null;
872+
values[N] = null;
873+
N--;
874+
}
875+
}
876+
public K min(){ return keys[0]; }
877+
public K max(){ return keys[N-1]; }
878+
public K select(int k){ return keys[k]; }
879+
public K ceiling(K k){
880+
int i = binarySearch(k);
881+
return keys[i];
882+
}
883+
public Iterable<K> keys(int lo, int hi){
884+
if(lo > hi) return null;
885+
if(hi >= N ) throw new ArrayIndexOutOfBoundsException();
886+
ListFIFOQueue<K> queue = new ListFIFOQueue<>();
887+
for(int i = binarySearch(keys[lo]); i < binarySearch(keys[hi]); i++){
888+
queue.enqueue(keys[i]);
889+
System.out.println(i);
890+
}
891+
if(contains(keys[hi])) queue.enqueue(keys[hi]);
892+
return queue;
893+
}
894+
public Boolean contains(K k){
895+
int i = binarySearch(k);
896+
return i < N;
844897
}
845898
}
846899
传统二分法:

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/three/BinarySearchST.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void main(String[] args) {
3333
System.out.println(table.get("d"));
3434
table.delete("d");
3535
System.out.println(table.get("d"));
36-
Iterable<String> queue = table.keys(0, 3);
36+
Iterable<String> queue = table.keys(0, 2);
3737
Iterator<String> it = queue.iterator();
3838
while (it.hasNext()) {
3939
System.out.println(it.next());

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/three/SearchSTAbstract.java

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public K ceiling(K k){
5353
}
5454
public Iterable<K> keys(int lo, int hi){
5555
if(lo > hi) return null;
56+
if(hi >= N ) throw new ArrayIndexOutOfBoundsException();
5657
ListFIFOQueue<K> queue = new ListFIFOQueue<>();
5758
for(int i = binarySearch(keys[lo]); i < binarySearch(keys[hi]); i++){
5859
queue.enqueue(keys[i]);

0 commit comments

Comments
 (0)