{"id":2050,"date":"2022-01-14T20:30:39","date_gmt":"2022-01-14T12:30:39","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2050"},"modified":"2022-01-14T20:30:40","modified_gmt":"2022-01-14T12:30:40","slug":"heap","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/14\/heap\/","title":{"rendered":"\u6570\u636e\u7ed3\u6784&#8211;\u5806"},"content":{"rendered":"\n<p>\u6458\u81eahttps:\/\/www.jianshu.com\/p\/6b526aa481b1<\/p>\n\n\n\n<p>\u5806\u5c31\u662f\u7528\u6570\u7ec4\u5b9e\u73b0\u7684\u4e8c\u53c9\u6811\uff0c\u6240\u4ee5\u5b83\u6ca1\u6709\u4f7f\u7528\u7236\u6307\u9488\u6216\u8005\u5b50\u6307\u9488\u3002\u5806\u6839\u636e\u201c\u5806\u5c5e\u6027\u201d\u6765\u6392\u5e8f\uff0c\u201c\u5806\u5c5e\u6027\u201d\u51b3\u5b9a\u4e86\u6811\u4e2d\u8282\u70b9\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u5806\u7684\u5e38\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul><li>\u6784\u5efa\u4f18\u5148\u961f\u5217<\/li><li>\u652f\u6301\u5806\u6392\u5e8f<\/li><li>\u5feb\u901f\u627e\u51fa\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u6700\u5c0f\u503c\uff08\u6216\u8005\u6700\u5927\u503c\uff09<\/li><\/ul>\n\n\n\n<h2>\u5806\u5c5e\u6027<\/h2>\n\n\n\n<p>\u5806\u5206\u4e3a\u4e24\u79cd\uff1a<em>\u6700\u5927\u5806<\/em>\u548c<em>\u6700\u5c0f\u5806<\/em>\uff0c\u4e24\u8005\u7684\u5dee\u522b\u5728\u4e8e\u8282\u70b9\u7684\u6392\u5e8f\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u5728\u6700\u5927\u5806\u4e2d\uff0c\u7236\u8282\u70b9\u7684\u503c\u6bd4\u6bcf\u4e00\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u90fd\u8981\u5927\u3002\u5728\u6700\u5c0f\u5806\u4e2d\uff0c\u7236\u8282\u70b9\u7684\u503c\u6bd4\u6bcf\u4e00\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u90fd\u8981\u5c0f\u3002\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u201c\u5806\u5c5e\u6027\u201d\uff0c\u5e76\u4e14\u8fd9\u4e2a\u5c5e\u6027\u5bf9\u5806\u4e2d\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u6210\u7acb\u3002<\/p>\n\n\n\n<p>\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-221.png\" alt=\"\" class=\"wp-image-2053\" width=\"200\" height=\"137\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-221.png 365w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-221-300x205.png 300w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u6700\u5927\u5806\uff0c\uff0c\u56e0\u4e3a\u6bcf\u4e00\u4e2a\u7236\u8282\u70b9\u7684\u503c\u90fd\u6bd4\u5176\u5b50\u8282\u70b9\u8981\u5927\u3002<code>10<\/code> \u6bd4 <code>7<\/code> \u548c <code>2<\/code> \u90fd\u5927\u3002<code>7<\/code> \u6bd4 <code>5<\/code> \u548c <code>1<\/code>\u90fd\u5927\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u8fd9\u4e00\u5c5e\u6027\uff0c\u90a3\u4e48\u6700\u5927\u5806\u603b\u662f\u5c06\u5176\u4e2d\u7684\u6700\u5927\u503c\u5b58\u653e\u5728\u6811\u7684\u6839\u8282\u70b9\u3002\u800c\u5bf9\u4e8e\u6700\u5c0f\u5806\uff0c\u6839\u8282\u70b9\u4e2d\u7684\u5143\u7d20\u603b\u662f\u6811\u4e2d\u7684\u6700\u5c0f\u503c\u3002\u5806\u5c5e\u6027\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5806\u5e38\u5e38\u88ab\u5f53\u505a\u4f18\u5148\u961f\u5217\u4f7f\u7528\uff0c\u56e0\u4e3a\u53ef\u4ee5\u5feb\u901f\u5730\u8bbf\u95ee\u5230\u201c\u6700\u91cd\u8981\u201d\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>\u6ce8\u610f\uff1a<\/strong>\u5806\u7684\u6839\u8282\u70b9\u4e2d\u5b58\u653e\u7684\u662f\u6700\u5927\u6216\u8005\u6700\u5c0f\u5143\u7d20\uff0c\u4f46\u662f\u5176\u4ed6\u8282\u70b9\u7684\u6392\u5e8f\u987a\u5e8f\u662f\u672a\u77e5\u7684\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u6700\u5927\u5806\u4e2d\uff0c\u6700\u5927\u7684\u90a3\u4e00\u4e2a\u5143\u7d20\u603b\u662f\u4f4d\u4e8e index 0 \u7684\u4f4d\u7f6e\uff0c\u4f46\u662f\u6700\u5c0f\u7684\u5143\u7d20\u5219\u672a\u5fc5\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002&#8211;\u552f\u4e00\u80fd\u591f\u4fdd\u8bc1\u7684\u662f\u6700\u5c0f\u7684\u5143\u7d20\u662f\u4e00\u4e2a\u53f6\u8282\u70b9\uff0c\u4f46\u662f\u4e0d\u786e\u5b9a\u662f\u54ea\u4e00\u4e2a\u3002<\/p><\/blockquote>\n\n\n\n<h2>\u5806\u548c\u666e\u901a\u6811\u7684\u533a\u522b<\/h2>\n\n\n\n<p>\u5806\u5e76\u4e0d\u80fd\u53d6\u4ee3\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5b83\u4eec\u4e4b\u95f4\u6709\u76f8\u4f3c\u4e4b\u5904\u4e5f\u6709\u4e00\u4e9b\u4e0d\u540c\u3002\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u4e24\u8005\u7684\u4e3b\u8981\u5dee\u522b\uff1a<\/p>\n\n\n\n<p><strong>\u8282\u70b9\u7684\u987a\u5e8f\u3002<\/strong>\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u5de6\u5b50\u8282\u70b9\u5fc5\u987b\u6bd4\u7236\u8282\u70b9\u5c0f\uff0c\u53f3\u5b50\u8282\u70b9\u5fc5\u987b\u5fc5\u6bd4\u7236\u8282\u70b9\u5927\u3002\u4f46\u662f\u5728\u5806\u4e2d\u5e76\u975e\u5982\u6b64\u3002\u5728\u6700\u5927\u5806\u4e2d\u4e24\u4e2a\u5b50\u8282\u70b9\u90fd\u5fc5\u987b\u6bd4\u7236\u8282\u70b9\u5c0f\uff0c\u800c\u5728\u6700\u5c0f\u5806\u4e2d\uff0c\u5b83\u4eec\u90fd\u5fc5\u987b\u6bd4\u7236\u8282\u70b9\u5927\u3002<\/p>\n\n\n\n<p><strong>\u5185\u5b58\u5360\u7528\u3002<\/strong>\u666e\u901a\u6811\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u5b83\u4eec\u5b58\u50a8\u7684\u6570\u636e\u8981\u591a\u3002\u4f60\u5fc5\u987b\u4e3a\u8282\u70b9\u5bf9\u8c61\u4ee5\u53ca\u5de6\/\u53f3\u5b50\u8282\u70b9\u6307\u9488\u5206\u914d\u5185\u5b58\u3002\u5806\u4ec5\u4ec5\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u6765\u5b58\u50a8\u6570\u7ec4\uff0c\u4e14\u4e0d\u4f7f\u7528\u6307\u9488\u3002<\/p>\n\n\n\n<p><strong>\u5e73\u8861\u3002<\/strong>\u4e8c\u53c9\u641c\u7d22\u6811\u5fc5\u987b\u662f\u201c\u5e73\u8861\u201d\u7684\u60c5\u51b5\u4e0b\uff0c\u5176\u5927\u90e8\u5206\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u624d\u80fd\u8fbe\u5230<strong>O(log n)<\/strong>\u3002\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u4f4d\u7f6e\u63d2\u5165\/\u5220\u9664\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528 AVL \u6811\u6216\u8005\u7ea2\u9ed1\u6811\uff0c\u4f46\u662f\u5728\u5806\u4e2d\u5b9e\u9645\u4e0a\u4e0d\u9700\u8981\u6574\u68f5\u6811\u90fd\u662f\u6709\u5e8f\u7684\u3002\u6211\u4eec\u53ea\u9700\u8981\u6ee1\u8db3\u5806\u5c5e\u6027\u5373\u53ef\uff0c\u6240\u4ee5\u5728\u5806\u4e2d\u5e73\u8861\u4e0d\u662f\u95ee\u9898\u3002\u56e0\u4e3a\u5806\u4e2d\u6570\u636e\u7684\u7ec4\u7ec7\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u8bc1<strong>O(log n)<\/strong> \u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p><strong>\u641c\u7d22\u3002<\/strong>\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u4f1a\u5f88\u5feb\uff0c\u4f46\u662f\u5728\u5806\u4e2d\u641c\u7d22\u4f1a\u5f88\u6162\u3002\u5728\u5806\u4e2d\u641c\u7d22\u4e0d\u662f\u7b2c\u4e00\u4f18\u5148\u7ea7\uff0c\u56e0\u4e3a\u4f7f\u7528\u5806\u7684\u76ee\u7684\u662f\u5c06\u6700\u5927\uff08\u6216\u8005\u6700\u5c0f\uff09\u7684\u8282\u70b9\u653e\u5728\u6700\u524d\u9762\uff0c\u4ece\u800c\u5feb\u901f\u7684\u8fdb\u884c\u76f8\u5173\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h2>\u6765\u81ea\u6570\u7ec4\u7684\u6811<\/h2>\n\n\n\n<p>\u7528\u6570\u7ec4\u6765\u5b9e\u73b0\u6811\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u8bb8\u770b\u8d77\u6765\u6709\u70b9\u53e4\u602a\uff0c\u4f46\u662f\u5b83\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u4e0a\u90fd\u662f\u5f88\u9ad8\u6548\u7684\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u51c6\u5907\u5c06\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u6811\u8fd9\u6837\u5b58\u50a8\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 10, 7, 2, 5, 1 ]\n<\/code><\/pre>\n\n\n\n<p>\u5c31\u8fd9\u4e48\u591a\uff01\u6211\u4eec\u9664\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u7ec4\u4ee5\u5916\uff0c\u4e0d\u9700\u8981\u4efb\u4f55\u989d\u5916\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u5141\u8bb8\u4f7f\u7528\u6307\u9488\uff0c\u90a3\u4e48\u6211\u4eec\u600e\u4e48\u77e5\u9053\u54ea\u4e00\u4e2a\u8282\u70b9\u662f\u7236\u8282\u70b9\uff0c\u54ea\u4e00\u4e2a\u8282\u70b9\u662f\u5b83\u7684\u5b50\u8282\u70b9\u5462\uff1f\u95ee\u5f97\u597d\uff01\u8282\u70b9\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6eindex \u548c\u5b83\u7684\u7236\u8282\u70b9\u4ee5\u53ca\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e4b\u95f4\u6709\u4e00\u4e2a\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\u5982\u679c <code>i<\/code> \u662f\u8282\u70b9\u7684\u7d22\u5f15\uff0c\u90a3\u4e48\u4e0b\u9762\u7684\u516c\u5f0f\u5c31\u7ed9\u51fa\u4e86\u5b83\u7684\u7236\u8282\u70b9\u548c\u5b50\u8282\u70b9\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parent(i) = floor((i - 1)\/2)\nleft(i)   = 2i + 1\nright(i)  = 2i + 2\n<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f <code>right(i)<\/code> \u5c31\u662f\u7b80\u5355\u7684 <code>left(i) + 1<\/code>\u3002\u5de6\u53f3\u8282\u70b9\u603b\u662f\u5904\u4e8e\u76f8\u90bb\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u7406\u89e3\u6570\u7ec4\u7d22\u5f15\u548c\u8282\u70b9\u4f4d\u7f6e\u4e4b\u95f4\u7684\u5173\u7cfb\u975e\u5e38\u91cd\u8981\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u66f4\u5927\u7684\u5806\uff0c\u5b83\u670915\u4e2a\u8282\u70b9\u88ab\u5206\u6210\u4e864\u5c42\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-222.png\" alt=\"\" class=\"wp-image-2056\" width=\"470\" height=\"239\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-222.png 781w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-222-300x153.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-222-768x391.png 768w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-223.png\" alt=\"\" class=\"wp-image-2057\" width=\"538\" height=\"127\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-223.png 878w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-223-300x71.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-223-768x183.png 768w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/figure>\n\n\n\n<p>\u56fe\u7247\u4e2d\u7684\u6570\u5b57\u4e0d\u662f\u8282\u70b9\u7684\u503c\uff0c\u800c\u662f\u5b58\u50a8\u8fd9\u4e2a\u8282\u70b9\u7684\u6570\u7ec4\u7d22\u5f15\uff01\u8fd9\u91cc\u662f\u6570\u7ec4\u7d22\u5f15\u548c\u6811\u7684\u5c42\u7ea7\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-224.png\" alt=\"\" class=\"wp-image-2058\" width=\"534\" height=\"106\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-224.png 851w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-224-300x60.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-224-768x153.png 768w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/figure>\n\n\n\n<p>\u7531\u4e0a\u56fe\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u7ec4\u4e2d\u7236\u8282\u70b9\u603b\u662f\u5728\u5b50\u8282\u70b9\u7684\u524d\u9762\u3002<\/p>\n\n\n\n<h2>\u53ef\u4ee5\u7528\u5806\u505a\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p>\u6709\u4e24\u4e2a\u539f\u59cb\u64cd\u4f5c\u7528\u4e8e\u4fdd\u8bc1\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u4ee5\u540e\u5806\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6700\u5927\u5806\u6216\u8005\u6700\u5c0f\u5806\uff1a<\/p>\n\n\n\n<ul><li><code>shiftUp()<\/code>: \u5982\u679c\u4e00\u4e2a\u8282\u70b9\u6bd4\u5b83\u7684\u7236\u8282\u70b9\u5927\uff08\u6700\u5927\u5806\uff09\u6216\u8005\u5c0f\uff08\u6700\u5c0f\u5806\uff09\uff0c\u90a3\u4e48\u9700\u8981\u5c06\u5b83\u540c\u7236\u8282\u70b9\u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u662f\u8fd9\u4e2a\u8282\u70b9\u5728\u6570\u7ec4\u7684\u4f4d\u7f6e\u4e0a\u5347\u3002<\/li><li><code>shiftDown()<\/code>: \u5982\u679c\u4e00\u4e2a\u8282\u70b9\u6bd4\u5b83\u7684\u5b50\u8282\u70b9\u5c0f\uff08\u6700\u5927\u5806\uff09\u6216\u8005\u5927\uff08\u6700\u5c0f\u5806\uff09\uff0c\u90a3\u4e48\u9700\u8981\u5c06\u5b83\u5411\u4e0b\u79fb\u52a8\u3002\u8fd9\u4e2a\u64cd\u4f5c\u4e5f\u79f0\u4f5c\u201c\u5806\u5316\uff08heapify\uff09\u201d\u3002<\/li><\/ul>\n\n\n\n<p>shiftUp \u6216\u8005 shiftDown \u662f\u4e00\u4e2a\u9012\u5f52\u7684\u8fc7\u7a0b\uff0c\u6240\u4ee5\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f <strong>O(log n)<\/strong>\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e\u8fd9\u4e24\u4e2a\u539f\u59cb\u64cd\u4f5c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul><li><code>insert(value)<\/code>: \u5728\u5806\u7684\u5c3e\u90e8\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528 <code>shiftUp<\/code> \u6765\u4fee\u590d\u5bf9\u3002<\/li><li><code>remove()<\/code>: \u79fb\u9664\u5e76\u8fd4\u56de\u6700\u5927\u503c\uff08\u6700\u5927\u5806\uff09\u6216\u8005\u6700\u5c0f\u503c\uff08\u6700\u5c0f\u5806\uff09\u3002\u4e3a\u4e86\u5c06\u8fd9\u4e2a\u8282\u70b9\u5220\u9664\u540e\u7684\u7a7a\u4f4d\u586b\u8865\u4e0a\uff0c\u9700\u8981\u5c06\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u79fb\u5230\u6839\u8282\u70b9\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528 <code>shiftDown<\/code> \u65b9\u6cd5\u6765\u4fee\u590d\u5806\u3002<\/li><li><code>removeAtIndex(index)<\/code>: \u548c <code>remove()<\/code> \u4e00\u6837\uff0c\u5dee\u522b\u5728\u4e8e\u53ef\u4ee5\u79fb\u9664\u5806\u4e2d\u4efb\u610f\u8282\u70b9\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6839\u8282\u70b9\u3002\u5f53\u5b83\u4e0e\u5b50\u8282\u70b9\u6bd4\u8f83\u4f4d\u7f6e\u4e0d\u65f6\u65e0\u5e8f\u65f6\u4f7f\u7528 <code>shiftDown()<\/code>\uff0c\u5982\u679c\u4e0e\u7236\u8282\u70b9\u6bd4\u8f83\u53d1\u73b0\u65e0\u5e8f\u5219\u4f7f\u7528 <code>shiftUp()<\/code>\u3002<\/li><li><code>replace(index, value)<\/code>\uff1a\u5c06\u4e00\u4e2a\u66f4\u5c0f\u7684\u503c\uff08\u6700\u5c0f\u5806\uff09\u6216\u8005\u66f4\u5927\u7684\u503c\uff08\u6700\u5927\u5806\uff09\u8d4b\u503c\u7ed9\u4e00\u4e2a\u8282\u70b9\u3002\u7531\u4e8e\u8fd9\u4e2a\u64cd\u4f5c\u7834\u574f\u4e86\u5806\u5c5e\u6027\uff0c\u6240\u4ee5\u9700\u8981\u4f7f\u7528 <code>shiftUp()<\/code> \u6765\u4fee\u590d\u5806\u5c5e\u6027\u3002<\/li><\/ul>\n\n\n\n<p>\u4e0a\u9762\u6240\u6709\u7684\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f <strong>O(log n)<\/strong>\uff0c\u56e0\u4e3a shiftUp \u548c shiftDown \u90fd\u5f88\u8d39\u65f6\u3002\u8fd8\u6709\u5c11\u6570\u4e00\u4e9b\u64cd\u4f5c\u9700\u8981\u66f4\u591a\u7684\u65f6\u95f4\uff1a<\/p>\n\n\n\n<ul><li><code>search(value)<\/code>:\u5806\u4e0d\u662f\u4e3a\u5feb\u901f\u641c\u7d22\u800c\u5efa\u7acb\u7684\uff0c\u4f46\u662f <code>replace()<\/code> \u548c <code>removeAtIndex()<\/code> \u64cd\u4f5c\u9700\u8981\u627e\u5230\u8282\u70b9\u5728\u6570\u7ec4\u4e2d\u7684index\uff0c\u6240\u4ee5\u4f60\u9700\u8981\u5148\u627e\u5230\u8fd9\u4e2aindex\u3002\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<strong>O(n)<\/strong>\u3002<\/li><li><code>buildHeap(array)<\/code>:\u901a\u8fc7\u53cd\u590d\u8c03\u7528 <code>insert()<\/code> \u65b9\u6cd5\u5c06\u4e00\u4e2a\uff08\u65e0\u5e8f\uff09\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u4e2a\u5806\u3002\u5982\u679c\u4f60\u8db3\u591f\u806a\u660e\uff0c\u4f60\u53ef\u4ee5\u5728 <strong>O(n)<\/strong> \u65f6\u95f4\u5185\u5b8c\u6210\u3002<\/li><li>\u5806\u6392\u5e8f\uff1a\u7531\u4e8e\u5806\u5c31\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u72ec\u7279\u7684\u5c5e\u6027\u5c06\u6570\u7ec4\u4ece\u4f4e\u5230\u9ad8\u6392\u5e8f\u3002\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<strong>O(n lg n)<\/strong>\u3002<\/li><\/ul>\n\n\n\n<p>\u5806\u8fd8\u6709\u4e00\u4e2a <code>peek()<\/code> \u65b9\u6cd5\uff0c\u4e0d\u7528\u5220\u9664\u8282\u70b9\u5c31\u8fd4\u56de\u6700\u5927\u503c\uff08\u6700\u5927\u5806\uff09\u6216\u8005\u6700\u5c0f\u503c\uff08\u6700\u5c0f\u5806\uff09\u3002\u65f6\u95f4\u590d\u6742\u5ea6 <strong>O(1)<\/strong> \u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u8fd8\u662f\u4f7f\u7528 <code>insert()<\/code> \u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\uff0c\u548c\u901a\u8fc7 <code>remove()<\/code>\u79fb\u9664\u6700\u5927\u6216\u8005\u6700\u5c0f\u503c\u3002\u4e24\u8005\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f<strong>O(log n)<\/strong>\u3002\u5176\u5176\u4ed6\u7684\u64cd\u4f5c\u662f\u7528\u4e8e\u652f\u6301\u66f4\u9ad8\u7ea7\u7684\u5e94\u7528\uff0c\u6bd4\u5982\u8bf4\u5efa\u7acb\u4e00\u4e2a\u4f18\u5148\u961f\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81eahttps:\/\/www.jianshu.com\/p\/6b526aa481b1 \u5806\u5c31\u662f\u7528\u6570\u7ec4\u5b9e\u73b0\u7684\u4e8c\u53c9\u6811\uff0c\u6240 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/14\/heap\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u6570\u636e\u7ed3\u6784&#8211;\u5806<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2050"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2050"}],"version-history":[{"count":5,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2050\/revisions"}],"predecessor-version":[{"id":2059,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2050\/revisions\/2059"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2050"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}