《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅳ
3.1.4 无序链表中的顺序查找
符号表中使用的数据结构的一个简单选择是链表,每个结点存储一个键值对,如以下代码所示。get()的实现即为遍历链表,用equals()方法比较需被查找的键和每个节点中的键。如果匹配成功我们就返回null。put()的实现也是遍历链表,用equals()方法比较需被查找的键。如果匹配成功我们就用第二个参数指定的值更新和改键现关联的值,否则我们就用给定的键值对创建一个新的节点并将其插入到链表的开头。这种方法也被称为顺序查找:在查找中我们一个一个地顺序遍历符号表中的所有键并使用equals()方法来寻找与被查找的键匹配的键。
算法(SequentialSearchST)用链表实现了符号表的基本API,这里我们将size()、keys()和即时型的delete()方法留作联系。
/*************************************************************************
* 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));
}
}
这种基于链表的实现能够用于和我们的用例类似的、需要大型符号表的应用?我们已经说过,分析符号表算法比分析排序算法更加困难,因为不同的用例所进行的操作序列各不相同。对于FrequencyCounter,最常见的情形是虽然查找和插入的使用模式是不可预测的,但他妈的使用肯定不是随机的。因此我们主要研究最坏情况下的性能。为了方便,我们使用命令中表示一次成功的查找来命中表示一次失败的查找。使用基于链表的符号表的索引引用例的轨迹如下图。

符号表的实现是i用了一个私有内部Node类来在链表中保存键和值。get()的实现会顺序地搜索链表查找给定的键(找到则返回相关联的值)。put()的实现也会顺序地搜索链表查找给定的键,如果找到则更新相关联的值,否则它会用给定的键值对创建一个新的结点并将其插入到链表的开头。size()、keys()和即时型的delete()方法的实现自行练习。
命题A。在含有N对键值的基于(无序)链表的符号表中,未命中的查找和插入操作都需要N次比较。未命中的查找和插入都需要N次比较。命中的查找在最坏情况下需要N次比较。特别地,向一个空表中插入N个不用的键余姚~N^2 / 2次比较。
证明。在表中查找一个不存在的键时,我们会将表中的每个键和给定的键比较。因为不允许出现重复的键,每次插入操作之前我们都需要这样查找一遍。
推论想空表中插入N个不同的键需要~N^2 / 2次比较。
《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅳ的更多相关文章
- 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅲ
3.1.3 用例举例 在学习它的实现之前我们还是应该先看看如何使用它.相应的我们这里考察两个用例:一个用来跟踪算法在小规模输入下的行为测试用例和一个来寻找更高效的实现的性能测试用例. 3.1.3.1 ...
- 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅱ
3.1.2 有序的符号表 典型的应用程序中,键都是Comparable的对象,因此可以使用a.compare(b)来比较a和b两个键.许多符号表的实现都利用Comparable接口带来的键的有序性来更 ...
- 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅰ
3.1符号表 符号表最主要的目的就是将一个键和一个值联系起来.用例能够将一个键值对插入符号表并希望在之后能够从符号表的所有键值对中按照键值姐找到对应的值.要实现符号表,我们首先要定义其背后的数据结构, ...
- 符号表(Symbol Tables)
小时候我们都翻过词典,现在接触过电脑的人大多数都会用文字处理软件(例如微软的word,附带拼写检查).拼写检查本身也是一个词典,只不过容量比较小.现实生活中有许多词典的应用: 拼写检查 数据库管理应用 ...
- C++Primer 4th edition读书笔记-第二章
1 变量的定义用于为变量分配存储空间,还可以为变量指定初始值.在一个程序中,变量有且只有一个定义.声明用于向程序表明变量的名字和类型.定义也是声明:当定义变量时,我们声明了它的类型和名字.可以通过使用 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)
2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅵ
· 学后心得体会与部分习题实现 心得体会: 曾经只是了解了优先队列的基本性质,并会调用C++ STL库中的priority_queue以及 java.util.PriorityQueue<E&g ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...
- 《C++ Primer 4th》读书笔记 序
注:本系列读书笔记是博主写作于两三年前的,所以是基于<C++ Primer>第四版的,目前该书已更新至第五版,第五版是基于C++11标准的,貌似更新挺多的.博主今年应届硕士毕业,如若过阵子 ...
随机推荐
- SRM 599 DIV1
A 首先发现对于2操作,每种素因子可以单独考虑,然后取出步数最多的计入答案,然后分别加上对每种素因子的1操作; 第二步我犯了个错误,以为最优方案是把素因子指数按二进制操作,在1的位置执行1操作,0的位 ...
- Web开发之RSET API
REST介绍 如果要说什么是REST的话,那最好先从Web(万维网)说起. 什么是Web呢?读者可以查看维基百科的词条(http://zh.wikipedia.org/zh-cn/Web),具体的我就 ...
- [置顶] js对象
js中,一切事物都是对象.对象是一切的基础. 而具体到某一个对象时. 对象则是包含一组变量和函数的集合实例 我们先来中体会下je对象的全局. 接下来就具体揭开这个对象的面纱吧 ja对象分类 Funct ...
- thinkphp分页时修改last显示标题
需要修改Page.class.php里lastSuffix为false,这样才能修改last显示标题. 然后就可以设置了 或者直接在方法中声明: $p->lastSuffix = false; ...
- 关于bootstrap--表单控件(disabled表单禁用、显示表单验证的样式)
1.disabled: (1)在input中加入disabled可使表单禁用,如图: <input class="form-control input-lg" id=&quo ...
- WPF - 使用Microsoft.Win32.OpenFileDialog打开文件,使用Microsoft.Win32.SaveFileDialog将文件另存
1. WPF 使用这个方法打开文件,很方便,而且可以记住上次打开的路径. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.W ...
- lucene3.6.0 经典案例 入门教程
第一步:下载并导入lucene的核心包(注意版本问题): 例如Lucene3.6版本:将lucene-core-3.6.0.jar拷贝到项目的libs 文件夹里. 例如Lucene4.6版本:将l ...
- qt下面例子学习(部分功能)
from aa import Ui_Formfrom PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *from ...
- MySQL具体解释(7)-----------MySQL线程池总结(一)
线程池是Mysql5.6的一个核心功能.对于server应用而言,不管是web应用服务还是DB服务,高并发请求始终是一个绕不开的话题.当有大量请求并发訪问时,一定伴随着资源的不断创建和释放.导致资源利 ...
- AVR32开发环境搭建
下面是搭建AVR32开发环境的过程记录: 1.AVR32的编译环境下载 (到这里下载 as5installer-stable-5.1.208-full.exe) 如果你在安装的过程中碰到如下问题: ...