# 数据结构

## 数据结构和基本实现

### 数据存储

总结一下一些常用数据结构，和它们支持的查找顺序。

| 数据结构                                                                                                                                                                                                                     | 支持写查删                                                                                                                        | 可以的实现                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------- |
| <p><a href="shu-ju-jie-gou/ji-chu-shu-ju-jie-gou/untitled-1">Stack/Queue</a></p><p><a href="shu-ju-jie-gou/ji-chu-shu-ju-jie-gou/untitled-1">栈/队列</a></p>                                                                | `O(1)`**首尾**写查删**value**                                                                                                     | <p>LinkedList</p><p>链表</p>      |
| <p><a href="shu-ju-jie-gou/ji-chu-shu-ju-jie-gou/sortedlistsortedset-er-cha-shu-shi-xian">SortedContainers</a></p><p><a href="shu-ju-jie-gou/ji-chu-shu-ju-jie-gou/sortedlistsortedset-er-cha-shu-shi-xian">有序结构</a></p> | <p><code>O(logn)</code>写查删<strong>value</strong></p><p><code>O(logn)</code>查<strong>value</strong>→<strong>rank</strong></p> | <p>BalancedTree</p><p>二叉平衡树</p> |
| <p><a href="shu-ju-jie-gou/ji-chu-shu-ju-jie-gou/ha-xi-ji-he-he-ha-xi-ying-she">Set/Dict</a></p><p><a href="shu-ju-jie-gou/ji-chu-shu-ju-jie-gou/ha-xi-ji-he-he-ha-xi-ying-she">集合/字典</a></p>                            | `O(1)`写查&#x5220;**(key, value)**                                                                                             | <p>HashTable</p><p>哈希表</p>      |

基本上所有的数据类型都是建立在`List`和`LinkedList`的基础上。Python中有些方便的库能支持不同的查找顺序，下面也有一些简单的基于`List`或`LinkedList`的实现。

### 支持快速写查删数据结构

还有一些衍生数据结构，在一些场景很有用：

| Data Structure                                                                                                                                                                                                                     | Function                                                               | 可以的实现                                  |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | -------------------------------------- |
| <p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/tan-xin-suan-fa-de-peng-you-you-xian-dui-lie">Binary Heap</a></p><p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/tan-xin-suan-fa-de-peng-you-you-xian-dui-lie">二叉堆</a></p> | <p><code>O(1)</code> 查找最大最小值</p><p><code>O(logn)</code> 插入/删除最大最小值</p> | <p>Complete Binary Tree</p><p>满二叉树</p> |
| <p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/ordereddict-ha-xi-lian-biao-shi-xian">OrderedDict</a></p><p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/ordereddict-ha-xi-lian-biao-shi-xian">有序字典</a></p>                | `O(1)`查找首尾(key, value)                                                 | <p>LinkedHashmap</p><p>哈希链表</p>        |
| <p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/zhao-lin-ju-de-bang-shou-dan-tiao-zhan">Monotonic Stack</a></p><p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/zhao-lin-ju-de-bang-shou-dan-tiao-zhan">单调栈</a></p>         | <p><code>O(n)</code> 查找数组每一位的</p><p>nextGreater/nextSmaller元素</p>      | <p>Stack</p><p>栈</p>                   |
| <p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/jian-zhi-zhu-shou-qian-zhui-shu">Trie</a></p><p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/jian-zhi-zhu-shou-qian-zhui-shu">前缀树</a></p>                                  | <p><code>O(n)</code> 查找字符串是词典中的</p><p>前缀/单词</p>                        | <p>Dict->Dict</p><p>字典</p>             |
| <p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/kuai-cha-lian-tong-tu-bing-cha-ji">UnionFind</a></p><p><a href="shu-ju-jie-gou/yan-sheng-shu-ju-jie-gou/kuai-cha-lian-tong-tu-bing-cha-ji">并查集</a></p>                         | `O(1)` 查找图的连通性                                                         | <p>N-array Tree</p><p>多叉树的森林</p>       |
