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:

  1. If too many elements were hashed into the same key: looking inside this key may take O(n) time.
  2. 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的更多相关文章

  1. 【DataStructure】Linked Data Structures

    Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. ...

  2. A library of generic data structures

    A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...

  3. 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 ...

  4. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  5. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  6. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

  7. Choose Concurrency-Friendly Data Structures

    What is a high-performance data structure? To answer that question, we're used to applying normal co ...

  8. 无锁数据结构(Lock-Free Data Structures)

    一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Serve ...

  9. [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 ...

随机推荐

  1. 关于css的一些知识点整理

    一.标签的类型:   行内:span.a.b.i.strong.em.   1.共处一行   2.不支持设置宽高 display:block; 转换成块 块:h1-h6 p div  ul ol 1. ...

  2. GnuPG 的PGP使用

    1. 生成秘钥对(此处采用默认的RSA, 2048位) $ gpg --gen-key gpg (GnuPG) ; Copyright (C) Free Software Foundation, In ...

  3. 仿souhu页面设计

    仿搜狐页面设计 Html页面设计代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...

  4. Hadoop笔记HDFS(2)

    高级Hadoop MapReduce管理 1 调试部署好的Hadoop的配置 2 运行基准测试检验Hadoop的安装 3 重新利用JVM提升性能 4 容错性 5 调试脚本-分析失败任务原因 6 设置失 ...

  5. SVN错误及处理

    SVN无法读取current修复方法 Can't read file : End of file found 文件:repository/db/txn_current.repository/db/cu ...

  6. C#的多态性

    参考网址:http://www.cnblogs.com/zhangkai2237/archive/2012/12/20/2826734.html 多态的定义:同一操作作用于不同的对象,可以有不同的解释 ...

  7. python的反射机制

    转载自:http://www.cnblogs.com/feixuelove1009/p/5576206.html 对编程语言比较熟悉的朋友,应该知道"反射"这个机制.Python作 ...

  8. 深入浅出设计模式——代理模式(Proxy Pattern)

    模式动机在某些情况下,一个客户不想或者不能直接引用一个对象,此时可以通过一个称之为“代理”的第三者来实现间接引用.代理对象可以在客户端和目标对象之间起到中介的作用,并且可以通过代理对象去掉客户不能看到 ...

  9. (原创)LAMP搭建之一:图解如何安装并检查LAMP

    LAMP搭建之一:图解如何安装并检查LAMP 第一步:安装Linux(RedHat5) 第二步:rpm -qa httpd(查看apache是否安装) rpm -qa php(查看php是否安装) r ...

  10. Oracle的控制文件

    一.控制文件 oracle的控制文件是极其重要的文件,它是一个较小的二进制文件. 记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等等  在参数文件中描述其位置, ...