▶ 书中第三章部分程序,加上自己补充的代码,包含公共符号表、集合类型
● 公共符号表,用于普通查找表的基本类
package package01; import java.util.NoSuchElementException;
import java.util.TreeMap;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class class01<Key extends Comparable<Key>, Value> implements Iterable<Key>
{
private TreeMap<Key, Value> st; public class01()
{
st = new TreeMap<Key, Value>();
} public Value get(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<get> key == null.\n");
return st.get(key);
} public void put(Key key, Value val)
{
if (key == null)
throw new IllegalArgumentException("\n<put> key == null.\n");
if (val == null)
st.remove(key);
else
st.put(key, val);
} public void delete(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<delete> key == null.\n");
st.remove(key);
} public boolean contains(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<contains> key == null.\n");
return st.containsKey(key);
} public int size()
{
return st.size();
} public boolean isEmpty()
{
return size() == 0;
} public Iterable<Key> keys()
{
return st.keySet();
} public Key min()
{
if (isEmpty())
throw new NoSuchElementException("\n<min> empty.\n");
return st.firstKey();
} public Key max()
{
if (isEmpty())
throw new NoSuchElementException("\n<max> empty.\n");
return st.lastKey();
} public Key ceiling(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<min> key == null.\n");
Key k = st.ceilingKey(key);
if (k == null)
throw new NoSuchElementException("\n<min> k == null.\n");
return k;
} public Key floor(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<min> key == null.\n");
Key k = st.floorKey(key);
if (k == null)
throw new NoSuchElementException("\n<min> k == null.\n");
return k;
} public static void main(String[] args)
{
class01<String, Integer> st = new class01<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));
}
}
● 集合类型
package package01; import java.util.NoSuchElementException;
import java.util.Iterator;
import java.util.TreeSet;
import edu.princeton.cs.algs4.StdOut; public class class01<Key extends Comparable<Key>> implements Iterable<Key>
{
private TreeSet<Key> set; public class01()
{
set = new TreeSet<Key>();
} public class01(class01<Key> x)
{
set = new TreeSet<Key>(x.set);
} public void add(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<add> key == null.\n");
set.add(key);
} public boolean contains(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<contains> key == null.\n");
return set.contains(key);
} public void delete(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<delete> key == null.\n");
set.remove(key);
} public int size()
{
return set.size();
} public boolean isEmpty()
{
return size() == 0;
} public Iterator<Key> iterator()
{
return set.iterator();
} public Key max()
{
if (isEmpty())
throw new NoSuchElementException("\n<max> empty.\n");
return set.last();
} public Key min()
{
if (isEmpty())
throw new NoSuchElementException("\n<min> key == null.\n");
return set.first();
} public Key ceiling(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<ceiling> key == null.\n");
Key k = set.ceiling(key);
if (k == null)
throw new NoSuchElementException("\n<ceiling> k == null.\n");
return k;
} public Key floor(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<floor> key == null.\n");
Key k = set.floor(key);
if (k == null)
throw new NoSuchElementException("\n<floor> k == null.\n");
return k;
} public class01<Key> union(class01<Key> that)
{
if (that == null)
throw new IllegalArgumentException("\n<floor> key == null.\n");
class01<Key> c = new class01<Key>();
for (Key x : this)
c.add(x);
for (Key x : that)
c.add(x);
return c;
} public class01<Key> intersects(class01<Key> that)
{
if (that == null)
throw new IllegalArgumentException("\n<floor> key == null.\n");
class01<Key> c = new class01<Key>();
if (size() < that.size()) // 遍历较小的集合,去较大的集合中匹配,无所谓?
{
for (Key x : this)
{
if (that.contains(x))
c.add(x);
}
}
else
{
for (Key x : that)
{
if (contains(x))
c.add(x);
}
}
return c;
} public boolean equals(Object other)
{
if (other == this)
return true;
if (other == null)
return false;
if (other.getClass() != getClass())
return false;
class01 that = (class01) other;
return set.equals(that.set);
} @Override
public int hashCode()
{
throw new UnsupportedOperationException("\n<hashCode> hashCode() not supported,\n");
} @Override
public String toString() // 把集合的元素放进大括号中列出
{
String s = set.toString();
return "{ " + s.substring(1, s.length() - 1) + " }";
} public static void main(String[] args)
{
class01<String> set = new class01<String>();
StdOut.println("set = " + set); // 输出空集合 set.add("www.cs.princeton.edu"); // 插入一些元素用于测试方法
set.add("www.cs.princeton.edu");
set.add("www.princeton.edu");
set.add("www.math.princeton.edu");
set.add("www.yale.edu");
set.add("www.amazon.com");
set.add("www.simpsons.com");
set.add("www.stanford.edu");
set.add("www.google.com");
set.add("www.ibm.com");
set.add("www.apple.com");
set.add("www.slashdot.com");
set.add("www.whitehouse.gov");
set.add("www.espn.com");
set.add("www.snopes.com");
set.add("www.movies.com");
set.add("www.cnn.com");
set.add("www.iitb.ac.in"); StdOut.println(set.contains("www.cs.princeton.edu"));
StdOut.println(!set.contains("www.harvardsucks.com"));
StdOut.println();
StdOut.println("ceiling(www.simpsonr.com) = " + set.ceiling("www.simpsonr.com"));
StdOut.println("ceiling(www.simpsons.com) = " + set.ceiling("www.simpsons.com"));
StdOut.println("floor(www.simpsonr.com) = " + set.floor("www.simpsonr.com"));
StdOut.println("floor(www.simpsons.com) = " + set.floor("www.simpsons.com"));
StdOut.println();
StdOut.println("set = " + set);
StdOut.println();
for (String s : set) // 直接列出表中元素
StdOut.println(s);
StdOut.println();
class01<String> set2 = new class01<String>(set);
StdOut.println(set.equals(set2));
}
}