{"id":96426,"date":"2026-08-27T16:33:00","date_gmt":"2026-08-27T08:33:00","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/96426.html"},"modified":"2026-08-27T16:33:00","modified_gmt":"2026-08-27T08:33:00","slug":"%e3%80%90c%e3%80%91%e3%80%8ac%e7%ba%a2%e9%bb%91%e6%a0%91%e4%bb%8e0%e5%88%b01%ef%bc%9a%e6%8f%92%e5%85%a5%e3%80%81%e6%97%8b%e8%bd%ac%e3%80%81%e8%bf%ad%e4%bb%a3%e5%99%a8%ef%bc%8c%e5%b9%b6%e6%a8%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/96426.html","title":{"rendered":"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b"},"content":{"rendered":"<h2>\u4e00. \u7ea2\u2fca\u6811\u7684\u6982\u5ff5<\/h2>\n<p><span style=\"color:#fe2c24\">\u7ea2\u2fca\u6811\u662f\u2f00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811<\/span>&#xff0c;\u4ed6\u7684\u6bcf\u4e2a\u7ed3\u70b9\u589e\u52a0<span style=\"color:#fe2c24\">\u2f00\u4e2a\u5b58\u50a8\u4f4d\u6765\u8868\u793a\u7ed3\u70b9\u7684\u989c\u2f8a<\/span>&#xff0c;\u53ef\u4ee5\u662f<span style=\"color:#fe2c24\">\u7ea2\u2f8a\u6216\u8005\u2fca\u2f8a<\/span>\u3002\u901a\u8fc7\u5bf9\u4efb\u4f55\u2f00\u6761\u4ece\u6839\u5230\u53f6\u2f26\u7684\u8def\u5f84\u4e0a\u5404\u4e2a\u7ed3\u70b9\u7684\u989c\u2f8a\u8fdb\u2f8f\u7ea6\u675f&#xff0c;\u7ea2\u2fca\u6811\u786e\u4fdd<span style=\"color:#fe2c24\">\u6ca1\u6709\u2f00\u6761\u8def\u5f84\u4f1a\u6bd4\u5176\u4ed6\u8def\u5f84\u957f\u51fa2\u500d<\/span>&#xff0c;\u56e0\u2f7d\u662f\u63a5\u8fd1\u5e73\u8861\u7684\u3002<\/p>\n<hr \/>\n<h3>1.\u7ea2\u9ed1\u6811\u7684\u89c4\u5219&#xff1a;<\/h3>\n<li>\u6bcf\u4e2a\u7ed3\u70b9\u4e0d\u662f<span style=\"color:#fe2c24\">\u7ea2\u2f8a\u5c31\u662f\u2fca\u2f8a<\/span><\/li>\n<li>\u6839\u7ed3\u70b9<span style=\"color:#fe2c24\">\u662f\u2fca\u2f8a\u7684<\/span><\/li>\n<li>\u5982\u679c\u2f00\u4e2a\u7ed3\u70b9\u662f<span style=\"color:#fe2c24\">\u7ea2\u2f8a\u7684&#xff0c;<\/span>\u5219\u5b83\u7684<span style=\"color:#fe2c24\">\u4e24\u4e2a\u5b69\u2f26\u7ed3\u70b9\u5fc5\u987b\u662f\u2fca\u2f8a\u7684<\/span>&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u4efb\u610f\u2f00\u6761\u8def\u5f84\u4e0d\u4f1a\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u3002<\/li>\n<li>\u5bf9\u4e8e\u4efb\u610f\u2f00\u4e2a\u7ed3\u70b9&#xff0c;\u4ece\u8be5\u7ed3\u70b9\u5230\u5176\u6240<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u6709NULL\u7ed3\u70b9\u7684\u7b80\u5355\u8def\u5f84\u4e0a&#xff0c;\u5747\u5305\u542b\u76f8\u540c\u6570\u91cf\u7684\u2fca\u2f8a\u7ed3\u70b9<\/span><\/span><\/li>\n<p>\u8bf4\u660e&#xff1a;\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b49\u4e66\u7c4d\u4e0a\u8865\u5145\u4e86\u2f00\u6761\u6bcf\u4e2a\u53f6\u2f26\u7ed3\u70b9(NIL)\u90fd\u662f\u2fca\u2f8a\u7684\u89c4\u5219\u3002\u4ed6\u8fd9\u2fa5\u6240\u6307\u7684\u53f6\u2f26\u7ed3\u70b9\u4e0d\u662f\u4f20\u7edf\u7684\u610f\u4e49\u4e0a\u7684\u53f6\u2f26\u7ed3\u70b9&#xff0c;\u2f7d\u662f\u6211\u4eec\u8bf4\u7684\u7a7a\u7ed3\u70b9&#xff0c;\u6709\u4e9b\u4e66\u7c4d\u4e0a\u4e5f\u628aNIL\u53eb\u505a\u5916\u90e8\u7ed3\u70b9\u3002NIL\u662f\u4e3a\u4e86\u2f45\u4fbf\u51c6\u786e\u7684\u6807\u8bc6\u51fa\u6240\u6709\u8def\u5f84&#xff0c;\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u5728\u540e\u7eed\u8bb2\u89e3\u5b9e\u73b0\u7684\u7ec6\u8282\u4e2d\u4e5f\u5ffd\u7565\u4e86NIL\u7ed3\u70b9&#xff0c;\u6240\u4ee5\u6211\u4eec\u77e5\u9053\u2f00\u4e0b\u8fd9\u4e2a\u6982\u5ff5\u5373\u53ef\u3002<\/p>\n<p>\u7b80\u5355\u56fe\u793a\u4e00\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1092\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083241-6a8ff629459b5.png\" width=\"2016\" \/><\/p>\n<h3>2.\u8865\u5145\u95ee\u9898<\/h3>\n<p>1.\u4e3a\u4ec0\u4e48\u5b83\u7684\u6839\u8282\u70b9\u662f\u9ed1\u8272\u7684&#xff0c;\u4e0d\u662f\u7ea2\u8272\u7684&#xff1f;<\/p>\n<\/p>\n<p>\u7b54&#xff1a;<span style=\"color:#fe2c24\">\u5176\u5b9e\u4e0d\u662f\u5fc5\u987b&#xff0c;\u662f\u89c4\u5b9a\u6210\u9ed1\u8272\u66f4\u7701\u4e8b<\/span>\u3002<\/p>\n<p>\u7ea2\u9ed1\u6811\u672c\u8eab\u6ca1\u6709\u89c4\u5b9a\u6839\u5fc5\u987b\u662f\u9ed1\u7684&#xff0c;\u5b83\u53ea\u89c4\u5b9a\u4e86\u6bcf\u6761\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u8981\u4e00\u6837\u3002\u8fd9\u4e2a\u89c4\u5219\u8db3\u591f\u4fdd\u8bc1\u6811\u7684\u5927\u81f4\u5e73\u8861\u3002\u95ee\u9898\u51fa\u5728\u5b9e\u73b0\u4e0a\u3002\u5982\u679c\u6839\u662f\u7ea2\u7684&#xff0c;\u63d2\u5165\u5220\u9664\u7684\u65f6\u5019&#xff0c;\u4f60\u603b\u5f97\u989d\u5916\u60f3\u4e00\u4e0b&#xff1a;\u201c\u54ce&#xff0c;\u8fd9\u4e2a\u6839\u662f\u4e0d\u662f\u7ea2\u7684&#xff1f;\u8981\u4e0d\u8981\u7ed9\u5b83\u6539\u6210\u9ed1\u7684&#xff1f;\u201d\u6539\u6765\u6539\u53bb\u5f88\u70e6\u3002<\/p>\n<p>\u6240\u4ee5\u5e72\u8106\u4e00\u5f00\u59cb\u5c31\u628a\u6839\u5b9a\u6210\u9ed1\u7684&#xff0c;\u7701\u5f97\u6bcf\u6b21\u90fd\u8981\u5904\u7406\u8fd9\u4e2a\u7279\u6b8a\u60c5\u51b5\u3002\u4ee3\u7801\u5199\u8d77\u6765\u5c11\u4e00\u4e2a\u5206\u652f&#xff0c;\u8111\u5b50\u5c11\u60f3\u4e00\u4ef6\u4e8b\u3002\u8bf4\u767d\u4e86&#xff0c;<span style=\"color:#fe2c24\">\u4e0d\u662f\u6280\u672f\u4e0a\u7684\u5fc5\u987b&#xff0c;\u662f\u8bbe\u8ba1\u4e0a\u7684\u65b9\u4fbf<\/span>\u3002\u89c4\u5219\u7edf\u4e00\u4e86&#xff0c;\u5b9e\u73b0\u5c31\u7b80\u5355\u4e86\u3002<\/p>\n<hr \/>\n<p>2.\u601d\u8003\u2f00\u4e0b&#xff0c;\u7ea2\u2fca\u6811\u5982\u4f55\u786e\u4fdd\u6700\u957f\u8def\u5f84\u4e0d\u8d85\u8fc7\u6700\u77ed\u8def\u5f84\u76842\u500d\u7684&#xff1f;<\/p>\n<p>\u2022 \u7531\u89c4\u52194\u53ef\u77e5&#xff0c;\u4ece\u6839\u5230NULL\u7ed3\u70b9\u7684\u6bcf\u6761\u8def\u5f84\u90fd\u6709\u76f8\u540c\u6570\u91cf\u7684\u2fca\u2f8a\u7ed3\u70b9&#xff0c;\u6240\u4ee5\u6781\u7aef\u573a\u666f\u4e0b&#xff0c;\u6700\u77ed\u8def\u5f84\u5c31\u662f\u5168\u662f\u9ed1\u8272\u7ed3\u70b9\u7684\u8def\u5f84&#xff0c;\u5047\u8bbe\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u4e3abh(black height)\u3002<br \/>\n\u2022 \u7531\u89c4\u52192\u548c\u89c4\u52193\u53ef\u77e5&#xff0c;\u4efb\u610f\u2f00\u6761\u8def\u5f84\u4e0d\u4f1a\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9&#xff0c;\u6240\u4ee5\u6781\u7aef\u573a\u666f\u4e0b&#xff0c;\u6700\u2ed3\u7684\u8def\u5f84\u5c31\u662f\u2f00\u9ed1\u2f00\u7ea2\u95f4\u9694\u7ec4\u6210&#xff0c;\u90a3\u4e48\u6700\u957f\u8def\u5f84\u7684\u957f\u5ea6\u4e3a2*bh\u3002<br \/>\n\u2022 \u7efc\u5408\u7ea2\u9ed1\u6811\u76844\u70b9\u89c4\u5219\u2f7d\u8a00&#xff0c;\u7406\u8bba\u4e0a\u7684\u5168\u9ed1\u6700\u77ed\u8def\u5f84\u548c\u2f00\u9ed1\u2f00\u7ea2\u7684\u6700\u957f\u8def\u5f84\u5e76\u4e0d\u662f\u5728\u6bcf\u68f5\u7ea2\u9ed1\u6811\u90fd\u5b58\u5728\u7684\u3002\u5047\u8bbe\u4efb\u610f\u2f00\u6761\u4ece\u6839\u5230NULL\u7ed3\u70b9\u8def\u5f84\u7684\u2ed3\u5ea6\u4e3ax&#xff0c;\u90a3\u4e48bh &lt;&#061; h &lt;&#061; 2*bh\u3002<\/p>\n<hr \/>\n<h3>3.\u7ea2\u2fca\u6811\u7684\u6548\u7387&#xff1a;<\/h3>\n<p><span style=\"color:#fe2c24\">\u5047\u8bbeN\u662f\u7ea2\u2fca\u6811\u6811\u4e2d\u7ed3\u70b9\u6570\u91cf&#xff0c;h\u6700\u77ed\u8def\u5f84\u7684\u2ed3\u5ea6&#xff0c;\u90a3\u4e48 2^h\u00a0\u22121, \u7531\u6b64\u63a8\u51fah 1 &lt;&#061; N &lt; 2^2h-1<\/span>\u7531\u6b64\u63a8\u51fa<span style=\"color:#fe2c24\">h \u2248 logN <\/span>&#xff0c;\u4e5f\u5c31\u662f\u610f\u5473\u7740\u7ea2\u2fca\u6811\u589e\u5220\u67e5\u6539\u6700\u574f\u4e5f\u5c31\u662f\u2f9b\u6700\u2ed3\u8def\u5f84 2 \u2217 logN &#xff0c;\u90a3\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u8fd8\u662fO(logN)<\/p>\n<p>\u7ea2\u2fca\u6811\u7684\u8868\u8fbe\u76f8\u5bf9AVL\u6811\u8981\u62bd\u8c61\u2f00\u4e9b&#xff0c;AVL\u6811\u901a\u8fc7\u2fbc\u5ea6\u5dee\u76f4\u89c2\u7684\u63a7\u5236\u4e86\u5e73\u8861\u3002\u7ea2\u2fca\u6811\u901a\u8fc74\u6761\u89c4\u5219\u7684\u989c\u2f8a\u7ea6\u675f&#xff0c;\u95f4\u63a5\u7684\u5b9e\u73b0\u4e86\u8fd1\u4f3c\u5e73\u8861&#xff0c;\u4ed6\u4eec\u6548\u7387\u90fd\u662f\u540c\u2f00\u6863\u6b21&#xff0c;\u4f46\u662f\u76f8\u5bf9\u2f7d\u2f94&#xff0c;\u63d2\u2f0a\u76f8\u540c\u6570\u91cf\u7684\u7ed3\u70b9&#xff0c;\u7ea2\u2fca\u6811\u7684\u65cb\u8f6c\u6b21\u6570\u662f\u66f4\u5c11\u7684&#xff0c;\u56e0\u4e3a\u4ed6\u5bf9\u5e73\u8861\u7684\u63a7\u5236\u6ca1\u90a3\u4e48\u4e25\u683c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"828\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083241-6a8ff629f1b39.png\" width=\"1828\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1092\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083242-6a8ff62a85490.png\" width=\"2016\" \/><\/p>\n<hr \/>\n<h2>\u4e8c.\u7ea2\u9ed1\u6811\u7684\u5b9e\u73b0<\/h2>\n<p>\u7ea2\u9ed1\u6811\u672c\u8eab\u4e0d\u5173\u5fc3\u4f60\u5b58\u7684\u662f\u4ec0\u4e48&#xff0c;\u5b83\u53ea\u7ba1\u628a\u00a0T\u5f53\u4f5c\u4e00\u4e2a\u6574\u4f53\u5b58\u5230\u8282\u70b9\u91cc\u3002\u81f3\u4e8e\u00a0T\u00a0\u662f\u4ec0\u4e48&#xff0c;\u7531\u4e0a\u5c42\u5bb9\u5668\u6765\u51b3\u5b9a&#xff1a;<\/p>\n<ul>\n<li>\n<p>set\u00a0\u7ed9\u7ea2\u9ed1\u6811\u4f20\u7684\u662f<span style=\"color:#fe2c24\">\u00a0Key<\/span>&#xff0c;\u6240\u4ee5\u7ea2\u9ed1\u6811\u91cc\u5b58\u7684\u5c31\u662f\u952e\u503c\u672c\u8eab<\/p>\n<\/li>\n<li>\n<p>map\u00a0\u7ed9\u7ea2\u9ed1\u6811\u4f20\u7684\u662f<span style=\"color:#fe2c24\">\u00a0pair&lt;const Key, V&gt;<\/span>&#xff0c;\u6240\u4ee5\u7ea2\u9ed1\u6811\u91cc\u5b58\u7684\u5c31\u662f\u952e\u503c\u5bf9&#xff0c;\u5176\u4e2d<span style=\"color:#fe2c24\">\u00a0Key\u00a0\u5e26\u00a0const\u00a0\u662f\u9632\u6b62\u952e\u88ab\u4fee\u6539<\/span><\/p>\n<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u7ea2\u9ed1\u6811\u5c31\u50cf\u4e2a\u901a\u7528\u7684\u67b6\u5b50&#xff0c;set\u00a0\u548c\u00a0map\u00a0\u53ea\u662f\u5f80\u8fd9\u4e2a\u67b6\u5b50\u4e0a\u653e\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u4e1c\u897f\u3002<\/p>\n<hr \/>\n<h3>1.\u5b9a\u4e49\u7ea2\u9ed1\u6811\u7684\u8282\u70b9\u7ed3\u6784<\/h3>\n<p>\/\/ \u989c\u8272\u679a\u4e3e<br \/>\nenum Colour<br \/>\n{<br \/>\n    RED,<br \/>\n    BLACK<br \/>\n};<\/p>\n<p>\/\/ \u7ea2\u9ed1\u6811\u8282\u70b9\u7ed3\u6784\u4f53<br \/>\ntemplate&lt;class T&gt;<br \/>\nstruct RBTreeNode<br \/>\n{<br \/>\n    \/\/ &#8212;&#8212;&#8212;- \u4e09\u53c9\u94fe &#8212;&#8212;&#8212;-<br \/>\n    RBTreeNode&lt;T&gt;* _left;   \/\/ \u5de6\u5b69\u5b50<br \/>\n    RBTreeNode&lt;T&gt;* _right;  \/\/ \u53f3\u5b69\u5b50<br \/>\n    RBTreeNode&lt;T&gt;* _parent; \/\/ \u7236\u8282\u70b9&#xff08;\u65b9\u4fbf\u5411\u4e0a\u56de\u6eaf&#xff09;<\/p>\n<p>    \/\/\u6570\u636e\u57df<br \/>\n    T _data;        \/\/ \u5b58\u50a8\u7684\u6570\u636e&#xff0c;\u53ef\u80fd\u662f Key \u6216 pair&lt;const Key, V&gt;<\/p>\n<p>    \/\/\u989c\u8272\u6807\u8bb0\u8272&#xff0c;\u7ea2\u6216\u9ed1<br \/>\n    Colour _col; <\/p>\n<p>    \/\/\u6784\u9020\u51fd\u6570<br \/>\n    RBTreeNode(const T&amp; data)<br \/>\n        : _left(nullptr)<br \/>\n        , _right(nullptr)<br \/>\n        , _parent(nullptr)<br \/>\n        , _data(data)<br \/>\n        , _col(RED)   \/\/ \u65b0\u8282\u70b9\u9ed8\u8ba4\u7ea2\u8272<br \/>\n    {}<br \/>\n};<\/p>\n<hr \/>\n<h3>2.\u5b9a\u4e49\u7ea2\u9ed1\u6811\u7ed3\u6784<\/h3>\n<p>\u5b9a\u4e49\u7ea2\u9ed1\u6811\u7ed3\u6784&#xff08;KV&#xff09;<\/p>\n<li>K&#xff1a;<span style=\"color:#fe2c24\">\u952e\u503c key \u7684\u7c7b\u578b\u3002<\/span><\/li>\n<li>T&#xff1a;<span style=\"color:#fe2c24\">\u6570\u636e\u7684\u7c7b\u578b<\/span>&#xff0c;\u5982\u679c\u662f map&#xff0c;\u5219\u4e3a pair&lt;const K, V&gt;&#xff1b;\u5982\u679c\u662f set&#xff0c;\u5219\u4e3a K\u3002<\/li>\n<hr \/>\n<p>\u7ea2\u9ed1\u6811\u4f5c\u4e3a\u4e00\u4e2a\u5e95\u5c42\u5bb9\u5668&#xff0c;\u9700\u8981\u540c\u65f6\u652f\u6301 set \u548c map \u4e24\u79cd\u4e0a\u5c42\u5bb9\u5668\u3002\u4f46\u95ee\u9898\u662f&#xff0c;set \u5b58\u7684\u662f Key&#xff0c;map \u5b58\u7684\u662f pair&lt;const Key, V&gt;&#xff0c;\u4e24\u8005\u5728\u63d2\u5165\u65f6\u6bd4\u8f83\u6570\u636e\u7684\u65b9\u5f0f\u4e0d\u540c\u3002\u5982\u679c\u7ea2\u9ed1\u6811\u5199\u6b7b\u4e86\u6bd4\u8f83\u65b9\u5f0f&#xff0c;\u5c31\u53ea\u80fd\u9002\u914d\u4e00\u79cd\u5bb9\u5668&#xff0c;\u6ca1\u6cd5\u901a\u7528\u3002<\/p>\n<p>\u6539\u9020\u4e4b\u524d\u7684\u4ee3\u7801&#xff1a;<\/p>\n<p>\/\/ \u7ea2\u9ed1\u6811\u7c7b\u6a21\u677f<br \/>\n\/\/ K&#xff1a;\u952e\u7684\u7c7b\u578b&#xff08;\u7528\u4e8e\u6bd4\u8f83\u67e5\u627e&#xff09;<br \/>\n\/\/ T&#xff1a;\u8282\u70b9\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b&#xff08;set\u4f20Key&#xff0c;map\u4f20pair&lt;const K, V&gt;&#xff09;<br \/>\ntemplate&lt;class K, class T&gt;<br \/>\nclass RBTree<br \/>\n{<br \/>\n    typedef RBTreeNode&lt;T&gt; Node;<\/p>\n<p>public:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570&#xff1a;\u521d\u59cb\u65f6\u6811\u4e3a\u7a7a&#xff0c;\u6839\u8282\u70b9\u7f6e\u7a7a<br \/>\n    RBTree() : _root(nullptr) {}<\/p>\n<p>    \/\/ \u63d2\u5165\u8282\u70b9&#xff1a;\u5c06\u6570\u636e\u63d2\u5165\u5230\u7ea2\u9ed1\u6811\u4e2d&#xff0c;\u5e76\u4fdd\u6301\u7ea2\u9ed1\u6811\u7684\u6027\u8d28<br \/>\n    bool Insert(const T&amp; data);<\/p>\n<p>    \/\/ &#8230; \u5176\u4ed6\u63a5\u53e3&#xff08;\u67e5\u627e\u3001\u5220\u9664\u3001\u4e2d\u5e8f\u904d\u5386\u7b49&#xff09;<\/p>\n<p>private:<br \/>\n    Node* _root;  \/\/ \u6839\u8282\u70b9\u6307\u9488<\/p>\n<p>\u5728\u7ea2\u9ed1\u6811\u7684\u63d2\u5165\u8282\u70b9\u63a5\u53e3\u4e2d\u8981\u5148\u901a\u8fc7\u6bd4<span style=\"color:#fe2c24\">\u8f83\u8282\u70b9\u4e2d\u6570\u636e\u7684\u5927\u5c0f\u6765\u67e5\u627e\u9002\u5408\u63d2\u5165\u7684\u4f4d\u7f6e<\/span>&#xff0c;\u4f46\u662f\u7ea2\u9ed1\u6811\u5e76\u4e0d\u77e5\u9053\u6570\u636e data \u5230\u5e95\u662f <span style=\"color:#fe2c24\">key \u8fd8\u662f pair&#xff0c;\u6ca1\u6709\u529e\u6cd5\u786e\u5b9a\u3002\u5047\u8bbe\u6570\u636e data \u662f key<\/span>&#xff0c;\u90a3\u4e48\u76f4\u63a5\u53d6 <span style=\"color:#fe2c24\">key <\/span>\u6765\u4f5c\u6bd4\u8f83&#xff1b;\u5982\u679c\u6570\u636e data \u662f pair&#xff0c;\u5219\u9700\u8981\u53d6<span style=\"color:#fe2c24\"> first <\/span>\u6765\u4f5c\u6bd4\u8f83\u3002<\/p>\n<hr \/>\n<p>\u90a3\u5982\u4f55\u89e3\u51b3\u5462&#xff1f;STL\u6e90\u7801\u7ed9\u51fa\u4e86\u8fd9\u6837\u7684\u7ed3\u679c&#xff1a;<span style=\"color:#fe2c24\">\u901a\u8fc7\u4f20\u7ed9\u6a21\u677f\u53c2\u6570 KeyOfValue \u7684\u662f set \u7684\u4eff\u51fd\u6570\u8fd8\u662f map \u7684\u4eff\u51fd\u6570\u6765\u5e94\u5bf9\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u6bd4\u8f83&#xff1a;<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"420\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083243-6a8ff62b3c84a.png\" width=\"1050\" \/><\/p>\n<hr \/>\n<p>\u63a5\u7740\u770b\u6539\u9020\u4e4b\u540e\u7684\u4ee3\u7801&#xff1a;<\/p>\n<\/p>\n<p>\u7ea2\u9ed1\u6811\u53ea\u8d1f\u8d23\u5b58\u6570\u636e\u548c\u7ef4\u62a4\u5e73\u8861&#xff0c;\u4f46\u5b83\u4e0d\u77e5\u9053\u4e0a\u5c42\u4f20\u8fdb\u6765\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\u3002\u6bd4\u8f83\u7684\u65f6\u5019&#xff0c;\u5982\u679c\u662f\u00a0set&#xff0c;\u76f4\u63a5\u6bd4\u8f83\u00a0Key\u00a0\u5c31\u884c&#xff1b;\u5982\u679c\u662f\u00a0map&#xff0c;\u9700\u8981\u53d6\u00a0pair\u00a0\u7684\u00a0first\u00a0\u6765\u6bd4\u8f83\u3002<\/p>\n<p>\u4e3a\u4e86\u8ba9\u7ea2\u9ed1\u6811\u80fd\u7edf\u4e00\u5904\u7406\u8fd9\u4e24\u79cd\u60c5\u51b5&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u7ed9\u5b83\u589e\u52a0\u4e86\u4e00\u4e2a\u6a21\u677f\u53c2\u6570\u00a0KeyOfT&#xff0c;\u5b83\u662f\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b&#xff0c;\u7531\u00a0set\u00a0\u548c\u00a0map\u00a0\u5404\u81ea\u5b9e\u73b0\u5e76\u4f20\u8fdb\u6765<\/span><\/span>&#xff1a;<\/p>\n<ul>\n<li>set\u00a0\u4f20\u7684\u662f\u00a0SetKeyOfT&#xff0c;\u4ece\u00a0T\u00a0\u4e2d\u53d6\u51fa\u00a0Key<\/li>\n<li>map\u00a0\u4f20\u7684\u662f\u00a0MapKeyOfT&#xff0c;\u4ece\u00a0T\u00a0\u4e2d\u53d6\u51fa\u00a0pair.first<\/li>\n<\/ul>\n<p>\u6709\u4e86\u8fd9\u4e2a\u4eff\u51fd\u6570&#xff0c;\u7ea2\u9ed1\u6811\u5728\u63d2\u5165\u65f6\u5c31\u4e0d\u7528\u5173\u5fc3\u00a0T\u00a0\u5230\u5e95\u662f\u4ec0\u4e48\u7c7b\u578b\u4e86&#xff0c;\u53ea\u9700\u8981\u8c03\u7528\u00a0KeyOfT\u00a0\u4ece\u6570\u636e\u4e2d\u53d6\u51fa\u952e\u503c\u6765\u6bd4\u8f83\u5373\u53ef\u3002<\/p>\n<p>\u65b0\u7684\u7ea2\u9ed1\u6811\u7684\u5b9a\u4e49&#xff1a;<\/p>\n<ul>\n<li>K&#xff1a;\u952e\u503ckey\u7684\u7c7b\u578b\u3002<\/li>\n<li>T&#xff1a;\u6570\u636e\u7684\u7c7b\u578b&#xff0c;\u5982\u679c\u662f map&#xff0c;\u5219\u4e3a pair&lt;const K, V&gt;&#xff1b;\u5982\u679c\u662f set&#xff0c;\u5219\u4e3a K\u3002<\/li>\n<li>KeyOfT&#xff1a;\u901a\u8fc7 T \u7684\u7c7b\u578b\u6765\u83b7\u53d6 key \u503c\u7684\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b\u3002<\/li>\n<\/ul>\n<p>\/\/ \u7ea2\u9ed1\u6811\u7c7b\u6a21\u677f&#xff08;\u6539\u9020\u540e&#xff09;<br \/>\ntemplate&lt;class K, class T, class KeyOfT&gt;<br \/>\nclass RBTree<br \/>\n{<br \/>\n    \/\/ \u7ed9\u7ea2\u9ed1\u6811\u8282\u70b9\u7c7b\u578b\u8d77\u522b\u540d<br \/>\n    typedef RBTreeNode&lt;T&gt; Node;<\/p>\n<p>public:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570&#xff1a;\u6811\u521d\u59cb\u4e3a\u7a7a&#xff0c;\u6839\u8282\u70b9\u6307\u5411 nullptr<br \/>\n    RBTree() : _root(nullptr) {}<\/p>\n<p>    \/\/ \u63d2\u5165\u8282\u70b9<br \/>\n    \/\/ \u53c2\u6570 data \u662f\u8981\u63d2\u5165\u7684\u6570\u636e&#xff08;\u53ef\u80fd\u662f Key \u6216 pair&#xff09;<br \/>\n    \/\/ \u8fd4\u56de\u503c&#xff1a;\u5f53\u524d\u662f bool&#xff08;true\/false&#xff09;&#xff0c;\u540e\u7eed\u4f1a\u6539\u6210 pair&lt;iterator, bool&gt;<br \/>\n    \/\/         \u4ee5\u4fbf\u548c STL \u63a5\u53e3\u4fdd\u6301\u4e00\u81f4<br \/>\n    bool Insert(const T&amp; data);<\/p>\n<p>    \/\/ &#8230;..\u5176\u4ed6\u63a5\u53e3\u8fed\u4ee3\u5668\u3001\u67e5\u627e\u3001\u5220\u9664\u3001operator[] \u7b49&#xff09;<\/p>\n<p>private:<br \/>\n    Node* _root;  \/\/ \u6839\u8282\u70b9\u6307\u9488&#xff0c;\u6574\u68f5\u6811\u7684\u5165\u53e3<br \/>\n};<\/p>\n<hr \/>\n<p>\u56fe\u793a\u58f0\u660e&#xff1a;<\/p>\n<p>\u6211\u4eec\u901a\u8fc7<span style=\"color:#fe2c24\"> T \u7684\u7c7b\u578b\u548c\u5bf9\u5e94\u7684\u53d6 T \u7c7b\u578b\u5bf9\u8c61\u7684\u503c\u7684\u4eff\u51fd\u6570<\/span>&#xff0c;\u5c31\u53ef\u4ee5\u8fdb\u884c\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u6bd4\u8f83\u4e86&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"885\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083243-6a8ff62b80640.png\" width=\"1080\" \/><\/p>\n<hr \/>\n<h3>3.\u7ea2\u9ed1\u6811\u7684\u63d2\u5165<\/h3>\n<h4>\u7ea2\u9ed1\u6811\u63d2\u2f0a\u2f00\u4e2a\u503c\u7684\u2f24\u6982\u8fc7\u7a0b&#xff1a;<\/h4>\n<p>1. \u63d2\u2f0a\u2f00\u4e2a\u503c\u6309\u2f06\u53c9\u641c\u7d22\u6811\u89c4\u5219\u8fdb\u2f8f\u63d2\u2f0a&#xff0c;\u63d2\u2f0a\u540e\u6211\u4eec\u53ea\u9700\u8981\u89c2\u5bdf\u662f\u5426\u7b26\u5408\u7ea2\u2fca\u6811\u76844\u6761\u89c4\u5219\u3002<br \/>\n2. \u5982\u679c\u662f\u7a7a\u6811\u63d2\u2f0a&#xff0c;\u65b0\u589e\u7ed3\u70b9\u662f\u2fca\u2f8a\u7ed3\u70b9\u3002\u5982\u679c\u662f\u2fae\u7a7a\u6811\u63d2\u2f0a&#xff0c;\u65b0\u589e\u7ed3\u70b9\u5fc5\u987b\u7ea2\u2f8a\u7ed3\u70b9&#xff0c;\u56e0\u4e3a\u2fae\u7a7a\u6811\u63d2\u2f0a&#xff0c;\u65b0\u589e\u2fca\u2f8a\u7ed3\u70b9\u5c31\u7834\u574f\u4e86\u89c4\u52194&#xff0c;\u89c4\u52194\u662f\u5f88\u96be\u7ef4\u62a4\u7684\u3002<br \/>\n3. \u2fae\u7a7a\u6811\u63d2\u2f0a\u540e&#xff0c;\u65b0\u589e\u7ed3\u70b9\u5fc5\u987b\u7ea2\u2f8a\u7ed3\u70b9&#xff0c;\u5982\u679c\u2f57\u4eb2\u7ed3\u70b9\u662f\u2fca\u2f8a\u7684&#xff0c;\u5219\u6ca1\u6709\u8fdd\u53cd\u4efb\u4f55\u89c4\u5219&#xff0c;\u63d2\u2f0a\u7ed3\u675f<br \/>\n4. \u2fae\u7a7a\u6811\u63d2\u2f0a\u540e&#xff0c;\u65b0\u589e\u7ed3\u70b9\u5fc5\u987b\u7ea2\u2f8a\u7ed3\u70b9&#xff0c;\u5982\u679c\u2f57\u4eb2\u7ed3\u70b9\u662f\u7ea2\u2f8a\u7684&#xff0c;\u5219\u8fdd\u53cd\u89c4\u52193\u3002\u8fdb\u2f00\u6b65\u5206\u6790&#xff0c;c\u662f\u7ea2\u2f8a&#xff0c;p\u4e3a\u7ea2&#xff0c;g\u5fc5\u4e3a\u2fca&#xff0c;\u8fd9\u4e09\u4e2a\u989c\u2f8a\u90fd\u56fa\u5b9a\u4e86&#xff0c;\u5173\u952e\u7684\u53d8\u5316\u770bu\u7684\u60c5\u51b5&#xff0c;\u9700\u8981\u6839\u636eu\u5206\u4e3a\u4ee5\u4e0b\u2f0f\u79cd\u60c5\u51b5\u5206\u522b\u5904\u7406\u3002<\/p>\n<p>\u8bf4\u660e&#xff1a;\u4e0b\u56fe\u4e2d\u5047\u8bbe\u6211\u4eec\u628a\u65b0\u589e\u7ed3\u70b9\u6807\u8bc6\u4e3ac (cur)&#xff0c;c\u7684\u2f57\u4eb2\u6807\u8bc6\u4e3ap(parent)&#xff0c;p\u7684\u2f57\u4eb2\u6807\u8bc6\u4e3a<br \/>\ng(grandfather)&#xff0c;p\u7684\u5144\u5f1f\u6807\u8bc6\u4e3au&#xff08;uncle&#xff09;\u3002<\/p>\n<h4>\u60c5\u51b51&#xff1a;\u53d8\u2f8a<\/h4>\n<p>c\u4e3a\u7ea2&#xff0c;p\u4e3a\u7ea2&#xff0c;g\u4e3a\u2fca&#xff0c;u\u5b58\u5728\u4e14\u4e3a\u7ea2&#xff0c;\u5219\u5c06p\u548cu\u53d8\u2fca&#xff0c;g\u53d8\u7ea2\u3002\u5728\u628ag\u5f53\u505a\u65b0\u7684c&#xff0c;\u7ee7\u7eed\u5f80\u4e0a\u66f4\u65b0\u3002<br \/>\n\u5206\u6790&#xff1a;\u56e0\u4e3ap\u548cu\u90fd\u662f\u7ea2\u2f8a&#xff0c;g\u662f\u2fca\u2f8a&#xff0c;\u628ap\u548cu\u53d8\u2fca&#xff0c;\u5de6\u8fb9\u2f26\u6811\u8def\u5f84\u5404\u589e\u52a0\u2f00\u4e2a\u2fca\u2f8a\u7ed3\u70b9&#xff0c;g\u518d\u53d8\u7ea2&#xff0c;\u76f8\u5f53\u4e8e\u4fdd\u6301g\u6240\u5728\u2f26\u6811\u7684\u2fca\u2f8a\u7ed3\u70b9\u7684\u6570\u91cf\u4e0d\u53d8&#xff0c;\u540c\u65f6\u89e3\u51b3\u4e86c\u548cp\u8fde\u7eed\u7ea2\u2f8a\u7ed3\u70b9\u7684\u95ee\u9898&#xff0c;\u9700\u8981\u7ee7\u7eed\u5f80\u4e0a\u66f4\u65b0\u662f\u56e0\u4e3a&#xff0c;g\u662f\u7ea2\u2f8a&#xff0c;\u5982\u679cg\u7684\u2f57\u4eb2\u8fd8\u662f\u7ea2\u2f8a&#xff0c;\u90a3\u4e48\u5c31\u8fd8\u9700\u8981\u7ee7\u7eed\u5904\u7406&#xff1b;\u5982\u679cg\u7684\u2f57\u4eb2\u662f\u2fca\u2f8a&#xff0c;\u5219\u5904\u7406\u7ed3\u675f\u4e86&#xff1b;\u5982\u679cg\u5c31\u662f\u6574\u68f5\u6811\u7684\u6839&#xff0c;\u518d\u628ag\u53d8\u56de\u2fca\u2f8a\u3002<br \/>\n<span style=\"color:#fe2c24\">\u60c5\u51b51\u53ea\u53d8\u2f8a&#xff0c;\u4e0d\u65cb\u8f6c\u3002\u6240\u4ee5\u2f46\u8bbac\u662fp\u7684\u5de6\u8fd8\u662f\u53f3&#xff0c;p\u662fg\u7684\u5de6\u8fd8\u662f\u53f3&#xff0c;\u90fd\u662f\u4e0a\u2faf\u7684\u53d8\u2f8a\u5904\u7406\u2f45\u5f0f\u3002<\/span><\/p>\n<p>\u56fe0&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1028\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083243-6a8ff62bea155.png\" width=\"2864\" \/><\/p>\n<ul>\n<li>\u00a0\u8ddfAVL\u6811\u7c7b\u4f3c&#xff0c;\u56fe0\u6211\u4eec\u5c55\u2f70\u4e86\u2f00\u79cd\u5177\u4f53\u60c5\u51b5&#xff0c;\u4f46\u662f\u5b9e\u9645\u4e2d\u9700\u8981\u8fd9\u6837\u5904\u7406\u7684\u6709\u5f88\u591a\u79cd\u60c5\u51b5\u3002<\/li>\n<li>\u00a0\u56fe1\u5c06\u4ee5\u4e0a\u7c7b\u4f3c\u7684\u5904\u7406\u8fdb\u2f8f\u4e86\u62bd\u8c61\u8868\u8fbe&#xff0c;d\/e\/f\u4ee3\u8868\u6bcf\u6761\u8def\u5f84\u62e5\u6709hb\u4e2a\u2fca\u2f8a\u7ed3\u70b9\u7684\u2f26\u6811&#xff0c;a\/b\u4ee3\u8868\u6bcf\u6761\u8def\u5f84\u62e5\u6709hb-1\u4e2a\u2fca\u2f8a\u7ed3\u70b9\u7684\u6839\u4e3a\u7ea2\u7684\u2f26\u6811&#xff0c;hb&gt;&#061;0\u3002<\/li>\n<li>\u00a0\u56fe2\/\u56fe3\/\u56fe4&#xff0c;\u5206\u522b\u5c55\u2f70\u4e86hb &#061;&#061; 0\/hb &#061;&#061; 1\/hb &#061;&#061; 2\u7684\u5177\u4f53\u60c5\u51b5\u7ec4\u5408\u5206\u6790&#xff0c;\u5f53hb\u7b49\u4e8e2\u65f6&#xff0c;\u8fd9\u2fa5\u7ec4\u5408\u60c5\u51b5\u4e0a\u767e\u4ebf\u79cd&#xff0c;\u8fd9\u4e9b\u6837\u4f8b\u662f\u5e2e\u52a9\u6211\u4eec\u7406\u89e3&#xff0c;\u4e0d\u8bba\u60c5\u51b5\u591a\u5c11\u79cd&#xff0c;\u591a\u4e48\u590d\u6742&#xff0c;\u5904\u7406\u2f45\u5f0f\u2f00\u6837\u7684&#xff0c;\u53d8\u2f8a\u518d\u7ee7\u7eed\u5f80\u4e0a\u5904\u7406\u5373\u53ef&#xff0c;\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u770b\u62bd\u8c61\u56fe\u5373\u53ef\u3002<\/li>\n<\/ul>\n<p>\u56fe1&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1816\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083245-6a8ff62d59494.png\" width=\"2864\" \/><\/p>\n<p>\u56fe2&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1144\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083247-6a8ff62fd9640.png\" width=\"2864\" \/><\/p>\n<p>\u56fe3&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2080\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083249-6a8ff6316e93a.png\" width=\"2864\" \/><\/p>\n<hr \/>\n<p>\u56fe4&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2644\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083252-6a8ff6344e88f.png\" width=\"2864\" \/><\/p>\n<hr \/>\n<h4>\u60c5\u51b52&#xff1a;\u5355\u65cb&#043;\u53d8\u2f8a<\/h4>\n<p>c\u4e3a\u7ea2&#xff0c;p\u4e3a\u7ea2&#xff0c;g\u4e3a\u2fca&#xff0c;u\u4e0d\u5b58\u5728\u6216\u8005u\u5b58\u5728\u4e14\u4e3a\u2fca&#xff0c;u\u4e0d\u5b58\u5728&#xff0c;\u5219c\u2f00\u5b9a\u662f\u65b0\u589e\u7ed3\u70b9&#xff0c;u\u5b58\u5728\u4e14\u4e3a\u2fca&#xff0c;\u5219c\u2f00\u5b9a\u4e0d\u662f\u65b0\u589e&#xff0c;c\u4e4b\u524d\u662f\u2fca\u2f8a\u7684&#xff0c;\u662f\u5728c\u7684\u2f26\u6811\u4e2d\u63d2\u2f0a&#xff0c;\u7b26\u5408\u60c5\u51b51&#xff0c;\u53d8\u2f8a\u5c06c\u4ece\u2fca\u2f8a\u53d8\u6210\u7ea2\u2f8a&#xff0c;\u66f4\u65b0\u4e0a\u6765\u7684\u3002<\/p>\n<p>\u5206\u6790&#xff1a;p\u5fc5\u987b\u53d8\u2fca&#xff0c;\u624d\u80fd\u89e3\u51b3&#xff0c;\u8fde\u7eed\u7ea2\u2f8a\u7ed3\u70b9\u7684\u95ee\u9898&#xff0c;u\u4e0d\u5b58\u5728\u6216\u8005\u662f\u2fca\u2f8a\u7684&#xff0c;\u8fd9\u2fa5\u5355\u7eaf\u7684\u53d8\u2f8a\u2f46\u6cd5\u89e3\u51b3\u95ee\u9898&#xff0c;\u9700\u8981\u65cb\u8f6c&#043;\u53d8\u2f8a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"139\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083256-6a8ff63819e39.png\" width=\"934\" \/><\/p>\n<hr \/>\n<p>\u5982\u679cp\u662fg\u7684\u5de6&#xff0c;c\u662fp\u7684\u5de6&#xff0c;\u90a3\u4e48\u4ee5g\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u53f3\u5355\u65cb&#xff0c;\u518d\u628ap\u53d8\u2fca&#xff0c;g\u53d8\u7ea2\u5373\u53ef\u3002p\u53d8\u6210\u8bfe\u8fd9\u9897\u6811\u65b0\u7684\u6839&#xff0c;\u8fd9\u6837\u2f26\u6811\u2fca\u2f8a\u7ed3\u70b9\u7684\u6570\u91cf\u4e0d\u53d8&#xff0c;\u6ca1\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u4e86&#xff0c;\u4e14\u4e0d\u9700\u8981\u5f80\u4e0a\u66f4\u65b0&#xff0c;\u56e0\u4e3ap\u7684\u2f57\u4eb2\u662f\u2fca\u2f8a\u8fd8\u662f\u7ea2\u2f8a\u6216\u8005\u7a7a\u90fd\u4e0d\u8fdd\u53cd\u89c4\u5219\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"140\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083256-6a8ff638310cf.png\" width=\"965\" \/><\/p>\n<hr \/>\n<p>\u5982\u679cp\u662fg\u7684\u53f3&#xff0c;c\u662fp\u7684\u53f3&#xff0c;\u90a3\u4e48\u4ee5g\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u5de6\u5355\u65cb&#xff0c;\u518d\u628ap\u53d8\u2fca&#xff0c;g\u53d8\u7ea2\u5373\u53ef\u3002p\u53d8\u6210\u8bfe\u8fd9\u9897\u6811\u65b0\u7684\u6839&#xff0c;\u8fd9\u6837\u2f26\u6811\u2fca\u2f8a\u7ed3\u70b9\u7684\u6570\u91cf\u4e0d\u53d8&#xff0c;\u6ca1\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u4e86&#xff0c;\u4e14\u4e0d\u9700\u8981\u5f80\u4e0a\u66f4\u65b0&#xff0c;\u56e0\u4e3ap\u7684\u2f57\u4eb2\u662f\u2fca\u2f8a\u8fd8\u662f\u7ea2\u2f8a\u6216\u8005\u7a7a\u90fd\u4e0d\u8fdd\u53cd\u89c4\u5219\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"978\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083256-6a8ff63848173.png\" width=\"894\" \/><\/p>\n<h4>\u60c5\u51b53&#xff1a;\u53cc\u65cb&#043;\u53d8\u2f8a<\/h4>\n<p>c\u4e3a\u7ea2&#xff0c;p\u4e3a\u7ea2&#xff0c;g\u4e3a\u2fca&#xff0c;u\u4e0d\u5b58\u5728\u6216\u8005u\u5b58\u5728\u4e14\u4e3a\u2fca&#xff0c;u\u4e0d\u5b58\u5728&#xff0c;\u5219c\u2f00\u5b9a\u662f\u65b0\u589e\u7ed3\u70b9&#xff0c;u\u5b58\u5728\u4e14\u4e3a\u2fca&#xff0c;\u5219c\u2f00\u5b9a\u4e0d\u662f\u65b0\u589e&#xff0c;c\u4e4b\u524d\u662f\u2fca\u2f8a\u7684&#xff0c;\u662f\u5728c\u7684\u2f26\u6811\u4e2d\u63d2\u2f0a&#xff0c;\u7b26\u5408\u60c5\u51b51&#xff0c;\u53d8\u2f8a\u5c06c\u4ece\u2fca\u2f8a\u53d8\u6210\u7ea2\u2f8a&#xff0c;\u66f4\u65b0\u4e0a\u6765\u7684\u3002<\/p>\n<p>\u5206\u6790&#xff1a;p\u5fc5\u987b\u53d8\u2fca&#xff0c;\u624d\u80fd\u89e3\u51b3&#xff0c;\u8fde\u7eed\u7ea2\u2f8a\u7ed3\u70b9\u7684\u95ee\u9898&#xff0c;u\u4e0d\u5b58\u5728\u6216\u8005\u662f\u2fca\u2f8a\u7684&#xff0c;\u8fd9\u2fa5\u5355\u7eaf\u7684\u53d8\u2f8a\u2f46\u6cd5\u89e3\u51b3\u95ee\u9898&#xff0c;\u9700\u8981\u65cb\u8f6c&#043;\u53d8\u2f8a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083256-6a8ff6388b309.png\" width=\"936\" \/><\/p>\n<hr \/>\n<p>\u5982\u679cp\u662fg\u7684\u5de6&#xff0c;c\u662fp\u7684\u53f3&#xff0c;\u90a3\u4e48\u5148\u4ee5p\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u5de6\u5355\u65cb&#xff0c;\u518d\u4ee5g\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u53f3\u5355\u65cb&#xff0c;\u518d\u628ac\u53d8\u2fca&#xff0c;g\u53d8\u7ea2\u5373\u53ef\u3002c\u53d8\u6210\u8bfe\u8fd9\u9897\u6811\u65b0\u7684\u6839&#xff0c;\u8fd9\u6837\u2f26\u6811\u2fca\u2f8a\u7ed3\u70b9\u7684\u6570\u91cf\u4e0d\u53d8&#xff0c;\u6ca1\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u4e86&#xff0c;\u4e14\u4e0d\u9700\u8981\u5f80\u4e0a\u66f4\u65b0&#xff0c;\u56e0\u4e3ac\u7684\u2f57\u4eb2\u662f\u2fca\u2f8a\u8fd8\u662f\u7ea2\u2f8a\u6216\u8005\u7a7a\u90fd\u4e0d\u8fdd\u53cd\u89c4\u5219\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"135\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083256-6a8ff638a22d9.png\" width=\"920\" \/><\/p>\n<hr \/>\n<p>\u5982\u679cp\u662fg\u7684\u53f3&#xff0c;c\u662fp\u7684\u5de6&#xff0c;\u90a3\u4e48\u5148\u4ee5p\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u53f3\u5355\u65cb&#xff0c;\u518d\u4ee5g\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u5de6\u5355\u65cb&#xff0c;\u518d\u628ac\u53d8<br \/>\n\u2fca&#xff0c;g\u53d8\u7ea2\u5373\u53ef\u3002c\u53d8\u6210\u8bfe\u8fd9\u9897\u6811\u65b0\u7684\u6839&#xff0c;\u8fd9\u6837\u2f26\u6811\u2fca\u2f8a\u7ed3\u70b9\u7684\u6570\u91cf\u4e0d\u53d8&#xff0c;\u6ca1\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u4e86&#xff0c;\u4e14\u4e0d\u9700\u8981\u5f80\u4e0a\u66f4\u65b0&#xff0c;\u56e0\u4e3ac\u7684\u2f57\u4eb2\u662f\u2fca\u2f8a\u8fd8\u662f\u7ea2\u2f8a\u6216\u8005\u7a7a\u90fd\u4e0d\u8fdd\u53cd\u89c4\u5219\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"998\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083256-6a8ff638b90e8.png\" width=\"864\" \/><\/p>\n<h4>\u7ea2\u2fca\u6811\u7684\u63d2\u2f0a\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h4>\n<p>\/\/ \u7ea2\u9ed1\u6811\u63d2\u5165<br \/>\n\/\/ \u65cb\u8f6c\u4ee3\u7801\u8ddf AVL \u6811\u4e00\u6837&#xff0c;\u53ea\u662f\u4e0d\u9700\u8981\u66f4\u65b0\u5e73\u8861\u56e0\u5b50<br \/>\n\/\/ \u6838\u5fc3\u56fd\u6210&#xff1a;\u63d2\u5165\u65b0\u8282\u70b9 &#8212;&gt; \u68c0\u6d4b\u662f\u5426\u8fdd\u53cd\u89c4\u5219 &#8212;&gt; \u53d8\u8272\/\u65cb\u8f6c\u4fee\u590d<\/p>\n<p>bool Insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\n    \/\/\u7b2c1\u6b65&#xff1a;\u6811\u4e3a\u7a7a&#xff0c;\u76f4\u63a5\u63d2\u5165\u6839\u8282\u70b9    if (_root &#061;&#061; nullptr)<br \/>\n    {<br \/>\n        _root &#061; new Node(kv);<br \/>\n        _root-&gt;_col &#061; BLACK;  \/\/ \u6839\u8282\u70b9\u5fc5\u987b\u662f\u9ed1\u8272<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c2\u6b65&#xff1a;\u67e5\u627e\u63d2\u5165\u4f4d\u7f6e<br \/>\n    Node* parent &#061; nullptr;<br \/>\n    Node* cur &#061; _root;<\/p>\n<p>    while (cur)<br \/>\n    {<br \/>\n        if (cur-&gt;_kv.first &lt; kv.first)<br \/>\n        {<br \/>\n            parent &#061; cur;<br \/>\n            cur &#061; cur-&gt;_right;<br \/>\n        }<br \/>\n        else if (cur-&gt;_kv.first &gt; kv.first)<br \/>\n        {<br \/>\n            parent &#061; cur;<br \/>\n            cur &#061; cur-&gt;_left;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            return false;  \/\/ \u952e\u5df2\u5b58\u5728&#xff0c;\u63d2\u5165\u5931\u8d25<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/\u7b2c3\u6b65&#xff1a;\u63d2\u5165\u65b0\u8282\u70b9<br \/>\n    cur &#061; new Node(kv);<br \/>\n    cur-&gt;_col &#061; RED;  \/\/ \u65b0\u8282\u70b9\u9ed8\u8ba4\u7ea2\u8272&#xff08;\u4e0d\u5f71\u54cd\u9ed1\u8272\u8def\u5f84&#xff09;<\/p>\n<p>    \/\/ \u8fde\u63a5\u7236\u8282\u70b9<br \/>\n    if (parent-&gt;_kv.first &lt; kv.first)<br \/>\n    {<br \/>\n        parent-&gt;_right &#061; cur;<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        parent-&gt;_left &#061; cur;<br \/>\n    }<br \/>\n    cur-&gt;_parent &#061; parent;<\/p>\n<p>    \/\/\u7b2c4\u6b65&#xff1a;\u8c03\u6574\u5e73\u8861<br \/>\n    \/\/ \u7236\u8282\u70b9\u662f\u7ea2\u8272\u624d\u9700\u8981\u8c03\u6574&#xff08;\u8fdd\u53cd\u4e0d\u80fd\u8fde\u7eed\u7ea2\u89c4\u5219&#xff09;<br \/>\n    while (parent &amp;&amp; parent-&gt;_col &#061;&#061; RED)<br \/>\n    {<br \/>\n        Node* grandfather &#061; parent-&gt;_parent;<\/p>\n<p>        \/\/\u60c5\u51b5\u4e00&#xff1a;\u7236\u8282\u70b9\u662f\u7956\u7236\u7684\u5de6\u5b69\u5b50<br \/>\n        \/\/      g(\u9ed1)<br \/>\n        \/\/     \/     \\\\<br \/>\n        \/\/  p(\u7ea2)   u(?)<\/p>\n<p>        if (parent &#061;&#061; grandfather-&gt;_left)<br \/>\n        {<br \/>\n            Node* uncle &#061; grandfather-&gt;_right;<\/p>\n<p>            \/\/ \u5b50\u60c5\u51b51&#xff1a;\u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2\u8272 \u2192 \u53d8\u8272\u5373\u53ef<br \/>\n            if (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n            {<br \/>\n                parent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\n                grandfather-&gt;_col &#061; RED;<br \/>\n                \/\/ \u7ee7\u7eed\u5f80\u4e0a\u5904\u7406<br \/>\n                cur &#061; grandfather;<br \/>\n                parent &#061; cur-&gt;_parent;<br \/>\n            }<br \/>\n            else  \/\/ \u5b50\u60c5\u51b52&#043;3&#xff1a;\u53d4\u53d4\u4e0d\u5b58\u5728 \u6216 \u53d4\u53d4\u4e3a\u9ed1\u8272 \u2192 \u9700\u8981\u65cb\u8f6c<br \/>\n            {<br \/>\n                if (cur &#061;&#061; parent-&gt;_left)<br \/>\n                {<br \/>\n                    \/\/ \u5b50\u60c5\u51b52&#xff1a;\u5de6\u5de6 \u2192 \u53f3\u5355\u65cb<br \/>\n                    \/\/      g<br \/>\n                    \/\/     \/ \\\\<br \/>\n                    \/\/    p   u<br \/>\n                    \/\/   \/<br \/>\n                    \/\/  c<br \/>\n                    RotateR(grandfather);<br \/>\n                    parent-&gt;_col &#061; BLACK;<br \/>\n                    grandfather-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ \u5b50\u60c5\u51b53&#xff1a;\u5de6\u53f3 \u2192 \u5de6\u53f3\u53cc\u65cb<br \/>\n                    \/\/      g<br \/>\n                    \/\/     \/ \\\\<br \/>\n                    \/\/    p   u<br \/>\n                    \/\/     \\\\<br \/>\n                    \/\/      c<br \/>\n                    RotateL(parent);<br \/>\n                    RotateR(grandfather);<br \/>\n                    cur-&gt;_col &#061; BLACK;<br \/>\n                    grandfather-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                break;  \/\/ \u65cb\u8f6c\u540e\u6811\u5df2\u5e73\u8861&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/\u60c5\u51b5\u4e8c&#xff1a;\u7236\u8282\u70b9\u662f\u7956\u7236\u7684\u53f3\u5b69\u5b50&#xff08;\u5bf9\u79f0<br \/>\n        \/\/      g(\u9ed1)<br \/>\n        \/\/     \/     \\\\<br \/>\n        \/\/  u(?)   p(\u7ea2)<\/p>\n<p>        else<br \/>\n        {<br \/>\n            Node* uncle &#061; grandfather-&gt;_left;<\/p>\n<p>            \/\/ \u5b50\u60c5\u51b51&#xff1a;\u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2\u8272 \u2192 \u53d8\u8272\u5373\u53ef<br \/>\n            if (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n            {<br \/>\n                parent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\n                grandfather-&gt;_col &#061; RED;<br \/>\n                cur &#061; grandfather;<br \/>\n                parent &#061; cur-&gt;_parent;<br \/>\n            }<br \/>\n            else  \/\/ \u5b50\u60c5\u51b52&#043;3&#xff1a;\u53d4\u53d4\u4e0d\u5b58\u5728 \u6216 \u53d4\u53d4\u4e3a\u9ed1\u8272 &#8211;&gt; \u9700\u8981\u65cb\u8f6c<br \/>\n            {<br \/>\n                if (cur &#061;&#061; parent-&gt;_right)<br \/>\n                {<br \/>\n                    \/\/ \u5b50\u60c5\u51b52&#xff1a;\u53f3\u53f3 \u2192 \u5de6\u5355\u65cb<br \/>\n                    \/\/      g<br \/>\n                    \/\/     \/ \\\\<br \/>\n                    \/\/    u   p<br \/>\n                    \/\/         \\\\<br \/>\n                    \/\/          c<br \/>\n                    RotateL(grandfather);<br \/>\n                    parent-&gt;_col &#061; BLACK;<br \/>\n                    grandfather-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ \u5b50\u60c5\u51b53&#xff1a;\u53f3\u5de6 \u2192 \u53f3\u5de6\u53cc\u65cb<br \/>\n                    \/\/      g<br \/>\n                    \/\/     \/ \\\\<br \/>\n                    \/\/    u   p<br \/>\n                    \/\/       \/<br \/>\n                    \/\/      c<br \/>\n                    RotateR(parent);<br \/>\n                    RotateL(grandfather);<br \/>\n                    cur-&gt;_col &#061; BLACK;<br \/>\n                    grandfather-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                break;  \/\/ \u65cb\u8f6c\u540e\u6811\u5df2\u5e73\u8861&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c5\u6b65&#xff1a;\u4fdd\u8bc1\u6839\u8282\u70b9\u662f\u9ed1\u8272<br \/>\n    _root-&gt;_col &#061; BLACK;<br \/>\n    return true;<br \/>\n}<\/p>\n<hr \/>\n<h3>4.\u7ea2\u2fca\u6811\u7684\u67e5\u627e&#xff1a;<\/h3>\n<p>\u6309\u2f06\u53c9\u641c\u7d22\u6811\u903b\u8f91\u5b9e\u73b0\u5373\u53ef&#xff0c;\u641c\u7d22\u6548\u7387\u4e3a O(logN)<\/p>\n<p>\/\/ \u67e5\u627e\u51fd\u6570&#xff1a;\u6839\u636e\u952e\u503c key \u5728\u7ea2\u9ed1\u6811\u4e2d\u67e5\u627e\u5bf9\u5e94\u7684\u8282\u70b9<br \/>\n\/\/ \u8fd4\u56de\u503c&#xff1a;\u627e\u5230\u8fd4\u56de\u8282\u70b9\u6307\u9488&#xff0c;\u627e\u4e0d\u5230\u8fd4\u56de nullptr<\/p>\n<p>Node* Find(const K&amp; key)<br \/>\n{<br \/>\n    Node* cur &#061; _root;  \/\/ \u4ece\u6839\u8282\u70b9\u5f00\u59cb\u67e5\u627e<\/p>\n<p>    while (cur)  \/\/ \u53ea\u8981\u5f53\u524d\u8282\u70b9\u4e0d\u4e3a\u7a7a&#xff0c;\u7ee7\u7eed\u67e5\u627e<br \/>\n    {<br \/>\n        if (cur-&gt;_kv.first &lt; key)      \/\/ \u5f53\u524d\u8282\u70b9\u7684\u952e &lt; \u76ee\u6807\u952e<br \/>\n        {<br \/>\n            cur &#061; cur-&gt;_right;         \/\/ \u76ee\u6807\u66f4\u5927&#xff0c;\u5f80\u53f3\u5b50\u6811\u8d70<br \/>\n        }<br \/>\n        else if (cur-&gt;_kv.first &gt; key) \/\/ \u5f53\u524d\u8282\u70b9\u7684\u952e &gt; \u76ee\u6807\u952e<br \/>\n        {<br \/>\n            cur &#061; cur-&gt;_left;          \/\/ \u76ee\u6807\u66f4\u5c0f&#xff0c;\u5f80\u5de6\u5b50\u6811\u8d70<br \/>\n        }<br \/>\n        else                           \/\/ \u5f53\u524d\u8282\u70b9\u7684\u952e &#061;&#061; \u76ee\u6807\u952e<br \/>\n        {<br \/>\n            return cur;                \/\/ \u627e\u5230\u4e86&#xff0c;\u8fd4\u56de\u5f53\u524d\u8282\u70b9<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return nullptr;  \/\/ \u904d\u5386\u5b8c\u8fd8\u6ca1\u627e\u5230&#xff0c;\u8fd4\u56de\u7a7a\u6307\u9488<br \/>\n}<\/p>\n<hr \/>\n<h3>5.\u7ea2\u2fca\u6811\u7684\u9a8c\u8bc1&#xff1a;<\/h3>\n<p>\u8fd9\u2fa5\u83b7\u53d6\u6700\u2ed3\u8def\u5f84\u548c\u6700\u77ed\u8def\u5f84&#xff0c;\u68c0\u67e5\u6700\u2ed3\u8def\u5f84\u4e0d\u8d85\u8fc7\u6700\u77ed\u8def\u5f84\u76842\u500d\u662f\u4e0d\u53ef\u2f8f\u7684&#xff0c;\u56e0\u4e3a\u5c31\u7b97\u6ee1\u2f9c\u8fd9\u4e2a\u6761\u4ef6&#xff0c;\u7ea2\u2fca\u6811\u4e5f\u53ef\u80fd\u989c\u2f8a\u4e0d\u6ee1\u2f9c\u89c4\u5219&#xff0c;\u5f53\u524d\u6682\u65f6\u6ca1\u51fa\u95ee\u9898&#xff0c;\u540e\u7eed\u7ee7\u7eed\u63d2\u2f0a\u8fd8\u662f\u4f1a\u51fa\u95ee\u9898\u7684\u3002\u6240\u4ee5\u6211\u4eec\u8fd8\u662f\u53bb\u68c0\u67e54\u70b9\u89c4\u5219&#xff0c;\u6ee1\u2f9c\u8fd94\u70b9\u89c4\u5219&#xff0c;\u2f00\u5b9a\u80fd\u4fdd\u8bc1\u6700\u2ed3\u8def\u5f84\u4e0d\u8d85\u8fc7\u6700\u77ed\u8def\u5f84\u76842\u500d\u3002<\/p>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u00a0\u89c4\u52191\u679a\u4e3e\u989c\u2f8a\u7c7b\u578b&#xff0c;\u5929\u7136\u5b9e\u73b0\u4fdd\u8bc1\u4e86\u989c\u2f8a\u4e0d\u662f\u2fca\u2f8a\u5c31\u662f\u7ea2\u2f8a\u3002<\/span><\/span><\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u89c4\u52192\u76f4\u63a5\u68c0\u67e5\u6839\u5373\u53ef<\/span><\/span><\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u89c4\u52193\u524d\u5e8f\u904d\u5386\u68c0\u67e5&#xff0c;\u9047\u5230\u7ea2\u2f8a\u7ed3\u70b9\u67e5\u5b69\u2f26\u4e0d\u592a\u2f45\u4fbf&#xff0c;\u56e0\u4e3a\u5b69\u2f26\u6709\u4e24\u4e2a&#xff0c;\u4e14\u4e0d\u2f00\u5b9a\u5b58\u5728&#xff0c;\u53cd\u8fc7\u6765\u68c0\u67e5\u2f57\u4eb2\u7684\u989c\u2f8a\u5c31\u2f45\u4fbf\u591a\u4e86\u3002<\/span><\/span><\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u89c4\u52194\u524d\u5e8f\u904d\u5386&#xff0c;\u904d\u5386\u8fc7\u7a0b\u4e2d\u2f64\u5f62\u53c2\u8bb0\u5f55\u8ddf\u5230\u5f53\u524d\u7ed3\u70b9\u7684blackNum(\u2fca\u2f8a\u7ed3\u70b9\u6570\u91cf)&#xff0c;\u524d\u5e8f\u904d\u5386\u9047\u5230\u2fca\u2f8a\u7ed3\u70b9\u5c31&#043;&#043;blackNum&#xff0c;\u2f9b\u5230\u7a7a\u5c31\u8ba1\u7b97\u51fa\u4e86\u2f00\u6761\u8def\u5f84\u7684\u2fca\u2f8a\u7ed3\u70b9\u6570\u91cf\u3002\u518d\u4efb\u610f\u2f00\u6761\u8def\u5f84\u2fca\u2f8a\u7ed3\u70b9\u6570\u91cf\u4f5c\u4e3a\u53c2\u8003\u503c&#xff0c;\u4f9d\u6b21\u2f50\u8f83\u5373\u53ef\u3002<\/span><\/span><\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1628\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083257-6a8ff639089c8.png\" width=\"2384\" \/><\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7528\u4e8e<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u9a8c\u8bc1\u4e00\u68f5\u6811\u662f\u5426\u6ee1\u8db3\u7ea2\u9ed1\u6811\u7684\u6027\u8d28<\/span><\/span>&#xff0c;\u4e3b\u8981\u68c0\u67e5\u4e09\u6761&#xff1a;<\/p>\n<p>\u6839\u8282\u70b9\u662f\u9ed1\u8272\u7684&#xff1b;<\/p>\n<p>\u6ca1\u6709\u8fde\u7eed\u7684\u7ea2\u8272\u8282\u70b9&#xff1b;<\/p>\n<p>\u6240\u6709\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u76f8\u540c\u3002<\/p>\n<p>IsBalance &#8212;-\u00a0\u00a0\u5165\u53e3\u51fd\u6570<\/p>\n<p>bool IsBalance()<br \/>\n{<br \/>\n    \/\/ \u7a7a\u6811\u4e5f\u7b97\u7ea2\u9ed1\u6811<br \/>\n    if (_root &#061;&#061; nullptr)<br \/>\n        return true;<\/p>\n<p>    \/\/ \u89c4\u52191&#xff1a;\u6839\u8282\u70b9\u5fc5\u987b\u662f\u9ed1\u8272<br \/>\n    if (_root-&gt;_col &#061;&#061; RED)<br \/>\n        return false;<\/p>\n<p>    \/\/\u8ba1\u7b97\u53c2\u8003\u503c refNum<br \/>\n    \/\/ \u53d6\u6700\u5de6\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u4f5c\u4e3a\u57fa\u51c6\u503c<br \/>\n    \/\/ \u6240\u6709\u8def\u5f84\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u90fd\u5fc5\u987b\u7b49\u4e8e\u8fd9\u4e2a\u503c<br \/>\n    int refNum &#061; 0;<br \/>\n    Node* cur &#061; _root;<br \/>\n    while (cur)<br \/>\n    {<br \/>\n        if (cur-&gt;_col &#061;&#061; BLACK)<br \/>\n        {<br \/>\n            &#043;&#043;refNum;  \/\/ \u7edf\u8ba1\u6700\u5de6\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570<br \/>\n        }<br \/>\n        cur &#061; cur-&gt;_left;  \/\/ \u4e00\u76f4\u5f80\u5de6\u8d70<br \/>\n    }<\/p>\n<p>    \/\/ \u9012\u5f52\u68c0\u67e5\u6bcf\u6761\u8def\u5f84<br \/>\n    return Check(_root, 0, refNum);<br \/>\n}<\/p>\n<p>\u901a\u4fd7\u7406\u89e3&#xff1a;\u5148\u7b97\u51fa\u6807\u51c6\u7b54\u6848&#xff08;\u6700\u5de6\u8def\u5f84\u6709\u591a\u5c11\u4e2a\u9ed1\u8272\u8282\u70b9&#xff09;&#xff0c;\u7136\u540e\u62ff\u7740\u8fd9\u4e2a\u6807\u51c6\u53bb\u68c0\u67e5\u6bcf\u4e00\u6761\u8def\u5f84\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<p>Check &#8212;-\u00a0\u9012\u5f52\u68c0\u67e5\u51fd\u6570<\/p>\n<p>bool Check(Node* root, int blackNum, const int refNum)<br \/>\n{<br \/>\n    \/\/1. \u8d70\u5230\u7a7a\u8282\u70b9<br \/>\n    \/\/ \u8bf4\u660e\u4e00\u6761\u8def\u5f84\u8d70\u5b8c\u4e86&#xff0c;\u68c0\u67e5\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u662f\u5426\u8fbe\u6807<br \/>\n    if (root &#061;&#061; nullptr)<br \/>\n    {<br \/>\n        if (refNum !&#061; blackNum)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;\u5b58\u5728\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u4e0d\u76f8\u7b49\u7684\u8def\u5f84&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/\/2. \u68c0\u67e5\u8fde\u7eed\u7ea2\u8272\u8282\u70b9<br \/>\n    \/\/ \u5f53\u524d\u8282\u70b9\u662f\u7ea2\u8272&#xff0c;\u7236\u8282\u70b9\u4e5f\u662f\u7ea2\u8272 &#8212;&gt; \u8fdd\u89c4<br \/>\n    \/\/ \u68c0\u67e5\u5b69\u5b50\u4e0d\u65b9\u4fbf&#xff08;\u4e24\u4e2a\u5b69\u5b50\u4e0d\u4e00\u5b9a\u5b58\u5728&#xff09;&#xff0c;\u53cd\u8fc7\u6765\u68c0\u67e5\u7236\u4eb2\u66f4\u65b9\u4fbf<br \/>\n    if (root-&gt;_col &#061;&#061; RED &amp;&amp; root-&gt;_parent-&gt;_col &#061;&#061; RED)<br \/>\n    {<br \/>\n        cout &lt;&lt; root-&gt;_kv.first &lt;&lt; &#034;\u5b58\u5728\u8fde\u7eed\u7684\u7ea2\u8272\u8282\u70b9&#034; &lt;&lt; endl;<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/3. \u7edf\u8ba1\u9ed1\u8272\u8282\u70b9<br \/>\n    \/\/ \u5f53\u524d\u8282\u70b9\u662f\u9ed1\u8272&#xff0c;\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570 &#043;1<br \/>\n    if (root-&gt;_col &#061;&#061; BLACK)<br \/>\n    {<br \/>\n        blackNum&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/4. \u9012\u5f52\u68c0\u67e5\u5de6\u53f3\u5b50\u6811<br \/>\n    \/\/ \u4e24\u6761\u8def\u5f84\u90fd\u5fc5\u987b\u6ee1\u8db3\u6761\u4ef6<br \/>\n    return Check(root-&gt;_left, blackNum, refNum)<br \/>\n        &amp;&amp; Check(root-&gt;_right, blackNum, refNum);<br \/>\n}<\/p>\n<hr \/>\n<h3>6.\u7ea2\u9ed1\u6811\u7684\u5220\u9664<\/h3>\n<p>\u7ea2\u2fca\u6811\u7684\u5220\u9664\u6bd4\u8f83\u590d\u6742&#xff0c;\u638c\u63e1\u7ea2\u9ed1\u6811\u7684\u5dee\u8ddd\u5df2\u7ecf\u8db3\u591f\u4e86&#xff0c;\u672c\u8282\u4e0d\u505a\u8bb2\u89e3&#xff0c;\u6709\u5174\u8da3\u7684\u8bdd\u53ef\u53c2\u8003&#xff1a;\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u6216\u8005\u300aSTL\u6e90\u7801\u5256\u6790\u300b\u4e2d\u8bb2\u89e3\u3002<\/p>\n<hr \/>\n<p id=\"articleContentId\">\u4e0b\u9762\u7684\u4ee3\u7801\u8fd9\u662f\u4e00\u68f5\u624b\u5199\u7684\u7ea2\u9ed1\u6811&#xff0c;\u4e13\u95e8\u7ed9 set \u548c map \u5f53\u5e95\u5c42\u6846\u67b6\u7528\u7684&#xff0c;\u56e0\u4e3a\u4e3a\u4e86\u6a21\u62df\u5b9e\u73b0map\u548cset(\u91cc\u9762\u7684\u547d\u540d\u6211\u505a\u4e86\u5927\u81f4\u4fee\u6539\u65b9\u4fbf\u540e\u7eed\u5b66\u4e60&#xff09;\u3002<\/p>\n<p>#pragma once<br \/>\n\/\/ \u989c\u8272\u679a\u4e3e&#xff1a;\u6807\u8bb0\u8282\u70b9\u662f\u7ea2\u8272\u8fd8\u662f\u9ed1\u8272<br \/>\n\/\/ \u7ea2\u9ed1\u6811\u9760\u989c\u8272\u6765\u7ef4\u6301\u5e73\u8861&#xff0c;\u7ea2\u8272\u8282\u70b9\u4e0d\u80fd\u8fde\u7eed\u51fa\u73b0<br \/>\nenum Colour<br \/>\n{<br \/>\n    RED,<br \/>\n    BLACK<br \/>\n};<\/p>\n<p>\/\/ \u7ea2\u9ed1\u6811\u8282\u70b9<br \/>\n\/\/ \u4e09\u53c9\u94fe\u7ed3\u6784&#xff1a;\u5de6\u5b69\u5b50 &#043; \u53f3\u5b69\u5b50 &#043; \u7236\u8282\u70b9&#xff08;\u65b9\u4fbf\u5411\u4e0a\u56de\u6eaf&#xff09;<br \/>\n\/\/ \u5b58\u6570\u636e &#043; \u989c\u8272\u6807\u8bb0<br \/>\n\/\/ \u65b0\u8282\u70b9\u9ed8\u8ba4\u7ea2\u8272&#xff0c;\u63d2\u5165\u65f6\u8c03\u6574\u6210\u672c\u66f4\u4f4e<br \/>\ntemplate&lt;class T&gt;<br \/>\nstruct RBTreeNode<br \/>\n{<br \/>\n    RBTreeNode&lt;T&gt;* _left;     \/\/ \u5de6\u5b69\u5b50<br \/>\n    RBTreeNode&lt;T&gt;* _right;    \/\/ \u53f3\u5b69\u5b50<br \/>\n    RBTreeNode&lt;T&gt;* _parent;   \/\/ \u7236\u8282\u70b9&#xff08;\u5411\u4e0a\u56de\u6eaf\u7528&#xff09;<\/p>\n<p>    T _data;                  \/\/ \u6570\u636e\u57df&#xff08;set\u5b58Key&#xff0c;map\u5b58pair&#xff09;<br \/>\n    Colour _col;              \/\/ \u989c\u8272\u6807\u8bb0<\/p>\n<p>    RBTreeNode(const T&amp; data)<br \/>\n        : _left(nullptr)<br \/>\n        , _right(nullptr)<br \/>\n        , _parent(nullptr)<br \/>\n        , _data(data)<br \/>\n        , _col(RED)           \/\/ \u65b0\u8282\u70b9\u9ed8\u8ba4\u7ea2\u8272<br \/>\n    {}<br \/>\n};<\/p>\n<p>\/\/ \u7ea2\u9ed1\u6811\u8fed\u4ee3\u5668<br \/>\n\/\/ \u652f\u6301 *\u3001-&gt;\u3001&#043;&#043;\u3001&#8211;\u3001&#061;&#061;\u3001!&#061;<br \/>\n\/\/ \u904d\u5386\u987a\u5e8f\u662f\u4e2d\u5e8f\u904d\u5386&#xff08;\u5de6-&gt;\u6839-&gt;\u53f3&#xff09;&#xff0c;\u5f97\u5230\u6709\u5e8f\u5e8f\u5217<\/p>\n<p>template&lt;class T, class Ref, class Ptr&gt;<br \/>\nstruct __RBTreeIterator<br \/>\n{<br \/>\n    typedef RBTreeNode&lt;T&gt; Node;<br \/>\n    typedef __RBTreeIterator&lt;T, Ref, Ptr&gt; Self;<br \/>\n    Node* _node;              \/\/ \u6307\u5411\u5f53\u524d\u8282\u70b9<\/p>\n<p>    __RBTreeIterator(Node* node &#061; nullptr)   \/\/ \u5e26\u9ed8\u8ba4\u503c&#xff0c;\u65b9\u4fbf\u6784\u9020end()<br \/>\n        : _node(node)<br \/>\n    {}<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u89e3\u5f15\u7528\u64cd\u4f5c &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    Ref operator*()<br \/>\n    {<br \/>\n        return _node-&gt;_data;   \/\/ \u8fd4\u56de\u8282\u70b9\u6570\u636e\u7684\u5f15\u7528<br \/>\n    }<\/p>\n<p>    Ptr operator-&gt;()<br \/>\n    {<br \/>\n        return &amp;_node-&gt;_data;  \/\/ \u8fd4\u56de\u8282\u70b9\u6570\u636e\u7684\u6307\u9488<br \/>\n    }<\/p>\n<p>    \/\/\u6bd4\u8f83\u64cd\u4f5c<br \/>\n    bool operator!&#061;(const Self&amp; s) const<br \/>\n    {<br \/>\n        return _node !&#061; s._node;<br \/>\n    }<\/p>\n<p>    bool operator&#061;&#061;(const Self&amp; s) const<br \/>\n    {<br \/>\n        return _node &#061;&#061; s._node;<br \/>\n    }<\/p>\n<p>    \/\/ \u524d\u7f6e&#043;&#043;&#xff1a;\u4e2d\u5e8f\u540e\u7ee7&#xff08;\u4e0b\u4e00\u4e2a\u8282\u70b9&#xff09;<br \/>\n    \/\/ \u5982\u679c\u6709\u53f3\u5b50\u6811&#xff0c;\u627e\u53f3\u5b50\u6811\u7684\u6700\u5de6\u8282\u70b9<br \/>\n    \/\/ \u5982\u679c\u6ca1\u6709\u53f3\u5b50\u6811&#xff0c;\u5411\u4e0a\u627e\u7b2c\u4e00\u4e2a\u4e0d\u662f\u7236\u8282\u70b9\u53f3\u5b69\u5b50\u7684\u7956\u5148<br \/>\n    Self&amp; operator&#043;&#043;()<br \/>\n    {<br \/>\n        if (_node-&gt;_right)<br \/>\n        {<br \/>\n            \/\/ \u6709\u53f3\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\u7684\u6700\u5de6\u8282\u70b9<br \/>\n            Node* left &#061; _node-&gt;_right;<br \/>\n            while (left-&gt;_left)<br \/>\n            {<br \/>\n                left &#061; left-&gt;_left;<br \/>\n            }<br \/>\n            _node &#061; left;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            \/\/ \u6ca1\u53f3\u5b50\u6811 -&gt; \u5f80\u4e0a\u627e<br \/>\n            \/\/ \u76f4\u5230\u5f53\u524d\u8282\u70b9\u4e0d\u662f\u7236\u8282\u70b9\u7684\u53f3\u5b69\u5b50\u65f6\u505c<br \/>\n            Node* parent &#061; _node-&gt;_parent;<br \/>\n            Node* cur &#061; _node;<br \/>\n            while (parent &amp;&amp; cur &#061;&#061; parent-&gt;_right)<br \/>\n            {<br \/>\n                cur &#061; cur-&gt;_parent;<br \/>\n                parent &#061; parent-&gt;_parent;<br \/>\n            }<br \/>\n            _node &#061; parent;<br \/>\n        }<br \/>\n        return *this;<br \/>\n    }<\/p>\n<p>    \/\/ \u524d\u7f6e&#8211;&#xff1a;\u4e2d\u5e8f\u524d\u9a71&#xff08;\u4e0a\u4e00\u4e2a\u8282\u70b9&#xff09;<br \/>\n    \/\/ \u5982\u679c\u6709\u5de6\u5b50\u6811&#xff0c;\u627e\u5de6\u5b50\u6811\u7684\u6700\u53f3\u8282\u70b9<br \/>\n    \/\/ \u5982\u679c\u6ca1\u6709\u5de6\u5b50\u6811&#xff0c;\u5411\u4e0a\u627e\u7b2c\u4e00\u4e2a\u4e0d\u662f\u7236\u8282\u70b9\u5de6\u5b69\u5b50\u7684\u7956\u5148<br \/>\n    Self&amp; operator&#8211;()<br \/>\n    {<br \/>\n        if (_node-&gt;_left)<br \/>\n        {<br \/>\n            \/\/ \u6709\u5de6\u5b50\u6811 -&gt; \u5de6\u5b50\u6811\u7684\u6700\u53f3\u8282\u70b9<br \/>\n            Node* right &#061; _node-&gt;_left;<br \/>\n            while (right-&gt;_right)<br \/>\n            {<br \/>\n                right &#061; right-&gt;_right;<br \/>\n            }<br \/>\n            _node &#061; right;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            \/\/ \u6ca1\u5de6\u5b50\u6811 -&gt; \u5f80\u4e0a\u627e<br \/>\n            \/\/ \u76f4\u5230\u5f53\u524d\u8282\u70b9\u4e0d\u662f\u7236\u8282\u70b9\u7684\u5de6\u5b69\u5b50\u65f6\u505c<br \/>\n            Node* parent &#061; _node-&gt;_parent;<br \/>\n            Node* cur &#061; _node;<br \/>\n            while (parent &amp;&amp; cur &#061;&#061; parent-&gt;_left)<br \/>\n            {<br \/>\n                cur &#061; cur-&gt;_parent;<br \/>\n                parent &#061; parent-&gt;_parent;<br \/>\n            }<br \/>\n            _node &#061; parent;<br \/>\n        }<br \/>\n        return *this;<br \/>\n    }<br \/>\n};<\/p>\n<p>\/\/ \u7ea2\u9ed1\u6811\u672c\u4f53<br \/>\n\/\/ K&#xff1a;\u952e\u7684\u7c7b\u578b&#xff0c;\u7528\u4e8e\u6bd4\u8f83\u67e5\u627e<br \/>\n\/\/ T&#xff1a;\u5b58\u7684\u6570\u636e\u7c7b\u578b&#xff08;set\u4f20Key&#xff0c;map\u4f20pair&lt;const Key, V&gt;&#xff09;<br \/>\n\/\/ KeyOfT&#xff1a;\u4eff\u51fd\u6570\u7c7b&#xff0c;\u4eceT\u4e2d\u53d6\u51fa\u952e\u503cK\u6765\u6bd4\u8f83<br \/>\ntemplate&lt;class K, class T, class KeyOfT&gt;<br \/>\nstruct RBTree<br \/>\n{<br \/>\n    typedef RBTreeNode&lt;T&gt; Node;<\/p>\n<p>public:<br \/>\n    typedef __RBTreeIterator&lt;T, T&amp;, T*&gt; iterator;<\/p>\n<p>    \/\/ begin&#xff1a;\u8fd4\u56de\u6700\u5de6\u8282\u70b9&#xff08;\u6700\u5c0f\u503c&#xff09;<br \/>\n    \/\/ \u4ece\u6839\u4e00\u76f4\u5f80\u5de6\u8d70\u5230\u5e95<br \/>\n    iterator begin()<br \/>\n    {<br \/>\n        Node* left &#061; _root;<br \/>\n        while (left &amp;&amp; left-&gt;_left)<br \/>\n        {<br \/>\n            left &#061; left-&gt;_left;<br \/>\n        }<br \/>\n        return iterator(left);<br \/>\n    }<\/p>\n<p>    \/\/ end&#xff1a;\u8fd4\u56denullptr&#xff08;\u7ed3\u675f\u6807\u5fd7&#xff09;<br \/>\n    iterator end()<br \/>\n    {<br \/>\n        return iterator(nullptr);<br \/>\n    }<\/p>\n<p>    \/\/ \u63d2\u5165\u51fd\u6570 \u2014\u2014 \u6838\u5fc3<br \/>\n    \/\/ \u6b65\u9aa4&#xff1a;<br \/>\n    \/\/   1. \u6811\u4e3a\u7a7a &#8211;&gt; \u76f4\u63a5\u63d2&#xff0c;\u6839\u8282\u70b9\u67d3\u9ed1<br \/>\n    \/\/   2. \u6309BST\u89c4\u5219\u627e\u63d2\u5165\u4f4d\u7f6e<br \/>\n    \/\/   3. \u63d2\u5165\u65b0\u8282\u70b9&#xff0c;\u9ed8\u8ba4\u7ea2\u8272<br \/>\n    \/\/   4. \u7236\u8282\u70b9\u4e3a\u7ea2\u8272 &#8211;&gt; \u9700\u8981\u8c03\u6574&#xff08;\u53d8\u8272\/\u65cb\u8f6c&#xff09;<br \/>\n    \/\/   5. \u6700\u540e\u6839\u8282\u70b9\u5f3a\u5236\u9ed1\u8272<br \/>\n    \/\/ \u8fd4\u56de\u503c&#xff1a;pair&lt;iterator, bool&gt;&#xff08;\u8fed\u4ee3\u5668\u6307\u5411\u63d2\u5165\u8282\u70b9&#xff0c;bool\u8868\u793a\u662f\u5426\u6210\u529f&#xff09;<br \/>\n    pair&lt;iterator, bool&gt; Insert(const T&amp; data)<br \/>\n    {<br \/>\n        KeyOfT kot;  \/\/ \u4eff\u51fd\u6570\u5bf9\u8c61&#xff0c;\u7528\u6765\u53d6\u51fa\u952e\u503c<\/p>\n<p>        \/\/\u6811\u4e3a\u7a7a<br \/>\n        if (_root &#061;&#061; nullptr)<br \/>\n        {<br \/>\n            _root &#061; new Node(data);<br \/>\n            _root-&gt;_col &#061; BLACK;<br \/>\n            return make_pair(iterator(_root), true);<br \/>\n        }<\/p>\n<p>        \/\/ \u627e\u63d2\u5165\u4f4d\u7f6e<br \/>\n        Node* parent &#061; nullptr;<br \/>\n        Node* cur &#061; _root;<br \/>\n        while (cur)<br \/>\n        {<br \/>\n            if (kot(cur-&gt;_data) &lt; kot(data))<br \/>\n            {<br \/>\n                parent &#061; cur;<br \/>\n                cur &#061; cur-&gt;_right;<br \/>\n            }<br \/>\n            else if (kot(cur-&gt;_data) &gt; kot(data))<br \/>\n            {<br \/>\n                parent &#061; cur;<br \/>\n                cur &#061; cur-&gt;_left;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                return make_pair(iterator(cur), false);  \/\/ \u952e\u5df2\u5b58\u5728&#xff0c;\u63d2\u5165\u5931\u8d25<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u63d2\u5165\u65b0\u8282\u70b9<br \/>\n        cur &#061; new Node(data);<br \/>\n        Node* newnode &#061; cur;     \/\/ \u4fdd\u5b58\u65b0\u8282\u70b9\u6307\u9488&#xff0c;\u7528\u4e8e\u8fd4\u56de<br \/>\n        cur-&gt;_col &#061; RED;         \/\/ \u65b0\u8282\u70b9\u9ed8\u8ba4\u7ea2\u8272<\/p>\n<p>        if (kot(parent-&gt;_data) &lt; kot(data))<br \/>\n        {<br \/>\n            parent-&gt;_right &#061; cur;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            parent-&gt;_left &#061; cur;<br \/>\n        }<br \/>\n        cur-&gt;_parent &#061; parent;<\/p>\n<p>        \/\/ \u8c03\u6574\u5e73\u8861&#xff08;\u6838\u5fc3\u96be\u70b9&#xff09;<br \/>\n        \/\/ \u7236\u8282\u70b9\u662f\u7ea2\u8272 &#8211;&gt; \u8fdd\u53cd\u4e0d\u80fd\u8fde\u7eed\u7ea2\u89c4\u5219&#xff0c;\u9700\u8981\u5904\u7406<br \/>\n        \/\/ \u5904\u7406\u65b9\u5f0f&#xff1a;\u770b\u53d4\u53d4\u7684\u989c\u8272<br \/>\n        \/\/    \u53d4\u53d4\u662f\u7ea2\u8272 &#8211;&gt; \u53d8\u8272&#xff0c;\u7ee7\u7eed\u5f80\u4e0a<br \/>\n        \/\/    \u53d4\u53d4\u662f\u9ed1\u8272\u6216\u4e0d\u5b58\u5728 &#8211;&gt; \u65cb\u8f6c &#043; \u53d8\u8272&#xff0c;\u7ed3\u675f<br \/>\n        while (parent &amp;&amp; parent-&gt;_col &#061;&#061; RED)<br \/>\n        {<br \/>\n            Node* grandfater &#061; parent-&gt;_parent;<br \/>\n            assert(grandfater);                    \/\/ \u7237\u7237\u5fc5\u987b\u5b58\u5728<br \/>\n            assert(grandfater-&gt;_col &#061;&#061; BLACK);     \/\/ \u7237\u7237\u5fc5\u987b\u662f\u9ed1\u8272<\/p>\n<p>            \/\/ \u60c5\u51b5A&#xff1a;\u7236\u8282\u70b9\u662f\u7237\u7237\u7684\u5de6\u5b69\u5b50<br \/>\n            if (parent &#061;&#061; grandfater-&gt;_left)<br \/>\n            {<br \/>\n                Node* uncle &#061; grandfater-&gt;_right;<\/p>\n<p>                \/\/ \u60c5\u51b51&#xff1a;\u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2\u8272 \u2192 \u53d8\u8272&#xff0c;\u7ee7\u7eed\u5f80\u4e0a\u5904\u7406<br \/>\n                if (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n                {<br \/>\n                    parent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\n                    grandfater-&gt;_col &#061; RED;<br \/>\n                    cur &#061; grandfater;<br \/>\n                    parent &#061; cur-&gt;_parent;<br \/>\n                }<br \/>\n                \/\/ \u60c5\u51b52&#043;3&#xff1a;\u53d4\u53d4\u4e0d\u5b58\u5728\u6216\u4e3a\u9ed1\u8272 &#8211;&gt; \u65cb\u8f6c &#043; \u53d8\u8272<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ \u60c5\u51b52&#xff1a;\u5de6\u5de6&#xff08;cur\u662fparent\u7684\u5de6\u5b69\u5b50&#xff09;&#8211;&gt; \u53f3\u5355\u65cb<br \/>\n                    if (cur &#061;&#061; parent-&gt;_left)<br \/>\n                    {<br \/>\n                        RotateR(grandfater);<br \/>\n                        parent-&gt;_col &#061; BLACK;<br \/>\n                        grandfater-&gt;_col &#061; RED;<br \/>\n                    }<br \/>\n                    \/\/ \u60c5\u51b53&#xff1a;\u5de6\u53f3&#xff08;cur\u662fparent\u7684\u53f3\u5b69\u5b50&#xff09;&#8211;&gt; \u5de6\u53f3\u53cc\u65cb<br \/>\n                    else<br \/>\n                    {<br \/>\n                        RotateL(parent);<br \/>\n                        RotateR(grandfater);<br \/>\n                        cur-&gt;_col &#061; BLACK;<br \/>\n                        grandfater-&gt;_col &#061; RED;<br \/>\n                    }<br \/>\n                    break;  \/\/ \u8c03\u6574\u5b8c\u6210&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n                }<br \/>\n            }<br \/>\n            \/\/\u60c5\u51b5B&#xff1a;\u7236\u8282\u70b9\u662f\u7237\u7237\u7684\u53f3\u5b69\u5b50&#xff08;\u5bf9\u79f0&#xff09;<br \/>\n            else<br \/>\n            {<br \/>\n                Node* uncle &#061; grandfater-&gt;_left;<\/p>\n<p>                \/\/ \u60c5\u51b51&#xff1a;\u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2\u8272 &#8211;&gt; \u53d8\u8272&#xff0c;\u7ee7\u7eed\u5f80\u4e0a\u5904\u7406<br \/>\n                if (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n                {<br \/>\n                    parent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\n                    grandfater-&gt;_col &#061; RED;<br \/>\n                    cur &#061; grandfater;<br \/>\n                    parent &#061; cur-&gt;_parent;<br \/>\n                }<br \/>\n                \/\/ \u60c5\u51b52&#043;3&#xff1a;\u53d4\u53d4\u4e0d\u5b58\u5728\u6216\u4e3a\u9ed1\u8272 &#8211;&gt; \u65cb\u8f6c &#043; \u53d8\u8272<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ \u60c5\u51b52&#xff1a;\u53f3\u53f3&#xff08;cur\u662fparent\u7684\u53f3\u5b69\u5b50&#xff09;\u2192 \u5de6\u5355\u65cb<br \/>\n                    if (cur &#061;&#061; parent-&gt;_right)<br \/>\n                    {<br \/>\n                        RotateL(grandfater);<br \/>\n                        parent-&gt;_col &#061; BLACK;<br \/>\n                        grandfater-&gt;_col &#061; RED;<br \/>\n                    }<br \/>\n                    \/\/ \u60c5\u51b53&#xff1a;\u53f3\u5de6&#xff08;cur\u662fparent\u7684\u5de6\u5b69\u5b50&#xff09;&#8211;&gt; \u53f3\u5de6\u53cc\u65cb<br \/>\n                    else<br \/>\n                    {<br \/>\n                        RotateR(parent);<br \/>\n                        RotateL(grandfater);<br \/>\n                        cur-&gt;_col &#061; BLACK;<br \/>\n                        grandfater-&gt;_col &#061; RED;<br \/>\n                    }<br \/>\n                    break;  \/\/ \u8c03\u6574\u5b8c\u6210&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/\u6700\u540e\u4fdd\u8bc1\u6839\u8282\u70b9\u662f\u9ed1\u8272<br \/>\n        _root-&gt;_col &#061; BLACK;<br \/>\n        return make_pair(iterator(newnode), true);<br \/>\n    }<\/p>\n<p>    \/\/ \u4e2d\u5e8f\u904d\u5386&#xff1a;\u5de6&#8211;&gt;\u6839&#8211;&gt;\u53f3&#xff0c;\u5f97\u5230\u6709\u5e8f\u5e8f\u5217<br \/>\n    \/\/ \u7528\u6765\u9a8c\u8bc1\u6811\u662f\u5426\u6ee1\u8db3\u4e8c\u53c9\u641c\u7d22\u6811\u6027\u8d28<\/p>\n<p>    void InOrder()<br \/>\n    {<br \/>\n        _InOrder(_root);<br \/>\n        cout &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u9a8c\u8bc1\u7ea2\u9ed1\u6811\u662f\u5426\u5408\u6cd5<br \/>\n    \/\/ \u68c0\u67e5\u4e09\u9879&#xff1a;<br \/>\n    \/\/   1. \u6839\u8282\u70b9\u662f\u9ed1\u8272<br \/>\n    \/\/   2. \u6ca1\u6709\u8fde\u7eed\u7684\u7ea2\u8272\u8282\u70b9<br \/>\n    \/\/   3. \u6240\u6709\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u76f8\u540c<br \/>\n    bool IsBalance()<br \/>\n    {<br \/>\n        if (_root &#061;&#061; nullptr)<br \/>\n        {<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        \/\/ \u89c4\u52191&#xff1a;\u6839\u5fc5\u987b\u662f\u9ed1\u8272<br \/>\n        if (_root-&gt;_col &#061;&#061; RED)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;\u6839\u8282\u70b9\u4e0d\u662f\u9ed1\u8272&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        \/\/ benchmark&#xff1a;\u6700\u5de6\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf&#xff08;\u4f5c\u4e3a\u6807\u51c6\u503c&#xff09;<br \/>\n        int benchmark &#061; 0;<br \/>\n        return PrevCheck(_root, 0, benchmark);<br \/>\n    }<\/p>\n<p>private:<\/p>\n<p>    \/\/ PrevCheck&#xff1a;\u9012\u5f52\u68c0\u67e5\u6bcf\u6761\u8def\u5f84<br \/>\n    \/\/ \u53c2\u6570&#xff1a;<br \/>\n    \/\/   root       \u5f53\u524d\u8282\u70b9<br \/>\n    \/\/   blackNum   \u5f53\u524d\u8def\u5f84\u5df2\u7d2f\u8ba1\u7684\u9ed1\u8272\u8282\u70b9\u6570<br \/>\n    \/\/   benchmark  \u57fa\u51c6\u503c&#xff08;\u6240\u6709\u8def\u5f84\u5fc5\u987b\u76f8\u7b49&#xff09;<br \/>\n    \/\/ \u8fd4\u56de\u503c&#xff1a;true\u8868\u793a\u8be5\u8def\u5f84\u5408\u6cd5&#xff0c;false\u8868\u793a\u4e0d\u5408\u6cd5<\/p>\n<p>    bool PrevCheck(Node* root, int blackNum, int&amp; benchmark)<br \/>\n    {<br \/>\n        \/\/\u8d70\u5230\u7a7a&#xff0c;\u8bf4\u660e\u4e00\u6761\u8def\u5f84\u8d70\u5b8c\u4e86<br \/>\n        if (root &#061;&#061; nullptr)<br \/>\n        {<br \/>\n            \/\/ \u7b2c\u4e00\u6b21\u8d70\u5230\u7a7a&#xff0c;\u8bbe\u7f6e\u57fa\u51c6\u503c<br \/>\n            if (benchmark &#061;&#061; 0)<br \/>\n            {<br \/>\n                benchmark &#061; blackNum;<br \/>\n                return true;<br \/>\n            }<br \/>\n            \/\/ \u68c0\u67e5\u5f53\u524d\u8def\u5f84\u7684\u9ed1\u8282\u70b9\u6570\u662f\u5426\u7b49\u4e8e\u57fa\u51c6\u503c<br \/>\n            if (blackNum !&#061; benchmark)<br \/>\n            {<br \/>\n                cout &lt;&lt; &#034;\u67d0\u6761\u8def\u5f84\u9ed1\u8272\u8282\u70b9\u7684\u6570\u91cf\u4e0d\u76f8\u7b49&#034; &lt;&lt; endl;<br \/>\n                return false;<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        \/\/\u7edf\u8ba1\u9ed1\u8272\u8282\u70b9<br \/>\n        if (root-&gt;_col &#061;&#061; BLACK)<br \/>\n        {<br \/>\n            &#043;&#043;blackNum;<br \/>\n        }<\/p>\n<p>        \/\/ \u89c4\u52192&#xff1a;\u4e0d\u80fd\u6709\u8fde\u7eed\u7ea2\u8272\u8282\u70b9<br \/>\n        \/\/ \u68c0\u67e5\u7236\u4eb2\u6bd4\u68c0\u67e5\u5b69\u5b50\u66f4\u65b9\u4fbf&#xff08;\u5b69\u5b50\u53ef\u80fd\u4e0d\u5b58\u5728&#xff09;<br \/>\n        if (root-&gt;_col &#061;&#061; RED &amp;&amp; root-&gt;_parent-&gt;_col &#061;&#061; RED)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;\u5b58\u5728\u8fde\u7eed\u7684\u7ea2\u8272\u8282\u70b9&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        \/\/ \u9012\u5f52\u68c0\u67e5\u5de6\u53f3\u5b50\u6811<br \/>\n        return PrevCheck(root-&gt;_left, blackNum, benchmark)<br \/>\n            &amp;&amp; PrevCheck(root-&gt;_right, blackNum, benchmark);<br \/>\n    }<\/p>\n<p>    \/\/ \u4e2d\u5e8f\u904d\u5386\u9012\u5f52\u5b9e\u73b0<br \/>\n   void _InOrder(Node* root)<br \/>\n    {<br \/>\n        if (root &#061;&#061; nullptr)<br \/>\n        {<br \/>\n            return;<br \/>\n        }<br \/>\n        _InOrder(root-&gt;_left);<br \/>\n        cout &lt;&lt; root-&gt;_kv.first &lt;&lt; &#034;:&#034; &lt;&lt; root-&gt;_kv.second &lt;&lt; endl;<br \/>\n        _InOrder(root-&gt;_right);<br \/>\n    }<\/p>\n<p>    \/\/ \u5de6\u5355\u65cb<br \/>\n    \/\/ \u9002\u7528\u573a\u666f&#xff1a;\u53f3\u53f3\u578b&#xff08;parent-&gt;_bf &#061;&#061; 2&#xff0c;subR-&gt;_bf &#061;&#061; 1&#xff09;<br \/>\n    \/\/ \u65cb\u8f6c\u540e&#xff1a;parent\u548csubR\u7684\u5e73\u8861\u56e0\u5b50\u90fd\u4e3a0<br \/>\n    void RotateL(Node* parent)<br \/>\n    {<br \/>\n        Node* subR &#061; parent-&gt;_right;<br \/>\n        Node* subRL &#061; subR-&gt;_left;<\/p>\n<p>        \/\/ subRL \u4ea4\u7ed9 parent \u505a\u53f3\u5b50\u6811<br \/>\n        parent-&gt;_right &#061; subRL;<br \/>\n        if (subRL)<br \/>\n        {<br \/>\n            subRL-&gt;_parent &#061; parent;<br \/>\n        }<\/p>\n<p>        Node* ppNode &#061; parent-&gt;_parent;<\/p>\n<p>        \/\/ parent \u6210\u4e3a subR \u7684\u5de6\u5b50\u6811<br \/>\n        subR-&gt;_left &#061; parent;<br \/>\n        parent-&gt;_parent &#061; subR;<\/p>\n<p>        \/\/ subR \u9876\u66ff parent \u7684\u4f4d\u7f6e<br \/>\n        if (_root &#061;&#061; parent)<br \/>\n        {<br \/>\n            _root &#061; subR;<br \/>\n            subR-&gt;_parent &#061; nullptr;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            if (ppNode-&gt;_left &#061;&#061; parent)<br \/>\n            {<br \/>\n                ppNode-&gt;_left &#061; subR;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                ppNode-&gt;_right &#061; subR;<br \/>\n            }<br \/>\n            subR-&gt;_parent &#061; ppNode;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u53f3\u5355\u65cb<br \/>\n    \/\/ \u9002\u7528\u573a\u666f&#xff1a;\u5de6\u5de6\u578b&#xff08;parent-&gt;_bf &#061;&#061; -2&#xff0c;subL-&gt;_bf &#061;&#061; -1&#xff09;<br \/>\n    \/\/ \u65cb\u8f6c\u540e&#xff1a;parent\u548csubL\u7684\u5e73\u8861\u56e0\u5b50\u90fd\u4e3a0<br \/>\n    void RotateR(Node* parent)<br \/>\n    {<br \/>\n        Node* subL &#061; parent-&gt;_left;<br \/>\n        Node* subLR &#061; subL-&gt;_right;<\/p>\n<p>        \/\/ subLR \u4ea4\u7ed9 parent \u505a\u5de6\u5b50\u6811<br \/>\n        parent-&gt;_left &#061; subLR;<br \/>\n        if (subLR)<br \/>\n        {<br \/>\n            subLR-&gt;_parent &#061; parent;<br \/>\n        }<\/p>\n<p>        Node* ppNode &#061; parent-&gt;_parent;<\/p>\n<p>        \/\/ parent \u6210\u4e3a subL \u7684\u53f3\u5b50\u6811<br \/>\n        subL-&gt;_right &#061; parent;<br \/>\n        parent-&gt;_parent &#061; subL;<\/p>\n<p>        \/\/ subL \u9876\u66ff parent \u7684\u4f4d\u7f6e<br \/>\n        if (_root &#061;&#061; parent)<br \/>\n        {<br \/>\n            _root &#061; subL;<br \/>\n            subL-&gt;_parent &#061; nullptr;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            if (ppNode-&gt;_left &#061;&#061; parent)<br \/>\n            {<br \/>\n                ppNode-&gt;_left &#061; subL;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                ppNode-&gt;_right &#061; subL;<br \/>\n            }<br \/>\n            subL-&gt;_parent &#061; ppNode;<br \/>\n        }<br \/>\n    }<\/p>\n<p>private:<br \/>\n    Node* _root &#061; nullptr;  \/\/ \u6839\u8282\u70b9\u6307\u9488<br \/>\n};<\/p>\n<p>\u4ee3\u7801\u5177\u4f53\u5e72\u4e86\u8fd9\u51e0\u4ef6\u4e8b&#xff1a;<br \/>\n\u5b58\u6570\u636e&#xff1a;\u8282\u70b9\u91cc\u6709\u4e00\u4e2a _data&#xff0c;\u53ef\u4ee5\u5b58 Key&#xff08;\u7ed9 set \u7528&#xff09;&#xff0c;\u4e5f\u53ef\u4ee5\u5b58 pair&lt;const Key, V&gt;&#xff08;\u7ed9 map \u7528&#xff09;<\/p>\n<p>\u63d2\u5165\u65f6\u81ea\u52a8\u8c03\u5e73\u8861&#xff1a;\u63d2\u5b8c\u6570\u636e\u540e\u81ea\u52a8\u53d8\u8272\u3001\u65cb\u8f6c&#xff0c;\u4fdd\u8bc1\u6811\u59cb\u7ec8\u5e73\u8861&#xff0c;\u67e5\u627e\u6548\u7387\u7a33\u5b9a\u5728 O(logN)<\/p>\n<p>\u904d\u5386&#xff1a;\u63d0\u4f9b\u4e86\u8fed\u4ee3\u5668&#xff0c;\u652f\u6301 begin() \/ end()&#xff0c;\u53ef\u4ee5\u7528\u8303\u56f4 for \u904d\u5386<\/p>\n<p>\u9a8c\u8bc1&#xff1a;\u81ea\u5e26 IsBalance()&#xff0c;\u68c0\u67e5\u6709\u6ca1\u6709\u8fde\u7eed\u7ea2\u8282\u70b9\u3001\u6bcf\u6761\u8def\u5f84\u9ed1\u8282\u70b9\u6570\u662f\u5426\u4e00\u81f4<\/p>\n<hr \/>\n<h2>\u4e09.\u7ea2\u9ed1\u6811\u7684\u8fed\u4ee3\u5668<\/h2>\n<p>\u8fed\u4ee3\u5668\u8bbe\u8ba1\u8bf4\u660e&#xff1a;<\/p>\n<p>\u8fed\u4ee3\u5668\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u5bb9\u5668&#xff0c;\u8ba9\u7528\u6237\u65e0\u9700\u5173\u5fc3\u5e95\u5c42\u7684\u5177\u4f53\u6570\u636e\u7ed3\u6784\u3002map \u548c set \u7684\u8fed\u4ee3\u5668\u672c\u8d28\u4e0a\u5c31\u662f\u5bf9\u7ea2\u9ed1\u6811\u8fed\u4ee3\u5668\u7684\u5c01\u88c5\u3002<\/p>\n<p>\u5982\u679c\u8981\u5728\u7ea2\u9ed1\u6811\u4e2d\u589e\u52a0\u8fed\u4ee3\u5668\u652f\u6301&#xff0c;\u9700\u8981\u89e3\u51b3\u4ee5\u4e0b\u4e24\u4e2a\u6838\u5fc3\u95ee\u9898&#xff1a;<\/p>\n<h3>1.begin() \u4e0e end() \u7684\u5b9a\u4f4d<\/h3>\n<p>STL \u89c4\u5b9a&#xff0c;begin()\u00a0\u4e0e\u00a0end()\u00a0\u6784\u6210\u4e00\u4e2a\u524d\u95ed\u540e\u5f00\u7684\u533a\u95f4\u00a0[begin, end)\u3002<\/p>\n<p>\u5bf9\u7ea2\u9ed1\u6811\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\u3002\u5728 SGI-STL \u7684\u6e90\u7801\u5b9e\u73b0\u4e2d&#xff0c;\u7ea2\u9ed1\u6811\u8bbe\u6709\u54e8\u5175\u5934\u8282\u70b9&#xff08;header&#xff09;&#xff1a;<\/p>\n<ul>\n<li>begin()\u00a0\u6307\u5411\u7ea2\u9ed1\u6811\u4e2d\u7684\u6700\u5c0f\u8282\u70b9&#xff08;\u5373\u6700\u5de6\u4fa7\u8282\u70b9&#xff09;<\/li>\n<li>end()\u00a0\u6307\u5411\u5934\u8282\u70b9&#xff08;header&#xff09;<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"579\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083258-6a8ff63acda3f.png\" width=\"875\" \/><\/p>\n<hr \/>\n<p>\u95ee\u9898\u6765\u4e86&#xff0c;end() \u5e94\u8be5\u653e\u5728\u54ea\u91cc&#xff1f;<\/p>\n<p>begin()\u00a0\u653e\u5728\u6700\u5de6\u4fa7\u8282\u70b9\u5f88\u5bb9\u6613\u7406\u89e3&#xff0c;\u4f46\u00a0end()\u00a0\u7684\u4f4d\u7f6e\u9700\u8981\u601d\u8003&#xff1a;\u5b83\u5e94\u8be5\u653e\u5728\u6700\u5927\u8282\u70b9&#xff08;\u6700\u53f3\u4fa7\u8282\u70b9&#xff09;\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u95ee\u9898\u5728\u4e8e\u8fd9\u4e2a<span style=\"color:#fe2c24\">\u4e0b\u4e00\u4e2a\u4f4d\u7f6e<\/span>\u5177\u4f53\u5728\u54ea\u91cc&#xff1f;<\/p>\n<p>\u7406\u8bba\u4e0a&#xff0c;\u53ef\u4ee5\u5728\u7ea2\u9ed1\u6811\u4e2d\u589e\u8bbe\u4e00\u4e2a\u5934\u8282\u70b9&#xff08;header&#xff09;\u4f5c\u4e3a\u54e8\u5175&#xff0c;\u8ba9\u00a0end()\u00a0\u6307\u5411\u5b83&#xff0c;\u8fd9\u6837\u5c31\u80fd\u5408\u6cd5\u8868\u793a<span style=\"color:#fe2c24\">\u672b\u5c3e\u4e4b\u540e<\/span>\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u4e0d\u8fc7\u6211\u4eec\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u7406\u89e3\u5e95\u5c42\u539f\u7406&#xff0c;\u505a\u6a21\u62df\u5b9e\u73b0\u65f6\u6ca1\u5fc5\u8981\u641e\u5f97\u90a3\u4e48\u590d\u6742\u3002\u4e3a\u4e86\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01&#xff0c;\u6211\u4eec\u4e0d\u8bbe\u7f6e header \u8282\u70b9&#xff0c;\u76f4\u63a5\u8ba9\u00a0end()\u00a0\u8fd4\u56de\u00a0nullptr\u00a0\u5373\u53ef\u3002<\/p>\n<p>\u8fd9\u6837\u867d\u7136\u548c STL \u6e90\u7801\u7684\u5b9e\u73b0\u6709\u7ec6\u5fae\u5dee\u522b&#xff0c;\u4f46\u6838\u5fc3\u903b\u8f91\u662f\u76f8\u901a\u7684&#xff0c;\u4e0d\u5f71\u54cd\u5bf9\u8fed\u4ee3\u5668\u673a\u5236\u7684\u7406\u89e3\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"679\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083259-6a8ff63b12309.png\" width=\"1083\" \/><\/p>\n<p>\/\/ \u7ea2\u9ed1\u6811\u8fed\u4ee3\u5668\u63a5\u53e3<br \/>\n\/\/ begin() \u8fd4\u56de\u6307\u5411\u6700\u5c0f\u8282\u70b9\u7684\u8fed\u4ee3\u5668<br \/>\n\/\/ end() \u8fd4\u56de\u6307\u5411 nullptr \u7684\u8fed\u4ee3\u5668&#xff08;\u7ed3\u675f\u6807\u5fd7&#xff09;<\/p>\n<p>template&lt;class K, class T, class KeyOfT&gt;<br \/>\nstruct RBTree<br \/>\n{<br \/>\n    typedef RBTreeNode&lt;T&gt; Node;  \/\/ \u8282\u70b9\u7c7b\u578b\u522b\u540d<\/p>\n<p>public:<br \/>\n    typedef _RBTreeIterator&lt;T, T&amp;, T*&gt; iterator;  \/\/ \u8fed\u4ee3\u5668\u7c7b\u578b\u522b\u540d<\/p>\n<p>    \/\/ begin()&#xff1a;\u8fd4\u56de\u6307\u5411\u6811\u4e2d\u6700\u5c0f\u8282\u70b9\u7684\u8fed\u4ee3\u5668<br \/>\n    \/\/ \u6700\u5c0f\u8282\u70b9\u5c31\u662f\u6700\u5de6\u4fa7\u7684\u8282\u70b9&#xff08;\u4e00\u76f4\u5f80\u5de6\u8d70\u5230\u5e95&#xff09;<br \/>\n    \/\/ \u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O(logN)<br \/>\n    iterator begin()<br \/>\n    {<br \/>\n        Node* left &#061; _root;<br \/>\n        while (left &amp;&amp; left-&gt;_left)  \/\/ \u4e00\u76f4\u5f80\u5de6\u8d70&#xff0c;\u76f4\u5230\u6ca1\u6709\u5de6\u5b69\u5b50<br \/>\n        {<br \/>\n            left &#061; left-&gt;_left;<br \/>\n        }<br \/>\n        return iterator(left);  \/\/ \u7528\u6700\u5de6\u8282\u70b9\u6784\u9020\u8fed\u4ee3\u5668<\/p>\n<p>        \/\/ \u7531\u4e8e\u8fed\u4ee3\u5668\u6784\u9020\u51fd\u6570\u652f\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362&#xff08;\u5355\u53c2\u6784\u9020&#xff09;&#xff0c;<br \/>\n        \/\/ \u4e5f\u53ef\u4ee5\u76f4\u63a5\u5199&#xff1a;return left; \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06 Node* \u8f6c\u6362\u4e3a iterator<br \/>\n    }<\/p>\n<p>    \/\/ end()&#xff1a;\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u672b\u5c3e\u4e4b\u540e\u4f4d\u7f6e\u7684\u8fed\u4ee3\u5668<br \/>\n    \/\/ \u6211\u4eec\u7684\u5b9e\u73b0\u4e2d\u76f4\u63a5\u7528 nullptr \u8868\u793a\u7ed3\u675f<br \/>\n    \/\/ \u548c STL \u6e90\u7801\u4e2d\u7528\u54e8\u5175\u5934\u8282\u70b9\u4e0d\u540c&#xff0c;\u4f46\u529f\u80fd\u7b49\u4ef7<\/p>\n<p>    iterator end()<br \/>\n    {<br \/>\n        return iterator(nullptr);  \/\/ nullptr \u4f5c\u4e3a\u7ed3\u675f\u6807\u5fd7<br \/>\n        \/\/ \u4e5f\u53ef\u4ee5\u7b80\u5199\u4e3a&#xff1a;return nullptr;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    Node* _root &#061; nullptr;  \/\/ \u6839\u8282\u70b9<br \/>\n};<\/p>\n<hr \/>\n<h3>2.operator&#043;&#043;()\u4e0eoperator&#8211;() \u00a0<\/h3>\n<p>\u54b1\u662f\u6309\u7167\u4e2d\u5e8f\u904d\u5386&#xff08;\u5de6\u5b50\u6811 &#8211;&gt;\u00a0\u6839 &#8211;&gt; \u53f3\u5b50\u6811&#xff09;\u00a0\u6765\u8d70&#xff0c;\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5&#xff1a;\u00a0<\/p>\n<p>1.<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5f53\u8fed\u4ee3\u5668\u00a0<\/span><span style=\"background-color:#ffd900\">it<\/span><span style=\"background-color:#ffd900\">\u00a0\u6307\u5411\u7684\u8282\u70b9\u6709\u53f3\u5b50\u6811\u65f6&#xff08;\u5373\u00a0<\/span><span style=\"background-color:#ffd900\">_node-&gt;_right<\/span><span style=\"background-color:#ffd900\">\u00a0\u4e0d\u4e3a\u7a7a&#xff09;<\/span><\/span>&#xff0c;\u6267\u884c\u00a0&#043;&#043;it\u00a0\u64cd\u4f5c\u65f6&#xff0c;\u4e0b\u4e00\u4e2a\u8981\u8bbf\u95ee\u7684\u8282\u70b9\u662f\u53f3\u5b50\u6811\u4e2d\u5e8f\u904d\u5386\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002\u53f3\u5b50\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u987a\u5e8f\u4e3a&#xff1a;\u5de6\u5b50\u6811 &#8211;&gt;\u00a0\u6839 &#8211;&gt;\u00a0\u53f3\u5b50\u6811&#xff0c;\u6240\u4ee5\u7b2c\u4e00\u4e2a\u88ab\u8bbf\u95ee\u7684\u8282\u70b9\u5c31\u662f\u53f3\u5b50\u6811\u4e2d\u7684\u6700\u5de6\u8282\u70b9&#xff0c;\u4e5f\u5c31\u662f\u53f3\u5b50\u6811\u4e2d\u503c\u6700\u5c0f\u7684\u8282\u70b9\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"496\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083259-6a8ff63b75113.png\" width=\"790\" \/><\/p>\n<hr \/>\n<p>2.\u5f53\u8fed\u4ee3\u5668 it \u6307\u5411\u7684\u8282\u70b9\u6ca1\u6709\u53f3\u5b50\u6811\u65f6&#xff08;_node-&gt;_right &#061;&#061; nullptr&#xff09;&#xff0c;\u8bf4\u660e\u4ee5 it \u4e3a\u6839\u7684\u5b50\u6811\u5df2\u7ecf\u904d\u5386\u5b8c\u4e86\u3002\u6b64\u65f6\u9700\u8981\u5411\u4e0a\u56de\u6eaf&#xff0c;\u627e\u5230\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u7956\u5148\u8282\u70b9&#xff1a;\u5f53\u524d\u8282\u70b9\u4e0d\u662f\u5176\u7236\u8282\u70b9\u7684\u53f3\u5b69\u5b50&#xff08;\u5373\u5f53\u524d\u8282\u70b9\u662f\u7236\u8282\u70b9\u7684\u5de6\u5b69\u5b50&#xff09;\u3002\u8fd9\u4e2a\u7956\u5148\u8282\u70b9&#xff0c;\u5c31\u662f\u4e0b\u4e00\u4e2a\u8981\u8bbf\u95ee\u7684\u4f4d\u7f6e<\/p>\n<p>\u5219 it&#043;&#043; \u8981\u8bbf\u95ee\u7684\u8282\u70b9\u662f&#xff1a;it \u6307\u5411\u8282\u70b9\u7684\u7236\u4eb2\u7684\u7236\u4eb2&#xff08;\u5373\u8282\u70b9 13&#xff09;\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"424\" src=\"2026-08-271wtehm1dwa5.png\" width=\"795\" \/><\/p>\n<p>it \u6307\u5411\u8282\u70b9 11&#xff0c;\u800c\u8282\u70b9 11 \u7684\u53f3\u5b50\u6811\u4e3a\u7a7a&#xff1b;\u540c\u65f6 it \u662f\u7236\u8282\u70b9 8 \u7684\u53f3\u5b69\u5b50\u3002 \u6b64\u65f6&#xff0c;it&#043;&#043; \u8981\u8bbf\u95ee\u7684\u8282\u70b9\u662f&#xff1a;it \u6240\u6307\u5411\u8282\u70b9\u7684\u7236\u4eb2\u7684\u7236\u4eb2&#xff0c;\u4e5f\u5c31\u662f\u8282\u70b9 13\u3002<\/p>\n<hr \/>\n<p>3.\u5f53 it \u6307\u5411\u7684\u8282\u70b9\u6ca1\u6709\u53f3\u5b50\u6811&#xff0c;\u4e14\u8be5\u8282\u70b9\u662f\u7236\u8282\u70b9\u7684\u5de6\u5b69\u5b50\u65f6&#xff0c;\u8bf4\u660e\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u7684\u5b50\u6811\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u4e86&#xff0c;\u800c\u7236\u8282\u70b9\u8fd8\u6ca1\u6709\u88ab\u8bbf\u95ee\u3002\u56e0\u6b64\u4e0b\u4e00\u4e2a\u8bbf\u95ee\u7684\u5c31\u662f\u5b83\u7684\u7236\u8282\u70b9\u3002\u5219 it&#043;&#043; \u8981\u8bbf\u95ee\u7684\u8282\u70b9\u662f&#xff1a;it \u6307\u5411\u8282\u70b9\u7684\u7236\u4eb2\u8282\u70b9&#xff08;\u5373\u8282\u70b9 17&#xff09;\u3002<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"480\" src=\"2026-08-27nhohft4lmbo.png\" width=\"963\" \/><\/p>\n<p>it \u6307\u5411\u8282\u70b9 15\u7684\u53f3\u5b50\u6811\u4e3a\u7a7a&#xff0c;\u4e14 it \u662f\u5b83\u7236\u8282\u70b9 17 \u7684\u5de6\u5b69\u5b50 \u5219 it&#043;&#043; \u8981\u8bbf\u95ee\u7684\u8282\u70b9\u662f&#xff1a;it \u6307\u5411\u8282\u70b9\u7684\u7236\u8282\u70b9&#xff08;\u5373\u8282\u70b9 17&#xff09;<\/p>\n<p><span style=\"color:#fe2c24\">\u6ce8\u610f<\/span>&#xff1a;\u5f53\u00a0it\u00a0\u8bbf\u95ee\u5230\u6700\u540e\u4e00\u4e2a\u8282\u70b9&#xff08;\u5373\u6811\u4e2d\u7684\u6700\u5927\u8282\u70b9&#xff09;\u65f6&#xff0c;\u8be5\u8282\u70b9\u6ca1\u6709\u53f3\u5b50\u6811&#xff0c;\u56e0\u6b64\u4f1a\u89e6\u53d1\u5411\u4e0a\u56de\u6eaf\u7684\u903b\u8f91\u3002\u6b64\u65f6\u4e0d\u65ad\u5411\u4e0a\u627e\u7236\u8282\u70b9&#xff0c;\u4e00\u8def\u56de\u6eaf\u5230\u6839\u8282\u70b9\u3002\u7531\u4e8e\u6839\u8282\u70b9\u4e5f\u6ca1\u6709\u7236\u8282\u70b9&#xff0c;\u6700\u7ec8\u4f1a\u8d70\u5230\u00a0nullptr&#xff0c;\u8fed\u4ee3\u5668\u53d8\u6210\u00a0end()\u3002\u800c\u6211\u4eec\u7684\u00a0end()\u00a0\u5e95\u5c42\u5c31\u662f\u7528\u00a0nullptr\u00a0\u8868\u793a\u7684&#xff0c;\u6240\u4ee5\u5f53\u00a0it\u00a0\u8bbf\u95ee\u5b8c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u540e\u518d\u6267\u884c\u00a0&#043;&#043;&#xff0c;\u5b83\u5c31\u81ea\u52a8\u7b49\u4e8e\u00a0end()\u00a0\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"360\" src=\"2026-08-27bxaqo0itcvq.png\" width=\"1083\" \/><\/p>\n<p>\u5982\u679c cur \u8d70\u5230\u6700\u540e\u4e00\u4e2a\u8282\u70b9 27 \u65f6&#xff1a; \u7ecf\u8fc7 while \u5faa\u73af&#xff0c;cur \u4f1a\u8d70\u5230\u6839&#xff0c;\u6b64\u65f6 cur \u7236\u4eb2\u4e3a\u7a7a&#xff0c;\u7ed3\u675f\u5faa\u73af&#xff0c;\u8fd4\u56de\u7a7a&#xff0c;\u4e5f\u5373\u662f end ()<\/p>\n<hr \/>\n<li>\n<p>T&#xff1a;\u6570\u636e\u7684\u7c7b\u578b&#xff0c;\u5982\u679c\u662f map&#xff0c;\u5219\u4e3a pair&lt;const K, V&gt;&#xff1b;\u5982\u679c\u662f set&#xff0c;\u5219\u4e3a K\u3002<\/p>\n<\/li>\n<li>Ref&#xff1a;\u6570\u636e\u7684\u5f15\u7528\u3002<\/li>\n<li>Ptr&#xff1a;\u6570\u636e\u7684\u6307\u9488\u3002<\/li>\n<p>\/\/ \u7ea2\u9ed1\u6811\u8fed\u4ee3\u5668<br \/>\n\/\/ \u6a21\u62df\u5b9e\u73b0 STL \u7684\u8fed\u4ee3\u5668&#xff0c;\u652f\u6301\u4e2d\u5e8f\u904d\u5386&#xff08;\u6709\u5e8f\u904d\u5386&#xff09;<\/p>\n<p>template&lt;class T, class Ref, class Ptr&gt;<br \/>\nstruct __RBTreeIterator<br \/>\n{<br \/>\n    \/\/ \u7c7b\u578b\u522b\u540d<br \/>\n    typedef RBTreeNode&lt;T&gt; Node;                       \/\/ \u8282\u70b9\u7c7b\u578b<br \/>\n    typedef __RBTreeIterator&lt;T, Ref, Ptr&gt; Self;      \/\/ \u8fed\u4ee3\u5668\u81ea\u8eab\u7c7b\u578b<\/p>\n<p>    Node* _node;  \/\/ \u8282\u70b9\u6307\u9488&#xff0c;\u6307\u5411\u5f53\u524d\u8fed\u4ee3\u5668\u4f4d\u7f6e<\/p>\n<p>    \/\/\u6784\u9020\u51fd\u6570<br \/>\n    \/\/ \u7528\u8282\u70b9\u6307\u9488\u6784\u9020\u8fed\u4ee3\u5668&#xff08;\u652f\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362&#xff09;<br \/>\n    __RBTreeIterator(Node* node &#061; nullptr)  \/\/ \u5e26\u9ed8\u8ba4\u503c&#xff0c;\u65b9\u4fbf\u6784\u9020 end()<br \/>\n        : _node(node)<br \/>\n    {}<\/p>\n<p>    \/\/ \u89e3\u5f15\u7528\u64cd\u4f5c<br \/>\n    \/\/ operator* &#xff1a;\u8fd4\u56de\u8282\u70b9\u6570\u636e\u7684\u5f15\u7528<br \/>\n    Ref operator*()<br \/>\n    {<br \/>\n        return _node-&gt;_data;<br \/>\n    }<\/p>\n<p>    \/\/ operator-&gt;&#xff1a;\u8fd4\u56de\u8282\u70b9\u6570\u636e\u7684\u6307\u9488<br \/>\n    Ptr operator-&gt;()<br \/>\n    {<br \/>\n        return &amp;_node-&gt;_data;<br \/>\n    }<\/p>\n<p>    \/\/ \u6bd4\u8f83\u64cd\u4f5c<br \/>\n    \/\/ \u6bd4\u8f83\u4e24\u4e2a\u8fed\u4ee3\u5668\u662f\u5426\u76f8\u7b49&#xff08;\u6bd4\u8f83\u8282\u70b9\u6307\u9488\u5373\u53ef&#xff09;<br \/>\n    bool operator!&#061;(const Self&amp; s) const<br \/>\n    {<br \/>\n        return _node !&#061; s._node;<br \/>\n    }<\/p>\n<p>    bool operator&#061;&#061;(const Self&amp; s) const<br \/>\n    {<br \/>\n        return _node &#061;&#061; s._node;<br \/>\n    }<\/p>\n<p>    \/\/ operator&#043;&#043;&#xff1a;\u524d\u7f6e&#043;&#043;&#xff0c;\u6307\u5411\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9<br \/>\n    \/\/ \u4e2d\u5e8f\u904d\u5386\u987a\u5e8f&#xff1a;\u5de6\u5b50\u6811 &#8211;&gt; \u6839 &#8211;&gt; \u53f3\u5b50\u6811<\/p>\n<p>    Self&amp; operator&#043;&#043;()<br \/>\n    {<br \/>\n        \/\/ \u60c5\u51b51&#xff1a;\u5f53\u524d\u8282\u70b9\u6709\u53f3\u5b50\u6811<br \/>\n        \/\/ \u4e0b\u4e00\u4e2a\u8282\u70b9 &#061; \u53f3\u5b50\u6811\u4e2d\u7684\u6700\u5de6\u8282\u70b9&#xff08;\u6700\u5c0f\u503c&#xff09;<br \/>\n        if (_node-&gt;_right !&#061; nullptr)<br \/>\n        {<br \/>\n            Node* left &#061; _node-&gt;_right;<br \/>\n            while (left-&gt;_left)      \/\/ \u4e00\u76f4\u5f80\u5de6\u8d70<br \/>\n            {<br \/>\n                left &#061; left-&gt;_left;<br \/>\n            }<br \/>\n            _node &#061; left;            \/\/ \u627e\u5230\u53f3\u5b50\u6811\u7684\u6700\u5de6\u8282\u70b9<br \/>\n        }<\/p>\n<p>        \/\/ \u60c5\u51b52&#xff1a;\u5f53\u524d\u8282\u70b9\u6ca1\u6709\u53f3\u5b50\u6811<br \/>\n        \/\/ \u9700\u8981\u5411\u4e0a\u56de\u6eaf&#xff0c;\u76f4\u5230\u5f53\u524d\u8282\u70b9\u662f\u5176\u7236\u8282\u70b9\u7684\u5de6\u5b69\u5b50<br \/>\n        else<br \/>\n        {<br \/>\n            Node* parent &#061; _node-&gt;_parent;<br \/>\n            Node* cur &#061; _node;<\/p>\n<p>            \/\/ \u4e0d\u65ad\u5411\u4e0a\u8d70&#xff0c;\u76f4\u5230 cur \u662f parent \u7684\u5de6\u5b69\u5b50<br \/>\n            \/\/ \u6216\u8005 parent \u4e3a\u7a7a&#xff08;\u8bf4\u660e\u8d70\u5230\u4e86\u6839&#xff0c;&#043;&#043; \u540e\u53d8\u6210 end()&#xff09;<br \/>\n            while (parent &amp;&amp; cur &#061;&#061; parent-&gt;_right)<br \/>\n            {<br \/>\n                cur &#061; parent;<br \/>\n                parent &#061; parent-&gt;_parent;<br \/>\n            }<br \/>\n            _node &#061; parent;  \/\/ \u5982\u679c parent \u4e3a\u7a7a&#xff0c;_node \u4e3a nullptr \u2192 end()<br \/>\n        }<\/p>\n<p>        return *this;  \/\/ \u8fd4\u56de\u81ea\u8eab\u7684\u5f15\u7528&#xff08;\u652f\u6301\u94fe\u5f0f\u64cd\u4f5c&#xff09;<br \/>\n    }<\/p>\n<p>    \/\/ operator&#8211;&#xff1a;\u524d\u7f6e&#8211;&#xff0c;\u6307\u5411\u4e2d\u5e8f\u904d\u5386\u7684\u4e0a\u4e00\u4e2a\u8282\u70b9<br \/>\n    \/\/ \u4e2d\u5e8f\u9006\u5e8f\u904d\u5386\u987a\u5e8f&#xff1a;\u53f3\u5b50\u6811 &#8211;&gt; \u6839 &#8211;&gt; \u5de6\u5b50\u6811&#xff08;\u5bf9\u79f0&#xff09;<\/p>\n<p>    Self&amp; operator&#8211;()<br \/>\n    {<br \/>\n        \/\/ \u60c5\u51b51&#xff1a;\u5f53\u524d\u8282\u70b9\u6709\u5de6\u5b50\u6811<br \/>\n        \/\/ \u4e0a\u4e00\u4e2a\u8282\u70b9 &#061; \u5de6\u5b50\u6811\u4e2d\u7684\u6700\u53f3\u8282\u70b9&#xff08;\u6700\u5927\u503c&#xff09;<br \/>\n        if (_node-&gt;_left !&#061; nullptr)<br \/>\n        {<br \/>\n            Node* right &#061; _node-&gt;_left;<br \/>\n            while (right-&gt;_right)    \/\/ \u4e00\u76f4\u5f80\u53f3\u8d70<br \/>\n            {<br \/>\n                right &#061; right-&gt;_right;<br \/>\n            }<br \/>\n            _node &#061; right;           \/\/ \u627e\u5230\u5de6\u5b50\u6811\u7684\u6700\u53f3\u8282\u70b9<br \/>\n        }<\/p>\n<p>        \/\/\u60c5\u51b52&#xff1a;\u5f53\u524d\u8282\u70b9\u6ca1\u6709\u5de6\u5b50\u6811<br \/>\n        \/\/ \u9700\u8981\u5411\u4e0a\u56de\u6eaf&#xff0c;\u76f4\u5230\u5f53\u524d\u8282\u70b9\u662f\u5176\u7236\u8282\u70b9\u7684\u53f3\u5b69\u5b50<br \/>\n        else<br \/>\n        {<br \/>\n            Node* parent &#061; _node-&gt;_parent;<br \/>\n            Node* cur &#061; _node;<\/p>\n<p>            while (parent &amp;&amp; cur &#061;&#061; parent-&gt;_left)<br \/>\n            {<br \/>\n                cur &#061; parent;<br \/>\n                parent &#061; parent-&gt;_parent;<br \/>\n            }<br \/>\n            _node &#061; parent;<br \/>\n        }<\/p>\n<p>        return *this;<br \/>\n    }<br \/>\n};<\/p>\n<hr \/>\n<h2>\u56db.map\u548cset\u7684\u63d2\u5165<\/h2>\n<p>map\u00a0\u548c\u00a0set\u00a0\u7684\u63d2\u5165\u64cd\u4f5c&#xff0c;\u5e95\u5c42\u90fd\u662f\u8c03\u7528\u7ea2\u9ed1\u6811\u7684\u63d2\u5165\u63a5\u53e3\u3002\u6240\u4ee5\u60f3\u8981\u7406\u89e3\u5b83\u4eec&#xff0c;\u5f97\u5148\u641e\u6e05\u695a\u7ea2\u9ed1\u6811\u7684\u63d2\u5165\u662f\u600e\u4e48\u505a\u7684\u3002<\/p>\n<p>\u7ea2\u9ed1\u6811\u7684\u63d2\u5165\u903b\u8f91\u662f\u8fd9\u6837\u7684&#xff1a;\u5148\u6839\u636e\u952e\u503c\u53bb\u6811\u91cc\u627e&#xff0c;\u770b\u8fd9\u4e2a\u952e\u5b58\u4e0d\u5b58\u5728\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u5df2\u7ecf\u5b58\u5728&#xff0c;\u63d2\u5165\u5931\u8d25&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u00a0pair&#xff1a;&lt;\u6307\u5411\u8be5\u952e\u6240\u5728\u8282\u70b9\u7684\u8fed\u4ee3\u5668, false&gt;<\/li>\n<li>\u5982\u679c\u4e0d\u5b58\u5728&#xff0c;\u5c31\u63d2\u5165\u65b0\u8282\u70b9&#xff0c;\u7136\u540e\u8fd4\u56de&#xff1a;&lt;\u6307\u5411\u65b0\u8282\u70b9\u7684\u8fed\u4ee3\u5668, true&gt;<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u7684\u00a0T\u00a0\u662f\u7ea2\u9ed1\u6811\u5b58\u7684\u6570\u636e\u7c7b\u578b&#xff1a;<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u00a0set&#xff0c;T\u00a0\u5c31\u662f\u00a0K&#xff08;\u76f4\u63a5\u5b58\u952e&#xff09;<\/li>\n<li>\u5bf9\u4e8e\u00a0map&#xff0c;T\u00a0\u662f\u00a0pair&lt;const K, V&gt;&#xff08;\u5b58\u952e\u503c\u5bf9&#xff09;<\/li>\n<\/ul>\n<p>\/\/ \u7ea2\u9ed1\u6811\u63d2\u5165\u51fd\u6570<br \/>\n\/\/ \u8fd4\u56de\u503c&#xff1a;pair&lt;iterator, bool&gt;<br \/>\n\/\/ first&#xff1a;\u6307\u5411\u63d2\u5165\u8282\u70b9&#xff08;\u6216\u5df2\u5b58\u5728\u8282\u70b9&#xff09;\u7684\u8fed\u4ee3\u5668<br \/>\n\/\/ second&#xff1a;true \u8868\u793a\u63d2\u5165\u6210\u529f&#xff0c;false \u8868\u793a\u952e\u5df2\u5b58\u5728<\/p>\n<p>pair&lt;iterator, bool&gt; Insert(const T&amp; data)<br \/>\n{<br \/>\n    \/\/ \u60c5\u51b51&#xff1a;\u6811\u4e3a\u7a7a<br \/>\n    if (_root &#061;&#061; nullptr)<br \/>\n    {<br \/>\n        _root &#061; new Node(data);    \/\/ \u521b\u5efa\u6839\u8282\u70b9<br \/>\n        _root-&gt;_col &#061; BLACK;       \/\/ \u6839\u8282\u70b9\u5fc5\u987b\u4e3a\u9ed1\u8272<\/p>\n<p>        return make_pair(iterator(_root), true);<br \/>\n    }<\/p>\n<p>    \/\/ \u60c5\u51b52&#xff1a;\u6811\u4e0d\u4e3a\u7a7a&#xff0c;\u627e\u63d2\u5165\u4f4d\u7f6e<br \/>\n    Node* parent &#061; nullptr;<br \/>\n    Node* cur &#061; _root;<\/p>\n<p>    while (cur)  \/\/ cur \u4e3a\u7a7a\u65f6&#xff0c;\u8bf4\u660e\u627e\u5230\u63d2\u5165\u4f4d\u7f6e\u4e86<br \/>\n    {<br \/>\n        \/\/ \u7528\u4eff\u51fd\u6570\u53d6\u51fa\u952e\u503c\u8fdb\u884c\u6bd4\u8f83<br \/>\n        if (KeyOfT()(data) &gt; KeyOfT()(cur-&gt;_data))  \/\/ \u63d2\u5165\u952e &gt; \u5f53\u524d\u952e &#8211;&gt; \u5f80\u53f3\u8d70<br \/>\n        {<br \/>\n            parent &#061; cur;<br \/>\n            cur &#061; cur-&gt;_right;<br \/>\n        }<br \/>\n        else if (KeyOfT()(data) &lt; KeyOfT()(cur-&gt;_data))  \/\/ \u63d2\u5165\u952e &lt; \u5f53\u524d\u952e &#8211;&gt; \u5f80\u5de6\u8d70<br \/>\n        {<br \/>\n            parent &#061; cur;<br \/>\n            cur &#061; cur-&gt;_left;<br \/>\n        }<br \/>\n        else  \/\/ \u952e\u5df2\u5b58\u5728&#xff0c;\u4e0d\u5141\u8bb8\u91cd\u590d\u63d2\u5165<br \/>\n        {<br \/>\n            return make_pair(iterator(cur), false);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u60c5\u51b53&#xff1a;\u63d2\u5165\u65b0\u8282\u70b9<br \/>\n    cur &#061; new Node(data);      \/\/ \u521b\u5efa\u65b0\u8282\u70b9<br \/>\n    Node* newnode &#061; cur;       \/\/ \u4fdd\u5b58\u65b0\u8282\u70b9\u4f4d\u7f6e&#xff08;\u7528\u4e8e\u8fd4\u56de&#xff09;<br \/>\n    cur-&gt;_col &#061; RED;           \/\/ \u65b0\u8282\u70b9\u9ed8\u8ba4\u7ea2\u8272<\/p>\n<p>    \/\/ \u8fde\u63a5\u7236\u8282\u70b9<br \/>\n    if (KeyOfT()(cur-&gt;_data) &gt; KeyOfT()(parent-&gt;_data))<br \/>\n    {<br \/>\n        parent-&gt;_right &#061; cur;<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        parent-&gt;_left &#061; cur;<br \/>\n    }<br \/>\n    cur-&gt;_parent &#061; parent;     \/\/ \u8bbe\u7f6e\u7236\u6307\u9488<\/p>\n<p>    \/\/ \u60c5\u51b54&#xff1a;\u8c03\u6574\u5e73\u8861<br \/>\n    \/\/ \u7236\u8282\u70b9\u662f\u7ea2\u8272\u65f6\u624d\u9700\u8981\u5904\u7406&#xff08;\u8fdd\u53cd\u4e0d\u80fd\u8fde\u7eed\u7ea2\u89c4\u5219&#xff09;<br \/>\n    while (parent &amp;&amp; parent-&gt;_col &#061;&#061; RED)<br \/>\n    {<br \/>\n        Node* grandfater &#061; parent-&gt;_parent;<br \/>\n        assert(grandfater);<br \/>\n        assert(grandfater-&gt;_col &#061;&#061; BLACK);<\/p>\n<p>        \/\/ \u60c5\u51b5A&#xff1a;\u7236\u8282\u70b9\u662f\u7237\u7237\u7684\u5de6\u5b69\u5b50<br \/>\n        \/\/      g(\u9ed1)<br \/>\n        \/\/     \/     \\\\<br \/>\n        \/\/  p(\u7ea2)   u(?)<br \/>\n        if (parent &#061;&#061; grandfater-&gt;_left)<br \/>\n        {<br \/>\n            Node* uncle &#061; grandfater-&gt;_right;<\/p>\n<p>            \/\/ \u5b50\u60c5\u51b51&#xff1a;\u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2\u8272 &#8211;&gt; \u53d8\u8272&#xff0c;\u7ee7\u7eed\u5f80\u4e0a\u5904\u7406<br \/>\n            if (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n            {<br \/>\n                parent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\n                grandfater-&gt;_col &#061; RED;<br \/>\n                cur &#061; grandfater;<br \/>\n                parent &#061; cur-&gt;_parent;<br \/>\n            }<br \/>\n            \/\/ \u5b50\u60c5\u51b52&#043;3&#xff1a;\u53d4\u53d4\u4e0d\u5b58\u5728\u6216\u4e3a\u9ed1\u8272 &#8211;&gt; \u65cb\u8f6c &#043; \u53d8\u8272<br \/>\n            else<br \/>\n            {<br \/>\n                \/\/ \u5b50\u60c5\u51b52&#xff1a;\u5de6\u5de6 &#8211;&gt; \u53f3\u5355\u65cb<br \/>\n                \/\/      g<br \/>\n                \/\/     \/ \\\\<br \/>\n                \/\/    p   u<br \/>\n                \/\/   \/<br \/>\n                \/\/  c<br \/>\n                if (cur &#061;&#061; parent-&gt;_left)<br \/>\n                {<br \/>\n                    RotateR(grandfater);<br \/>\n                    parent-&gt;_col &#061; BLACK;<br \/>\n                    grandfater-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                \/\/ \u5b50\u60c5\u51b53&#xff1a;\u5de6\u53f3 &#8211;&gt; \u5de6\u53f3\u53cc\u65cb<br \/>\n                \/\/      g<br \/>\n                \/\/     \/ \\\\<br \/>\n                \/\/    p   u<br \/>\n                \/\/     \\\\<br \/>\n                \/\/      c<br \/>\n                else<br \/>\n                {<br \/>\n                    RotateL(parent);<br \/>\n                    RotateR(grandfater);<br \/>\n                    cur-&gt;_col &#061; BLACK;<br \/>\n                    grandfater-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                break;  \/\/ \u8c03\u6574\u5b8c\u6210&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/  \u60c5\u51b5B&#xff1a;\u7236\u8282\u70b9\u662f\u7237\u7237\u7684\u53f3\u5b69\u5b50&#xff08;\u5bf9\u79f0&#xff09;<br \/>\n        \/\/      g(\u9ed1)<br \/>\n        \/\/     \/     \\\\<br \/>\n        \/\/  u(?)   p(\u7ea2)<br \/>\n        else<br \/>\n        {<br \/>\n            Node* uncle &#061; grandfater-&gt;_left;<\/p>\n<p>            \/\/ \u5b50\u60c5\u51b51&#xff1a;\u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2\u8272 &#8211;&gt; \u53d8\u8272&#xff0c;\u7ee7\u7eed\u5f80\u4e0a\u5904\u7406<br \/>\n            if (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n            {<br \/>\n                parent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\n                grandfater-&gt;_col &#061; RED;<br \/>\n                cur &#061; grandfater;<br \/>\n                parent &#061; cur-&gt;_parent;<br \/>\n            }<br \/>\n            \/\/ \u5b50\u60c5\u51b52&#043;3&#xff1a;\u53d4\u53d4\u4e0d\u5b58\u5728\u6216\u4e3a\u9ed1\u8272 &#8211;&gt; \u65cb\u8f6c &#043; \u53d8\u8272<br \/>\n            else<br \/>\n            {<br \/>\n                \/\/ \u5b50\u60c5\u51b52&#xff1a;\u53f3\u53f3 &#8211;&gt; \u5de6\u5355\u65cb<br \/>\n                \/\/      g<br \/>\n                \/\/     \/ \\\\<br \/>\n                \/\/    u   p<br \/>\n                \/\/         \\\\<br \/>\n                \/\/          c<br \/>\n                if (cur &#061;&#061; parent-&gt;_right)<br \/>\n                {<br \/>\n                    RotateL(grandfater);<br \/>\n                    parent-&gt;_col &#061; BLACK;<br \/>\n                    grandfater-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                \/\/ \u5b50\u60c5\u51b53&#xff1a;\u53f3\u5de6 &#8211;&gt; \u53f3\u5de6\u53cc\u65cb<br \/>\n                \/\/      g<br \/>\n                \/\/     \/ \\\\<br \/>\n                \/\/    u   p<br \/>\n                \/\/       \/<br \/>\n                \/\/      c<br \/>\n                else<br \/>\n                {<br \/>\n                    RotateR(parent);<br \/>\n                    RotateL(grandfater);<br \/>\n                    cur-&gt;_col &#061; BLACK;<br \/>\n                    grandfater-&gt;_col &#061; RED;<br \/>\n                }<br \/>\n                break;  \/\/ \u8c03\u6574\u5b8c\u6210&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u60c5\u51b55&#xff1a;\u6700\u540e\u4fdd\u8bc1\u6839\u8282\u70b9\u662f\u9ed1\u8272<br \/>\n    _root-&gt;_col &#061; BLACK;<br \/>\n    return make_pair(iterator(newnode), true);<br \/>\n}<\/p>\n<hr \/>\n<h2>\u4e94.map \u7684\u6a21\u62df\u5b9e\u73b0<\/h2>\n<p>map\u7684\u5e95\u5c42\u7ed3\u6784\u5c31\u662f\u7ea2\u9ed1\u6811&#xff0c;\u56e0\u6b64\u5728 map \u4e2d\u76f4\u63a5\u5c01\u88c5\u4e00\u68f5\u7ea2\u9ed1\u6811&#xff0c;\u7136\u540e\u5c06\u5176\u63a5\u53e3\u5305\u88c5\u4e0b\u5c31\u884c\u3002<\/p>\n<p>namespace hjq<br \/>\n{<\/p>\n<p>    template&lt;class K, class V&gt;<br \/>\n    class map<br \/>\n    {<\/p>\n<p>        \/\/ \u4eff\u51fd\u6570 MapKeyOfT<br \/>\n        \/\/ \u4f5c\u7528&#xff1a;\u4ece pair&lt;K, V&gt; \u4e2d\u53d6\u51fa\u952e\u503c K<br \/>\n        \/\/ \u7ea2\u9ed1\u6811\u5728\u6bd4\u8f83\u65f6\u4e0d\u77e5\u9053\u5b58\u7684\u662f Key \u8fd8\u662f pair&#xff0c;\u901a\u8fc7\u8fd9\u4e2a\u4eff\u51fd\u6570\u544a\u8bc9\u5b83\u600e\u4e48\u53d6\u952e<\/p>\n<p>        struct MapKeyOfT<br \/>\n        {<br \/>\n            const K&amp; operator()(const pair&lt;K, V&gt;&amp; kv)<br \/>\n            {<br \/>\n                return kv.first;   \/\/ \u8fd4\u56de pair \u4e2d\u7684 key<br \/>\n            }<br \/>\n        };<\/p>\n<p>    public:<\/p>\n<p>        \/\/ \u8fed\u4ee3\u5668\u7c7b\u578b\u522b\u540d<br \/>\n        \/\/ \u6ce8\u610f&#xff1a;\u8fd9\u91cc\u7684 iterator \u662f\u7ea2\u9ed1\u6811\u7684\u8fed\u4ee3\u5668&#xff0c;map \u76f4\u63a5\u590d\u7528<br \/>\n        \/\/ typename \u7684\u4f5c\u7528&#xff1a;<br \/>\n        \/\/ \u7f16\u8bd1\u5230\u8fd9\u91cc\u65f6&#xff0c;\u7ea2\u9ed1\u6811\u6a21\u677f\u53ef\u80fd\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316&#xff0c;\u7f16\u8bd1\u5668\u4e0d\u8ba4\u8bc6<br \/>\n        \/\/ RBTree&lt;K, pair&lt;K, V&gt;, MapKeyOfT&gt;::iterator \u662f\u7c7b\u578b\u8fd8\u662f\u9759\u6001\u6210\u5458&#xff1f;<br \/>\n        \/\/ \u52a0\u4e0a typename \u544a\u8bc9\u7f16\u8bd1\u5668&#xff1a;\u8fd9\u662f\u4e2a\u7c7b\u578b&#xff0c;\u7b49\u5b9e\u4f8b\u5316\u540e\u518d\u53bb\u67e5\u627e<\/p>\n<p>        typedef typename RBTree&lt;K, pair&lt;K, V&gt;, MapKeyOfT&gt;::iterator iterator;<\/p>\n<p>        \/\/ begin()&#xff1a;\u8fd4\u56de\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668<br \/>\n        \/\/ \u76f4\u63a5\u590d\u7528\u7ea2\u9ed1\u6811\u7684 begin()<\/p>\n<p>        iterator begin()<br \/>\n        {<br \/>\n            return _t.begin();<br \/>\n        }<\/p>\n<p>        \/\/ end()&#xff1a;\u8fd4\u56de\u6307\u5411\u672b\u5c3e\u7684\u8fed\u4ee3\u5668<br \/>\n        \/\/ \u76f4\u63a5\u590d\u7528\u7ea2\u9ed1\u6811\u7684 end()<\/p>\n<p>        iterator end()<br \/>\n        {<br \/>\n            return _t.end();<br \/>\n        }<\/p>\n<p>        \/\/ insert&#xff1a;\u63d2\u5165\u952e\u503c\u5bf9<br \/>\n        \/\/ \u76f4\u63a5\u8c03\u7528\u7ea2\u9ed1\u6811\u7684 Insert \u63a5\u53e3<br \/>\n        \/\/ \u8fd4\u56de\u503c&#xff1a;pair&lt;iterator, bool&gt;<br \/>\n        \/\/  first&#xff1a;\u6307\u5411\u63d2\u5165\u8282\u70b9&#xff08;\u6216\u5df2\u5b58\u5728\u8282\u70b9&#xff09;\u7684\u8fed\u4ee3\u5668<br \/>\n        \/\/  second&#xff1a;true \u8868\u793a\u63d2\u5165\u6210\u529f&#xff0c;false \u8868\u793a\u952e\u5df2\u5b58\u5728<\/p>\n<p>        pair&lt;iterator, bool&gt; insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n        {<br \/>\n            return _t.Insert(kv);<br \/>\n        }<\/p>\n<p>        \/\/ operator[]&#xff1a;\u901a\u8fc7\u952e\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c<br \/>\n        \/\/ \u529f\u80fd&#xff1a;<br \/>\n        \/\/   1. key \u5b58\u5728 &#8211;&gt; \u8fd4\u56de\u5bf9\u5e94 value \u7684\u5f15\u7528<br \/>\n        \/\/   2. key \u4e0d\u5b58\u5728 &#8211;&gt; \u63d2\u5165 &lt;key, V()&gt;&#xff08;V() \u662f\u9ed8\u8ba4\u503c&#xff09;&#xff0c;\u518d\u8fd4\u56de value \u7684\u5f15\u7528<br \/>\n        \/\/<br \/>\n        \/\/ \u5b9e\u73b0\u539f\u7406&#xff1a;<br \/>\n        \/\/   1. \u7528 make_pair(key, V()) \u6784\u9020\u4e00\u4e2a\u952e\u503c\u5bf9&#xff08;V() \u662f\u7f3a\u7701\u503c&#xff09;<br \/>\n        \/\/   2. \u8c03\u7528 insert \u5c1d\u8bd5\u63d2\u5165<br \/>\n        \/\/   3. \u4e0d\u7ba1\u63d2\u5165\u6210\u529f\u8fd8\u662f\u5931\u8d25&#xff0c;\u90fd\u901a\u8fc7\u8fed\u4ee3\u5668\u53d6\u51fa value \u8fd4\u56de<\/p>\n<p>        V&amp; operator[](const K&amp; key)<br \/>\n        {<br \/>\n            \/\/ make_pair(key, V()) \u7528 key \u548c V \u7c7b\u578b\u7684\u9ed8\u8ba4\u503c\u6784\u9020 pair<br \/>\n            \/\/ V() \u662f\u7f3a\u7701\u503c&#xff0c;\u6bd4\u5982 int \u662f 0&#xff0c;string \u662f &#034;&#034;<br \/>\n            pair&lt;iterator, bool&gt; ret &#061; insert(make_pair(key, V()));<\/p>\n<p>            \/\/ ret.first \u662f\u6307\u5411\u8be5\u952e\u503c\u5bf9\u7684\u8fed\u4ee3\u5668<br \/>\n            \/\/ ret.first-&gt;second \u53d6\u51fa value&#xff0c;\u8fd4\u56de\u5f15\u7528<br \/>\n            return ret.first-&gt;second;<br \/>\n        }<\/p>\n<p>    private:<\/p>\n<p>        \/\/ \u7ea2\u9ed1\u6811\u5e95\u5c42\u5bb9\u5668<br \/>\n        \/\/ \u6a21\u677f\u53c2\u6570&#xff1a;<br \/>\n        \/\/  K&#xff1a;\u952e\u7684\u7c7b\u578b<br \/>\n        \/\/  pair&lt;K, V&gt;&#xff1a;\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b<br \/>\n        \/\/  MapKeyOfT&#xff1a;\u4eff\u51fd\u6570&#xff0c;\u4ece pair \u4e2d\u53d6\u51fa\u952e<\/p>\n<p>        RBTree&lt;K, pair&lt;K, V&gt;, MapKeyOfT&gt; _t;<br \/>\n    };<br \/>\n}<\/p>\n<hr \/>\n<h2>\u516d.set \u7684\u6a21\u62df\u5b9e\u73b0<\/h2>\n<p>set \u7684\u5e95\u5c42\u4e3a\u7ea2\u9ed1\u6811&#xff0c;\u53ea\u9700\u5728 set \u5185\u90e8\u5c01\u88c5\u4e00\u68f5\u7ea2\u9ed1\u6811&#xff0c;\u5c31\u53ef\u4ee5\u5c06\u8be5\u5bb9\u5668\u5b9e\u73b0\u51fa\u6765&#xff08;\u53c2\u8003 map&#xff09;\u3002<\/p>\n<p>namespace hjq<br \/>\n{<\/p>\n<p>    template&lt;class K&gt;<br \/>\n    class set<br \/>\n    {<\/p>\n<p>        \/\/ \u4eff\u51fd\u6570 SetKeyOfT<br \/>\n        \/\/ \u4f5c\u7528&#xff1a;\u4ece K \u4e2d\u53d6\u51fa\u952e\u503c K<br \/>\n        \/\/ \u5bf9\u4e8e set \u6765\u8bf4&#xff0c;\u6570\u636e\u672c\u8eab K \u5c31\u662f\u952e&#xff0c;\u6240\u4ee5\u76f4\u63a5\u8fd4\u56de key \u672c\u8eab<\/p>\n<p>        struct SetKeyOfT<br \/>\n        {<br \/>\n            const K&amp; operator()(const K&amp; key)<br \/>\n            {<br \/>\n                return key;  \/\/ set \u4e2d key \u5c31\u662f\u6570\u636e\u672c\u8eab<br \/>\n            }<br \/>\n        };<\/p>\n<p>    public:<\/p>\n<p>        \/\/ \u8fed\u4ee3\u5668\u7c7b\u578b\u522b\u540d<br \/>\n        \/\/ \u6ce8\u610f&#xff1a;typename \u7684\u4f5c\u7528\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 iterator \u662f\u4e00\u4e2a\u7c7b\u578b<br \/>\n        \/\/ \u56e0\u4e3a\u7f16\u8bd1\u5230\u8fd9\u91cc\u65f6&#xff0c;RBTree&lt;K, K, SetKeyOfT&gt; \u53ef\u80fd\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316<br \/>\n        \/\/ \u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 ::iterator \u662f\u7c7b\u578b\u8fd8\u662f\u9759\u6001\u6210\u5458\u53d8\u91cf<br \/>\n        \/\/ \u52a0\u4e0a typename \u8ba9\u7f16\u8bd1\u5668\u7b49\u5b9e\u4f8b\u5316\u540e\u518d\u53bb\u67e5\u627e<\/p>\n<p>        typedef typename RBTree&lt;K, K, SetKeyOfT&gt;::iterator iterator;<\/p>\n<p>        \/\/ begin()&#xff1a;\u8fd4\u56de\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668<br \/>\n        \/\/ \u76f4\u63a5\u590d\u7528\u7ea2\u9ed1\u6811\u7684 begin()<br \/>\n        iterator begin()<br \/>\n        {<br \/>\n            return _t.begin();<br \/>\n        }<\/p>\n<p>        \/\/ end()&#xff1a;\u8fd4\u56de\u6307\u5411\u672b\u5c3e\u7684\u8fed\u4ee3\u5668<br \/>\n        \/\/ \u76f4\u63a5\u590d\u7528\u7ea2\u9ed1\u6811\u7684 end()<\/p>\n<p>        iterator end()<br \/>\n        {<br \/>\n            return _t.end();<br \/>\n        }<\/p>\n<p>        \/\/ insert&#xff1a;\u63d2\u5165\u5143\u7d20<br \/>\n        \/\/ \u76f4\u63a5\u8c03\u7528\u7ea2\u9ed1\u6811\u7684 Insert \u63a5\u53e3<br \/>\n        \/\/ \u8fd4\u56de\u503c&#xff1a;pair&lt;iterator, bool&gt;<br \/>\n        \/\/ first&#xff1a;\u6307\u5411\u63d2\u5165\u8282\u70b9&#xff08;\u6216\u5df2\u5b58\u5728\u8282\u70b9&#xff09;\u7684\u8fed\u4ee3\u5668<br \/>\n        \/\/ second&#xff1a;true \u8868\u793a\u63d2\u5165\u6210\u529f&#xff0c;false \u8868\u793a\u952e\u5df2\u5b58\u5728<\/p>\n<p>        pair&lt;iterator, bool&gt; insert(const K&amp; key)<br \/>\n        {<br \/>\n            return _t.Insert(key);  \/\/ \u5e95\u5c42\u8c03\u7528\u7ea2\u9ed1\u6811\u7684\u63d2\u5165<br \/>\n        }<\/p>\n<p>        \/\/ inorder&#xff1a;\u4e2d\u5e8f\u904d\u5386<br \/>\n        \/\/ \u7528\u4e8e\u6253\u5370 set \u4e2d\u7684\u6240\u6709\u5143\u7d20&#xff08;\u6709\u5e8f&#xff09;<\/p>\n<p>        void inorder()<br \/>\n        {<br \/>\n            _t.InOrder();<br \/>\n        }<\/p>\n<p>    private:<\/p>\n<p>        \/\/ \u7ea2\u9ed1\u6811\u5e95\u5c42\u5bb9\u5668<br \/>\n        \/\/ \u6a21\u677f\u53c2\u6570&#xff1a;<br \/>\n        \/\/  K&#xff1a;\u952e\u7684\u7c7b\u578b<br \/>\n        \/\/  K&#xff1a;\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b&#xff08;set \u4e2d\u6570\u636e\u548c\u952e\u662f\u540c\u4e00\u4e2a\u7c7b\u578b&#xff09;<br \/>\n        \/\/  SetKeyOfT&#xff1a;\u4eff\u51fd\u6570&#xff0c;\u4ece\u6570\u636e\u4e2d\u53d6\u51fa\u952e&#xff08;\u76f4\u63a5\u8fd4\u56de\u81ea\u8eab&#xff09;<br \/>\n        RBTree&lt;K, K, SetKeyOfT&gt; _t;<br \/>\n    };<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00. \u7ea2\u2fca\u6811\u7684\u6982\u5ff5\u7ea2\u2fca\u6811\u662f\u2f00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811&#xff0c;\u4ed6\u7684\u6bcf\u4e2a\u7ed3\u70b9\u589e\u52a0\u2f00\u4e2a\u5b58\u50a8\u4f4d\u6765\u8868\u793a\u7ed3\u70b9\u7684\u989c\u2f8a&#xff0c;\u53ef\u4ee5\u662f\u7ea2\u2f8a\u6216\u8005\u2fca\u2f8a\u3002\u901a\u8fc7\u5bf9\u4efb\u4f55\u2f00\u6761\u4ece\u6839\u5230\u53f6\u2f26\u7684\u8def\u5f84\u4e0a\u5404\u4e2a\u7ed3\u70b9\u7684\u989c\u2f8a\u8fdb\u2f8f\u7ea6\u675f&#xff0c;\u7ea2\u2fca\u6811\u786e\u4fdd\u6ca1\u6709\u2f00\u6761\u8def\u5f84\u4f1a\u6bd4\u5176\u4ed6\u8def\u5f84\u957f\u51fa2\u500d&#xff0c;\u56e0\u2f7d\u662f\u63a5\u8fd1\u5e73\u8861\u7684\u30021.\u7ea2\u9ed1\u6811\u7684\u89c4\u5219&#xff1a;\u6bcf\u4e2a\u7ed3\u70b9\u4e0d\u662f\u7ea2\u2f8a\u5c31\u662f\u2fca\u2f8a\u6839\u7ed3\u70b9\u662f\u2fca\u2f8a\u7684\u5982\u679c\u2f00\u4e2a\u7ed3\u70b9\u662f\u7ea2\u2f8a\u7684&#xff0c;\u5219\u5b83\u7684\u4e24\u4e2a\u5b69\u2f26\u7ed3\u70b9\u5fc5\u987b\u662f\u2fca\u2f8a\u7684&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u4efb\u610f\u2f00\u6761\u8def\u5f84\u4e0d\u4f1a\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u3002\u5bf9\u4e8e\u4efb<\/p>\n","protected":false},"author":2,"featured_media":96406,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9139,11035,11036,11037,11038],"topic":[],"class_list":["post-96426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-9139","tag-11035","tag-mapset","tag-11037","tag-11038"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/96426.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00. \u7ea2\u2fca\u6811\u7684\u6982\u5ff5\u7ea2\u2fca\u6811\u662f\u2f00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811&#xff0c;\u4ed6\u7684\u6bcf\u4e2a\u7ed3\u70b9\u589e\u52a0\u2f00\u4e2a\u5b58\u50a8\u4f4d\u6765\u8868\u793a\u7ed3\u70b9\u7684\u989c\u2f8a&#xff0c;\u53ef\u4ee5\u662f\u7ea2\u2f8a\u6216\u8005\u2fca\u2f8a\u3002\u901a\u8fc7\u5bf9\u4efb\u4f55\u2f00\u6761\u4ece\u6839\u5230\u53f6\u2f26\u7684\u8def\u5f84\u4e0a\u5404\u4e2a\u7ed3\u70b9\u7684\u989c\u2f8a\u8fdb\u2f8f\u7ea6\u675f&#xff0c;\u7ea2\u2fca\u6811\u786e\u4fdd\u6ca1\u6709\u2f00\u6761\u8def\u5f84\u4f1a\u6bd4\u5176\u4ed6\u8def\u5f84\u957f\u51fa2\u500d&#xff0c;\u56e0\u2f7d\u662f\u63a5\u8fd1\u5e73\u8861\u7684\u30021.\u7ea2\u9ed1\u6811\u7684\u89c4\u5219&#xff1a;\u6bcf\u4e2a\u7ed3\u70b9\u4e0d\u662f\u7ea2\u2f8a\u5c31\u662f\u2fca\u2f8a\u6839\u7ed3\u70b9\u662f\u2fca\u2f8a\u7684\u5982\u679c\u2f00\u4e2a\u7ed3\u70b9\u662f\u7ea2\u2f8a\u7684&#xff0c;\u5219\u5b83\u7684\u4e24\u4e2a\u5b69\u2f26\u7ed3\u70b9\u5fc5\u987b\u662f\u2fca\u2f8a\u7684&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u4efb\u610f\u2f00\u6761\u8def\u5f84\u4e0d\u4f1a\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u3002\u5bf9\u4e8e\u4efb\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/96426.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-27T08:33:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083241-6a8ff629459b5.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"20 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/96426.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/96426.html\",\"name\":\"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-27T08:33:00+00:00\",\"dateModified\":\"2026-08-27T08:33:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/96426.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/96426.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/96426.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/96426.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00. \u7ea2\u2fca\u6811\u7684\u6982\u5ff5\u7ea2\u2fca\u6811\u662f\u2f00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811&#xff0c;\u4ed6\u7684\u6bcf\u4e2a\u7ed3\u70b9\u589e\u52a0\u2f00\u4e2a\u5b58\u50a8\u4f4d\u6765\u8868\u793a\u7ed3\u70b9\u7684\u989c\u2f8a&#xff0c;\u53ef\u4ee5\u662f\u7ea2\u2f8a\u6216\u8005\u2fca\u2f8a\u3002\u901a\u8fc7\u5bf9\u4efb\u4f55\u2f00\u6761\u4ece\u6839\u5230\u53f6\u2f26\u7684\u8def\u5f84\u4e0a\u5404\u4e2a\u7ed3\u70b9\u7684\u989c\u2f8a\u8fdb\u2f8f\u7ea6\u675f&#xff0c;\u7ea2\u2fca\u6811\u786e\u4fdd\u6ca1\u6709\u2f00\u6761\u8def\u5f84\u4f1a\u6bd4\u5176\u4ed6\u8def\u5f84\u957f\u51fa2\u500d&#xff0c;\u56e0\u2f7d\u662f\u63a5\u8fd1\u5e73\u8861\u7684\u30021.\u7ea2\u9ed1\u6811\u7684\u89c4\u5219&#xff1a;\u6bcf\u4e2a\u7ed3\u70b9\u4e0d\u662f\u7ea2\u2f8a\u5c31\u662f\u2fca\u2f8a\u6839\u7ed3\u70b9\u662f\u2fca\u2f8a\u7684\u5982\u679c\u2f00\u4e2a\u7ed3\u70b9\u662f\u7ea2\u2f8a\u7684&#xff0c;\u5219\u5b83\u7684\u4e24\u4e2a\u5b69\u2f26\u7ed3\u70b9\u5fc5\u987b\u662f\u2fca\u2f8a\u7684&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u4efb\u610f\u2f00\u6761\u8def\u5f84\u4e0d\u4f1a\u6709\u8fde\u7eed\u7684\u7ea2\u2f8a\u7ed3\u70b9\u3002\u5bf9\u4e8e\u4efb","og_url":"https:\/\/www.wsisp.com\/helps\/96426.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-27T08:33:00+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260827083241-6a8ff629459b5.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"20 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/96426.html","url":"https:\/\/www.wsisp.com\/helps\/96426.html","name":"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-27T08:33:00+00:00","dateModified":"2026-08-27T08:33:00+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/96426.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/96426.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/96426.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u3011\u300aC++\u7ea2\u9ed1\u6811\u4ece0\u52301\uff1a\u63d2\u5165\u3001\u65cb\u8f6c\u3001\u8fed\u4ee3\u5668\uff0c\u5e76\u6a21\u62df\u5b9e\u73b0set\u4e0emap\u300b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/96426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=96426"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/96426\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/96406"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=96426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=96426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=96426"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=96426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}