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

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

 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. sublime text2建成C语言(C++)编译环境

    四个步骤: 下载安装Sublime text2 for windows 下载安装 MinGW 与 系统变量设置 Sublime Building System 设置 编译测试 一.下载安装Sublim ...

  2. Release Notes for XE5

    开发者之前说明 http://docwiki.embarcadero.com/RADStudio/XE5/en/Release_Notes_for_XE5

  3. C#实现根据日期计算星期

    /// <summary> /// 根据日期返回 星期(返回结果为英文) /// </summary> /// <param name="date"& ...

  4. ALGO-22_蓝桥杯_算法训练_装箱问题(DP)

    问题描述 有一个箱子容量为V(正整数,<=V<=),同时有n个物品(<n<=),每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱内,使箱子的剩余空间为最小. 输 ...

  5. Qt学习——QListWidget控件的使用

    转载:GDUTLYP Qt提供QListWidget类列表框控件用来加载并显示多个列表项.QListWidgetItem类就是列表项类. 一般列表框控件中的列表项有两种加载方式: 一种是由用户手动添加 ...

  6. 导入jar包的方法

    右键项目弹出菜单,进行如下选择: 4.在配置页中,选中Libraries标签页,然后点击Add JARs选择刚才拷贝过来的jar包.最后点击apply and close. 添加完成后图标会发生变化 ...

  7. [转]SuperSocket

    public class SocketServer : AppServer<AppSession> { public SocketServer() : base(new DefaultRe ...

  8. 廖雪峰Java1-3流程控制-6 do-while循环

    do-while循环 do-while先执行循环,再判断条件. 条件满足时继续循环:条件不满足时退出:至少循环1次 int sum =0; int n = 1; do{ sum = sum + n; ...

  9. OA-DB-LINUX安装说明

    HOST配置: HOST1:OADB-NODE1.LSTECH.COM HOST2:OADB-NODE2.LSTECH.COM 每台主机配置两个不同VLAN的IP地址 OSUSER:oracle 1. ...

  10. 用微信小程序连接WordPress网站

    随着微信小程序的功能越来越强,特别对个人开发者的开放,让个人开发者有机会尝试微信小程序.如果你有自己的个人网站,就可以把个人网站搬到微信小程序里,通过小程序直接访问网站的内容. 要想微信小程序可以获取 ...