diff --git a/Chapter3/BinarySearchST.java b/Chapter3/BinarySearchST.java new file mode 100644 index 0000000..d5ef3e8 --- /dev/null +++ b/Chapter3/BinarySearchST.java @@ -0,0 +1,140 @@ +import javax.lang.model.util.ElementScanner6; + +/** + * BinarySearchST 二分查找的有序符号表 + */ +public class BinarySearchST, Value> { + + private Key[] keys; + private Value[] vals; + private int N; + + public BinarySearchST(int capacity) { + keys = (Key[]) new Comparable[capacity]; + vals = (Value[]) new Object[capacity]; + } + + public int size() { + return N; + } + + public boolean isEmpty() { + return size() == 0; + } + + public Value get(Key key) { + if (isEmpty()) + return null; + int i = rank(key); + if (i < N && keys[i].compareTo(key) == 0) + return vals[i]; + else + return null; + } + + public int rank(Key key) { + int lo = 0, hi = N - 1; + hi = N - 1; + while (lo <= hi) { + int mid = lo + (hi - lo) / 2; + int cmp = key.compareTo(keys[mid]); + if (cmp < 0) + hi = mid - 1; + else if (cmp > 0) + lo = mid + 1; + else + return mid; + } + return lo; + } + + public void put(Key key, Value val) { + int i = rank(key); + if (i < N && keys[i].compareTo(key) == 0) { + vals[i] = val; + return; + } + for (int j = N; j > i; j--) { + keys[j] = keys[j - 1]; + vals[j] = vals[j - 1]; + } + keys[i] = key; + vals[i] = val; + N++; + } + + public void delete(Key key) { + int i = rank(key); + if (keys[i].compareTo(key) != 0) + return; + for (int j = i; j < N; j++) { + keys[j] = keys[j + 1]; + vals[j] = vals[j + 1]; + } + N--; + } + + public Key min() { + return keys[0]; + } + + public Key max() { + return keys[N - 1]; + } + + public Key select(int k) { + return keys[k]; + } + + public Key ceiling(Key key) { + int i = rank(key); + return keys[i]; + } + + public Key floor(Key key) { + int i = rank(key); + if (keys[i].equals(key)) + return keys[i]; + else + return keys[i - 1]; + } + + public Iterable keys(Key lo, Key hi) { + Queue q = new Queue(); + for (int i = rank(lo); i < rank(hi); i++) + q.enqueue(keys[i]); + if (contains(hi)) + q.enqueue(keys[rank(hi)]); + return q; + } + + public boolean contains(Key key) { + int lo = 0, hi = N - 1; + while (lo <= hi) { + int mid = lo + (hi - lo) / 2; + int cmp = keys[mid].compareTo(key); + if (cmp < 0) { + lo = mid + 1; + } else if (cmp > 0) { + hi = mid - 1; + } else + return true; + } + return false; + } + + public static void main(String[] args) { + BinarySearchST vst = new BinarySearchST(13); + for (int i = 0; i < 10; i++) { + vst.put(i, i + 10); + } + if (vst.contains(3)) + System.out.println("Contains 3"); + if (!vst.contains(13)) + System.out.println("13 is not here"); + vst.delete(3); + System.out.println("Deleting 3"); + if (!vst.contains(3)) + System.out.println("3 is not here"); + } +} \ No newline at end of file diff --git a/Chapter3/SequentialSearchST.java b/Chapter3/SequentialSearchST.java new file mode 100644 index 0000000..a0b8c1f --- /dev/null +++ b/Chapter3/SequentialSearchST.java @@ -0,0 +1,71 @@ +/** + * SequentialSearchST + */ +public class SequentialSearchST { + + private Node first; + + private class Node { + Key key; + Value val; + Node next; + + public Node(Key key, Value val, Node next) { + this.key = key; + this.val = val; + this.next = next; + } + } + + public Value get(Key key) { + for (Node x = first; x != null; x = x.next) { + if (key.equals(x.key)) + return x.val; + } + return null; + } + + public void put(Key key, Value val) { + for (Node x = first; x != null; x = x.next) { + if (key.equals(x.key)) { + x.val = val; + return; + } + } + first = new Node(key, val, first); + } + + public int size() { + int size = 0; + for (Node x = first; x != null; x = x.next) + size++; + return size; + } + + public void delete(Key key) { + for (Node x = first; x != null; x = x.next) + if (x.next.key.equals(key)) { + x.next = x.next.next; + return; + } + } + + public Iterable Keys() { + Queue q = new Queue(); + for (Node x = first; x != null; x = x.next) { + q.enqueue(x.key); + } + return q; + } + + public static void main(String[] args) { + SequentialSearchST st = new SequentialSearchST(); + for (int i = 0; i < 13; i++) + st.put(i, i + 10); + System.out.printf("value of Key(3) is %d\n", st.get(3)); + st.delete(3); + System.out.println("Deleting key(3)"); + System.out.printf("The size now is %d\n", st.size()); + + } +} \ No newline at end of file