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

时间:2021-10-19 07:25:42

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

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

 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. HttpModule &amp&semi; HttpHandler

    ASP.NET 处理请求的过程 inetinfo.exe:www 服务进程,IIS 服务 和 ASPNET_ISAPI.dll 都寄存在此进程中. ASPNET_ISAPI.dll:处理 .aspx ...

  2. 暴力清除Android中的短信

    有些短信程序有bug,当短信(特别是彩信)没有接收完整,或者是一些异常情况下,你会收到一条短信但是看不到或者看不了. 此时郁闷的事情就来了,系统会提醒你还有1条未读短信,但是你满世界都找不到这条短信. ...

  3. 基础命名空间:序列化 System&period;Runtime&period;Serialization

    对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化. ...

  4. ASP&period;NET 应用程序生命周期

    1.请求到达IIS服务器,IIS根据文件后缀找到对应的ISAPI(Internet Server API)扩展来处理,这个配置可在网站属性里的“根目录”选项卡中的“配置”里看到.可以看到,ashx.a ...

  5. UVA 1451 Average

    A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the numbe ...

  6. Cesium 云服务

    前言 所有行业内都知道云是未来或者现在的趋势,但是真正的完完全全提供地理信息云服务的恐怕只有 Google 一家,然而今天我居然发现 Cesium 提供了云服务,你没有看错,就是曾经的开源 3D 渲染 ...

  7. &lbrack;Python接口自动化&rsqb;从零开始学习python自动化(1):环境搭建

    第一步:安装python编译环境 安装python编译环境之前,必须保证已安装jdk哈,如果为安装,请参考https://jingyan.baidu.com/article/6dad5075d1dc4 ...

  8. HTML&amp&semi;javaSkcript&amp&semi;CSS&amp&semi;jQuery&amp&semi;ajax(五)

    一.Framset标签定义了每个框架中的HTML文档, 1. <framset cols="25%,75%"> <frame src="frame_a. ...

  9. dwz Esc关闭dialog 窗口

    document.onkeydown = function(e){ // alert(1) var keycode = ""; if(navigator.appName == &q ...

  10. tf&period;nn&period;dropout

    tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 此函数是为了防止在训练中过拟合的操作,将训练输出按一定规则进行变 ...