符号表
Posted ppwq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了符号表相关的知识,希望对你有一定的参考价值。
无序链表的顺序查找
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)次比较。
以上是关于符号表的主要内容,如果未能解决你的问题,请参考以下文章
2021-09-11:给你一个32位的有符号整数x,返回将x中的数字部分反转后的结果。反转后整数超过 32 位的有符号整数的范围就返回0,假设环境不允许存储 64 位整数(有符号或无符号)。(代码片段