算法学习笔记之——priority queue、heapsort、symbol table、binary search trees

Priority Queue

类似一个Queue,但是按照priority的大小顺序来出队
一般存在两种方式来实施

  1. 排序法(ordered),在元素入队时即进行排序,这样插入操作为O(N),但出队为O(1)
  2. 不排序法(unordered),元素直接插入到后面,出队时先排序后提取,插入操作为O(1),出队为O(N)

采用二叉树

用队列模拟二叉树,root为a[1],子元素为a[2k]或a[2k+1]
父元素总是比子元素要大,提取max为a[1]
不符合规则的子元素(其value比父元素大)可以不断与父元素交换,直至符合要求;不符合规则的父元素(其value比子元素小)可不断与子元素中最大的元素交换,直至符合要求
插入操作:将元素插入到树的右下角,然后保证该元素符合要求
删除操作:将root与最后的元素对换,提出root,再将新的root规则化(下移)直至符合要求
删除与插入都是O(lg(N))

应用实例:基于事件的仿真(Event driven simulation)

堆排序(heapsort)

1. 将数组视为完全二叉树
2. 将二叉树从底往上构建maxheap,最终二叉树满足父元素不小于子元素
3. 将根元素(最大值)与最后的元素交换,并根元素出队(array的size-1),并将新的root下沉至符合要求
4. 重复3的操作直至根元素,即完成从小到大的排列

堆排序的操作时间与内存消耗都是角优的,为O(lg(N)),但是操作时间还是小于quicksort,而且排序是不稳定的(not stable)

符号表(symbol table)

Symbol table stores the information related about the symbol.

符号表将符号与其值对应起来,可以通过符号获取与更新其值,例如传递URL到DNS服务器来获取IP地址。

public class ST<Key extends Comparable<Key>, Value>
--------------------------------------------------------
ST() 								create an ordered symbol table
void put(Key key, Value val) 		put key-value pair into the table
Value get(Key key) 					value paired with key
void delete(Key key) 				remove key (and its value) from table
boolean contains(Key key) 			is there a value paired with key?
boolean isEmpty() 					is the table empty?
int size() 							number of key-value pairs
Key min() 							smallest key
Key max() 							largest key
Key floor(Key key) 					largest key less than or equal to key
Key ceiling(Key key) 				smallest key greater than or equal to key
int rank(Key key) 					number of keys less than key
Key select(int k) 					key of rank k
void deleteMin() 					delete smallest key
void deleteMax() 					delete largest key
int size(Key lo, Key hi) 			number of keys in [lo..hi]
Iterable<Key> keys(Key lo, Key hi) 	keys in [lo..hi], in sorted order
Iterable<Key> keys() 				all keys in the table, in sorted order
implementation search(GAC) insert(GAC) search(AVC) insert(AVC) ordered ops? operations on keys
sequential search(unordered list) N N N/2 N no equals()
binary search (ordered array) lg(N) N lg(N) N/2 yes compareTo()
BST N N 1.39lg(N) 1.39lg(N) next compareTo()

GAC: Guarantee average case
AVC: Average case
BST: Binary search trees

二叉排序树BST(Binary search trees)

一个BST要么为空,要么同时有左子树与右子数,字数可以为null
每个节点的key大于所有左子树元素,但是小于所有右子树的元素

插入操作程序如下,通过递归返回更新的子树来代替原来的树

public void put(Key key, Value val){ 	
	root = put(root, key, val); }

private Node put(Node x, Key key, Value val){
	if (x == null) return new Node(key, val);
	int cmp = key.compareTo(x.key);
	if (cmp < 0)
		x.left = put(x.left, key, val);
	else if (cmp > 0)
		x.right = put(x.right, key, val);
	else if (cmp == 0)
		x.val = val;
	return x;
}

BST的各种操作(search/insert/min/max/floor/ceiling/rank/select)用时都正比于数的高度

删除操作程序如下,操作用时为 \(\sqrt{N}\)

public void delete(Key key){ 
	root = delete(root, key); }

private Node delete(Node x, Key key) {
	if (x == null) return null;
	int cmp = key.compareTo(x.key);
	if (cmp < 0) x.left = delete(x.left, key);
	else if (cmp > 0) x.right = delete(x.right, key);
	else {
		if (x.right == null) return x.left;
		if (x.left == null) return x.right;

		Node t = x;
		x = min(t.right);
		x.right = deleteMin(t.right);
		x.left = t.left;
	}
	x.count = size(x.left) + size(x.right) + 1;
	return x;
}
posted @ 2018-09-17 10:59  落叶有声  阅读(191)  评论(0编辑  收藏  举报