{"id":10007081,"date":"2023-01-16T14:51:47","date_gmt":"2023-01-16T06:51:47","guid":{"rendered":"https:\/\/ityet.com\/?p=10007081"},"modified":"2023-01-16T14:51:47","modified_gmt":"2023-01-16T06:51:47","slug":"%e8%b7%b3%e8%b7%83%e8%a1%a8%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e4%b8%8e%e7%ae%97%e6%b3%95%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10007081","title":{"rendered":"\u8df3\u8dc3\u8868\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790"},"content":{"rendered":"<blockquote><p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u7eaa\u5353\u5fd7<\/p><\/blockquote>\n<p>\u76ee\u524d\u5e02\u9762\u4e0a\u5145\u65a5\u7740\u5927\u91cf\u5173\u4e8e\u8df3\u8dc3\u8868\u7ed3\u6784\u4e0eRedis\u7684\u6e90\u7801\u89e3\u6790\uff0c\u4f46\u662f\u7ecf\u8fc7\u957f\u671f\u89c2\u5bdf\u540e\u53d1\u73b0\u5927\u90fd\u53ea\u662f\u5728\u505c\u7559\u5728\u4ee3\u7801\u7684\u8868\u9762\uff0c\u800c\u6ca1\u6709\u7cfb\u7edf\u6027\u5730\u4ecb\u7ecd\u8df3\u8dc3\u8868\u7684\u7531\u6765\u4ee5\u53ca\u5404\u79cd\u5e38\u91cf\u7684\u7531\u6765\u3002\u4f5c\u4e3a\u4e00\u79cd\u6982\u7387\u6570\u636e\u7ed3\u6784\uff0c\u7406\u89e3\u5404\u79cd\u5e38\u91cf\u7684\u7531\u6765\u53ef\u4ee5\u66f4\u597d\u5730\u8fdb\u884c\u53d8\u5316\u5e76\u5e94\u7528\u5230\u9ad8\u6027\u80fd\u529f\u80fd\u5f00\u53d1\u4e2d\u3002\u672c\u6587\u6ca1\u6709\u91cd\u590d\u5730\u4ee5\u5bf9\u73b0\u6709\u4f18\u79c0\u5b9e\u73b0\u8fdb\u884c\u4ee3\u7801\u5206\u6790\uff0c\u800c\u662f\u901a\u8fc7\u5bf9\u8df3\u8dc3\u8868\u8fdb\u884c\u4e86\u7cfb\u7edf\u6027\u5730\u4ecb\u7ecd\u4e0e\u5f62\u5f0f\u5316\u5206\u6790\uff0c\u5e76\u7ed9\u51fa\u4e86\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u8df3\u8dc3\u8868\u6269\u5c55\u65b9\u5f0f\uff0c\u65b9\u4fbf\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u8df3\u8dc3\u8868\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u8df3\u8dc3\u8868[1,2,3]\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53d6\u4ee3\u5e73\u8861\u6811\u7684\u6982\u7387\u6570\u636e\u7ed3\u6784\u3002\u8df3\u8dc3\u8868\u62e5\u6709\u4e0e\u5e73\u8861\u6811\u76f8\u540c\u7684\u671f\u671b\u65f6\u95f4\u4e0a\u754c\uff0c\u5e76\u4e14\u66f4\u7b80\u5355\u3001\u66f4\u5feb\u3001\u662f\u7528\u66f4\u5c11\u7684\u7a7a\u95f4\u3002\u5728\u67e5\u627e\u4e0e\u5217\u8868\u7684\u7ebf\u6027\u64cd\u4f5c\u4e0a\uff0c\u6bd4\u5e73\u8861\u6811\u66f4\u5feb\uff0c\u5e76\u4e14\u66f4\u7b80\u5355\u3002<\/p>\n<p>\u6982\u7387\u5e73\u8861\u4e5f\u53ef\u4ee5\u88ab\u7528\u5728\u57fa\u4e8e\u6811\u7684\u6570\u636e\u7ed3\u6784[4]\u4e0a\uff0c\u4f8b\u5982\u6811\u5806\uff08Treap\uff09\u3002\u4e0e\u5e73\u8861\u4e8c\u53c9\u6811\u76f8\u540c\uff0c\u8df3\u8dc3\u8868\u4e5f\u5b9e\u73b0\u4e86\u4ee5\u4e0b\u4e24\u79cd\u64cd\u4f5c<\/p>\n<ol>\n<li>\u901a\u8fc7\u641c\u7d22\u5f15\u7528[5]\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u4ece\u4efb\u610f\u5143\u7d20\u5f00\u59cb\uff0c\u641c\u7d22\u5230\u5728\u5217\u8868\u4e2d\u95f4\u9694\u4e3ak\u7684\u5143\u7d20\u7684\u4efb\u610f\u671f\u671b\u65f6\u95f4\u662fO(logk)<\/li>\n<li>\u5b9e\u73b0\u7ebf\u6027\u8868\u7684\u5e38\u89c4\u64cd\u4f5c\uff08\u4f8b\u5982_\u5c06\u5143\u7d20\u63d2\u5165\u5230\u5217\u8868\u7b2ck\u4e2a\u5143\u7d20\u540e\u9762_\uff09<\/li>\n<\/ol>\n<p>\u8fd9\u51e0\u79cd\u64cd\u4f5c\u5728\u5e73\u8861\u6811\u4e2d\u4e5f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f46\u662f\u5728\u8df3\u8dc3\u8868\u4e2d\u5b9e\u73b0\u8d77\u6765\u66f4\u7b80\u5355\u800c\u4e14\u975e\u5e38\u7684\u5feb\uff0c\u5e76\u4e14\u901a\u5e38\u60c5\u51b5\u4e0b\u5f88\u96be\u5728\u5e73\u8861\u6811\u4e2d\u76f4\u63a5\u5b9e\u73b0\uff08\u6811\u7684\u7ebf\u7d22\u5316\u53ef\u4ee5\u5b9e\u73b0\u4e0e\u94fe\u8868\u76f8\u540c\u7684\u6548\u679c\uff0c\u4f46\u662f\u8fd9\u4f7f\u5f97\u5b9e\u73b0\u53d8\u5f97\u66f4\u52a0\u590d\u6742[6]\uff09<\/p>\n<h2>\u9884\u89c8<\/h2>\n<p>\u6700\u7b80\u5355\u7684\u652f\u6301\u67e5\u627e\u7684\u6570\u636e\u7ed3\u6784\u53ef\u80fd\u5c31\u662f\u94fe\u8868\u3002Figure.1\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u94fe\u8868\u3002\u5728\u94fe\u8868\u4e2d\u6267\u884c\u4e00\u6b21\u67e5\u627e\u7684\u65f6\u95f4\u6b63\u6bd4\u4e8e\u5fc5\u987b\u8003\u67e5\u7684\u8282\u70b9\u4e2a\u6570\uff0c\u8fd9\u4e2a\u4e2a\u6570\u6700\u591a\u662fN\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338639\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.1 Linked List<\/p>\n<p>Figure.2\u8868\u793a\u4e00\u4e2a\u94fe\u8868\uff0c\u5728\u8be5\u94fe\u8868\u4e2d\uff0c\u6bcf\u4e2a\u4e00\u4e2a\u8282\u70b9\u5c31\u6709\u4e00\u4e2a\u9644\u52a0\u7684\u6307\u9488\u6307\u5411\u5b83\u5728\u8868\u4e2d\u7684\u524d\u4e24\u4e2a\u4f4d\u7f6e\u4e0a\u7684\u8282\u70b9\u3002\u6b63\u56e0\u4e3a\u8fd9\u4e2a\u524d\u5411\u6307\u9488\uff0c\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u6700\u591a\u8003\u67e5\u2308N\/2\u2309+1\u4e2a\u8282\u70b9\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338640\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.2 Linked List with fingers to the 2nd forward elements<\/p>\n<p>Figure.3\u5c06\u8fd9\u79cd\u60f3\u6cd5\u6269\u5c55\uff0c\u6bcf\u4e2a\u5e8f\u6570\u662f4\u7684\u500d\u6570\u7684\u8282\u70b9\u90fd\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u5e8f\u6570\u4e3a4\u7684\u500d\u6570\u7684\u8282\u70b9\u3002\u53ea\u6709\u2308N\/4\u2309+2\u4e2a\u8282\u70b9\u88ab\u8003\u67e5\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338641\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.3 Linked List with fingers to the 4th forward elements<\/p>\n<p>\u8fd9\u79cd\u8df3\u8dc3\u5e45\u5ea6\u7684\u4e00\u822c\u60c5\u51b5\u5982Figure.4\u6240\u793a\u3002\u6bcf\u4e2a2i\u8282\u70b9\u5c31\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a2i\u8282\u70b9\uff0c\u524d\u5411\u6307\u9488\u7684\u95f4\u9694\u6700\u5927\u4e3aN\/2\u3002\u53ef\u4ee5\u8bc1\u660e\u603b\u7684\u6307\u9488\u6700\u5927\u4e0d\u4f1a\u8d85\u8fc72N\uff08\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u5206\u6790\uff09\uff0c\u4f46\u73b0\u5728\u5728\u4e00\u6b21\u67e5\u627e\u4e2d\u6700\u591a\u8003\u67e5\u2308logN\u2309\u4e2a\u8282\u70b9\u3002\u8fd9\u610f\u5473\u7740\u4e00\u6b21\u67e5\u627e\u4e2d\u603b\u7684\u65f6\u95f4\u6d88\u8017\u4e3aO(logN)\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u67e5\u627e\u57fa\u672c\u7b49\u540c\u4e8e\u4e8c\u5206\u67e5\u627e\uff08binary search\uff09\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338642\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.4 Linked List with fingers to the 2ith forward elements<\/p>\n<p>\u5728\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u7531\u4e00\u4e2a\u8282\u70b9\u8868\u793a\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e00\u4e2a\u9ad8\u5ea6\uff08height\uff09\u6216\u7ea7\u522b\uff08level\uff09\uff0c\u8868\u793a\u8282\u70b9\u6240\u62e5\u6709\u7684\u524d\u5411\u6307\u9488\u6570\u91cf\u3002\u6bcf\u4e2a\u8282\u70b9\u7684\u7b2ci\u4e2a\u524d\u5411\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u7ea7\u522b\u4e3ai\u6216\u66f4\u9ad8\u7684\u8282\u70b9\u3002<\/p>\n<p>\u5728\u524d\u9762\u63cf\u8ff0\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u90fd\u662f\u4e0e\u5143\u7d20\u6570\u91cf\u6709\u5173\u7684\uff0c\u5f53\u63d2\u5165\u6216\u5220\u9664\u65f6\u9700\u8981\u5bf9\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u8c03\u6574\u6765\u6ee1\u8db3\u8fd9\u6837\u7684\u7ea6\u675f\uff0c\u8fd9\u662f\u5f88\u5446\u677f\u4e14\u4f4e\u6548\u7684\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5_\u5c06\u6bcf\u4e2a2i\u8282\u70b9\u5c31\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a2i\u8282\u70b9_\u7684\u9650\u5236\u53bb\u6389\uff0c\u5f53\u65b0\u5143\u7d20\u63d2\u5165\u65f6\u4e3a\u6bcf\u4e2a\u65b0\u8282\u70b9\u5206\u914d\u4e00\u4e2a\u968f\u673a\u7684\u7ea7\u522b\u800c\u4e0d\u7528\u8003\u8651\u6570\u636e\u7ed3\u6784\u7684\u5143\u7d20\u6570\u91cf\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338643\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338644\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.5 Skip List<\/p>\n<h2>\u6570\u636e\u7ed3\u6784<\/h2>\n<p>\u5230\u6b64\u4e3a\u6b62\uff0c\u5df2\u7ecf\u5f97\u5230\u4e86\u6240\u6709\u8ba9\u94fe\u8868\u652f\u6301\u5feb\u901f\u67e5\u627e\u7684\u5145\u8981\u6761\u4ef6\uff0c\u800c\u8fd9\u79cd\u5f62\u5f0f\u7684\u6570\u636e\u7ed3\u6784\u5c31\u662f\u8df3\u8dc3\u8868\u3002\u63a5\u4e0b\u6765\u5c06\u4f1a\u4f7f\u7528\u66f4\u6b63\u89c4\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u8df3\u8dc3\u8868<\/p>\n<ol>\n<li>\u6240\u6709\u5143\u7d20\u5728\u8df3\u8dc3\u8868\u4e2d\u90fd\u662f\u7531\u4e00\u4e2a\u8282\u70b9\u8868\u793a\u3002<\/li>\n<li>\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e00\u4e2a\u9ad8\u5ea6\u6216\u7ea7\u522b\uff0c\u6709\u65f6\u5019\u4e5f\u53ef\u4ee5\u79f0\u4e4b\u4e3a\u9636\uff08step\uff09\uff0c\u8282\u70b9\u7684\u7ea7\u522b\u662f\u4e00\u4e2a\u4e0e\u5143\u7d20\u603b\u6570\u65e0\u5173\u7684\u968f\u673a\u6570\u3002\u89c4\u5b9aNULL\u7684\u7ea7\u522b\u662f\u221e\u3002<\/li>\n<li>\u6bcf\u4e2a\u7ea7\u522b\u4e3ak\u7684\u8282\u70b9\u90fd\u6709k\u4e2a\u524d\u5411\u6307\u9488\uff0c\u4e14\u7b2ci\u4e2a\u524d\u5411\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u7ea7\u522b\u4e3ai\u6216\u66f4\u9ad8\u7684\u8282\u70b9\u3002<\/li>\n<li>\u6bcf\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u90fd\u4e0d\u4f1a\u8d85\u8fc7\u4e00\u4e2a\u660e\u786e\u7684\u5e38\u91cfMaxLevel\u3002\u6574\u4e2a\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u662f\u6240\u6709\u8282\u70b9\u7684\u7ea7\u522b\u7684\u6700\u9ad8\u503c\u3002\u5982\u679c\u8df3\u8dc3\u8868\u662f\u7a7a\u7684\uff0c\u90a3\u4e48\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u5c31\u662f1\u3002<\/li>\n<li>\u5b58\u5728\u4e00\u4e2a\u5934\u8282\u70b9head\uff0c\u5b83\u7684\u7ea7\u522b\u662fMaxLevel\uff0c\u6240\u6709\u9ad8\u4e8e\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u7684\u524d\u5411\u6307\u9488\u90fd\u6307\u5411NULL\u3002<\/li>\n<\/ol>\n<p>\u7a0d\u540e\u5c06\u4f1a\u63d0\u5230\uff0c\u8282\u70b9\u7684\u67e5\u627e\u8fc7\u7a0b\u662f\u5728\u5934\u8282\u70b9\u4ece\u6700\u9ad8\u7ea7\u522b\u7684\u6307\u9488\u5f00\u59cb\uff0c\u6cbf\u7740\u8fd9\u4e2a\u7ea7\u522b\u4e00\u76f4\u8d70\uff0c\u76f4\u5230\u627e\u5230\u5927\u4e8e\u6b63\u5728\u5bfb\u627e\u7684\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff08\u6216\u8005\u662fNULL\uff09\uff0c\u5728\u6b64\u8fc7\u7a0b\u4e2d\u9664\u4e86\u5934\u8282\u70b9\u5916\u5e76\u6ca1\u6709\u4f7f\u7528\u5230\u6bcf\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\uff0c\u56e0\u6b64<strong>\u6bcf\u4e2a\u8282\u70b9\u65e0\u9700\u5b58\u50a8\u8282\u70b9\u7684\u7ea7\u522b<\/strong>\u3002<\/p>\n<p>\u5728\u8df3\u8dc3\u8868\u4e2d\uff0c\u7ea7\u522b\u4e3a1\u7684\u524d\u5411\u6307\u9488\u4e0e\u539f\u59cb\u7684\u94fe\u8868\u7ed3\u6784\u4e2dnext\u6307\u9488\u7684\u4f5c\u7528\u5b8c\u5168\u76f8\u540c\uff0c\u56e0\u6b64\u8df3\u8dc3\u8868\u652f\u6301\u6240\u6709\u94fe\u8868\u652f\u6301\u7684\u7b97\u6cd5\u3002<\/p>\n<p>\u5bf9\u5e94\u5230\u9ad8\u7ea7\u8bed\u8a00\u4e2d\u7684\u7ed3\u6784\u5b9a\u4e49\u5982\u4e0b\u6240\u793a\uff08\u540e\u7eed\u6240\u6709\u4ee3\u7801\u793a\u4f8b\u90fd\u5c06\u4f7f\u7528C\u8bed\u8a00\u63cf\u8ff0\uff09<\/p>\n<pre><code>#define SKIP_LIST_KEY_TYPE     int\n#define SKIP_LIST_VALUE_TYPE   int\n#define SKIP_LIST_MAX_LEVEL    32\n#define SKIP_LIST_P            0.5\n\nstruct Node {\n  SKIP_LIST_KEY_TYPE    key;\n  SKIP_LIST_VALUE_TYPE  value;\n  struct Node          *forwards[]; \/\/ flexible array member\n};\n\nstruct SkipList {\n  struct Node *head;\n  int          level;\n};\n\nstruct Node *CreateNode(int level) {\n  struct Node *node;\n  assert(level &gt; 0);\n  node = malloc(sizeof(struct Node) + sizeof(struct Node *) * level);\n  return node;\n}\n\nstruct SkipList *CreateSkipList() {\n  struct SkipList *list;\n  struct Node     *head;\n  int              i;\n\n  list = malloc(sizeof(struct SkipList));\n  head = CreateNode(SKIP_LIST_MAX_LEVEL);\n  for (i = 0; i &lt; SKIP_LIST_MAX_LEVEL; i++) {\n    head-&gt;forwards[i] = NULL;\n  }\n  list-&gt;head = head;\n  list-&gt;level = 1;\n\n  return list;\n}\n\n<\/code><\/pre>\n<p>\u4ece\u524d\u9762\u7684\u9884\u89c8\u7ae0\u8282\u4e2d\uff0c\u4e0d\u96be\u770b\u51faMaxLevel\u7684\u9009\u503c\u5f71\u54cd\u7740\u8df3\u8dc3\u8868\u7684\u67e5\u8be2\u6027\u80fd\uff0c\u5173\u4e8eMaxLevel\u7684\u9009\u503c\u5c06\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002\u5728\u6b64\u5148\u5c06MaxLevel\u5b9a\u4e49\u4e3a32\uff0c\u8fd9\u5bf9\u4e8e232\u4e2a\u5143\u7d20\u7684\u8df3\u8dc3\u8868\u662f\u8db3\u591f\u7684\u3002\u5ef6\u7eed\u9884\u89c8\u7ae0\u8282\u4e2d\u7684\u63cf\u8ff0\uff0c\u8df3\u8dc3\u8868\u7684\u6982\u7387\u88ab\u5b9a\u4e49\u4e3a0.5\uff0c\u5173\u4e8e\u8fd9\u4e2a\u503c\u7684\u9009\u53d6\u95ee\u9898\u5c06\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<h2>\u7b97\u6cd5<\/h2>\n<h3>\u641c\u7d22<\/h3>\n<p>\u5728\u8df3\u8dc3\u8868\u4e2d\u8fdb\u884c\u641c\u7d22\u7684\u8fc7\u7a0b\uff0c\u662f\u901a\u8fc7Z\u5b57\u5f62\u904d\u5386\u6240\u6709\u6ca1\u6709\u8d85\u8fc7\u8981\u5bfb\u627e\u7684\u76ee\u6807\u5143\u7d20\u7684\u524d\u5411\u6307\u9488\u6765\u5b8c\u6210\u7684\u3002\u5728\u5f53\u524d\u7ea7\u522b\u6ca1\u6709\u53ef\u4ee5\u79fb\u52a8\u7684\u524d\u5411\u6307\u9488\u65f6\uff0c\u5c06\u4f1a\u79fb\u52a8\u5230\u4e0b\u4e00\u7ea7\u522b\u8fdb\u884c\u641c\u7d22\u3002\u76f4\u5230\u5728\u7ea7\u522b\u4e3a1\u7684\u65f6\u5019\u4e14\u6ca1\u6709\u53ef\u4ee5\u79fb\u52a8\u7684\u524d\u5411\u6307\u9488\u65f6\u505c\u6b62\u641c\u7d22\uff0c\u6b64\u65f6\u76f4\u63a5\u6307\u5411\u7684\u8282\u70b9\uff08\u7ea7\u522b\u4e3a1\u7684\u524d\u5411\u6307\u9488\uff09\u5c31\u662f\u5305\u542b\u76ee\u6807\u5143\u7d20\u7684\u8282\u70b9\uff08\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u8bdd\uff09\u3002\u5728Figure.6\u4e2d\u5c55\u793a\u4e86\u5728\u8df3\u8dc3\u8868\u4e2d\u641c\u7d22\u5143\u7d2017\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338645\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.6 A search path to find element 17 in Skip List<\/p>\n<p>\u6574\u4e2a\u8fc7\u7a0b\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u56e0\u4e3a\u9ad8\u7ea7\u8bed\u8a00\u4e2d\u7684\u6570\u7ec4\u4e0b\u6807\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64forwards[0]\u8868\u793a\u8282\u70b9\u7684\u7ea7\u522b\u4e3a1\u7684\u524d\u5411\u6307\u9488\uff0c\u4f9d\u6b64\u7c7b\u63a8<\/p>\n<pre><code>struct Node *SkipListSearch(struct SkipList *list, SKIP_LIST_KEY_TYPE target) {\n  struct Node *current;\n  int          i;\n\n  current = list-&gt;head;\n  for (i = list-&gt;level - 1; i &gt;= 0; i--) {\n    while (current-&gt;forwards[i] &amp;&amp; current-&gt;forwards[i]-&gt;key &lt; target) {\n      current = current-&gt;forwards[i];\n    }\n  }\n\n  current = current-&gt;forwards[0];\n  if (current-&gt;key == target) {\n    return current;\n  } else {\n    return NULL;\n  }\n}\n\n<\/code><\/pre>\n<h3>\u63d2\u5165\u548c\u5220\u9664<\/h3>\n<p>\u5728\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6267\u884c\u548c\u641c\u7d22\u76f8\u540c\u7684\u903b\u8f91\u3002\u5728\u641c\u7d22\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u540d\u4e3aupdate\u7684\u5411\u91cf\uff0c\u5b83\u7ef4\u62a4\u7684\u662f\u641c\u7d22\u8fc7\u7a0b\u4e2d\u8df3\u8dc3\u8868\u6bcf\u4e2a\u7ea7\u522b\u4e0a\u904d\u5386\u5230\u7684\u6700\u53f3\u4fa7\u7684\u503c\uff0c\u8868\u793a\u63d2\u5165\u6216\u5220\u9664\u7684\u8282\u70b9\u7684\u5de6\u4fa7\u76f4\u63a5\u76f4\u63a5\u6307\u5411\u5b83\u7684\u8282\u70b9\uff0c\u7528\u4e8e\u5728\u63d2\u5165\u6216\u5220\u9664\u540e\u8c03\u6574\u8282\u70b9\u6240\u5728\u6240\u6709\u7ea7\u522b\u7684\u524d\u5411\u6307\u9488\uff08\u4e0e\u6734\u7d20\u7684\u94fe\u8868\u8282\u70b9\u63d2\u5165\u6216\u5220\u9664\u7684\u8fc7\u7a0b\u76f8\u540c\uff09\u3002<\/p>\n<p>\u5f53\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7ea7\u522b\u8d85\u8fc7\u5f53\u524d\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u65f6\uff0c\u9700\u8981\u589e\u52a0\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u5e76\u5c06update\u5411\u91cf\u4e2d\u5bf9\u5e94\u7ea7\u522b\u7684\u8282\u70b9\u4fee\u6539\u4e3ahead\u8282\u70b9\u3002<\/p>\n<p>Figure.7\u548cFigure.8\u5c55\u793a\u4e86\u5728\u8df3\u8dc3\u8868\u4e2d\u63d2\u5165\u5143\u7d2016\u7684\u8fc7\u7a0b\u3002\u9996\u5148\uff0c\u5728Figure.7\u4e2d\u6267\u884c\u4e0e\u641c\u7d22\u76f8\u540c\u7684\u67e5\u8be2\u8fc7\u7a0b\uff0c\u5728\u6bcf\u4e2a\u7ea7\u522b\u904d\u5386\u5230\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5728\u5bf9\u5e94\u5c42\u7ea7\u7684\u524d\u5411\u6307\u9488\u88ab\u6807\u8bb0\u4e3a\u7070\u8272\uff0c\u8868\u793a\u7a0d\u540e\u5c06\u4f1a\u5bf9\u9f50\u8fdb\u884c\u8c03\u6574\u3002\u63a5\u4e0b\u6765\u5728Figure.8\u4e2d\uff0c\u5728\u5143\u7d20\u4e3a13\u7684\u8282\u70b9\u540e\u63d2\u5165\u5143\u7d2016\uff0c\u5143\u7d2016\u5bf9\u5e94\u7684\u8282\u70b9\u7684\u7ea7\u522b\u662f5\uff0c\u8fd9\u6bd4\u8df3\u8dc3\u8868\u5f53\u524d\u7ea7\u522b\u8981\u9ad8\uff0c\u56e0\u6b64\u9700\u8981\u589e\u52a0\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u52305\uff0c\u5e76\u5c06head\u8282\u70b9\u5bf9\u5e94\u7ea7\u522b\u7684\u524d\u5411\u6307\u9488\u6807\u8bb0\u4e3a\u7070\u8272\u3002Figure.8\u4e2d\u6240\u6709\u865a\u7ebf\u90e8\u5206\u90fd\u8868\u793a\u8c03\u6574\u540e\u7684\u6548\u679c\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338646\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.7 Search path for inserting element 16<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338647\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.8 Insert element 16 and adjust forward pointers<\/p>\n<pre><code>struct Node *SkipListInsert(struct SkipList *list, SKIP_LIST_KEY_TYPE key, SKIP_LIST_VALUE_TYPE value) {\n  struct Node *update[SKIP_LIST_MAX_LEVEL];\n  struct Node *current;\n  int          i;\n  int          level;\n\n  current = list-&gt;head;\n  for (i = list-&gt;level - 1; i &gt;= 0; i--) {\n    while (current-&gt;forwards[i] &amp;&amp; current-&gt;forwards[i]-&gt;key &lt; target) {\n      current = current-&gt;forwards[i];\n    }\n    update[i] = current;\n  }\n\n  current = current-&gt;forwards[0];\n  if (current-&gt;key == target) {\n    current-&gt;value = value;\n    return current;\n  }\n\n  level = SkipListRandomLevel();\n  if (level &gt; list-&gt;level) {\n    for (i = list-&gt;level; i &lt; level; i++) {\n      update[i] = list-&gt;header;\n    }\n  }\n\n  current = CreateNode(level);\n  current-&gt;key = key;\n  current-&gt;value = value;\n\n  for (i = 0; i &lt; level; i++) {\n    current-&gt;forwards[i] = update[i]-&gt;forwards[i];\n    update[i]-&gt;forwards[i] = current;\n  }\n\n  return current;\n}\n\n<\/code><\/pre>\n<p>\u5728\u5220\u9664\u8282\u70b9\u540e\uff0c\u5982\u679c\u5220\u9664\u7684\u8282\u70b9\u662f\u8df3\u8dc3\u8868\u4e2d\u7ea7\u522b\u6700\u5927\u7684\u8282\u70b9\uff0c\u90a3\u4e48\u9700\u8981\u964d\u4f4e\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u3002<\/p>\n<p>Figure.9\u548cFigure.10\u5c55\u793a\u4e86\u5728\u8df3\u8dc3\u8868\u4e2d\u5220\u9664\u5143\u7d2019\u7684\u8fc7\u7a0b\u3002\u9996\u5148\uff0c\u5728Figure.9\u4e2d\u6267\u884c\u4e0e\u641c\u7d22\u76f8\u540c\u7684\u67e5\u8be2\u8fc7\u7a0b\uff0c\u5728\u6bcf\u4e2a\u7ea7\u522b\u904d\u5386\u5230\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5728\u5bf9\u5e94\u5c42\u7ea7\u7684\u524d\u5411\u6307\u9488\u88ab\u6807\u8bb0\u4e3a\u7070\u8272\uff0c\u8868\u793a\u7a0d\u540e\u5c06\u4f1a\u5bf9\u9f50\u8fdb\u884c\u8c03\u6574\u3002\u63a5\u4e0b\u6765\u5728Figure.10\u4e2d\uff0c\u9996\u5148\u901a\u8fc7\u8c03\u6574\u524d\u5411\u6307\u9488\u5c06\u5143\u7d2019\u5bf9\u5e94\u7684\u8282\u70b9\u4ece\u8df3\u8dc3\u8868\u4e2d\u5378\u8f7d\uff0c\u56e0\u4e3a\u5143\u7d2019\u5bf9\u5e94\u7684\u8282\u70b9\u662f\u7ea7\u522b\u6700\u9ad8\u7684\u8282\u70b9\uff0c\u56e0\u6b64\u5c06\u5176\u4ece\u8df3\u8dc3\u8868\u4e2d\u79fb\u9664\u540e\u9700\u8981\u8c03\u6574\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u3002Figure.10\u4e2d\u6240\u6709\u865a\u7ebf\u90e8\u5206\u90fd\u8868\u793a\u8c03\u6574\u540e\u7684\u6548\u679c\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338648\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.9 Search path for deleting element 19<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338649\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.10 Delete element 19 and adjust forward pointers<\/p>\n<pre><code>struct Node *SkipListDelete(struct SkipList *list, SKIP_LIST_KEY_TYPE key) {\n  struct Node *update[SKIP_LIST_MAX_LEVEL];\n  struct Node *current;\n  int          i;\n\n  current = list-&gt;head;\n  for (i = list-&gt;level - 1; i &gt;= 0; i--) {\n    while (current-&gt;forwards[i] &amp;&amp; current-&gt;forwards[i]-&gt;key &lt; key) {\n      current = current-&gt;forwards[i];\n    }\n    update[i] = current;\n  }\n\n  current = current-&gt;forwards[0];\n  if (current &amp;&amp; current-&gt;key == key) {\n    for (i = 0; i &lt; list-&gt;level; i++) {\n      if (update[i]-&gt;forwards[i] == current) {\n        update[i]-&gt;forwards[i] = current-&gt;forwards[i];\n      } else {\n        break;\n      }\n    }\n\n    while (list-&gt;level &gt; 1 &amp;&amp; list-&gt;head-&gt;forwards[list-&gt;level - 1] == NULL) {\n      list-&gt;level--;\n    }\n  }\n\n  return current;\n}\n\n<\/code><\/pre>\n<h3>\u751f\u6210\u968f\u673a\u7ea7\u522b<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338650\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>int SkipListRandomLevel() {\n  int level;\n  level = 1;\n  while (random() &lt; RAND_MAX * SKIP_LIST_P &amp;&amp; level &lt;= SKIP_LIST_MAX_LEVEL) {\n    level++;\n  }\n  return level;\n}\n\n<\/code><\/pre>\n<p>\u4ee5\u4e0b\u8868\u683c\u4e3a\u6309\u4e0a\u9762\u7b97\u6cd5\u6267\u884c232\u6b21\u540e\uff0c\u6240\u751f\u6210\u7684\u968f\u673a\u7ea7\u522b\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>1<\/strong><\/th>\n<th><strong>2<\/strong><\/th>\n<th><strong>3<\/strong><\/th>\n<th><strong>4<\/strong><\/th>\n<th><strong>5<\/strong><\/th>\n<th><strong>6<\/strong><\/th>\n<th><strong>7<\/strong><\/th>\n<th><strong>8<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2147540777<\/td>\n<td>1073690199<\/td>\n<td>536842769<\/td>\n<td>268443025<\/td>\n<td>134218607<\/td>\n<td>67116853<\/td>\n<td>33563644<\/td>\n<td>16774262<\/td>\n<\/tr>\n<tr>\n<td><strong>9<\/strong><\/td>\n<td><strong>10<\/strong><\/td>\n<td><strong>11<\/strong><\/td>\n<td><strong>12<\/strong><\/td>\n<td><strong>13<\/strong><\/td>\n<td><strong>14<\/strong><\/td>\n<td><strong>15<\/strong><\/td>\n<td><strong>16<\/strong><\/td>\n<\/tr>\n<tr>\n<td>8387857<\/td>\n<td>4193114<\/td>\n<td>2098160<\/td>\n<td>1049903<\/td>\n<td>523316<\/td>\n<td>262056<\/td>\n<td>131455<\/td>\n<td>65943<\/td>\n<\/tr>\n<tr>\n<td><strong>17<\/strong><\/td>\n<td><strong>18<\/strong><\/td>\n<td><strong>19<\/strong><\/td>\n<td><strong>20<\/strong><\/td>\n<td><strong>21<\/strong><\/td>\n<td><strong>22<\/strong><\/td>\n<td><strong>23<\/strong><\/td>\n<td><strong>24<\/strong><\/td>\n<\/tr>\n<tr>\n<td>32611<\/td>\n<td>16396<\/td>\n<td>8227<\/td>\n<td>4053<\/td>\n<td>2046<\/td>\n<td>1036<\/td>\n<td>492<\/td>\n<td>249<\/td>\n<\/tr>\n<tr>\n<td><strong>25<\/strong><\/td>\n<td><strong>26<\/strong><\/td>\n<td><strong>27<\/strong><\/td>\n<td><strong>28<\/strong><\/td>\n<td><strong>29<\/strong><\/td>\n<td><strong>30<\/strong><\/td>\n<td><strong>31<\/strong><\/td>\n<td><strong>32<\/strong><\/td>\n<\/tr>\n<tr>\n<td>121<\/td>\n<td>55<\/td>\n<td>34<\/td>\n<td>16<\/td>\n<td>7<\/td>\n<td>9<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>MaxLevel\u7684\u9009\u62e9<\/h3>\n<p>\u5728Figure.4\u4e2d\u66fe\u7ed9\u51fa\u8fc7\u5bf9\u4e8e10\u4e2a\u5143\u7d20\u7684\u8df3\u8dc3\u8868\u6700\u7406\u60f3\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u5176\u4e2d5\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u662f1\uff0c3\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u662f2\uff0c1\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u662f3\uff0c1\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u662f4\u3002<\/p>\n<p>\u8fd9\u5f15\u7533\u51fa\u4e00\u4e2a\u95ee\u9898\uff1a_\u65e2\u7136\u76f8\u540c\u5143\u7d20\u6570\u91cf\u4e0b\uff0c\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u4e0d\u540c\u4f1a\u6709\u4e0d\u540c\u7684\u6027\u80fd\uff0c\u90a3\u4e48\u8df3\u8dc3\u8868\u7684\u7ea7\u522b\u4e3a\u591a\u5c11\u624d\u5408\u9002\uff1f_<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338651\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u5206\u6790<\/h2>\n<h3>\u7a7a\u95f4\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<p>\u524d\u9762\u63d0\u5230\u8fc7\uff0c_\u5206\u6570p\u4ee3\u8868\u8282\u70b9\u540c\u65f6\u5e26\u6709\u7b2ci\u5c42\u524d\u5411\u6307\u9488\u548c\u7b2ci+1\u5c42\u524d\u5411\u6307\u9488\u7684\u6982\u7387_\uff0c\u800c\u6bcf\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u6700\u5c11\u662f1\uff0c\u56e0\u6b64\u7ea7\u522b\u4e3ai\u7684\u524d\u5411\u6307\u9488\u6570\u4e3anpi\u22121\u3002\u5b9a\u4e49S(n)\u8868\u793a\u6240\u6709\u524d\u5411\u6307\u9488\u7684\u603b\u91cf\uff0c\u7531\u7b49\u6bd4\u6570\u5217\u6c42\u548c\u516c\u5f0f\u53ef\u5f97<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338652\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5bf9S(n)\u6c42\u6781\u9650\uff0c\u6709<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338653\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6613\u8bc1\uff0c\u8fd9\u662f\u4e00\u4e2a\u5173\u4e8ep\u7684\u5355\u8c03\u9012\u589e\u51fd\u6570\uff0c\u56e0\u6b64p\u7684\u503c\u8d8a\u5927\uff0c\u8df3\u8dc3\u8868\u4e2d\u524d\u5411\u6307\u9488\u7684\u603b\u6570\u8d8a\u591a\u3002\u56e0\u4e3a1\u2212p\u662f\u5df2\u77e5\u7684\u5e38\u6570\uff0c\u6240\u4ee5\u8bf4\u8df3\u8dc3\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u662fO(n)\u3002<\/p>\n<h3>\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<h4>\u975e\u5f62\u5f0f\u5316\u5206\u6790<\/h4>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338654\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>\u5f62\u5f0f\u5316\u5206\u6790<\/h4>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338655\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5ef6\u7eed_\u5206\u6570p\u4ee3\u8868\u8282\u70b9\u540c\u65f6\u5e26\u6709\u7b2ci\u5c42\u524d\u5411\u6307\u9488\u548c\u7b2ci+1\u5c42\u524d\u5411\u6307\u9488\u7684\u6982\u7387_\u7684\u5b9a\u4e49\uff0c\u8003\u8651\u53cd\u65b9\u5411\u5206\u6790\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<p>\u641c\u7d22\u7684\u8def\u5f84\u603b\u662f\u5c0f\u4e8e\u5fc5\u987b\u6267\u884c\u7684\u6bd4\u8f83\u7684\u6b21\u6570\u7684\u3002\u9996\u5148\u9700\u8981\u8003\u67e5\u4ece\u7ea7\u522b1\uff08\u5728\u641c\u7d22\u5230\u5143\u7d20\u524d\u904d\u5386\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff09\u722c\u5347\u5230\u7ea7\u522bL(n)\u6240\u9700\u8981\u53cd\u5411\u8ddf\u8e2a\u7684\u6307\u9488\u6570\u91cf\u3002\u867d\u7136\u5728\u641c\u7d22\u65f6\u53ef\u4ee5\u786e\u5b9a\u6bcf\u4e2a\u8282\u70b9\u7684\u7ea7\u522b\u90fd\u662f\u5df2\u77e5\u4e14\u786e\u5b9a\u7684\uff0c\u5728\u8fd9\u91cc\u4ecd\u8ba4\u4e3a\u53ea\u6709\u5f53\u6574\u4e2a\u641c\u7d22\u8def\u5f84\u90fd\u88ab\u53cd\u5411\u8ffd\u8e2a\u540e\u624d\u80fd\u88ab\u786e\u5b9a\uff0c\u5e76\u4e14\u5728\u722c\u5347\u5230\u7ea7\u522bL(n)\u4e4b\u524d\u90fd\u4e0d\u4f1a\u63a5\u89e6\u5230head\u8282\u70b9\u3002<\/p>\n<p>\u5728\u722c\u5347\u8fc7\u7a0b\u4e2d\u4efb\u4f55\u7279\u5b9a\u7684\u70b9\uff0c\u90fd\u8ba4\u4e3a\u662f\u5728\u5143\u7d20x\u7684\u7b2ci\u4e2a\u524d\u5411\u6307\u9488\uff0c\u5e76\u4e14\u4e0d\u77e5\u9053\u5143\u7d20x\u5de6\u4fa7\u6240\u6709\u5143\u7d20\u7684\u7ea7\u522b\u6216\u5143\u7d20x\u7684\u7ea7\u522b\uff0c\u4f46\u662f\u53ef\u4ee5\u77e5\u9053\u5143\u7d20x\u7684\u7ea7\u522b\u81f3\u5c11\u662fi\u3002\u5143\u7d20x\u7684\u7ea7\u522b\u5927\u4e8ei\u7684\u6982\u7387\u662fp\uff0c\u53ef\u4ee5\u901a\u8fc7\u8003\u8651\u89c6\u8ba4\u4e3a\u8fd9\u4e2a\u53cd\u5411\u722c\u5347\u7684\u8fc7\u7a0b\u662f\u4e00\u7cfb\u5217\u7531\u6210\u529f\u7684\u722c\u5347\u5230\u66f4\u9ad8\u7ea7\u522b\u6216\u5931\u8d25\u5730\u5411\u5de6\u79fb\u52a8\u7684\u968f\u673a\u72ec\u7acb\u5b9e\u9a8c\u3002<\/p>\n<p>\u5728\u722c\u5347\u5230\u7ea7\u522bL(n)\u8fc7\u7a0b\u4e2d\uff0c\u5411\u5de6\u79fb\u52a8\u7684\u6b21\u6570\u7b49\u4e8e\u5728\u8fde\u7eed\u968f\u673a\u8bd5\u9a8c\u4e2d\u7b2cL(n)\u22121\u6b21\u6210\u529f\u524d\u7684\u5931\u8d25\u7684\u6b21\u6570\uff0c\u8fd9\u7b26\u5408\u8d1f\u4e8c\u9879\u5206\u5e03\u3002\u671f\u671b\u7684\u5411\u4e0a\u79fb\u52a8\u6b21\u6570\u4e00\u5b9a\u662fL(n)\u22121\u3002\u56e0\u6b64\u53ef\u4ee5\u5f97\u5230\u65e0\u9650\u5217\u8868\u4e2d\u722c\u5347\u5230L(n)\u7684\u4ee3\u4ef7C C=prob(L(n)\u22121)+NB(L(n)\u22121,p) \u5217\u8868\u957f\u5ea6\u65e0\u9650\u5927\u662f\u4e00\u4e2a\u60b2\u89c2\u7684\u5047\u8bbe\u3002\u5f53\u53cd\u5411\u722c\u5347\u7684\u8fc7\u7a0b\u4e2d\u63a5\u89e6\u5230head\u8282\u70b9\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5411\u4e0a\u722c\u5347\u800c\u4e0d\u9700\u8981\u4efb\u4f55\u5411\u5de6\u79fb\u52a8\u3002\u56e0\u6b64\u53ef\u4ee5\u5f97\u5230n\u4e2a\u5143\u7d20\u5217\u8868\u4e2d\u722c\u5347\u5230L(n)\u7684\u4ee3\u4ef7C(n)<\/p>\n<p>C(n)\u2264probC=prob(L(n)\u22121)+NB(L(n)\u22121,p)<\/p>\n<p>\u56e0\u6b64n\u4e2a\u5143\u7d20\u5217\u8868\u4e2d\u722c\u5347\u5230L(n)\u7684\u4ee3\u4ef7C(n)\u7684\u6570\u5b66\u671f\u671b\u548c\u65b9\u5dee\u4e3a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338656\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u56e0\u4e3a1\/p\u662f\u5df2\u77e5\u5e38\u6570\uff0c\u56e0\u6b64\u8df3\u8dc3\u8868\u7684\u641c\u7d22\u3001\u63d2\u5165\u548c\u5220\u9664\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662fO(logn)\u3002<\/p>\n<h4>P\u7684\u9009\u62e9<\/h4>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338657\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338658\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Figure.11 Normalized search times<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338659\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u6269\u5c55<\/h2>\n<h3>\u5feb\u901f\u968f\u673a\u8bbf\u95ee<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043338660\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>#define SKIP_LIST_KEY_TYPE     int\n#define SKIP_LIST_VALUE_TYPE   int\n#define SKIP_LIST_MAX_LEVEL    32\n#define SKIP_LIST_P            0.5\n\nstruct Node; \/\/ forward definition\n\nstruct Forward {\n  struct Node *forward;\n  int          span;\n}\n\nstruct Node {\n  SKIP_LIST_KEY_TYPE    key;\n  SKIP_LIST_VALUE_TYPE  value;\n  struct Forward        forwards[]; \/\/ flexible array member\n};\n\nstruct SkipList {\n  struct Node *head;\n  int          level;\n  int          length;\n};\n\nstruct Node *CreateNode(int level) {\n  struct Node *node;\n  assert(level &gt; 0);\n  node = malloc(sizeof(struct Node) + sizeof(struct Forward) * level);\n  return node;\n}\n\nstruct SkipList *CreateSkipList() {\n  struct SkipList *list;\n  struct Node     *head;\n  int              i;\n\n  list = malloc(sizeof(struct SkipList));\n  head = CreateNode(SKIP_LIST_MAX_LEVEL);\n  for (i = 0; i &lt; SKIP_LIST_MAX_LEVEL; i++) {\n    head-&gt;forwards[i].forward = NULL;\n    head-&gt;forwards[i].span = 0;\n  }\n\n  list-&gt;head = head;\n  list-&gt;level = 1;\n\n  return list;\n}\n\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u9700\u8981\u4fee\u6539\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u6765\u4fdd\u8bc1\u5728\u8df3\u8dc3\u8868\u4fee\u6539\u540e\u8de8\u5ea6\u7684\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u63d2\u5165\u8fc7\u7a0b\u4e2d\u9700\u8981\u4f7f\u7528indices\u8bb0\u5f55\u5728\u6bcf\u4e2a\u5c42\u7ea7\u904d\u5386\u5230\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u8fd9\u6837\u901a\u8fc7\u505a\u7b80\u5355\u7684\u51cf\u6cd5\u64cd\u4f5c\u5c31\u53ef\u4ee5\u77e5\u9053\u6bcf\u4e2a\u5c42\u7ea7\u904d\u5386\u5230\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5230\u65b0\u63d2\u5165\u8282\u70b9\u7684\u8de8\u5ea6\u3002<\/p>\n<pre><code>struct Node *SkipListInsert(struct SkipList *list, SKIP_LIST_KEY_TYPE key, SKIP_LIST_VALUE_TYPE value) {\n  struct Node *update[SKIP_LIST_MAX_LEVEL];\n  struct Node *current;\n  int          indices[SKIP_LIST_MAX_LEVEL];\n  int          i;\n  int          level;\n\n  current = list-&gt;head;\n  for (i = list-&gt;level - 1; i &gt;= 0; i--) {\n    if (i == list-&gt;level - 1) {\n      indices[i] = 0;\n    } else {\n      indices[i] = indices[i + 1];\n    }\n    \n    while (current-&gt;forwards[i].forward &amp;&amp; current-&gt;forwards[i].forward-&gt;key &lt; target) {\n      indices[i] += current-&gt;forwards[i].span;\n      current = current-&gt;forwards[i].forward;\n    }\n    update[i] = current;\n  }\n\n  current = current-&gt;forwards[0].forward;\n  if (current-&gt;key == target) {\n    current-&gt;value = value;\n    return current;\n  }\n\n  level = SkipListRandomLevel();\n  if (level &gt; list-&gt;level) {\n    for (i = list-&gt;level; i &lt; level; i++) {\n      indices[i] = 0;\n      update[i] = list-&gt;header;\n      update[i]-&gt;forwards[i].span = list-&gt;length;\n    }\n  }\n\n  current = CreateNode(level);\n  current-&gt;key = key;\n  current-&gt;value = value;\n\n  for (i = 0; i &lt; level; i++) {\n    current-&gt;forwards[i].forward = update[i]-&gt;forwards[i].forward;\n    update[i]-&gt;forwards[i].forward = current;\n    current-&gt;forwards[i].span = update[i]-&gt;forwards[i].span - (indices[0] - indices[i]);\n    update[i]-&gt;forwards[i].span = (indices[0] - indices[i]) + 1;\n  }\n\n  list.length++;\n\n  return current;\n}\n\n<\/code><\/pre>\n<pre><code>struct Node *SkipListDelete(struct SkipList *list, SKIP_LIST_KEY_TYPE key) {\n  struct Node *update[SKIP_LIST_MAX_LEVEL];\n  struct Node *current;\n  int          i;\n\n  current = list-&gt;head;\n  for (i = list-&gt;level - 1; i &gt;= 0; i--) {\n    while (current-&gt;forwards[i].forward &amp;&amp; current-&gt;forwards[i].forward-&gt;key &lt; key) {\n      current = current-&gt;forwards[i].forward;\n    }\n    update[i] = current;\n  }\n\n  current = current-&gt;forwards[0].forward;\n  if (current &amp;&amp; current-&gt;key == key) {\n    for (i = 0; i &lt; list-&gt;level; i++) {\n      if (update[i]-&gt;forwards[i].forward == current) {\n        update[i]-&gt;forwards[i].forward = current-&gt;forwards[i];\n        update[i]-&gt;forwards[i].span += current-&gt;forwards[i].span - 1;\n      } else {\n        break;\n      }\n    }\n\n    while (list-&gt;level &gt; 1 &amp;&amp; list-&gt;head-&gt;forwards[list-&gt;level - 1] == NULL) {\n      list-&gt;level--;\n    }\n  }\n\n  return current;\n}\n\n<\/code><\/pre>\n<p>\u5f53\u5b9e\u73b0\u4e86\u5feb\u901f\u968f\u673a\u8bbf\u95ee\u4e4b\u540e\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u6307\u9488\u64cd\u4f5c\u5373\u53ef\u5b9e\u73b0\u533a\u95f4\u67e5\u8be2\u529f\u80fd\u3002<\/p>\n<h2>\u53c2\u8003\u6587\u732e<\/h2>\n<ol>\n<li>Pugh, W. (1989). A skip list cookbook. Tech. Rep. CS-TR-2286.1, Dept. of Computer Science, Univ. of Maryland, College Park, MD [July 1989]<\/li>\n<li>Pugh, W. (1989). Skip lists: A probabilistic alternative to balanced trees. Lecture Notes in Computer Science, 437\u2013449. <a href=\"https:\/\/link.segmentfault.com\/?enc=Gp827XLVL16RaVNDFi57Qg%3D%3D.Af6GTyKBjhetPmPHip4%2B92gD5HSQrEoh8NH%2FbBvgxqb12nwPoICa0gtD35BE8SVQ\" rel=\"nofollow\">https:\/\/doi.org\/10.1007\/3-540-51542-9_36<\/a><\/li>\n<li>Weiss, M. A. (1996)._Data Structures and Algorithm Analysis in C (2nd Edition)_(2nd ed.). Pearson.<\/li>\n<li>Aragon, Cecilia &#038; Seidel, Raimund. (1989). Randomized Search Trees. 540-545. 10.1109\/SFCS.1989.63531.<\/li>\n<li>Wikipedia contributors. (2022b, November 22)._Finger search_. Wikipedia. <a href=\"https:\/\/link.segmentfault.com\/?enc=pmGL8ZbakMlwkgdNG7Bd%2BQ%3D%3D.jqJf9y7cJ4GCr%2BKZyzO8T9gnH0dNdxRd7BZvYEd4%2F4meJ6gvqigs89lAI8lloHUm\" rel=\"nofollow\">https:\/\/en.wikipedia.org\/wiki\/Finger_search<\/a><\/li>\n<li>Wikipedia contributors. (2022a, October 24)._Threaded binary tree_. Wikipedia. <a href=\"https:\/\/link.segmentfault.com\/?enc=Lpe7kstqRKrigZj%2BqwdchQ%3D%3D.usa7b7eako0Dw3iMo07r0okA5w1DTamZ7LYXI5YkodNS%2FyPc34WIaT714i39dwxzdbBYWevReaLS%2FYMH3mhn4w%3D%3D\" rel=\"nofollow\">https:\/\/en.wikipedia.org\/wiki\/Threaded\/_binary\/_tree<\/a><\/li>\n<li>Wikipedia contributors. (2023, January 4)._Negative binomial distribution_. Wikipedia. <a href=\"https:\/\/link.segmentfault.com\/?enc=NLH6y%2BpOq5j%2BBfdZH2n4%2FA%3D%3D.svtdQ3Vf35tbDAlK0TaCmDZQageUoRwh%2FzxghmPPUBdW9ViVtAs0ugc7UzA9JlVri31xIbLUB200zzUa64kTmQ%3D%3D\" rel=\"nofollow\">https:\/\/en.wikipedia.org\/wiki\/Negative\/_binomial\/_distribution<\/a><\/li>\n<li>Redis contributors._Redis ordered set implementation_. GitHub. <a href=\"https:\/\/link.segmentfault.com\/?enc=JWJL6jsQvzVMiE%2BE86nAgQ%3D%3D.ItekMXrDtSCLdBaAd44XLKMpeHW5pJMI4yhPkLifIgE%3D\" rel=\"nofollow\">https:\/\/github.com\/redis\/redis<\/a><\/li>\n<\/ol>\n<h2>\u540e\u7eed\u5de5\u4f5c<\/h2>\n<ol>\n<li>\u786e\u5b9a\u6027\u8df3\u8dc3\u8868<\/li>\n<li>\u65e0\u9501\u5e76\u53d1\u5b89\u5168\u8df3\u8dc3\u8868<\/li>\n<\/ol>\n<h4>\u56e0\u6587\u7ae0\u5c55\u793a\u5b8c\u6574\u6027\u9700\u8981\uff0c\u6240\u4ee5\u90e8\u5206\u516c\u5f0f\u5185\u5bb9\u91c7\u7528\u622a\u56fe\u4e0a\u4f20\uff0c\u6392\u7248\u4e0d\u5f53\u4e4b\u5904\u8fd8\u671b\u5305\u6db5\u3002<\/h4>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043338637\">https:\/\/segmentfault.com\/a\/1190000043338637<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u7eaa\u5353\u5fd7 \u76ee\u524d\u5e02\u9762\u4e0a\u5145\u65a5\u7740\u5927\u91cf\u5173\u4e8e\u8df3\u8dc3\u8868\u7ed3\u6784\u4e0eRedis\u7684\u6e90\u7801\u89e3\u6790\uff0c\u4f46\u662f\u7ecf\u8fc7\u957f\u671f\u89c2\u5bdf\u540e\u53d1\u73b0\u5927\u90fd\u53ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10007081","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10007081"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007081\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10007081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10007081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10007081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}