▶ 书中第三章部分程序,加上自己补充的代码,包含公共符号表、集合类型

● 公共符号表,用于普通查找表的基本类

 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));
}
}

《算法》第三章部分程序 part 5的更多相关文章

  1. 《算法》第三章部分程序 part 6

    ▶ 书中第三章部分程序,加上自己补充的代码,包含双向索引表.文建索引.稀疏向量类型 ● 双向索引表 package package01; import edu.princeton.cs.algs4.S ...

  2. 《算法》第三章部分程序 part 4

    ▶ 书中第三章部分程序,加上自己补充的代码,包括散列表.线性探查表 ● 散列表 package package01; import edu.princeton.cs.algs4.Queue; impo ...

  3. 《算法》第三章部分程序 part 3

    ▶ 书中第三章部分程序,加上自己补充的代码,红黑树 ● 红黑树,大部分方法与注释与二叉树相同 package package01; import java.util.NoSuchElementExce ...

  4. 《算法》第三章部分程序 part 2

    ▶ 书中第三章部分程序,加上自己补充的代码,平衡二叉搜索树 ● 平衡二叉搜索树 package package01; import java.util.NoSuchElementException; ...

  5. 《算法》第三章部分程序 part 1

    ▶ 书中第三章部分程序,加上自己补充的代码,包括单词频率统计,(单链表)顺序查找表,二分查找表 ● 单词频率统计 package package01; import edu.princeton.cs. ...

  6. 《算法》第二章部分程序 part 3

    ▶ 书中第二章部分程序,加上自己补充的代码,包括各种优化的快排 package package01; import edu.princeton.cs.algs4.In; import edu.prin ...

  7. 《算法》第一章部分程序 part 1

    ▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...

  8. 《算法》第二章部分程序 part 5

    ▶ 书中第二章部分程序,加上自己补充的代码,包括利用优先队列进行多路归并和堆排序 ● 利用优先队列进行多路归并 package package01; import edu.princeton.cs.a ...

  9. 《算法》第二章部分程序 part 4

    ▶ 书中第二章部分程序,加上自己补充的代码,包括优先队列和索引优先队列 ● 优先队列 package package01; import java.util.Comparator; import ja ...

随机推荐

  1. DLL的晚绑定与早绑定

    调用DLL中的函数可分为早绑定与晚绑定! 早绑定是指在编译期就已经确定函数地址! 晚绑定是指在运行期动态加载dll,并根据查表的方式获取dll内exports函数的地址,由于早绑定比较简单,在此不再讲 ...

  2. mongodb 慕课网

    ------------------------------------------------mongodb简述------------------------------------------- ...

  3. php中的this,self,parent

    this就是指向当前对象实例的指针,不指向任何其他对象或类 如$this->fun1(); self:指向类本身,也就是self是不指向任何已经实例化的对象 ,self使用来指向类中的静态属性或 ...

  4. 【linux】之查看物理CPU个数、核数、逻辑CPU个数

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...

  5. awk中使用shell的环境变量

    awk中使用shell的环境变量一:"'$var'"这种写法大家无需改变用'括起awk程序的习惯,是老外常用的写法.如:var="test"awk 'BEGIN ...

  6. [UE4]虚幻4的网络适合开发什么游戏

    使用虚幻4开发网络游戏的两种方式 一.虚幻4只做客户端,服务器端独立开发,适用于任何网络游戏 二.使用虚幻4同时开发客户端和服务器(使用虚幻4内置的服务器),适用于一部分网络游戏. 如果使用虚幻4作为 ...

  7. 第二章:安装zabbix过程

    2.2 安装zabbix过程 2.2.1 安装方式选择 编译安装 (服务较多,环境复杂) yum安装(干净环境) 使用yum 需要镜像yum源 http://www.cnblogs.com/clsn/ ...

  8. Map相关问题

    <!--加载地图开始--> <!DOCTYPE html><html><head> <meta charset="UTF-8" ...

  9. npm rebuild node-sass

    在使用npm安装node-sass的时候,可能会出现如下的报错: Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code ...

  10. 数据迁移_老集群RAC迁移数据恢复到新集群RAC

    数据迁移_老集群RAC迁移数据恢复到新集群RAC 作者:Eric 微信:loveoracle11g 1.把老集群RAC备份的数据远程拷贝到新集群RAC [root@old-rac-node1 ~]# ...