算法学习笔记之——priority queue、heapsort、symbol table、binary search trees
Priority Queue
类似一个Queue,但是按照priority的大小顺序来出队
一般存在两种方式来实施
- 排序法(ordered),在元素入队时即进行排序,这样插入操作为O(N),但出队为O(1)
- 不排序法(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;
}
算法学习笔记之——priority queue、heapsort、symbol table、binary search trees的更多相关文章
- Johnson 全源最短路径算法学习笔记
Johnson 全源最短路径算法学习笔记 如果你希望得到带互动的极简文字体验,请点这里 我们来学习johnson Johnson 算法是一种在边加权有向图中找到所有顶点对之间最短路径的方法.它允许一些 ...
- C / C++算法学习笔记(8)-SHELL排序
原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...
- Manacher算法学习笔记 | LeetCode#5
Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...
- Johnson算法学习笔记
\(Johnson\)算法学习笔记. 在最短路的学习中,我们曾学习了三种最短路的算法,\(Bellman-Ford\)算法及其队列优化\(SPFA\)算法,\(Dijkstra\)算法.这些算法可以快 ...
- 某科学的PID算法学习笔记
最近,在某社团的要求下,自学了PID算法.学完后,深切地感受到PID算法之强大.PID算法应用广泛,比如加热器.平衡车.无人机等等,是自动控制理论中比较容易理解但十分重要的算法. 下面是博主学习过程中 ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- 二次剩余Cipolla算法学习笔记
对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
- 算法学习笔记——sort 和 qsort 提供的快速排序
这里存放的是笔者在学习算法和数据结构时相关的学习笔记,记录了笔者通过网络和书籍资料中学习到的知识点和技巧,在供自己学习和反思的同时为有需要的人提供一定的思路和帮助. 从排序开始 基本的排序算法包括冒泡 ...
- R语言实现关联规则与推荐算法(学习笔记)
R语言实现关联规则 笔者前言:以前在网上遇到很多很好的关联规则的案例,最近看到一个更好的,于是便学习一下,写个学习笔记. 1 1 0 0 2 1 1 0 0 3 1 1 0 1 4 0 0 0 0 5 ...
随机推荐
- 13、Semantic-UI之表格与表单
13.1 定义基础样式表格 在HTML中可以通过table进行表格定义,在Semantic-UI中也可以通过class="ui table"定义表格. 示例:定义基础表格 &l ...
- 彻底的卸载SQL Server2005
昨天不小心把SQL Server 2005弄坏了.所以,对彻底卸载他进行了研究.在这里记录一下,嘿嘿~ 1.首先在控制面板中卸载所有与SQL Server 2005相关的程序,要记得最后一个卸载Mic ...
- [LeetCode 题解]:Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【Newtonsoft.Json.dll】操作列表JSON数据
JObject data = JObject.Parse(json); JArray array = JArray.Parse(data["list"] + "" ...
- CentOS下Docker与.netcore(三)之 三剑客之一Docker-Compose
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客 ...
- Windows上编译libjpeg
通常libjpeg可以使用如下命令行生成Visual Studio 2010的项目文件: nmake /f makefile.vc setup-v10 但可惜我们使用的是Visual Studio 2 ...
- Android理解:Activity状态和生命周期
http://blog.csdn.net/xiao__gui/article/details/11464603
- day12学python 多进程+进程池
多进程+进程池 多进程(不同进程不可直接访问数据) 引入(多进程套线程) 多进程 需导入multiprocessing模块 模板示例1 import threading,time,multiproce ...
- offsetWidth和width的区别
1.offsetWidth属性可以返回对象的padding+border+width属性值之和,style.width返回值就是定义的width属性值. 2.offsetWidth属性仅是可读属性,而 ...
- pc端美化select,jquery获取select中的option的text值
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...