符號表
阿新 • • 發佈:2018-05-16
LG AS 進行 分享 seq 找到 end 不同的 不同
無序鏈表的順序查找
public class SequentialSearchST<Key, Value> { 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); //未命中,新建結點 } }
向一個空表插入N個不同的鍵需要N2/2次比較,一次查找所需比較數,采用隨機命中的話是N/2,說明基於鏈表的實現和順序查找是非常低效的。
有序數組中的二分查找
public class BinarySearchST<Key extends Comparable<Key>, 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 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; 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 value) { //查找鍵,找到則更新值,否則創建新的元素 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 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) { } public void delete(Key key) { int i = rank(key); for (int j = i; j < N-1; j++) { keys[j] = keys[j+1]; vals[j] = vals[j+1]; } } public Iterable<Key> keys(Key lo, Key hi) { Queue<Key> q = new Queue<Key>(); for (int i = rank(lo); i < rank(hi); i++) q.enqueue(keys[i]); if (contains(hi)) q.enqueue(keys[rank(hi)]); return q; } }
N個鍵的有序數組進行二分查找最多需要(lgN+1)次比較。
符號表