当前位置:   article > 正文

算法数据结构(三十三)----有序表_数据结构有序表

数据结构有序表

 二叉树

定义:二叉树是每个结点最多有两个子树的树结构。
性质:
    1)二叉树的每个结点至多只有二棵子树(不存在度大于2的结点
    2)二叉树的子树有左右之分,次序不能颠倒
    3)二叉树的第i层至多有2^{i-1}个结点
    4)深度为k的二叉树至多有2^k-1个结点

 满二叉树

定义:一棵深度为k,且有2^k-1个节点的二叉树,称为满二叉树。
性质:每一层上的节点数都是最大节点数

 完全二叉树

定义:一棵二叉树中,除最后一层外,若其余层都是满的,并且最后一层或者是满的,或者是在右边缺少连续若干节点
性质:
    1)具有n个节点的完全二叉树的深度为log2(n+1)
    2)深度为k的完全二叉树,至少有2^(k-1)个节点,至多有2^k-1个节点

 线索二叉树

定义:利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为”线索”)

 哈夫曼树(最优二叉树)

定义:哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近

 二叉搜索树/二叉排序树/二叉查找树

定义:它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值

 平衡二叉树

定义:平衡二叉树又称AVL树,它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1

 伸展树

定义:伸展树是在查询或删除时对二叉查找树进行伸展操作,并保证从空树开始任意连续M次对树的操作最多花费O(MlogN)的时间。对二叉查找树进行伸展的意义是将访问路径上的节点尽可能的推向离树根最近的地方,有益于在下次访问时用时最少。通常一个节点被访问时,很可能不久会被再次访问。

红黑树

定义:是一种自平衡二叉查找树,其性质决定了最大一条路径节点个数不会超过最小的两倍
1)性质1. 节点是红色或黑色。
2)性质2. 根节点是黑色。
3)性质3 每个叶节点(NIL节点,空节点)是黑色的。
4)性质4 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
5)性质5. 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。

 B树

定义:二叉搜索树
性质:
    1)所有非叶子结点至多拥有两个儿子(Left和Right);
    2)所有结点存储一个关键字;
    3)非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树;

 B-树

定义:是一种多路搜索树

 B+树

定义:B+树是B-树的变体,也是一种多路搜索树,所有关键字都在叶子结点出现

 B*树

定义:是B+树的变体,在B+树的非根和非叶子结点再增加指向兄弟的指针,节点分裂优先给兄弟节点


 搜索二叉树

搜索二叉树一定要说明以什么标准来排序

经典的搜索二叉树,树上没有重复的用来排序的key

如果有重复节点的需求,可以在一个节点内部增加数据项

 搜索二叉树查询key(查询某个key存在还是不存在)

1)如果当前节点的value==key,返回true

2)如果当前节点的value<key,当前节点向左移动

3)如果当前节点的value>key,当前节点向右移动

4)如果当前节点变成null,返回false

 搜索二叉树插入新的key

和查询过程一样,但当前节点滑到空的时候,就插入在这里

 搜索二叉树删除key

0)先找到key所在的节点

1)如果该节点没有左孩子、没有右孩子,直接删除即可

2)如果该节点有左孩子、没有右孩子,直接用左孩子顶替该节点

3)如果该节点没有左孩子、有右孩子,直接用右孩子顶替该节点

4)如果该节点有左孩子、有右孩子,用该节点后继节点顶替该节点

 搜索二叉树特别不讲究

1)基础的搜索二叉树,添加、删除时候不照顾平衡性

2)数据状况很差时,性能就很差

给搜索二叉树引入两个动作:左旋、右旋

 


  AVL树、SB树、红黑树的共性:给搜索二叉树引入两个动作:左旋、右旋,调整平衡性

1)都是搜索二叉树

2)插入、删除、查询(一切查询)搜索二叉树怎么做,这些结构都这么做

3)使用调整的基本动作都只有左旋、右旋

4)插入、删除时,从最底层被影响到的节点开始,对往上路径的节点做平衡性检查

5)因为只对一条向上路径的每个节点做O(1)的检查和调整,所以可以做到O(logN)

 AVL树、SB树、红黑树的不同

1)平衡性的约束不同

AVL树最严格、SB树稍宽松、红黑树最宽松

2)插入、删除和搜索二叉树一样,但是额外,做各自的平衡性调整。各自的平衡性调整所使用的动作都是左旋或者右旋


 AVL

1)最严格的平衡性,任何节点左树高度和右树高度差不超过1

2)往上沿途检查每个节点时,都去检查四种违规情况:LLRRLRRL

3)不同情况虽然看起来复杂,但是核心点是:

LL(做一次右旋)、RR(做一次左旋)

LRRL(利用旋转让底层那个上到顶部)

  1. //定义结构
  2. public static class AVLNode<K extends Comparable<K>, V> {
  3. public K k;
  4. public V v;
  5. public AVLNode<K, V> l;
  6. public AVLNode<K, V> r;
  7. public int h;
  8. public AVLNode(K key, V value) {
  9. k = key;
  10. v = value;
  11. h = 1;
  12. }
  13. }
  14. public static class AVLTreeMap<K extends Comparable<K>, V> {
  15. private AVLNode<K, V> root;
  16. private int size;
  17. public AVLTreeMap() {
  18. root = null;
  19. size = 0;
  20. }
  21. //右旋
  22. private AVLNode<K, V> rightRotate(AVLNode<K, V> cur) {
  23. AVLNode<K, V> left = cur.l;
  24. cur.l = left.r;
  25. left.r = cur;
  26. cur.h = Math.max((cur.l != null ? cur.l.h : 0), (cur.r != null ? cur.r.h : 0)) + 1;
  27. left.h = Math.max((left.l != null ? left.l.h : 0), (left.r != null ? left.r.h : 0)) + 1;
  28. return left;
  29. }
  30. //左旋
  31. private AVLNode<K, V> leftRotate(AVLNode<K, V> cur) {
  32. AVLNode<K, V> right = cur.r;
  33. cur.r = right.l;
  34. right.l = cur;
  35. cur.h = Math.max((cur.l != null ? cur.l.h : 0), (cur.r != null ? cur.r.h : 0)) + 1;
  36. right.h = Math.max((right.l != null ? right.l.h : 0), (right.r != null ? right.r.h : 0)) + 1;
  37. return right;
  38. }
  39. //调整平衡性
  40. private AVLNode<K, V> maintain(AVLNode<K, V> cur) {
  41. if (cur == null) {
  42. return null;
  43. }
  44. int leftHeight = cur.l != null ? cur.l.h : 0;
  45. int rightHeight = cur.r != null ? cur.r.h : 0;
  46. if (Math.abs(leftHeight - rightHeight) > 1) {
  47. if (leftHeight > rightHeight) {
  48. int leftLeftHeight = cur.l != null && cur.l.l != null ? cur.l.l.h : 0;
  49. int leftRightHeight = cur.l != null && cur.l.r != null ? cur.l.r.h : 0;
  50. if (leftLeftHeight >= leftRightHeight) {
  51. cur = rightRotate(cur);
  52. } else {
  53. cur.l = leftRotate(cur.l);
  54. cur = rightRotate(cur);
  55. }
  56. } else {
  57. int rightLeftHeight = cur.r != null && cur.r.l != null ? cur.r.l.h : 0;
  58. int rightRightHeight = cur.r != null && cur.r.r != null ? cur.r.r.h : 0;
  59. if (rightRightHeight >= rightLeftHeight) {
  60. cur = leftRotate(cur);
  61. } else {
  62. cur.r = rightRotate(cur.r);
  63. cur = leftRotate(cur);
  64. }
  65. }
  66. }
  67. return cur;
  68. }
  69. private AVLNode<K, V> findLastIndex(K key) {
  70. AVLNode<K, V> pre = root;
  71. AVLNode<K, V> cur = root;
  72. while (cur != null) {
  73. pre = cur;
  74. if (key.compareTo(cur.k) == 0) {
  75. break;
  76. } else if (key.compareTo(cur.k) < 0) {
  77. cur = cur.l;
  78. } else {
  79. cur = cur.r;
  80. }
  81. }
  82. return pre;
  83. }
  84. private AVLNode<K, V> findLastNoSmallIndex(K key) {
  85. AVLNode<K, V> ans = null;
  86. AVLNode<K, V> cur = root;
  87. while (cur != null) {
  88. if (key.compareTo(cur.k) == 0) {
  89. ans = cur;
  90. break;
  91. } else if (key.compareTo(cur.k) < 0) {
  92. ans = cur;
  93. cur = cur.l;
  94. } else {
  95. cur = cur.r;
  96. }
  97. }
  98. return ans;
  99. }
  100. private AVLNode<K, V> findLastNoBigIndex(K key) {
  101. AVLNode<K, V> ans = null;
  102. AVLNode<K, V> cur = root;
  103. while (cur != null) {
  104. if (key.compareTo(cur.k) == 0) {
  105. ans = cur;
  106. break;
  107. } else if (key.compareTo(cur.k) < 0) {
  108. cur = cur.l;
  109. } else {
  110. ans = cur;
  111. cur = cur.r;
  112. }
  113. }
  114. return ans;
  115. }
  116. //添加节点
  117. private AVLNode<K, V> add(AVLNode<K, V> cur, K key, V value) {
  118. if (cur == null) {
  119. return new AVLNode<K, V>(key, value);
  120. } else {
  121. if (key.compareTo(cur.k) < 0) {
  122. cur.l = add(cur.l, key, value);
  123. } else {
  124. cur.r = add(cur.r, key, value);
  125. }
  126. cur.h = Math.max(cur.l != null ? cur.l.h : 0, cur.r != null ? cur.r.h : 0) + 1;
  127. return maintain(cur);
  128. }
  129. }
  130. // 在cur这棵树上,删掉key所代表的节点
  131. // 返回cur这棵树的新头部
  132. private AVLNode<K, V> delete(AVLNode<K, V> cur, K key) {
  133. if (key.compareTo(cur.k) > 0) {
  134. cur.r = delete(cur.r, key);
  135. } else if (key.compareTo(cur.k) < 0) {
  136. cur.l = delete(cur.l, key);
  137. } else {
  138. if (cur.l == null && cur.r == null) {
  139. cur = null;
  140. } else if (cur.l == null && cur.r != null) {
  141. cur = cur.r;
  142. } else if (cur.l != null && cur.r == null) {
  143. cur = cur.l;
  144. } else {
  145. AVLNode<K, V> des = cur.r;
  146. while (des.l != null) {
  147. des = des.l;
  148. }
  149. cur.r = delete(cur.r, des.k);
  150. des.l = cur.l;
  151. des.r = cur.r;
  152. cur = des;
  153. }
  154. }
  155. if (cur != null) {
  156. cur.h = Math.max(cur.l != null ? cur.l.h : 0, cur.r != null ? cur.r.h : 0) + 1;
  157. }
  158. return maintain(cur);
  159. }
  160. public int size() {
  161. return size;
  162. }
  163. //是否包含某个key
  164. public boolean containsKey(K key) {
  165. if (key == null) {
  166. return false;
  167. }
  168. AVLNode<K, V> lastNode = findLastIndex(key);
  169. return lastNode != null && key.compareTo(lastNode.k) == 0 ? true : false;
  170. }
  171. public void put(K key, V value) {
  172. if (key == null) {
  173. return;
  174. }
  175. AVLNode<K, V> lastNode = findLastIndex(key);
  176. if (lastNode != null && key.compareTo(lastNode.k) == 0) {
  177. lastNode.v = value;
  178. } else {
  179. size++;
  180. root = add(root, key, value);
  181. }
  182. }
  183. public void remove(K key) {
  184. if (key == null) {
  185. return;
  186. }
  187. if (containsKey(key)) {
  188. size--;
  189. root = delete(root, key);
  190. }
  191. }
  192. public V get(K key) {
  193. if (key == null) {
  194. return null;
  195. }
  196. AVLNode<K, V> lastNode = findLastIndex(key);
  197. if (lastNode != null && key.compareTo(lastNode.k) == 0) {
  198. return lastNode.v;
  199. }
  200. return null;
  201. }
  202. public K firstKey() {
  203. if (root == null) {
  204. return null;
  205. }
  206. AVLNode<K, V> cur = root;
  207. while (cur.l != null) {
  208. cur = cur.l;
  209. }
  210. return cur.k;
  211. }
  212. public K lastKey() {
  213. if (root == null) {
  214. return null;
  215. }
  216. AVLNode<K, V> cur = root;
  217. while (cur.r != null) {
  218. cur = cur.r;
  219. }
  220. return cur.k;
  221. }
  222. public K floorKey(K key) {
  223. if (key == null) {
  224. return null;
  225. }
  226. AVLNode<K, V> lastNoBigNode = findLastNoBigIndex(key);
  227. return lastNoBigNode == null ? null : lastNoBigNode.k;
  228. }
  229. public K ceilingKey(K key) {
  230. if (key == null) {
  231. return null;
  232. }
  233. AVLNode<K, V> lastNoSmallNode = findLastNoSmallIndex(key);
  234. return lastNoSmallNode == null ? null : lastNoSmallNode.k;
  235. }
  236. }

 SB树(size-balance-tree

1)让每一个叔叔节点为头的数,节点个数都不少于其任何一个侄子节点

2)也是从底层被影响节点开始向上做路径每个节点检查

3)与AVL树非常像,也是四种违规类型:LLRRLRRL

4)与AVL树非常像,核心点是:

LL(做一次右旋)、RR(做一次左旋)

LRRL(利用旋转让底层那个上到顶部)

5)与AVL树不同的是,每轮经过调整后,谁的孩子发生变化了,谁就再查

 SB树在使用时候的改进

1)删除时候可以不用检查

2)就把平衡性的调整放在插入的时候

3)因为这种只要变就递归的特性,别的树没有

4)可以在节点上封装别的数据项,来增加功能 

  1. public static class SBTNode<K extends Comparable<K>, V> {
  2. public K key;
  3. public V value;
  4. public SBTNode<K, V> l;
  5. public SBTNode<K, V> r;
  6. public int size; // 不同的key的数量
  7. public SBTNode(K key, V value) {
  8. this.key = key;
  9. this.value = value;
  10. size = 1;
  11. }
  12. }
  13. public static class SizeBalancedTreeMap<K extends Comparable<K>, V> {
  14. private SBTNode<K, V> root;
  15. private SBTNode<K, V> rightRotate(SBTNode<K, V> cur) {
  16. SBTNode<K, V> leftNode = cur.l;
  17. cur.l = leftNode.r;
  18. leftNode.r = cur;
  19. leftNode.size = cur.size;
  20. cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
  21. return leftNode;
  22. }
  23. private SBTNode<K, V> leftRotate(SBTNode<K, V> cur) {
  24. SBTNode<K, V> rightNode = cur.r;
  25. cur.r = rightNode.l;
  26. rightNode.l = cur;
  27. rightNode.size = cur.size;
  28. cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
  29. return rightNode;
  30. }
  31. private SBTNode<K, V> maintain(SBTNode<K, V> cur) {
  32. if (cur == null) {
  33. return null;
  34. }
  35. int leftSize = cur.l != null ? cur.l.size : 0;
  36. int leftLeftSize = cur.l != null && cur.l.l != null ? cur.l.l.size : 0;
  37. int leftRightSize = cur.l != null && cur.l.r != null ? cur.l.r.size : 0;
  38. int rightSize = cur.r != null ? cur.r.size : 0;
  39. int rightLeftSize = cur.r != null && cur.r.l != null ? cur.r.l.size : 0;
  40. int rightRightSize = cur.r != null && cur.r.r != null ? cur.r.r.size : 0;
  41. if (leftLeftSize > rightSize) {
  42. cur = rightRotate(cur);
  43. cur.r = maintain(cur.r);
  44. cur = maintain(cur);
  45. } else if (leftRightSize > rightSize) {
  46. cur.l = leftRotate(cur.l);
  47. cur = rightRotate(cur);
  48. cur.l = maintain(cur.l);
  49. cur.r = maintain(cur.r);
  50. cur = maintain(cur);
  51. } else if (rightRightSize > leftSize) {
  52. cur = leftRotate(cur);
  53. cur.l = maintain(cur.l);
  54. cur = maintain(cur);
  55. } else if (rightLeftSize > leftSize) {
  56. cur.r = rightRotate(cur.r);
  57. cur = leftRotate(cur);
  58. cur.l = maintain(cur.l);
  59. cur.r = maintain(cur.r);
  60. cur = maintain(cur);
  61. }
  62. return cur;
  63. }
  64. private SBTNode<K, V> findLastIndex(K key) {
  65. SBTNode<K, V> pre = root;
  66. SBTNode<K, V> cur = root;
  67. while (cur != null) {
  68. pre = cur;
  69. if (key.compareTo(cur.key) == 0) {
  70. break;
  71. } else if (key.compareTo(cur.key) < 0) {
  72. cur = cur.l;
  73. } else {
  74. cur = cur.r;
  75. }
  76. }
  77. return pre;
  78. }
  79. private SBTNode<K, V> findLastNoSmallIndex(K key) {
  80. SBTNode<K, V> ans = null;
  81. SBTNode<K, V> cur = root;
  82. while (cur != null) {
  83. if (key.compareTo(cur.key) == 0) {
  84. ans = cur;
  85. break;
  86. } else if (key.compareTo(cur.key) < 0) {
  87. ans = cur;
  88. cur = cur.l;
  89. } else {
  90. cur = cur.r;
  91. }
  92. }
  93. return ans;
  94. }
  95. private SBTNode<K, V> findLastNoBigIndex(K key) {
  96. SBTNode<K, V> ans = null;
  97. SBTNode<K, V> cur = root;
  98. while (cur != null) {
  99. if (key.compareTo(cur.key) == 0) {
  100. ans = cur;
  101. break;
  102. } else if (key.compareTo(cur.key) < 0) {
  103. cur = cur.l;
  104. } else {
  105. ans = cur;
  106. cur = cur.r;
  107. }
  108. }
  109. return ans;
  110. }
  111. // 现在,以cur为头的树上,新增,加(key, value)这样的记录
  112. // 加完之后,会对cur做检查,该调整调整
  113. // 返回,调整完之后,整棵树的新头部
  114. private SBTNode<K, V> add(SBTNode<K, V> cur, K key, V value) {
  115. if (cur == null) {
  116. return new SBTNode<K, V>(key, value);
  117. } else {
  118. cur.size++;
  119. if (key.compareTo(cur.key) < 0) {
  120. cur.l = add(cur.l, key, value);
  121. } else {
  122. cur.r = add(cur.r, key, value);
  123. }
  124. return maintain(cur);
  125. }
  126. }
  127. // 在cur这棵树上,删掉key所代表的节点
  128. // 返回cur这棵树的新头部
  129. private SBTNode<K, V> delete(SBTNode<K, V> cur, K key) {
  130. cur.size--;
  131. if (key.compareTo(cur.key) > 0) {
  132. cur.r = delete(cur.r, key);
  133. } else if (key.compareTo(cur.key) < 0) {
  134. cur.l = delete(cur.l, key);
  135. } else { // 当前要删掉cur
  136. if (cur.l == null && cur.r == null) {
  137. // free cur memory -> C++
  138. cur = null;
  139. } else if (cur.l == null && cur.r != null) {
  140. // free cur memory -> C++
  141. cur = cur.r;
  142. } else if (cur.l != null && cur.r == null) {
  143. // free cur memory -> C++
  144. cur = cur.l;
  145. } else { // 有左有右
  146. SBTNode<K, V> pre = null;
  147. SBTNode<K, V> des = cur.r;
  148. des.size--;
  149. while (des.l != null) {
  150. pre = des;
  151. des = des.l;
  152. des.size--;
  153. }
  154. if (pre != null) {
  155. pre.l = des.r;
  156. des.r = cur.r;
  157. }
  158. des.l = cur.l;
  159. des.size = des.l.size + (des.r == null ? 0 : des.r.size) + 1;
  160. // free cur memory -> C++
  161. cur = des;
  162. }
  163. }
  164. // cur = maintain(cur);
  165. return cur;
  166. }
  167. private SBTNode<K, V> getIndex(SBTNode<K, V> cur, int kth) {
  168. if (kth == (cur.l != null ? cur.l.size : 0) + 1) {
  169. return cur;
  170. } else if (kth <= (cur.l != null ? cur.l.size : 0)) {
  171. return getIndex(cur.l, kth);
  172. } else {
  173. return getIndex(cur.r, kth - (cur.l != null ? cur.l.size : 0) - 1);
  174. }
  175. }
  176. public int size() {
  177. return root == null ? 0 : root.size;
  178. }
  179. public boolean containsKey(K key) {
  180. if (key == null) {
  181. throw new RuntimeException("invalid parameter.");
  182. }
  183. SBTNode<K, V> lastNode = findLastIndex(key);
  184. return lastNode != null && key.compareTo(lastNode.key) == 0 ? true : false;
  185. }
  186. // (key,value) put -> 有序表 新增、改value
  187. public void put(K key, V value) {
  188. if (key == null) {
  189. throw new RuntimeException("invalid parameter.");
  190. }
  191. SBTNode<K, V> lastNode = findLastIndex(key);
  192. if (lastNode != null && key.compareTo(lastNode.key) == 0) {
  193. lastNode.value = value;
  194. } else {
  195. root = add(root, key, value);
  196. }
  197. }
  198. public void remove(K key) {
  199. if (key == null) {
  200. throw new RuntimeException("invalid parameter.");
  201. }
  202. if (containsKey(key)) {
  203. root = delete(root, key);
  204. }
  205. }
  206. public K getIndexKey(int index) {
  207. if (index < 0 || index >= this.size()) {
  208. throw new RuntimeException("invalid parameter.");
  209. }
  210. return getIndex(root, index + 1).key;
  211. }
  212. public V getIndexValue(int index) {
  213. if (index < 0 || index >= this.size()) {
  214. throw new RuntimeException("invalid parameter.");
  215. }
  216. return getIndex(root, index + 1).value;
  217. }
  218. public V get(K key) {
  219. if (key == null) {
  220. throw new RuntimeException("invalid parameter.");
  221. }
  222. SBTNode<K, V> lastNode = findLastIndex(key);
  223. if (lastNode != null && key.compareTo(lastNode.key) == 0) {
  224. return lastNode.value;
  225. } else {
  226. return null;
  227. }
  228. }
  229. public K firstKey() {
  230. if (root == null) {
  231. return null;
  232. }
  233. SBTNode<K, V> cur = root;
  234. while (cur.l != null) {
  235. cur = cur.l;
  236. }
  237. return cur.key;
  238. }
  239. public K lastKey() {
  240. if (root == null) {
  241. return null;
  242. }
  243. SBTNode<K, V> cur = root;
  244. while (cur.r != null) {
  245. cur = cur.r;
  246. }
  247. return cur.key;
  248. }
  249. public K floorKey(K key) {
  250. if (key == null) {
  251. throw new RuntimeException("invalid parameter.");
  252. }
  253. SBTNode<K, V> lastNoBigNode = findLastNoBigIndex(key);
  254. return lastNoBigNode == null ? null : lastNoBigNode.key;
  255. }
  256. public K ceilingKey(K key) {
  257. if (key == null) {
  258. throw new RuntimeException("invalid parameter.");
  259. }
  260. SBTNode<K, V> lastNoSmallNode = findLastNoSmallIndex(key);
  261. return lastNoSmallNode == null ? null : lastNoSmallNode.key;
  262. }
  263. }

聊聊红黑树

1)平衡性规定非常诡异

2)平衡性调整最为复杂

3)优点在于每次插入删除扰动较好,但是在今天看来这个优势也极其微弱了

原因:贪图扰动小的话,B+树、2-3-4树可能更好,还是那句话,到底图什么

4)除此之外,红黑树并不比AVL树、SB树、跳表更加优秀

5)面试上遇到,说清楚道理,不行就举报。。。


 跳表(skiplist

1)结构上根本和搜索二叉树无关

2)利用随机概率分布来使得高层索引可以无视数据规律,做到整体性能优良

3)思想是所有有序表中最先进的

4)结构简单就是多级单链表

  1. // 跳表的节点定义
  2. public static class SkipListNode<K extends Comparable<K>, V> {
  3. public K key;
  4. public V val;
  5. public ArrayList<SkipListNode<K, V>> nextNodes;
  6. public SkipListNode(K k, V v) {
  7. key = k;
  8. val = v;
  9. nextNodes = new ArrayList<SkipListNode<K, V>>();
  10. }
  11. // 遍历的时候,如果是往右遍历到的null(next == null), 遍历结束
  12. // 头(null), 头节点的null,认为最小
  13. // node -> 头,node(null, "") node.isKeyLess(!null) true
  14. // node里面的key是否比otherKey小,true,不是false
  15. public boolean isKeyLess(K otherKey) {
  16. // otherKey == null -> false
  17. return otherKey != null && (key == null || key.compareTo(otherKey) < 0);
  18. }
  19. public boolean isKeyEqual(K otherKey) {
  20. return (key == null && otherKey == null)
  21. || (key != null && otherKey != null && key.compareTo(otherKey) == 0);
  22. }
  23. }
  24. public static class SkipListMap<K extends Comparable<K>, V> {
  25. private static final double PROBABILITY = 0.5; // < 0.5 继续做,>=0.5 停
  26. private SkipListNode<K, V> head;
  27. private int size;
  28. private int maxLevel;
  29. public SkipListMap() {
  30. head = new SkipListNode<K, V>(null, null);
  31. head.nextNodes.add(null); // 0
  32. size = 0;
  33. maxLevel = 0;
  34. }
  35. // 从最高层开始,一路找下去,
  36. // 最终,找到第0层的<key的最右的节点
  37. private SkipListNode<K, V> mostRightLessNodeInTree(K key) {
  38. if (key == null) {
  39. return null;
  40. }
  41. int level = maxLevel;
  42. SkipListNode<K, V> cur = head;
  43. while (level >= 0) { // 从上层跳下层
  44. // cur level -> level-1
  45. cur = mostRightLessNodeInLevel(key, cur, level--);
  46. }
  47. return cur;
  48. }
  49. // 在level层里,如何往右移动
  50. // 现在来到的节点是cur,来到了cur的level层,在level层上,找到<key最后一个节点并返回
  51. private SkipListNode<K, V> mostRightLessNodeInLevel(K key,
  52. SkipListNode<K, V> cur,
  53. int level) {
  54. SkipListNode<K, V> next = cur.nextNodes.get(level);
  55. while (next != null && next.isKeyLess(key)) {
  56. cur = next;
  57. next = cur.nextNodes.get(level);
  58. }
  59. return cur;
  60. }
  61. public boolean containsKey(K key) {
  62. if (key == null) {
  63. return false;
  64. }
  65. SkipListNode<K, V> less = mostRightLessNodeInTree(key);
  66. SkipListNode<K, V> next = less.nextNodes.get(0);
  67. return next != null && next.isKeyEqual(key);
  68. }
  69. // 新增、改value
  70. public void put(K key, V value) {
  71. if (key == null) {
  72. return;
  73. }
  74. // 0层上,最右一个,< key 的Node -> >key
  75. SkipListNode<K, V> less = mostRightLessNodeInTree(key);
  76. SkipListNode<K, V> find = less.nextNodes.get(0);
  77. if (find != null && find.isKeyEqual(key)) {
  78. find.val = value;
  79. } else { // find == null 8 7 9
  80. size++;
  81. int newNodeLevel = 0;
  82. while (Math.random() < PROBABILITY) {
  83. newNodeLevel++;
  84. }
  85. // newNodeLevel
  86. while (newNodeLevel > maxLevel) {
  87. head.nextNodes.add(null);
  88. maxLevel++;
  89. }
  90. SkipListNode<K, V> newNode = new SkipListNode<K, V>(key, value);
  91. for (int i = 0; i <= newNodeLevel; i++) {
  92. newNode.nextNodes.add(null);
  93. }
  94. int level = maxLevel;
  95. SkipListNode<K, V> pre = head;
  96. while (level >= 0) {
  97. // level 层中,找到最右的 < key 的节点
  98. pre = mostRightLessNodeInLevel(key, pre, level);
  99. if (level <= newNodeLevel) {
  100. newNode.nextNodes.set(level, pre.nextNodes.get(level));
  101. pre.nextNodes.set(level, newNode);
  102. }
  103. level--;
  104. }
  105. }
  106. }
  107. public V get(K key) {
  108. if (key == null) {
  109. return null;
  110. }
  111. SkipListNode<K, V> less = mostRightLessNodeInTree(key);
  112. SkipListNode<K, V> next = less.nextNodes.get(0);
  113. return next != null && next.isKeyEqual(key) ? next.val : null;
  114. }
  115. public void remove(K key) {
  116. if (containsKey(key)) {
  117. size--;
  118. int level = maxLevel;
  119. SkipListNode<K, V> pre = head;
  120. while (level >= 0) {
  121. pre = mostRightLessNodeInLevel(key, pre, level);
  122. SkipListNode<K, V> next = pre.nextNodes.get(level);
  123. // 1)在这一层中,pre下一个就是key
  124. // 2)在这一层中,pre的下一个key是>要删除key
  125. if (next != null && next.isKeyEqual(key)) {
  126. // free delete node memory -> C++
  127. // level : pre -> next(key) -> ...
  128. pre.nextNodes.set(level, next.nextNodes.get(level));
  129. }
  130. // 在level层只有一个节点了,就是默认节点head
  131. if (level != 0 && pre == head && pre.nextNodes.get(level) == null) {
  132. head.nextNodes.remove(level);
  133. maxLevel--;
  134. }
  135. level--;
  136. }
  137. }
  138. }
  139. public K firstKey() {
  140. return head.nextNodes.get(0) != null ? head.nextNodes.get(0).key : null;
  141. }
  142. public K lastKey() {
  143. int level = maxLevel;
  144. SkipListNode<K, V> cur = head;
  145. while (level >= 0) {
  146. SkipListNode<K, V> next = cur.nextNodes.get(level);
  147. while (next != null) {
  148. cur = next;
  149. next = cur.nextNodes.get(level);
  150. }
  151. level--;
  152. }
  153. return cur.key;
  154. }
  155. public K ceilingKey(K key) {
  156. if (key == null) {
  157. return null;
  158. }
  159. SkipListNode<K, V> less = mostRightLessNodeInTree(key);
  160. SkipListNode<K, V> next = less.nextNodes.get(0);
  161. return next != null ? next.key : null;
  162. }
  163. public K floorKey(K key) {
  164. if (key == null) {
  165. return null;
  166. }
  167. SkipListNode<K, V> less = mostRightLessNodeInTree(key);
  168. SkipListNode<K, V> next = less.nextNodes.get(0);
  169. return next != null && next.isKeyEqual(key) ? next.key : less.key;
  170. }
  171. public int size() {
  172. return size;
  173. }
  174. }

有序表题目

题目一

 给定一个数组arr,和两个整数aba<=b

arr中有多少个子数组,累加和在[a,b]这个范围上

返回达标的子数组数量

 

         使用这种方式构建有序表:每一个节点第一个数字代表value,排序就根据这个value进行。第二个数字表示,自己+左孩子+右孩子value个数。如图代表3有3个,6有两个,5有2个

        这种结构,即可以接收重复值,还可以很快的算出,大于或者小于某个值的个数。

 题目二

有一个滑动窗口(讲过的):

1)L是滑动窗口最左位置、R是滑动窗口最右位置,一开始LR都在数组左侧

2)任何一步都可能R往右动,表示某个数进了窗口

3)任何一步都可能L往右动,表示某个数出了窗口

想知道每一个窗口状态的中位数

        与题目一相同的存储结构,剩下的就是R往右移,就往结构中加数据,L往右移就从结构总移出数据 

题目三

 设计一个结构包含如下两个方法:

void add(int index, int num):把num加入到index位置

int get(int index) :取出index位置的值

void remove(int index) :把index位置上的值删除

要求三个方法时间复杂度O(logN)

 

        设计一种数据结构,按照自然序排序:第一个数据为节点的value,第二个数字,表示该节点下有几个节点。

        如图,节点按自然序排序,所以图中记录的数据原始数据为:3 3 7 5 6


改写有序表的题目核心点 

1)分析增加什么数据项可以支持题目

2)有序表一定要保持内部参与排序的key不重复

3)增加这个数据项了,在平衡性调整时,保证这个数据项也能更新正确

4)做到上面3点,剩下就是搜索二叉树怎么实现你想要的接口的问题了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/858772
推荐阅读
相关标签
  

闽ICP备14008679号