[DS Basics] Data structures
1, LinkedList
composed of one and one Node: [data][next].
[head] -> [data][next] -> [data][next] -> [data][next] -> [null].
Empty linkedList: head == null.
V.S. Array DS: fast at insert/delete.
2, hashtable
“数组可以通过下标直接定位到相应的空间”,对就是这句,哈希表的做法其实很简单,就是把Key通过一个固定的算法函数,既所谓的哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间里,而当使用哈希表进行查询的时候,就是再次使用哈希函数将key转换为对应的数组下标,并定位到该空间获取value,如此一来,就可以充分利用到数组的定位性能进行数据定位。
![]()
Hash tables are O(1) average and amortized case complexity, however is suffers from O(n) worst case time complexity.
Hash tables suffer from O(n) worst time complexity due to two reasons:
- If too many elements were hashed into the same key: looking inside this key may take
O(n)time. - Once a hash table has passed its load balance - it has to rehash [create a new bigger table, and re-insert each element to the table].
HashMap<T>也是buckets的概念,但是如果你只override了T class的hashcode()方法返回constant,那么当你往这个HashMap里存放多个object的时候,它们都被hash到了同一个bucket,但它们还是会被当成不同的value来处理。所以只override hashcode()是不够的。
Collision resolution
大多数的hashtable implementations都有collision resolution strategy,所有的方法都会需要将keys(或指向key的指针)随同associated values也存到table中。
Separate chaining:
这种方法中each bucket has some sort of list of entries with the same index. 用Linked lists的Chained hash table很流行,有时会选择用ordered linked list。
3, Binary tree
Binary tree: at most 2 child nodes.
Full Binary tree: depth k and have 2^k -1 nodes.
Binary search tree: left child node< current node< right child node
Define BinaryNode:
class BinaryNode {
BinaryNode left;
BinaryNode right;
int element;
public BinaryNode(int element) {
this.element = element;
left = right = null;
}
}
Function to see if a tree is a BST:
public boolean isBST() {
return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean isBST2(BinaryNode currRoot, int low, int high) { //注意最低限和最高限,并且不断update它
if(currRoot.left != null) {
if(currRoot.left.element < low || currRoot.left.element > currRoot.element || !isBST2(currRoot.left, low, currRoot.element))
return false;
}
if(currRoot.right != null) {
if(currRoot.right.element > high || currRoot.right.element < currRoot.element || !isBST2(currRoot.right, currRoot.element, high))
return false;
}
return true;
}
Breadth-First-Search:[Queue], 这个算法就是要用Queue来实现, Java只有Queue interface, extends Collection
public void bfs() {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root== null) return;
queue.add(root);
while(!queue.isEmpty()) {
TreeNode node = queue.remove(); //LinkedList.remove() will remove the head of the linked list
System.out.print(node.val + " ");
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
}
Depth-First-Search:[Stack] Java有Stack class, implements List interface. 方法peek()返回top,方法pop()返回top并移除。
4, AVL tree
is a self balancing Binary Search Tree, In an AVL tree, the height of two sub-trees of ANY node can not differ more than 1. If any time the height differs more than 1, rebalancing is done to restore the property.
Rebalancing: LL, RR, LR, RL.
[DS Basics] Data structures的更多相关文章
- 【DataStructure】Linked Data Structures
Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. ...
- A library of generic data structures
A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- Choose Concurrency-Friendly Data Structures
What is a high-performance data structure? To answer that question, we're used to applying normal co ...
- 无锁数据结构(Lock-Free Data Structures)
一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Serve ...
- [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构
10.2 How would you design the data structures for a very large social network like Facebook or Linke ...
随机推荐
- GMF Q&A(1): 如何让palette支持拖拽(DnD)等10则
1,如何让palette支持拖拽(DnD) 在*PaletteFactory类中,把私有类NodeToolEntry 和LinkToolEntry的基类修改为PaletteToolEntry.并在构造 ...
- 手把手教iOS生成.a包及常见的问题
我的例子是打包MJRefresh 环境OS10.11.4, XCode Version 7.3.1 (7D1014) 生成.a如下: 创建新工程: 把下载的 MJRefresh-master加到你的 ...
- SQL疑难杂症【5 】大量数据查询的时候要考虑结果为空的情况
最近几天怪事儿出奇的多,同一个工单.同一个产品,在A线可以正常生产,但是在H线死活都无法生产,系统直接提示TimeOut,监控发现有一条SQL语句执行缓慢,Copy出来仔细查看,很简单的一条语句,如下 ...
- php : 工厂类演示
工厂类的目的: 通过类名, 动态创建该类的对象实例 <?php /* * 工厂类演示 */ class A{} class B{} // 工厂类: 有一个静态方法,通过该方法,能够获得指定类的对 ...
- Logistic回归模型和Python实现
回归分析是研究变量之间定量关系的一种统计学方法,具有广泛的应用. Logistic回归模型 线性回归 先从线性回归模型开始,线性回归是最基本的回归模型,它使用线性函数描述两个变量之间的关系,将连续或离 ...
- 今天的感悟,对于python中的list()与w3c教程
首先本来想百度一下python定义列表的时候用 list()与直接用[]有什么区别,其中没有找到相关直接资料,看到了W3c菜鸟教程中之前看到的tuple,不禁想起list(tuple)是用来将元组转换 ...
- STM32学习笔记(六) SysTick系统时钟滴答实验(stm32中断入门)
系统时钟滴答实验很不难,我就在面简单说下,但其中涉及到了STM32最复杂也是以后用途最广的外设-NVIC,如果说RCC是实时性所必须考虑的部分,那么NVIC就是stm32功能性实现的基础,NVIC的难 ...
- jQuery的环境配置
目前最新的版本,是 1.10.1 和 2.0.2,如果你需要引用到你线上的项目,就必须使用压缩版,去掉了注释和空白,是容量最小. 从CDN中载入JQuery,如Google中载入JQuery: htt ...
- JS历史
JavaScript伴随着互联网的发展一起发展.互联网周边技术的快速发展,刺激和推动了JavaScript语言的发展. 2006年,jQuery函数库诞生,作者为John Resig.jQuery为操 ...
- h.APR通道是个怎么回事
APR通道是Tomcat比较有特色的通道,在早期的JDK的NIO框架不成熟的时候,因为java的网络包的低效,Tomcat使用APR开源项目做网络IO,这样有效的缓解了java语言的不足,提供了一个高 ...