package edu.princeton.cs.algs4.ch31; import edu.princeton.cs.algs4.ch13.Queue; import edu.princeton.cs.introcs.*; /************************************************************************* * Compilation: javac SequentialSearchST.java * Execution: java SequentialSearchST * Dependencies: StdIn.java StdOut.java * Data files: http://algs4.cs.princeton.edu/31elementary/tinyST.txt * * Symbol table implementation with sequential search in an * unordered linked list of key-value pairs. * * % more tinyST.txt * S E A R C H E X A M P L E * * % java SequentialSearchST < tiny.txt * L 11 * P 10 * M 9 * X 7 * H 5 * C 4 * R 3 * A 8 * E 12 * S 0 * *************************************************************************/ /** * The <tt>SequentialSearchST</tt> class represents an (unordered) * symbol table of generic key-value pairs. * It supports the usual <em>put</em>, <em>get</em>, <em>contains</em>, * <em>delete</em>, <em>size</em>, and <em>is-empty</em> methods. * It also provides a <em>keys</em> method for iterating over all of the keys. * A symbol table implements the <em>associative array</em> abstraction: * when associating a value with a key that is already in the symbol table, * the convention is to replace the old value with the new value. * The class also uses the convention that values cannot be <tt>null</tt>. Setting the * value associated with a key to <tt>null</tt> is equivalent to deleting the key * from the symbol table. * <p> * This implementation uses a singly-linked list and sequential search. * It relies on the <tt>equals()</tt> method to test whether two keys * are equal. It does not call either the <tt>compareTo()</tt> or * <tt>hashCode()</tt> method. * The <em>put</em> and <em>delete</em> operations take linear time; the * <em>get</em> and <em>contains</em> operations takes linear time in the worst case. * The <em>size</em>, and <em>is-empty</em> operations take constant time. * Construction takes constant time. * <p> * For additional documentation, see <a href="http://algs4.cs.princeton.edu/31elementary">Section 3.1</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class SequentialSearchST<Key, Value> { private int N; // number of key-value pairs private Node first; // the linked list of key-value pairs // a helper linked list data type private class Node { private Key key; private Value val; private Node next; public Node(Key key, Value val, Node next) { this.key = key; this.val = val; this.next = next; } } /** * Initializes an empty symbol table. */ public SequentialSearchST() { } /** * Returns the number of key-value pairs in this symbol table. * @return the number of key-value pairs in this symbol table */ public int size() { return N; } /** * Is this symbol table empty? * @return <tt>true</tt> if this symbol table is empty and <tt>false</tt> otherwise */ public boolean isEmpty() { return size() == 0; } /** * Does this symbol table contain the given key? * @param key the key * @return <tt>true</tt> if this symbol table contains <tt>key</tt> and * <tt>false</tt> otherwise */ public boolean contains(Key key) { return get(key) != null; } /** * Returns the value associated with the given key. * @param key the key * @return the value associated with the given key if the key is in the symbol table * and <tt>null</tt> if the key is not in the symbol table */ public Value get(Key key) { for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) return x.val; } return null; } /** * Inserts the key-value pair into the symbol table, overwriting the old value * with the new value if the key is already in the symbol table. * If the value is <tt>null</tt>, this effectively deletes the key from the symbol table. * @param key the key * @param val the value */ public void put(Key key, Value val) { if (val == null) { delete(key); return; } 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++; } /** * Removes the key and associated value from the symbol table * (if the key is in the symbol table). * @param key the key */ public void delete(Key key) { first = delete(first, key); } // delete key in linked list beginning at Node x // warning: function call stack too large if table is large private Node delete(Node x, Key key) { if (x == null) return null; if (key.equals(x.key)) { N--; return x.next; } x.next = delete(x.next, key); return x; } /** * Returns all keys in the symbol table as an <tt>Iterable</tt>. * To iterate over all of the keys in the symbol table named <tt>st</tt>, * use the foreach notation: <tt>for (Key key : st.keys())</tt>. * @return all keys in the sybol table as an <tt>Iterable</tt> */ public Iterable<Key> keys() { Queue<Key> queue = new Queue<Key>(); for (Node x = first; x != null; x = x.next) queue.enqueue(x.key); return queue; } /** * Unit tests the <tt>SequentialSearchST</tt> data type. */ public static void main(String[] args) { SequentialSearchST<String, Integer> st = new SequentialSearchST<String, Integer>(); for (int i = 0; !StdIn.isEmpty(); i++) { String key = StdIn.readString(); st.put(key, i); } for (String s : st.keys()) StdOut.println(s + " " + st.get(s)); } } /************************************************************************* * Copyright 2002-2012, Robert Sedgewick and Kevin Wayne. * * This file is part of algs4-package.jar, which accompanies the textbook * * Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne, * Addison-Wesley Professional, 2011, ISBN 0-321-57351-X. * http://algs4.cs.princeton.edu * * * algs4-package.jar is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * algs4-package.jar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with algs4-package.jar. If not, see http://www.gnu.org/licenses. *************************************************************************/