算法4-查找

3.1.符号表
符号表是一种存储键值对的数据结构,支持两种操作;put插入和get查找;
无序符号表api:
public class ST
ST()
void put(Key key,Value value);
Value get(Key key);
void delete(Key key);
boolean contains(Key key);
boolean isEmpty();
int size();
Iterable keys();
boolean contains(Key key);

约束:
1.每个键仅对应一个值,表中不允许重复的键;
2.当用例代码向表中存入的键值对和表中已有的键冲突时,新值会覆盖旧值;
3.键及值均不能为null
可使用get方法测试某个键是否存在;
可使用put(Key,null)来删除某个键;
4.删除操作
(1)延时删除,即将键对应的值置为空,然后在某个时候删除所有值为空的键;->put(Key,null);
(2)即时删除,立刻从表中删除指定的键;->delete(key)

有序符号表api:
ST()
void put(Key key,Value val);
Value get(Key key);
void delete(Key key);
boolean contains(Key key);
boolean isEmpty();
int size();
Key min();
Key max();
Key floor(Key key);//小于等于key的最大键
Key ceiling(Key key);//大于等于key的最小键
int rank(Key key);//小于key的键的数量
Key select(int k);//排名为k的键
void deleteMin();//删除最小的键
void deleteMax();//删除最大的键
int size(Key li,Key lo);//[lo,hi]之间键的数量
Iterable keys(Key lo,Key hi);//[lo,hi]之间的所有键,已排序
Iterable keys();//表中所有键的集合,已排序;

JAVA运行:
E:\jdk14.0.2\bin\java.exe -Dfile.e
s\leetcode\lib\algs4.jar algorithm.search.FrequencyCounter

javac -cp F:\IdeaProjects\leetcode\lib\algs4.jar FrequencyCounter.java

//下面-cp的第一个参数为重点,表明从什么地方加载这个class类
java.exe -cp F:\IdeaProjects\leetcode\src;F:\IdeaProjects\leetcode\lib\algs4.jar algorithm.search.FrequencyCounter

//正确示例
javac -cp F:\IdeaProjects\leetcode\src;F:\IdeaProjects\leetcode\lib\algs4.jar algorithm\search\FrequencyCounter.java

java.exe -cp F:\IdeaProjects\leetcode\src;F:\IdeaProjects\leetcode\lib\algs4.jar algorithm/search/FrequencyCounter 1 <./test.txt

无序链表的顺序查找

package algorithm.search;

import java.util.Iterator;

public class SequentialSearchST<Key, Value> {
     
    private Node first;

    private class Node implements Iterable<Key> {
     
        Key key;
        Value value;
        Node next;

        public Node(Key key, Value value, Node next) {
     
            this.key = key;
            this.value = value;
            this.next = next;
        }

        @Override
        public Iterator<Key> iterator() {
     
            return new Iterator<Key>() {
     
                @Override
                public boolean hasNext() {
     
                    return first != null;
                }

                @Override
                public Key next() {
     
                    Key key = first.key;
                    first = first.next;
                    return key;
                }
            };
        }
    }

    public Value get(Key key) {
     
        for (Node x = first; x != null; x = x.next) {
     
            if (key.equals(x.key)) {
     
                return x.value;
            }
        }
        return null;
    }

    public void put(Key key, Value value) {
     
        for (Node x = first; x != null; x = x.next) {
     
            if (key.equals(x.key)) {
     
                x.value = value;
                return;
            }
        }
        first = new Node(key, value, first);
    }

    public int size() {
     
        int size = 0;
        for (Node x = first; x != null; x = x.next) {
     
            size++;
        }
        return size;
    }

    public void delete(Key key) {
     
        Node pre = null;
        for (Node x = first; x != null; x = x.next) {
     
            if (x.key.equals(key)) {
     
                if (pre == null) {
     
                    first = first.next;
                    return;
                }
                pre.next = x.next;
                return;
            }
            pre = x;
        }
    }

    public Iterator<Key> keys() {
     
        return first.iterator();
    }

    public static void main(String[] args) {
     
        SequentialSearchST<Integer, String> st = new SequentialSearchST<>();
        st.put(1, "1");
        st.put(2, "2");
        st.put(3, "3");
        st.delete(33);
        Iterator<Integer> keys = st.keys();
        while (keys.hasNext()){
     
            System.out.println(keys.next());
        }

    }
}

3.1.5.

你可能感兴趣的:(算法4-查找)