{"id":507,"date":"2019-03-08T15:40:15","date_gmt":"2019-03-08T07:40:15","guid":{"rendered":"https:\/\/fick707.com\/?p=507"},"modified":"2019-03-08T15:40:39","modified_gmt":"2019-03-08T07:40:39","slug":"java-treemap-deep-analysis","status":"publish","type":"post","link":"https:\/\/fick707.com\/?p=507","title":{"rendered":"java TreeMap\u6df1\u5165\u5206\u6790"},"content":{"rendered":"<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p>\u8f6c\u8f7d\u81ea\uff1ahttps:\/\/www.cnblogs.com\/pony1223\/p\/7892401.html<\/p>\n<p><strong>\u4e00\u3001\u8ba4\u8bc6TreeMap<\/strong><\/p>\n<p>\u4e4b\u524d\u7684\u6587\u7ae0\u8bb2\u89e3\u4e86HashMap\uff0c\u5b83\u4fdd\u8bc1\u4e86\u4ee5O(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8fdb\u884c\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\uff0c\u4ece\u5b58\u50a8\u89d2\u5ea6\u8003\u8651\uff0c\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u662f\u975e\u5e38\u4f18\u79c0\u7684\u3002<\/p>\n<p>\u5c3d\u7ba1\u5982\u6b64\uff0cHashMap\u8fd8\u662f\u6709\u81ea\u5df1\u7684\u5c40\u9650\u6027&#8212;-\u5b83\u4eec<strong>\u4e0d\u5177\u5907\u7edf\u8ba1\u6027\u80fd\uff0c\u6216\u8005\u8bf4\u5b83\u4eec\u7684\u7edf\u8ba1\u6027\u80fd\u65f6\u95f4\u590d\u6742\u5ea6\u5e76\u4e0d\u662f\u5f88\u597d\u624d\u66f4\u51c6\u786e\uff0c\u6240\u6709\u7684\u7edf\u8ba1\u5fc5\u987b\u904d\u5386\u6240\u6709Entry\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(N)<\/strong>\u3002\u6bd4\u5982Map\u7684Key\u67091\u30012\u30013\u30014\u30015\u30016\u30017\uff0c\u6211\u73b0\u5728\u8981\u7edf\u8ba1\uff1a<\/p>\n<ol>\n<li>\u6240\u6709Key\u6bd43\u5927\u7684\u952e\u503c\u5bf9\u6709\u54ea\u4e9b<\/li>\n<li>Key\u6700\u5c0f\u7684\u548cKey\u6700\u5927\u7684\u662f\u54ea\u4e24\u4e2a<\/li>\n<\/ol>\n<p>\u5c31\u7c7b\u4f3c\u8fd9\u4e9b\u64cd\u4f5c\uff0cHashMap\u505a\u5f97\u6bd4\u8f83\u5dee\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528TreeMap\u3002TreeMap\u7684Key\u6309\u7167\u81ea\u7136\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\u6216\u8005\u6839\u636e\u521b\u5efa\u6620\u5c04\u65f6\u63d0\u4f9b\u7684Comparator\u63a5\u53e3\u8fdb\u884c\u6392\u5e8f\u3002<strong>TreeMap\u4e3a\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\u8fd9\u4e9b\u64cd\u4f5c\u63d0\u4f9b\u4e86log(N)\u7684\u65f6\u95f4\u5f00\u9500<\/strong>\uff0c\u4ece\u5b58\u50a8\u89d2\u5ea6\u800c\u8a00\uff0c\u8fd9\u6bd4HashMap\u7684O(1)\u65f6\u95f4\u590d\u6742\u5ea6\u8981\u5dee\u4e9b\uff1b\u4f46\u662f\u5728\u7edf\u8ba1\u6027\u80fd\u4e0a\uff0cTreeMap\u540c\u6837\u53ef\u4ee5\u4fdd\u8bc1log(N)\u7684\u65f6\u95f4\u5f00\u9500\uff0c\u8fd9\u53c8\u6bd4HashMap\u7684O(N)\u65f6\u95f4\u590d\u6742\u5ea6\u597d\u4e0d\u5c11\u3002<\/p>\n<p>\u56e0\u6b64\u603b\u7ed3\u800c\u8a00\uff1a\u5982\u679c\u53ea\u9700\u8981\u5b58\u50a8\u529f\u80fd\uff0c\u4f7f\u7528HashMap\u662f\u4e00\u79cd\u66f4\u597d\u7684\u9009\u62e9\uff1b\u5982\u679c\u8fd8\u9700\u8981\u4fdd\u8bc1\u7edf\u8ba1\u6027\u80fd\u6216\u8005\u9700\u8981\u5bf9Key\u6309\u7167\u4e00\u5b9a\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\uff0c\u90a3\u4e48\u4f7f\u7528TreeMap\u662f\u4e00\u79cd\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u4e8c\u3001\u7ea2\u9ed1\u6811\u4ecb\u7ecd<\/strong><\/p>\n<p>\u7ea2\u9ed1\u6811\u53c8\u79f0\u7ea2-\u9ed1\u4e8c\u53c9\u6811\uff0c\u5b83\u9996\u5148\u662f\u4e00\u9897\u4e8c\u53c9\u6811\uff0c\u5b83\u5177\u4f53\u4e8c\u53c9\u6811\u6240\u6709\u7684\u7279\u6027\u3002\u540c\u65f6\u7ea2\u9ed1\u6811\u66f4\u662f\u4e00\u9897\u81ea\u5e73\u8861\u7684\u6392\u5e8f\u4e8c\u53c9\u6811\u3002<br \/>\n\u6211\u4eec\u77e5\u9053\u4e00\u9897\u57fa\u672c\u7684\u4e8c\u53c9\u6811\u4ed6\u4eec\u90fd\u9700\u8981\u6ee1\u8db3\u4e00\u4e2a\u57fa\u672c\u6027\u8d28&#8211;\u5373\u6811\u4e2d\u7684\u4efb\u4f55\u8282\u70b9\u7684\u503c\u5927\u4e8e\u5b83\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u4e14\u5c0f\u4e8e\u5b83\u7684\u53f3\u5b50\u8282\u70b9\u3002\u6309\u7167\u8fd9\u4e2a\u57fa\u672c\u6027\u8d28\u4f7f\u5f97\u6811\u7684\u68c0\u7d22\u6548\u7387\u5927\u5927\u63d0\u9ad8\u3002\u6211\u4eec\u77e5\u9053\u5728\u751f\u6210\u4e8c\u53c9\u6811\u7684\u8fc7\u7a0b\u662f\u975e\u5e38\u5bb9\u6613\u5931\u8861\u7684\uff0c\u6700\u574f\u7684\u60c5\u51b5\u5c31\u662f\u4e00\u8fb9\u5012\uff08\u53ea\u6709\u53f3\/\u5de6\u5b50\u6811\uff09\uff0c\u8fd9\u6837\u52bf\u5fc5\u4f1a\u5bfc\u81f4\u4e8c\u53c9\u6811\u7684\u68c0\u7d22\u6548\u7387\u5927\u5927\u964d\u4f4e\uff08O(n)\uff09\uff0c\u6240\u4ee5\u4e3a\u4e86\u7ef4\u6301\u4e8c\u53c9\u6811\u7684\u5e73\u8861\uff0c\u5927\u725b\u4eec\u63d0\u51fa\u4e86\u5404\u79cd\u5b9e\u73b0\u7684\u7b97\u6cd5\uff0c\u5982\uff1aAVL\uff0cSBT\uff0c\u4f38\u5c55\u6811\uff0cTREAP \uff0c\u7ea2\u9ed1\u6811\u7b49\u7b49\u3002<br \/>\n\u5e73\u8861\u4e8c\u53c9\u6811\u5fc5\u987b\u5177\u5907\u5982\u4e0b\u7279\u6027\uff1a\u5b83\u662f\u4e00\u68f5\u7a7a\u6811\u6216\u5b83\u7684\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u7684\u9ad8\u5ea6\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71\uff0c\u5e76\u4e14\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u90fd\u662f\u4e00\u68f5\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u4e5f\u5c31\u662f\u8bf4\u8be5\u4e8c\u53c9\u6811\u7684\u4efb\u4f55\u4e00\u4e2a\u7b49\u7b49\u5b50\u8282\u70b9\uff0c\u5176\u5de6\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u90fd\u76f8\u8fd1\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124211523265-768872994.png\" alt=\"\" \/><\/p>\n<p>\u7ea2\u9ed1\u6811\u987e\u540d\u601d\u4e49\u5c31\u662f\u8282\u70b9\u662f\u7ea2\u8272\u6216\u8005\u9ed1\u8272\u7684\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u5b83\u901a\u8fc7\u989c\u8272\u7684\u7ea6\u675f\u6765\u7ef4\u6301\u7740\u4e8c\u53c9\u6811\u7684\u5e73\u8861\u3002\u5bf9\u4e8e\u4e00\u68f5\u6709\u6548\u7684\u7ea2\u9ed1\u6811\u4e8c\u53c9\u6811\u800c\u8a00\u6211\u4eec\u5fc5\u987b\u589e\u52a0\u5982\u4e0b\u89c4\u5219\uff1a<\/p>\n<p>1\u3001\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ea\u80fd\u662f\u7ea2\u8272\u6216\u8005\u9ed1\u8272<\/p>\n<p>2\u3001\u6839\u8282\u70b9\u662f\u9ed1\u8272<\/p>\n<p>3\u3001\u6bcf\u4e2a\u53f6\u8282\u70b9\uff08NIL\u8282\u70b9\uff0c\u7a7a\u8282\u70b9\uff09\u662f\u9ed1\u8272\u7684\u3002<\/p>\n<p>4\u3001\u5982\u679c\u4e00\u4e2a\u7ed3\u70b9\u662f\u7ea2\u7684\uff0c\u5219\u5b83\u4e24\u4e2a\u5b50\u8282\u70b9\u90fd\u662f\u9ed1\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u5728\u4e00\u6761\u8def\u5f84\u4e0a\u4e0d\u80fd\u51fa\u73b0\u76f8\u90bb\u7684\u4e24\u4e2a\u7ea2\u8272\u7ed3\u70b9\u3002<\/p>\n<p>5\u3001\u4ece\u4efb\u4e00\u8282\u70b9\u5230\u5176\u6bcf\u4e2a\u53f6\u5b50\u7684\u6240\u6709\u8def\u5f84\u90fd\u5305\u542b\u76f8\u540c\u6570\u76ee\u7684\u9ed1\u8272\u8282\u70b9\u3002<\/p>\n<p>\u8fd9\u4e9b\u7ea6\u675f\u5f3a\u5236\u4e86\u7ea2\u9ed1\u6811\u7684\u5173\u952e\u6027\u8d28: \u4ece\u6839\u5230\u53f6\u5b50\u7684\u6700\u957f\u7684\u53ef\u80fd\u8def\u5f84\u4e0d\u591a\u4e8e\u6700\u77ed\u7684\u53ef\u80fd\u8def\u5f84\u7684\u4e24\u500d\u957f\u3002\u7ed3\u679c\u662f\u8fd9\u68f5\u6811\u5927\u81f4\u4e0a\u662f\u5e73\u8861\u7684\u3002\u56e0\u4e3a\u64cd\u4f5c\u6bd4\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u67e5\u627e\u67d0\u4e2a\u503c\u7684\u6700\u574f\u60c5\u51b5\u65f6\u95f4\u90fd\u8981\u6c42\u4e0e\u6811\u7684\u9ad8\u5ea6\u6210\u6bd4\u4f8b\uff0c\u8fd9\u4e2a\u5728\u9ad8\u5ea6\u4e0a\u7684\u7406\u8bba\u4e0a\u9650\u5141\u8bb8\u7ea2\u9ed1\u6811\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u90fd\u662f\u9ad8\u6548\u7684\uff0c\u800c\u4e0d\u540c\u4e8e\u666e\u901a\u7684\u4e8c\u53c9\u67e5\u627e\u6811\u3002\u6240\u4ee5\u7ea2\u9ed1\u6811\u5b83\u662f\u590d\u6742\u800c\u9ad8\u6548\u7684\uff0c\u5176\u68c0\u7d22\u6548\u7387O(log\u00a0<em>n<\/em>)\u3002\u4e0b\u56fe\u4e3a\u4e00\u9897\u5178\u578b\u7684\u7ea2\u9ed1\u4e8c\u53c9\u6811\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124211651984-1917137019.png\" alt=\"\" \/><\/p>\n<p>\u5bf9\u4e8e\u7ea2\u9ed1\u4e8c\u53c9\u6811\u800c\u8a00\u5b83\u4e3b\u8981\u5305\u62ec\u4e09\u5927\u57fa\u672c\u64cd\u4f5c\uff1a\u5de6\u65cb\u3001\u53f3\u65cb\u3001\u7740\u8272\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/cmsblogs.qiniudn.com\/wp-content\/uploads\/2014\/05\/2014051700004.gif\" alt=\"\" width=\"329\" height=\"241\" \/>\u5de6\u8fb9\u65cb\u8f6c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/cmsblogs.qiniudn.com\/wp-content\/uploads\/2014\/05\/2014051700005.gif\" alt=\"\" width=\"301\" height=\"258\" \/>\u53f3\u8fb9\u65cb\u8f6c<\/p>\n<p>\uff08\u56fe\u7247\u6765\u81ea\uff1a<a title=\"http:\/\/www.cnblogs.com\/yangecnu\/p\/Introduce-Red-Black-Tree.html\" href=\"http:\/\/www.cnblogs.com\/yangecnu\/p\/Introduce-Red-Black-Tree.html\">http:\/\/www.cnblogs.com\/yangecnu\/p\/Introduce-Red-Black-Tree.html<\/a>\uff09<\/p>\n<p>\u6ce8\uff1a\u7531\u4e8e\u672c\u6587\u4e3b\u8981\u662f\u8bb2\u89e3Java\u4e2dTreeMap\uff0c\u6240\u4ee5\u5e76\u6ca1\u6709\u5bf9\u7ea2\u9ed1\u6811\u8fdb\u884c\u975e\u5e38\u6df1\u5165\u7684\u4e86\u89e3\u548c\u7814\u7a76\uff0c\u5982\u679c\u60f3\u5bf9\u5176\u8fdb\u884c\u66f4\u52a0\u6df1\u5165\u7684\u7814\u7a76\u63d0\u4f9b\u51e0\u7bc7\u8f83\u597d\u7684\u535a\u6587\uff1a<\/p>\n<p>1\u3001<a href=\"http:\/\/blog.csdn.net\/v_JULY_v\/article\/category\/774945\">\u7ea2\u9ed1\u6811\u7cfb\u5217\u96c6\u9526<\/a><\/p>\n<p>2\u3001<a href=\"http:\/\/www.cnblogs.com\/fanzhidongyzby\/p\/3187912.html\">\u7ea2\u9ed1\u6811\u6570\u636e\u7ed3\u6784\u5256\u6790<\/a><\/p>\n<p>3\u3001<a href=\"http:\/\/blog.csdn.net\/eric491179912\/article\/details\/6179908\">\u7ea2\u9ed1\u6811<\/a><\/p>\n<p><strong>\u4e09\u3001TreeMap\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<div class=\"cnblogs_code\">\n<pre>public class TreeMap&lt;K,V&gt;\n    extends AbstractMap&lt;K,V&gt;\n    implements NavigableMap&lt;K,V&gt;, Cloneable, java.io.Serializable<\/pre>\n<\/div>\n<p>TreeMap\u7ee7\u627fAbstractMap\uff0c\u5b9e\u73b0NavigableMap\u3001Cloneable\u3001Serializable\u4e09\u4e2a\u63a5\u53e3\u3002\u5176\u4e2dAbstractMap\u8868\u660eTreeMap\u4e3a\u4e00\u4e2aMap\u5373\u652f\u6301key-value\u7684\u96c6\u5408\uff0c NavigableMap\uff08<a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/NavigableMap.html\">\u66f4\u591a<\/a>\uff09\u5219\u610f\u5473\u7740\u5b83\u652f\u6301\u4e00\u7cfb\u5217\u7684\u5bfc\u822a\u65b9\u6cd5\uff0c\u5177\u5907\u9488\u5bf9\u7ed9\u5b9a\u641c\u7d22\u76ee\u6807\u8fd4\u56de\u6700\u63a5\u8fd1\u5339\u914d\u9879\u7684\u5bfc\u822a\u65b9\u6cd5 \u3002<\/p>\n<p>TreeMap\u4e2d\u540c\u65f6\u4e5f\u5305\u542b\u4e86\u5982\u4e0b\u51e0\u4e2a\u91cd\u8981\u7684\u5c5e\u6027\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>\/\/\u6bd4\u8f83\u5668\uff0c\u56e0\u4e3aTreeMap\u662f\u6709\u5e8f\u7684\uff0c\u901a\u8fc7comparator\u63a5\u53e3\u6211\u4eec\u53ef\u4ee5\u5bf9TreeMap\u7684\u5185\u90e8\u6392\u5e8f\u8fdb\u884c\u7cbe\u5bc6\u7684\u63a7\u5236\n        private final Comparator&lt;? super K&gt; comparator;\n        \/\/TreeMap\u7ea2-\u9ed1\u8282\u70b9\uff0c\u4e3aTreeMap\u7684\u5185\u90e8\u7c7b\n        private transient Entry&lt;K,V&gt; root = null;\n        \/\/\u5bb9\u5668\u5927\u5c0f\n        private transient int size = 0;\n        \/\/TreeMap\u4fee\u6539\u6b21\u6570\n        private transient int modCount = 0;\n        \/\/\u7ea2\u9ed1\u6811\u7684\u8282\u70b9\u989c\u8272--\u7ea2\u8272\n        private static final boolean RED = false;\n        \/\/\u7ea2\u9ed1\u6811\u7684\u8282\u70b9\u989c\u8272--\u9ed1\u8272\n        private static final boolean BLACK = true;<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u5bf9\u4e8e\u53f6\u5b50\u8282\u70b9Entry\u662fTreeMap\u7684\u5185\u90e8\u7c7b\uff0c\u5b83\u6709\u51e0\u4e2a\u91cd\u8981\u7684\u5c5e\u6027\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>        \/\/\u952e\n        K key;\n        \/\/\u503c\n        V value;\n        \/\/\u5de6\u5b69\u5b50\n        Entry&lt;K,V&gt; left = null;\n        \/\/\u53f3\u5b69\u5b50\n        Entry&lt;K,V&gt; right = null;\n        \/\/\u7236\u4eb2\n        Entry&lt;K,V&gt; parent;\n        \/\/\u989c\u8272\n        boolean color = BLACK;<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p><strong>\u56db\u3001\u6838\u5fc3\u65b9\u6cd5put \u5206\u6790<\/strong><\/p>\n<p>\u5206\u6790put\u65b9\u6cd5\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u91c7\u7528\u5b9e\u4f8b\u6765\u8fdb\u884c\u5206\u6790\uff0c\u4e0b\u9762\u6211\u4eec\u662f\u6211\u4eec\u5199\u7684\u4e00\u6bb5\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>package com.pony1223;\n\nimport java.util.Map;\nimport java.util.TreeMap;\n\npublic class MapDemo\n{\n    public static void main(String[] args) \n    {\n        TreeMap&lt;Integer, String&gt; treeMap = new TreeMap&lt;Integer, String&gt;();\n        treeMap.put(10, \"10\");\n        treeMap.put(83, \"83\");\n        treeMap.put(15, \"15\");\n        treeMap.put(72, \"72\");\n        treeMap.put(20, \"20\");\n        treeMap.put(60, \"60\");\n        treeMap.put(30, \"30\");\n        treeMap.put(50, \"50\");\n\n        for (Map.Entry&lt;Integer, String&gt; entry : treeMap.entrySet()) {\n            System.out.println(entry.getKey() + \":\" + entry.getValue());\n        }\n        \n    }\n\n}<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u6211\u4eec\u6253\u5370\u7ed3\u679c\u53d1\u73b0\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124213205515-1339492964.png\" alt=\"\" \/><\/p>\n<p>\u4e8b\u5b9e\u518d\u6b21\u8bc1\u660eTreeMap\u662f\u6709\u5e8f\u7684\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7684\u5185\u5bb9\u4f1a\u7ed9\u51fa\u63d2\u5165\u6bcf\u6761\u6570\u636e\u4e4b\u540e\u7ea2\u9ed1\u6811\u7684\u6570\u636e\u7ed3\u6784\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002\u9996\u5148\u770b\u4e00\u4e0btreeMap\u7684put\u65b9\u6cd5\u7684\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>public V put(K key, V value) {\n           \/\/\u7528t\u8868\u793a\u4e8c\u53c9\u6811\u7684\u5f53\u524d\u8282\u70b9\n            Entry&lt;K,V&gt; t = root;\n            \/\/t\u4e3anull\u8868\u793a\u4e00\u4e2a\u7a7a\u6811\uff0c\u5373TreeMap\u4e2d\u6ca1\u6709\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u63d2\u5165\n            if (t == null) {\n                \/\/\u6bd4\u8f83key\u503c\uff0c\u4e2a\u4eba\u89c9\u5f97\u8fd9\u53e5\u4ee3\u7801\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u7a7a\u6811\u8fd8\u9700\u8981\u6bd4\u8f83\u3001\u6392\u5e8f\uff1f\n                compare(key, key); \/\/ type (and possibly null) check\n                \/\/\u5c06\u65b0\u7684key-value\u952e\u503c\u5bf9\u521b\u5efa\u4e3a\u4e00\u4e2aEntry\u8282\u70b9\uff0c\u5e76\u5c06\u8be5\u8282\u70b9\u8d4b\u4e88\u7ed9root\n                root = new Entry&lt;&gt;(key, value, null);\n                \/\/\u5bb9\u5668\u7684size = 1\uff0c\u8868\u793aTreeMap\u96c6\u5408\u4e2d\u5b58\u5728\u4e00\u4e2a\u5143\u7d20\n                size = 1;\n                \/\/\u4fee\u6539\u6b21\u6570 + 1\n                modCount++;\n                return null;\n            }\n            int cmp;     \/\/cmp\u8868\u793akey\u6392\u5e8f\u7684\u8fd4\u56de\u7ed3\u679c\n            Entry&lt;K,V&gt; parent;   \/\/\u7236\u8282\u70b9\n            \/\/ split comparator and comparable paths\n            Comparator&lt;? super K&gt; cpr = comparator;    \/\/\u6307\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\n            \/\/\u5982\u679ccpr\u4e0d\u4e3a\u7a7a\uff0c\u5219\u91c7\u7528\u65e2\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\u8fdb\u884c\u521b\u5efaTreeMap\u96c6\u5408\n            if (cpr != null) {\n                do {\n                    parent = t;      \/\/parent\u6307\u5411\u4e0a\u6b21\u5faa\u73af\u540e\u7684t\n                    \/\/\u6bd4\u8f83\u65b0\u589e\u8282\u70b9\u7684key\u548c\u5f53\u524d\u8282\u70b9key\u7684\u5927\u5c0f\n                    cmp = cpr.compare(key, t.key);\n                    \/\/cmp\u8fd4\u56de\u503c\u5c0f\u4e8e0\uff0c\u8868\u793a\u65b0\u589e\u8282\u70b9\u7684key\u5c0f\u4e8e\u5f53\u524d\u8282\u70b9\u7684key\uff0c\u5219\u4ee5\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u4f5c\u4e3a\u65b0\u7684\u5f53\u524d\u8282\u70b9\n                    if (cmp &lt; 0)\n                        t = t.left;\n                    \/\/cmp\u8fd4\u56de\u503c\u5927\u4e8e0\uff0c\u8868\u793a\u65b0\u589e\u8282\u70b9\u7684key\u5927\u4e8e\u5f53\u524d\u8282\u70b9\u7684key\uff0c\u5219\u4ee5\u5f53\u524d\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u4f5c\u4e3a\u65b0\u7684\u5f53\u524d\u8282\u70b9\n                    else if (cmp &gt; 0)\n                        t = t.right;\n                    \/\/cmp\u8fd4\u56de\u503c\u7b49\u4e8e0\uff0c\u8868\u793a\u4e24\u4e2akey\u503c\u76f8\u7b49\uff0c\u5219\u65b0\u503c\u8986\u76d6\u65e7\u503c\uff0c\u5e76\u8fd4\u56de\u65b0\u503c\n                    else\n                        return t.setValue(value);\n                } while (t != null);\n            }\n            \/\/\u5982\u679ccpr\u4e3a\u7a7a\uff0c\u5219\u91c7\u7528\u9ed8\u8ba4\u7684\u6392\u5e8f\u7b97\u6cd5\u8fdb\u884c\u521b\u5efaTreeMap\u96c6\u5408\n            else {\n                if (key == null)     \/\/key\u503c\u4e3a\u7a7a\u629b\u51fa\u5f02\u5e38\n                    throw new NullPointerException();\n                \/* \u4e0b\u9762\u5904\u7406\u8fc7\u7a0b\u548c\u4e0a\u9762\u4e00\u6837 *\/\n                Comparable&lt;? super K&gt; k = (Comparable&lt;? super K&gt;) key;\n                do {\n                    parent = t;\n                    cmp = k.compareTo(t.key);\n                    if (cmp &lt; 0)\n                        t = t.left;\n                    else if (cmp &gt; 0)\n                        t = t.right;\n                    else\n                        return t.setValue(value);\n                } while (t != null);\n            }\n            \/\/\u5c06\u65b0\u589e\u8282\u70b9\u5f53\u505aparent\u7684\u5b50\u8282\u70b9\n            Entry&lt;K,V&gt; e = new Entry&lt;&gt;(key, value, parent);\n            \/\/\u5982\u679c\u65b0\u589e\u8282\u70b9\u7684key\u5c0f\u4e8eparent\u7684key\uff0c\u5219\u5f53\u505a\u5de6\u5b50\u8282\u70b9\n            if (cmp &lt; 0)\n                parent.left = e;\n          \/\/\u5982\u679c\u65b0\u589e\u8282\u70b9\u7684key\u5927\u4e8eparent\u7684key\uff0c\u5219\u5f53\u505a\u53f3\u5b50\u8282\u70b9\n            else\n                parent.right = e;\n            \/*\n             *  \u4e0a\u9762\u5df2\u7ecf\u5b8c\u6210\u4e86\u6392\u5e8f\u4e8c\u53c9\u6811\u7684\u7684\u6784\u5efa\uff0c\u5c06\u65b0\u589e\u8282\u70b9\u63d2\u5165\u8be5\u6811\u4e2d\u7684\u5408\u9002\u4f4d\u7f6e\n             *  \u4e0b\u9762fixAfterInsertion()\u65b9\u6cd5\u5c31\u662f\u5bf9\u8fd9\u68f5\u6811\u8fdb\u884c\u8c03\u6574\u3001\u5e73\u8861\uff0c\u5177\u4f53\u8fc7\u7a0b\u53c2\u8003\u4e0a\u9762\u7684\u4e94\u79cd\u60c5\u51b5\n             *\/\n            fixAfterInsertion(e);\n            \/\/TreeMap\u5143\u7d20\u6570\u91cf + 1\n            size++;\n            \/\/TreeMap\u5bb9\u5668\u4fee\u6539\u6b21\u6570 + 1\n            modCount++;\n            return null;\n        }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u4ece\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5148\u603b\u7ed3\u4e00\u4e0bTreeMap\u6dfb\u52a0\u6570\u636e\u7684\u51e0\u4e2a\u6b65\u9aa4\uff1a<br \/>\n1.\u83b7\u53d6\u6839\u8282\u70b9\uff0c\u6839\u8282\u70b9\u4e3a\u7a7a\uff0c\u4ea7\u751f\u4e00\u4e2a\u6839\u8282\u70b9\uff0c\u5c06\u5176\u7740\u8272\u4e3a\u9ed1\u8272\uff0c\u9000\u51fa\u4f59\u4e0b\u6d41\u7a0b<br \/>\n2.\u83b7\u53d6\u6bd4\u8f83\u5668\uff0c\u5982\u679c\u4f20\u5165\u7684Comparator\u63a5\u53e3\u4e0d\u4e3a\u7a7a\uff0c\u4f7f\u7528\u4f20\u5165\u7684Comparator\u63a5\u53e3\u5b9e\u73b0\u7c7b\u8fdb\u884c\u6bd4\u8f83\uff1b\u5982\u679c\u4f20\u5165\u7684Comparator\u63a5\u53e3\u4e3a\u7a7a\uff0c\u5c06Key\u5f3a\u8f6c\u4e3aComparable\u63a5\u53e3\u8fdb\u884c\u6bd4\u8f83<br \/>\n3.\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u9010\u4e00\u4f9d\u7167\u89c4\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\u8fdb\u884c\u6bd4\u8f83\uff0c\u53d6\u6bd4\u8f83\u503ccmp\uff0c\u5982\u679ccmp=0\uff0c\u8868\u793a\u63d2\u5165\u7684Key\u5df2\u5b58\u5728\uff1b\u5982\u679ccmp&gt;0\uff0c\u53d6\u5f53\u524d\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\uff1b\u5982\u679ccmp&lt;0\uff0c\u53d6\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9<br \/>\n4.\u6392\u9664\u63d2\u5165\u7684Key\u5df2\u5b58\u5728\u7684\u60c5\u51b5\uff0c\u7b2c\uff083\uff09\u6b65\u7684\u6bd4\u8f83\u4e00\u76f4\u6bd4\u8f83\u5230\u5f53\u524d\u8282\u70b9t\u7684\u5de6\u5b50\u8282\u70b9\u6216\u53f3\u5b50\u8282\u70b9\u4e3anull\uff0c\u6b64\u65f6t\u5c31\u662f\u6211\u4eec\u5bfb\u627e\u5230\u7684\u8282\u70b9\uff0ccmp&gt;0\u5219\u51c6\u5907\u5f80t\u7684\u53f3\u5b50\u8282\u70b9\u63d2\u5165\u65b0\u8282\u70b9\uff0ccmp&lt;0\u5219\u51c6\u5907\u5f80t\u7684\u5de6\u5b50\u8282\u70b9\u63d2\u5165\u65b0\u8282\u70b9<br \/>\n5.new\u51fa\u4e00\u4e2a\u65b0\u8282\u70b9\uff0c\u9ed8\u8ba4\u4e3a\u9ed1\u8272\uff0c\u6839\u636ecmp\u7684\u503c\u5411t\u7684\u5de6\u8fb9\u6216\u8005\u53f3\u8fb9\u8fdb\u884c\u63d2\u5165<br \/>\n6.\u63d2\u5165\u4e4b\u540e\u8fdb\u884c\u4fee\u590d\uff0c\u5305\u62ec\u5de6\u65cb\u3001\u53f3\u65cb\u3001\u91cd\u65b0\u7740\u8272\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u8ba9\u6811\u4fdd\u6301\u5e73\u8861\u6027<br \/>\n\u7b2c1~\u7b2c5\u6b65\u90fd\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u7ea2\u9ed1\u6811\u6700\u6838\u5fc3\u7684\u5e94\u5f53\u662f\u7b2c6\u6b65\u63d2\u5165\u6570\u636e\u4e4b\u540e\u8fdb\u884c\u7684\u4fee\u590d\u5de5\u4f5c\uff0c\u5bf9\u5e94\u7684Java\u4ee3\u7801\u662fTreeMap\u4e2d\u7684fixAfterInsertion\u65b9\u6cd5\uff0c\u4e0b\u9762\u770b\u4e00\u4e0bput\u6bcf\u4e2a\u6570\u636e\u4e4b\u540eTreeMap\u90fd\u505a\u4e86\u4ec0\u4e48\u64cd\u4f5c\uff0c\u501f\u6b64\u6765\u7406\u6e05TreeMap\u7684\u5b9e\u73b0\u539f\u7406\u3002<\/p>\n<p><strong>put(10, &#8220;10&#8221;)<\/strong><\/p>\n<p>\u9996\u5148\u662fput(10, &#8220;10&#8221;)\uff0c\u7531\u4e8e\u6b64\u65f6TreeMap\u4e2d\u6ca1\u6709\u4efb\u4f55\u8282\u70b9\uff0c\u56e0\u6b6410\u4e3a\u6839\u4e14\u6839\u8282\u70b9\u4e3a\u9ed1\u8272\u8282\u70b9\uff0cput(10, &#8220;10&#8221;)\u4e4b\u540e\u7684\u6570\u636e\u7ed3\u6784\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124214618750-62744801.png\" alt=\"\" \/><\/p>\n<p><strong>put(83, &#8220;83&#8221;)<\/strong><\/p>\n<p>\u63a5\u7740\u662fput(83, &#8220;83&#8221;)\uff0c\u8fd9\u4e00\u6b65\u4e5f\u4e0d\u96be\uff0c83\u6bd410\u5927\uff0c\u56e0\u6b64\u572810\u7684\u53f3\u8282\u70b9\u4e0a\uff0c\u5373\u6267\u884c\u4e0a\u9762\u4ee3\u7801\u4e2ddo{}\u4ee3\u7801\u5757\uff0c\u5b83\u662f\u5b9e\u73b0\u6392\u5e8f\u4e8c\u53c9\u6811\u7684\u6838\u5fc3\u7b97\u6cd5\uff0c\u901a\u8fc7\u8be5\u7b97\u6cd5\u6211\u4eec\u53ef\u4ee5\u786e\u8ba4\u65b0\u589e\u8282\u70b9\u5728\u8be5\u6811\u7684\u6b63\u786e\u4f4d\u7f6e\u3002\u627e\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\u5c06\u63d2\u5165\u5373\u53ef\uff0c\u4f46\u662f\u7531\u4e8e83\u4e0d\u662f\u6839\u8282\u70b9\uff0c\u6211\u77e5\u9053TreeMap\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u7ea2\u9ed1\u6811\uff0c\u7ea2\u9ed1\u6811\u662f\u4e00\u68f5\u5e73\u8861\u6392\u5e8f\u4e8c\u53c9\u6811\uff0c\u666e\u901a\u7684\u6392\u5e8f\u4e8c\u53c9\u6811\u53ef\u80fd\u4f1a\u51fa\u73b0\u5931\u8861\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u4e0b\u4e00\u6b65\u5c31\u662f\u8981\u8fdb\u884c\u8c03\u6574\u3002fixAfterInsertion(e); \u8c03\u6574\u7684\u8fc7\u7a0b\u52a1\u5fc5\u4f1a\u6d89\u53ca\u5230\u7ea2\u9ed1\u6811\u7684\u5de6\u65cb\u3001\u53f3\u65cb\u3001\u7740\u8272\u4e09\u4e2a\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre> 1 \/**\n 2      * \u65b0\u589e\u8282\u70b9\u540e\u7684\u4fee\u590d\u64cd\u4f5c\n 3      * x \u8868\u793a\u65b0\u589e\u8282\u70b9\n 4      *\/\n 5      private void fixAfterInsertion(Entry&lt;K,V&gt; x) {\n 6             x.color = RED;    \/\/\u65b0\u589e\u8282\u70b9\u7684\u989c\u8272\u4e3a\u7ea2\u8272\n 7 \n 8             \/\/\u5faa\u73af \u76f4\u5230 x\u4e0d\u662f\u6839\u8282\u70b9\uff0c\u4e14x\u7684\u7236\u8282\u70b9\u4e0d\u4e3a\u7ea2\u8272\n 9             while (x != null &amp;&amp; x != root &amp;&amp; x.parent.color == RED) {\n10                 \/\/\u5982\u679cX\u7684\u7236\u8282\u70b9\uff08P\uff09\u662f\u5176\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u7684\u5de6\u8282\u70b9\n11                 if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {\n12                     \/\/\u83b7\u53d6X\u7684\u53d4\u8282\u70b9(U)\n13                     Entry&lt;K,V&gt; y = rightOf(parentOf(parentOf(x)));\n14                     \/\/\u5982\u679cX\u7684\u53d4\u8282\u70b9\uff08U\uff09 \u4e3a\u7ea2\u8272\uff08\u60c5\u51b5\u4e09\uff09\n15                     if (colorOf(y) == RED) {     \n16                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\uff08P\uff09\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n17                         setColor(parentOf(x), BLACK);\n18                         \/\/\u5c06X\u7684\u53d4\u8282\u70b9\uff08U\uff09\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n19                         setColor(y, BLACK);\n20                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u8bbe\u7f6e\u7ea2\u8272\n21                         setColor(parentOf(parentOf(x)), RED);\n22                         x = parentOf(parentOf(x));\n23                     }\n24                     \/\/\u5982\u679cX\u7684\u53d4\u8282\u70b9\uff08U\u4e3a\u9ed1\u8272\uff09\uff1b\u8fd9\u91cc\u4f1a\u5b58\u5728\u4e24\u79cd\u60c5\u51b5\uff08\u60c5\u51b5\u56db\u3001\u60c5\u51b5\u4e94\uff09\n25                     else {   \n26                         \/\/\u5982\u679cX\u8282\u70b9\u4e3a\u5176\u7236\u8282\u70b9\uff08P\uff09\u7684\u53f3\u5b50\u6811\uff0c\u5219\u8fdb\u884c\u5de6\u65cb\u8f6c\uff08\u60c5\u51b5\u56db\uff09\n27                         if (x == rightOf(parentOf(x))) {\n28                             \/\/\u5c06X\u7684\u7236\u8282\u70b9\u4f5c\u4e3aX\n29                             x = parentOf(x);\n30                             \/\/\u53f3\u65cb\u8f6c\n31                             rotateLeft(x);\n32                         }\n33                         \/\/\uff08\u60c5\u51b5\u4e94\uff09\n34                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\uff08P\uff09\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n35                         setColor(parentOf(x), BLACK);\n36                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u8bbe\u7f6e\u7ea2\u8272\n37                         setColor(parentOf(parentOf(x)), RED);\n38                         \/\/\u4ee5X\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u4e3a\u4e2d\u5fc3\u53f3\u65cb\u8f6c\n39                         rotateRight(parentOf(parentOf(x)));\n40                     }\n41                 }\n42                 \/\/\u5982\u679cX\u7684\u7236\u8282\u70b9\uff08P\uff09\u662f\u5176\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u7684\u53f3\u8282\u70b9\n43                 else {\n44                     \/\/\u83b7\u53d6X\u7684\u53d4\u8282\u70b9\uff08U\uff09\n45                     Entry&lt;K,V&gt; y = leftOf(parentOf(parentOf(x)));\n46                   \/\/\u5982\u679cX\u7684\u53d4\u8282\u70b9\uff08U\uff09 \u4e3a\u7ea2\u8272\uff08\u60c5\u51b5\u4e09\uff09\n47                     if (colorOf(y) == RED) {\n48                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\uff08P\uff09\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n49                         setColor(parentOf(x), BLACK);\n50                         \/\/\u5c06X\u7684\u53d4\u8282\u70b9\uff08U\uff09\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n51                         setColor(y, BLACK);\n52                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u8bbe\u7f6e\u7ea2\u8272\n53                         setColor(parentOf(parentOf(x)), RED);\n54                         x = parentOf(parentOf(x));\n55                     }\n56                   \/\/\u5982\u679cX\u7684\u53d4\u8282\u70b9\uff08U\u4e3a\u9ed1\u8272\uff09\uff1b\u8fd9\u91cc\u4f1a\u5b58\u5728\u4e24\u79cd\u60c5\u51b5\uff08\u60c5\u51b5\u56db\u3001\u60c5\u51b5\u4e94\uff09\n57                     else {\n58                         \/\/\u5982\u679cX\u8282\u70b9\u4e3a\u5176\u7236\u8282\u70b9\uff08P\uff09\u7684\u53f3\u5b50\u6811\uff0c\u5219\u8fdb\u884c\u5de6\u65cb\u8f6c\uff08\u60c5\u51b5\u56db\uff09\n59                         if (x == leftOf(parentOf(x))) {\n60                             \/\/\u5c06X\u7684\u7236\u8282\u70b9\u4f5c\u4e3aX\n61                             x = parentOf(x);\n62                            \/\/\u53f3\u65cb\u8f6c\n63                             rotateRight(x);\n64                         }\n65                         \/\/\uff08\u60c5\u51b5\u4e94\uff09\n66                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\uff08P\uff09\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n67                         setColor(parentOf(x), BLACK);\n68                         \/\/\u5c06X\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u8bbe\u7f6e\u7ea2\u8272\n69                         setColor(parentOf(parentOf(x)), RED);\n70                         \/\/\u4ee5X\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08G\uff09\u4e3a\u4e2d\u5fc3\u53f3\u65cb\u8f6c\n71                         rotateLeft(parentOf(parentOf(x)));\n72                     }\n73                 }\n74             }\n75             \/\/\u5c06\u6839\u8282\u70b9G\u5f3a\u5236\u8bbe\u7f6e\u4e3a\u9ed1\u8272\n76             root.color = BLACK;\n77         }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u6211\u4eec\u770b\u7b2c6\u884c\u7684\u4ee3\u7801\uff0c\u5b83<strong>\u5c06\u9ed8\u8ba4\u7684\u63d2\u5165\u7684\u90a3\u4e2a\u8282\u70b9\u7740\u8272\u6210\u4e3a<\/strong><strong>\u7ea2\u8272<\/strong>\uff0c\u8fd9\u5f88\u597d\u7406\u89e3\uff1a<\/p>\n<p>\u6839\u636e\u7ea2\u9ed1\u6811\u7684\u6027\u8d28\uff083\uff09\uff0c\u7ea2\u9ed1\u6811\u8981\u6c42\u4ece\u6839\u8282\u70b9\u5230\u53f6\u5b50\u6240\u6709\u53f6\u5b50\u8282\u70b9\u4e0a\u7ecf\u8fc7\u7684\u9ed1\u8272\u8282\u70b9\u4e2a\u6570\u662f\u76f8\u540c\u7684\uff0c\u56e0\u6b64\u5982\u679c\u63d2\u5165\u7684\u8282\u70b9\u7740\u8272\u4e3a\u9ed1\u8272\uff0c\u90a3\u5fc5\u7136\u6709\u53ef\u80fd\u5bfc\u81f4\u67d0\u6761\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u5927\u4e8e\u5176\u4ed6\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\uff0c\u56e0\u6b64\u9ed8\u8ba4\u63d2\u5165\u7684\u8282\u70b9\u5fc5\u987b\u662f\u7ea2\u8272\u7684\uff0c\u4ee5\u6b64\u6765\u7ef4\u6301\u7ea2\u9ed1\u6811\u7684\u6027\u8d28\uff083\uff09.<\/p>\n<p>\u5f53\u7136\u63d2\u5165\u8282\u70b9\u7740\u8272\u4e3a\u7ea2\u8272\u8282\u70b9\u540e\uff0c\u6709\u53ef\u80fd\u5bfc\u81f4\u7684\u95ee\u9898\u662f\u8fdd\u53cd\u6027\u8d28\uff082\uff09\uff0c\u5373\u51fa\u73b0\u8fde\u7eed\u4e24\u4e2a\u7ea2\u8272\u8282\u70b9\uff0c\u8fd9\u5c31\u9700\u8981\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\u53bb\u6539\u53d8\u6811\u7684\u7ed3\u6784\uff0c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u63a5\u7740\u770b<\/p>\n<pre>while (x != null &amp;&amp; x != root &amp;&amp; x.parent.color == RED)<\/pre>\n<p>\u7684\u5224\u65ad\uff0c\u524d\u4e24\u4e2a\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u4f46\u662f\u56e0\u4e3a83\u8fd9\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\u662f\u6839\u8282\u70b9\u7684\uff0c\u6839\u8282\u70b9\u662f\u9ed1\u8272\u8282\u70b9\uff0c\u56e0\u6b64\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u6ee1\u8db3\uff0cwhile\u5faa\u73af\u4e0d\u8fdb\u53bb\uff0c\u76f4\u63a5\u6267\u884c\u4e00\u6b21<\/p>\n<pre>root.color = BLACK;<\/pre>\n<p>\u884c\u7684\u4ee3\u7801\u7ed9\u6839\u8282\u70b9\u7740\u8272\u4e3a\u9ed1\u8272\uff08\u56e0\u4e3a\u5728\u65cb\u8f6c\u8fc7\u7a0b\u4e2d\u6709\u53ef\u80fd\u5bfc\u81f4\u6839\u8282\u70b9\u4e3a\u7ea2\u8272\uff0c\u800c\u7ea2\u9ed1\u6811\u7684\u6839\u8282\u70b9\u5fc5\u987b\u662f\u9ed1\u8272\uff0c\u56e0\u6b64\u6700\u540e\u4e0d\u7ba1\u6839\u8282\u70b9\u662f\u4e0d\u662f\u9ed1\u8272\uff0c\u90fd\u8981\u91cd\u65b0\u7740\u8272\u786e\u4fdd\u6839\u8282\u70b9\u662f\u9ed1\u8272\u7684\uff09\u3002<\/p>\n<p>\u90a3\u4e48put(83, &#8220;83&#8221;)\u4e4b\u540e\uff0c\u6574\u4e2a\u6811\u7684\u7ed3\u6784\u53d8\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124215733156-1716953325.png\" alt=\"\" \/><\/p>\n<p>\u4e00\u3001\u4e3a\u6839\u8282\u70b9<\/p>\n<p>\u82e5\u65b0\u63d2\u5165\u7684\u8282\u70b9N\u6ca1\u6709\u7236\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u5f53\u505a\u6839\u636e\u8282\u70b9\u63d2\u5165\u5373\u53ef\uff0c\u540c\u65f6\u5c06\u989c\u8272\u8bbe\u7f6e\u4e3a\u9ed1\u8272<\/p>\n<p>\u4e8c\u3001\u7236\u8282\u70b9\u4e3a\u9ed1\u8272<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u65b0\u8282\u70b9N\u540c\u6837\u662f\u76f4\u63a5\u63d2\u5165\uff0c\u540c\u65f6\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u7531\u4e8e\u6839\u636e\u89c4\u5219\u56db\u5b83\u4f1a\u5b58\u5728\u4e24\u4e2a\u9ed1\u8272\u7684\u53f6\u5b50\u8282\u70b9\uff0c\u503c\u4e3anull\u3002\u540c\u65f6\u7531\u4e8e\u65b0\u589e\u8282\u70b9N\u4e3a\u7ea2\u8272\uff0c\u6240\u4ee5\u901a\u8fc7\u5b83\u7684\u5b50\u8282\u70b9\u7684\u8def\u5f84\u4f9d\u7136\u4f1a\u4fdd\u5b58\u7740\u76f8\u540c\u7684\u9ed1\u8272\u8282\u70b9\u6570\uff0c\u540c\u6837\u6ee1\u8db3\u89c4\u52195\u3002<\/p>\n<p>\u5728\u770bput(15, &#8220;15&#8221;)\u4e4b\u524d\uff0c\u5fc5\u987b\u8981\u5148\u8fc7\u4e00\u4e0bfixAfterInsertion\u65b9\u6cd5\u3002\u7b2c11\u884c\uff5e\u7b2c41\u884c\u7684\u4ee3\u7801\u548c\u7b2c43\u884c~\u7b2c73\u884c\u7684\u4ee3\u7801\u662f\u4e00\u6837\u7684\uff0c\u65e0\u975e\u4e00\u4e2a\u662f\u64cd\u4f5c\u5de6\u5b50\u6811\u53e6\u4e00\u4e2a\u662f\u64cd\u4f5c\u53f3\u5b50\u6811\u800c\u5df2\uff0c\u56e0\u6b64\u5c31\u770b\u524d\u4e00\u534a\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre> 1 while (x != null &amp;&amp; x != root &amp;&amp; x.parent.color == RED) {\n 2     if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {\n 3         Entry&lt;K,V&gt; y = rightOf(parentOf(parentOf(x)));\n 4         if (colorOf(y) == RED) {\n 5             setColor(parentOf(x), BLACK);\n 6             setColor(y, BLACK);\n 7             setColor(parentOf(parentOf(x)), RED);\n 8             x = parentOf(parentOf(x));\n 9         } else {\n10             if (x == rightOf(parentOf(x))) {\n11                 x = parentOf(x);\n12                 rotateLeft(x);\n13             }\n14             setColor(parentOf(x), BLACK);\n15             setColor(parentOf(parentOf(x)), RED);\n16             rotateRight(parentOf(parentOf(x)));\n17         }\n18     }\n19     ....\n20 }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u7b2c2\u884c\u7684\u5224\u65ad\u6ce8\u610f\u4e00\u4e0b\uff0c\u7528\u8bed\u8a00\u63cf\u8ff0\u51fa\u6765\u5c31\u662f\uff1a<strong>\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9\u4e0e\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u662f\u5426\u540c\u4e00\u4e2a\u8282\u70b9<\/strong>\u3002\u7ffb\u8bd1\u4e00\u4e0b\u5c31\u662f\uff1a<strong>\u5f53\u524d\u8282\u70b9\u662f\u5426\u5de6\u5b50\u8282\u70b9\u63d2\u5165.<\/strong><\/p>\n<p><strong>\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\u8bf4\u5230\u7684\u60c5\u51b5\u4e09\u3001\u56db\u3001\u4e94\u7684\u610f\u601d\u662f\uff1b<\/strong><\/p>\n<p><strong>\u4e09\u3001\u82e5\u7236\u8282\u70b9P\u548cP\u7684\u5144\u5f1f\u8282\u70b9U\u90fd\u4e3a\u7ea2\u8272<\/strong><\/p>\n<p>\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\u82e5\u76f4\u63a5\u63d2\u5165\u80af\u5b9a\u4f1a\u51fa\u73b0\u4e0d\u5e73\u8861\u73b0\u8c61\uff08\u4e0d\u80fd\u51fa\u73b0\u4e24\u4e2a\u8fde\u7740\u7684\u7ea2\u8272\uff09\u3002\u600e\u4e48\u5904\u7406\uff1fP\u3001U\u8282\u70b9\u53d8\u9ed1\u3001G\u8282\u70b9\u53d8\u7ea2\u3002\u8fd9\u65f6\u7531\u4e8e\u7ecf\u8fc7\u8282\u70b9P\u3001U\u7684\u8def\u5f84\u90fd\u5fc5\u987b\u7ecf\u8fc7G\u6240\u4ee5\u5728\u8fd9\u4e9b\u8def\u5f84\u4e0a\u9762\u7684\u9ed1\u8282\u70b9\u6570\u76ee\u8fd8\u662f\u76f8\u540c\u7684\u3002\u4f46\u662f\u7ecf\u8fc7\u4e0a\u9762\u7684\u5904\u7406\uff0c\u53ef\u80fdG\u8282\u70b9\u7684\u7236\u8282\u70b9\u4e5f\u662f\u7ea2\u8272\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u9700\u8981\u5c06G\u8282\u70b9\u5f53\u505a\u65b0\u589e\u8282\u70b9\u9012\u5f52\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124221112015-1041895978.png\" alt=\"\" \/><\/p>\n<h3>\u56db\u3001\u82e5\u7236\u8282\u70b9P\u4e3a\u7ea2\u8272\uff0c\u53d4\u7236\u8282\u70b9U\u4e3a\u9ed1\u8272\u6216\u8005\u7f3a\u5c11\uff0c\u4e14\u65b0\u589e\u8282\u70b9N\u4e3aP\u8282\u70b9\u7684\u53f3\u5b69\u5b50<\/h3>\n<p>\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u5bf9\u65b0\u589e\u8282\u70b9N\u3001P\u8fdb\u884c\u4e00\u6b21\u5de6\u65cb\u8f6c\u3002\u8fd9\u91cc\u6240\u4ea7\u751f\u7684\u7ed3\u679c\u5176\u5b9e\u5e76\u6ca1\u6709\u5b8c\u6210\uff0c\u8fd8\u4e0d\u662f\u5e73\u8861\u7684\uff08\u8fdd\u53cd\u4e86\u89c4\u5219\u56db\uff09\uff0c\u8fd9\u662f\u6211\u4eec\u9700\u8981\u8fdb\u884c\u60c5\u51b55\u7684\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124221527140-103726934.png\" alt=\"\" \/><\/p>\n<h3>\u4e94\u3001\u7236\u8282\u70b9P\u4e3a\u7ea2\u8272\uff0c\u53d4\u7236\u8282\u70b9U\u4e3a\u9ed1\u8272\u6216\u8005\u7f3a\u5c11\uff0c\u65b0\u589e\u8282\u70b9N\u4e3a\u7236\u8282\u70b9P\u5de6\u5b69\u5b50<\/h3>\n<p>\u8fd9\u79cd\u60c5\u51b5\u6709\u53ef\u80fd\u662f\u7531\u4e8e\u60c5\u51b5\u56db\u800c\u4ea7\u751f\u7684\uff0c\u4e5f\u6709\u53ef\u80fd\u4e0d\u662f\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\u5148\u5df2P\u8282\u70b9\u4e3a\u4e2d\u5fc3\u8fdb\u884c\u53f3\u65cb\u8f6c\uff0c\u5728\u65cb\u8f6c\u540e\u4ea7\u751f\u7684\u6811\u4e2d\uff0c\u8282\u70b9P\u662f\u8282\u70b9N\u3001G\u7684\u7236\u8282\u70b9\u3002\u4f46\u662f\u8fd9\u68f5\u6811\u5e76\u4e0d\u89c4\u8303\uff0c\u5b83\u8fdd\u53cd\u4e86\u89c4\u52194\uff0c\u6240\u4ee5\u6211\u4eec\u5c06P\u3001G\u8282\u70b9\u7684\u989c\u8272\u8fdb\u884c\u4ea4\u6362\uff0c\u4f7f\u4e4b\u5176\u6ee1\u8db3\u89c4\u8303\u3002\u5f00\u59cb\u65f6\u6240\u6709\u7684\u8def\u5f84\u90fd\u9700\u8981\u7ecf\u8fc7G\u5176\u4ed6\u4eec\u7684\u9ed1\u8272\u8282\u70b9\u6570\u4e00\u6837\uff0c\u4f46\u662f\u73b0\u5728\u6240\u6709\u7684\u8def\u5f84\u6539\u4e3a\u7ecf\u8fc7P\uff0c\u4e14P\u4e3a\u6574\u68f5\u6811\u7684\u552f\u4e00\u9ed1\u8272\u8282\u70b9\uff0c\u6240\u4ee5\u8c03\u6574\u540e\u7684\u6811\u540c\u6837\u6ee1\u8db3\u89c4\u83035\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124222148078-49067841.png\" alt=\"\" \/><\/p>\n<p>\u603b\u7ed3\uff1a\u5bf9\u4e8e\u7b2c\u56db\u79cd\u60c5\u51b5\u548c\u7b2c\u4e94\u79cd\u60c5\u51b5\u800c\u8a00\u8fd9\u4e24\u79cd\u63d2\u5165\u65b9\u5f0f\u7684\u5904\u7406\u662f\u4e0d\u540c\u7684\uff0c\u533a\u522b\u662f\u7b2c\u56db\u79cd\u60c5\u51b5\u63d2\u5165\u591a\u4e00\u6b65\u5de6\u65cb\u64cd\u4f5c\u3002\u80fd\u770b\u51fa\uff0c<strong>\u7ea2\u9ed1\u6811\u7684\u63d2\u5165\u6700\u591a\u53ea\u9700\u8981\u8fdb\u884c\u4e24\u6b21\u65cb\u8f6c\uff0c\u4e0d\u7ba1\u8fd9\u68f5\u7ea2\u9ed1\u6811\u591a\u4e48\u590d\u6742\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u8fd9\u4e94\u79cd\u60c5\u51b5\u6765\u8fdb\u884c\u751f\u6210\u3002<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124222927093-651673821.png\" alt=\"\" \/><\/p>\n<p>\u5176\u4e2d\u5de6\u8fb9\u7684\u662f\u5de6\u5b50\u6811\u5916\u4fa7\u63d2\u5165\uff0c\u53f3\u8fb9\u7684\u662f\u5de6\u5b50\u6811\u5185\u4fa7\u63d2\u5165<\/p>\n<p><strong>put(15, &#8220;15&#8221;)<\/strong><\/p>\n<p>\u770b\u5b8cfixAfterInsertion\u65b9\u6cd5\u6d41\u7a0b\u4e4b\u540e\uff0c\u7ee7\u7eed\u6dfb\u52a0\u6570\u636e\uff0c\u8fd9\u6b21\u6dfb\u52a0\u7684\u662fput(15, &#8220;15&#8221;)\uff0c15\u6bd410\u5927\u4e14\u6bd483\u5c0f\uff0c\u56e0\u6b6415\u6700\u7ec8\u5e94\u5f53\u662f83\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u9ed8\u8ba4\u63d2\u5165\u7684\u662f\u7ea2\u8272\u8282\u70b9\uff0c\u56e0\u6b64\u9996\u5148\u5c0615\u4f5c\u4e3a\u7ea2\u8272\u8282\u70b9\u63d2\u516583\u7684\u5de6\u5b50\u8282\u70b9\u540e\u7684\u7ed3\u6784\u5e94\u5f53\u662f\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124222617156-1250942400.png\" alt=\"\" \/><\/p>\n<p>\u4f46\u662f\u663e\u7136\u8fd9\u91cc\u8fdd\u53cd\u4e86\u7ea2\u9ed1\u6811\u7684\u6027\u8d28\uff082\uff09\uff0c\u5373\u8fde\u7eed\u51fa\u73b0\u4e86\u4e24\u4e2a\u7ea2\u8272\u8282\u70b9\uff0c\u56e0\u6b64\u6b64\u65f6\u5fc5\u987b\u8fdb\u884c\u65cb\u8f6c\u3002\u56de\u770b\u524d\u9762fixAfterInsertion\u7684\u6d41\u7a0b\uff0c\u4e0a\u9762\u6f14\u793a\u7684\u662f\u5de6\u5b50\u6811\u63d2\u5165\u6d41\u7a0b\uff0c\u53f3\u5b50\u6811\u4e00\u6837\uff0c\u53ef\u4ee5\u770b\u5230\u8fd9\u662f\u53f3\u5b50\u6811\u5185\u4fa7\u63d2\u5165\uff0c\u9700\u8981\u8fdb\u884c\u4e24\u6b21\u65cb\u8f6c\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u5bf9\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9\u8fdb\u884c\u4e00\u6b21\u53f3\u65cb\u64cd\u4f5c<\/li>\n<li>\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9\u7740\u8272\u4e3a\u9ed1\u8272\uff0c\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7956\u7236\u8282\u70b9\u7740\u8272\u4e3a\u7ea2\u8272<\/li>\n<li>\u5bf9\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7956\u7236\u8282\u70b9\u8fdb\u884c\u4e00\u6b21\u5de6\u65cb\u64cd\u4f5c<\/li>\n<\/ol>\n<p>\u65cb\u8f6c\u662f\u7ea2\u9ed1\u6811\u4e2d\u6700\u96be\u7406\u89e3\u4e5f\u662f\u6700\u6838\u5fc3\u7684\u64cd\u4f5c\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u662f\u5bf9\u79f0\u7684\u64cd\u4f5c\uff0c\u6211\u4e2a\u4eba\u7684\u7406\u89e3\uff0c\u4ee5\u53f3\u65cb\u4e3a\u4f8b\uff0c\u5bf9\u67d0\u4e2a\u8282\u70b9x\u8fdb\u884c\u53f3\u65cb\uff0c\u5176\u5b9e\u8d28\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u964d\u4f4e\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u589e\u52a0\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6<\/strong><\/li>\n<li><strong>\u5c06x\u53d8\u4e3a\u5f53\u524d\u4f4d\u7f6e\u7684\u53f3\u5b50\u8282\u70b9<\/strong><\/li>\n<\/ul>\n<p>\u5de6\u65cb\u662f\u540c\u6837\u7684\u9053\u7406\uff0c\u5728\u65cb\u8f6c\u7684\u65f6\u5019\u4e00\u5b9a\u8981\u8bb0\u4f4f\u8fd9\u4e24\u53e5\u8bdd\uff0c\u8fd9\u5c06\u4f1a\u5e2e\u52a9\u6211\u4eec\u6e05\u695a\u5730\u77e5\u9053\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u65cb\u8f6c\u5982\u4f55\u8fdb\u884c\u3002<\/p>\n<p>\u5148\u770b\u4e00\u4e0b&#8221;\u5bf9\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9\u8fdb\u884c\u4e00\u6b21\u53f3\u65cb\u64cd\u4f5c&#8221;\uff0c\u6e90\u4ee3\u7801\u4e3arotateRight\u65b9\u6cd5\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>private void rotateRight(Entry&lt;K,V&gt; p) {\n        if (p != null) {\n            \/\/\u5c06L\u8bbe\u7f6e\u4e3aP\u7684\u5de6\u5b50\u6811\n            Entry&lt;K,V&gt; l = p.left;\n            \/\/\u5c06L\u7684\u53f3\u5b50\u6811\u8bbe\u7f6e\u4e3aP\u7684\u5de6\u5b50\u6811\n            p.left = l.right;\n            \/\/\u82e5L\u7684\u53f3\u5b50\u6811\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06P\u8bbe\u7f6eL\u7684\u53f3\u5b50\u6811\u7684\u7236\u8282\u70b9\n            if (l.right != null) \n                l.right.parent = p;\n            \/\/\u5c06P\u7684\u7236\u8282\u70b9\u8bbe\u7f6e\u4e3aL\u7684\u7236\u8282\u70b9\n            l.parent = p.parent;\n            \/\/\u5982\u679cP\u7684\u7236\u8282\u70b9\u4e3a\u7a7a\uff0c\u5219\u5c06L\u8bbe\u7f6e\u6839\u8282\u70b9\n            if (p.parent == null)\n                root = l;\n            \/\/\u82e5P\u4e3a\u5176\u7236\u8282\u70b9\u7684\u53f3\u5b50\u6811\uff0c\u5219\u5c06L\u8bbe\u7f6e\u4e3aP\u7684\u7236\u8282\u70b9\u7684\u53f3\u5b50\u6811\n            else if (p.parent.right == p)\n                p.parent.right = l;\n            \/\/\u5426\u5219\u5c06L\u8bbe\u7f6e\u4e3aP\u7684\u7236\u8282\u70b9\u7684\u5de6\u5b50\u6811\n            else \n                p.parent.left = l;\n            \/\/\u5c06P\u8bbe\u7f6e\u4e3aL\u7684\u53f3\u5b50\u6811\n            l.right = p;\n            \/\/\u5c06L\u8bbe\u7f6e\u4e3aP\u7684\u7236\u8282\u70b9\n            p.parent = l;\n        }\n    }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u5de6\u65cb\u4e0e\u53f3\u65cb\u662f\u4e00\u4e2a\u5bf9\u79f0\u7684\u64cd\u4f5c\uff0c\u5927\u5bb6\u53ef\u4ee5\u8bd5\u8bd5\u770b\u628a\u53f3\u56fe\u7684b\u8282\u70b9\u8fdb\u884c\u5de6\u65cb\uff0c\u5c31\u53d8\u6210\u4e86\u5de6\u56fe\u4e86\u3002\u8fd9\u91cc\u591a\u8bf4\u4e00\u53e5\uff0c<strong>\u65cb\u8f6c\u4e00\u5b9a\u8981\u8bf4\u660e\u662f\u5bf9\u54ea\u4e2a\u8282\u70b9\u8fdb\u884c\u65cb\u8f6c<\/strong>\uff0c\u7f51\u4e0a\u770b\u5f88\u591a\u6587\u7ae0\u8bb2\u5de6\u65cb\u3001\u53f3\u65cb\u90fd\u662f\u76f4\u63a5\u8bf4\u65cb\u8f6c\u4e4b\u540e\u600e\u4e48\u6837\u600e\u4e48\u6837\uff0c\u6211\u8ba4\u4e3a\u8131\u79bb\u5177\u4f53\u7684\u8282\u70b9\u8bb2\u65cb\u8f6c\u662f\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\u7684\u3002<\/p>\n<p>\u8fd9\u91cc\u53ef\u80fd\u4f1a\u6709\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff1ab\u6709\u5de6\u53f3\u4e24\u4e2a\u5b50\u8282\u70b9\u5206\u522b\u4e3ad\u548ce\uff0c\u4e3a\u4ec0\u4e48\u53f3\u65cb\u7684\u65f6\u5019\u8981\u5c06\u53f3\u5b50\u8282\u70b9e\u62ff\u5230a\u7684\u5de6\u5b50\u8282\u70b9\u800c\u4e0d\u662fb\u7684\u5de6\u5b50\u8282\u70b9d\uff1f<\/p>\n<p>\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u89e3\u91ca\u662f\uff1a<strong>\u5982\u679c\u5c06b\u7684\u5de6\u5b50\u8282\u70b9d\u62ff\u5230a\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u90a3\u4e48b\u53f3\u65cb\u540e\u53f3\u5b50\u8282\u70b9\u6307\u5411a\uff0cb\u539f\u6765\u7684\u53f3\u5b50\u8282\u70b9e\u5c31\u6210\u4e3a\u4e86\u4e00\u4e2a\u6e38\u79bb\u7684\u8282\u70b9\uff0c\u6e38\u79bb\u4e8e\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u4e4b\u5916<\/strong>\u3002<\/p>\n<p>\u56de\u5230\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u5bf983\u8fd9\u4e2a\u8282\u70b9\u8fdb\u884c\u53f3\u65cb\u4e4b\u540e\u8fd8\u6709\u4e00\u6b21\u7740\u8272\u64cd\u4f5c\uff082\uff09\uff0c\u5206\u522b\u662f\u5c06x\u7684\u7236\u8282\u70b9\u7740\u8272\u4e3a\u9ed1\u8272\uff0c\u5c06x\u7684\u7956\u7236\u8282\u70b9\u7740\u8272\u4e3a\u7ea2\u8272\uff0c\u7136\u540e\u5bf9\u8282\u70b910\u8fdb\u884c\u4e00\u6b21\u5de6\u65cb\u64cd\u4f5c\uff083\uff09\uff0c\u5de6\u65cb\u4e4b\u540e\u7684\u7ed3\u6784\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124223244078-1457287722.png\" alt=\"\" \/><\/p>\n<p><strong>put(72, &#8220;72&#8221;)<\/strong><\/p>\n<p>put(72, &#8220;72&#8221;)\u5c31\u5f88\u7b80\u5355\u4e86\uff0c72\u662f83\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u7531\u4e8e72\u7684\u7236\u8282\u70b9\u4ee5\u53ca\u53d4\u7236\u8282\u70b9\u90fd\u662f\u7ea2\u8272\u8282\u70b9\uff0c\u56e0\u6b64\u76f4\u63a5\u5c0672\u7684\u7236\u8282\u70b983\u3001\u5c0672\u7684\u53d4\u7236\u8282\u70b910\u7740\u8272\u4e3a\u9ed1\u8272\u5373\u53ef\uff0c72\u8fd9\u4e2a\u8282\u70b9\u7740\u8272\u4e3a\u7ea2\u8272\uff0c\u5373\u6ee1\u8db3\u7ea2\u9ed1\u6811\u7684\u7279\u6027\uff0c\u63d2\u516572\u4e4b\u540e\u7684\u7ed3\u6784\u56fe\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124223601859-142900299.png\" alt=\"\" \/><\/p>\n<p><strong>put(20, &#8220;20&#8221;)<\/strong><\/p>\n<p>put(20, &#8220;20&#8221;)\uff0c\u63d2\u5165\u7684\u4f4d\u7f6e\u5e94\u5f53\u662f72\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u9ed8\u8ba4\u63d2\u5165\u7ea2\u8272\uff0c\u63d2\u5165\u4e4b\u540e\u7684\u7ed3\u6784\u56fe\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124223758109-938030745.png\" alt=\"\" \/><\/p>\n<p>\u95ee\u9898\u5f88\u660e\u663e\uff0c\u51fa\u73b0\u4e86\u8fde\u7eed\u4e24\u4e2a\u7ea2\u8272\u8282\u70b9\uff0c20\u7684\u63d2\u5165\u4f4d\u7f6e\u662f\u4e00\u79cd\u5de6\u5b50\u6811\u5916\u4fa7\u63d2\u5165\u7684\u573a\u666f\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u8fdb\u884c\u7740\u8272+\u5bf9\u8282\u70b983\u8fdb\u884c\u4e00\u6b21\u53f3\u65cb\u5373\u53ef\uff0c\u7740\u8272+\u53f3\u65cb\u4e4b\u540e\u6570\u636e\u7ed3\u6784\u53d8\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124223846375-825960498.png\" alt=\"\" \/><\/p>\n<p><strong>put(60, &#8220;60&#8221;)<\/strong><\/p>\n<p>\u4e0b\u9762\u8fdb\u884cput(60, &#8220;60&#8221;)\u64cd\u4f5c\uff0c\u8282\u70b960\u63d2\u5165\u7684\u4f4d\u7f6e\u662f\u8282\u70b920\u7684\u53f3\u5b50\u8282\u70b9\uff0c\u7531\u4e8e\u8282\u70b960\u7684\u7236\u8282\u70b9\u4e0e\u53d4\u7236\u8282\u70b9\u90fd\u662f\u7ea2\u8272\u8282\u70b9\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u5c06\u8282\u70b960\u7684\u7236\u8282\u70b9\u4e0e\u53d4\u7236\u8282\u70b9\u7740\u8272\u4e3a\u9ed1\u8272\uff0c\u5c06\u8282\u70b960\u7684\u7ec4\u7236\u8282\u70b9\u7740\u8272\u4e3a\u7ea2\u8272\u5373\u53ef\u3002<\/p>\n<p>\u90a3\u4e48put(60, &#8220;60&#8221;)\u4e4b\u540e\u7684\u7ed3\u6784\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124223952281-1218713418.png\" alt=\"\" \/><\/p>\n<p><strong>put(30, &#8220;30&#8221;)<\/strong><\/p>\n<p>put(30, &#8220;30&#8221;)\uff0c\u8282\u70b930\u5e94\u5f53\u4e3a\u8282\u70b960\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u56e0\u6b64\u63d2\u5165\u8282\u70b930\u4e4b\u540e\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124224221156-298976549.png\" alt=\"\" \/><\/p>\n<p>\u663e\u7136\u8fd9\u91cc\u8fdd\u53cd\u4e86\u7ea2\u9ed1\u6811\u6027\u8d28\u5373\u8fde\u7eed\u51fa\u73b0\u4e86\u4e24\u4e2a\u7ea2\u8272\u8282\u70b9\uff0c\u56e0\u6b64\u8fd9\u91cc\u8981\u8fdb\u884c\u65cb\u8f6c\u3002<\/p>\n<p>put(30, &#8220;30&#8221;)\u7684\u64cd\u4f5c\u548cput(15, &#8220;15&#8221;)\u7684\u64cd\u4f5c\u7c7b\u4f3c\uff0c\u540c\u6837\u662f\u53f3\u5b50\u6811\u5185\u4fa7\u63d2\u5165\u7684\u573a\u666f\uff0c\u90a3\u4e48\u9700\u8981\u8fdb\u884c\u4e24\u6b21\u65cb\u8f6c\uff1a<\/p>\n<ol>\n<li>\u5bf9\u8282\u70b930\u7684\u7236\u8282\u70b9\u8282\u70b960\u8fdb\u884c\u4e00\u6b21\u53f3\u65cb<\/li>\n<li>\u53f3\u65cb\u4e4b\u540e\u5bf9\u8282\u70b960\u7684\u7956\u7236\u8282\u70b920\u8fdb\u884c\u4e00\u6b21\u5de6\u65cb<\/li>\n<\/ol>\n<p>\u53f3\u65cb+\u7740\u8272+\u5de6\u65cb\u4e4b\u540e\uff0cput(30, &#8220;30&#8221;)\u7684\u7ed3\u679c\u5e94\u5f53\u4e3a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124224332625-2051209453.png\" alt=\"\" \/><\/p>\n<p><strong>put(50, &#8220;50&#8221;)<\/strong><\/p>\n<p><strong><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124224542093-1165186117.png\" alt=\"\" \/><\/strong><\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>private void rotateLeft(Entry&lt;K,V&gt; p) {\n        if (p != null) {\n            \/\/\u83b7\u53d6P\u7684\u53f3\u5b50\u8282\u70b9\uff0c\u5176\u5b9e\u8fd9\u91cc\u5c31\u76f8\u5f53\u4e8e\u65b0\u589e\u8282\u70b9N\uff08\u60c5\u51b5\u56db\u800c\u8a00\uff09\n            Entry&lt;K,V&gt; r = p.right;\n            \/\/\u5c06R\u7684\u5de6\u5b50\u6811\u8bbe\u7f6e\u4e3aP\u7684\u53f3\u5b50\u6811\n            p.right = r.left;\n            \/\/\u82e5R\u7684\u5de6\u5b50\u6811\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06P\u8bbe\u7f6e\u4e3aR\u5de6\u5b50\u6811\u7684\u7236\u4eb2\n            if (r.left != null)\n                r.left.parent = p;\n            \/\/\u5c06P\u7684\u7236\u4eb2\u8bbe\u7f6eR\u7684\u7236\u4eb2\n            r.parent = p.parent;\n            \/\/\u5982\u679cP\u7684\u7236\u4eb2\u4e3a\u7a7a\uff0c\u5219\u5c06R\u8bbe\u7f6e\u4e3a\u8ddf\u8282\u70b9\n            if (p.parent == null)\n                root = r;\n            \/\/\u5982\u679cP\u4e3a\u5176\u7236\u8282\u70b9\uff08G\uff09\u7684\u5de6\u5b50\u6811\uff0c\u5219\u5c06R\u8bbe\u7f6e\u4e3aP\u7236\u8282\u70b9(G)\u5de6\u5b50\u6811\n            else if (p.parent.left == p)\n                p.parent.left = r;\n            \/\/\u5426\u5219R\u8bbe\u7f6e\u4e3aP\u7684\u7236\u8282\u70b9\uff08G\uff09\u7684\u53f3\u5b50\u6811\n            else\n                p.parent.right = r;\n            \/\/\u5c06P\u8bbe\u7f6e\u4e3aR\u7684\u5de6\u5b50\u6811\n            r.left = p;\n            \/\/\u5c06R\u8bbe\u7f6e\u4e3aP\u7684\u7236\u8282\u70b9\n            p.parent = r;\n        }\n    }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p><strong>\u00a0\u4e94\u3001delete\u65b9\u6cd5\u5206\u6790<\/strong><\/p>\n<p>\u9488\u5bf9\u4e8e\u7ea2\u9ed1\u6811\u7684\u589e\u52a0\u8282\u70b9\u800c\u8a00\uff0c\u5220\u9664\u663e\u5f97\u66f4\u52a0\u590d\u6742\uff0c\u4f7f\u539f\u672c\u5c31\u590d\u6742\u7684\u7ea2\u9ed1\u6811\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002\u540c\u65f6\u5220\u9664\u8282\u70b9\u548c\u589e\u52a0\u8282\u70b9\u4e00\u6837\uff0c\u540c\u6837\u662f\u627e\u5230\u5220\u9664\u7684\u8282\u70b9\uff0c\u5220\u9664\u4e4b\u540e\u8c03\u6574\u7ea2\u9ed1\u6811\u3002\u4f46\u662f\u8fd9\u91cc\u7684\u5220\u9664\u8282\u70b9\u5e76\u4e0d\u662f\u76f4\u63a5\u5220\u9664\uff0c\u800c\u662f\u901a\u8fc7\u8d70\u4e86\u201c\u5f2f\u8def\u201d\u901a\u8fc7\u4e00\u79cd\u6377\u5f84\u6765\u5220\u9664\u7684\uff1a\u627e\u5230\u88ab\u5220\u9664\u7684\u8282\u70b9D\u7684\u5b50\u8282\u70b9C\uff0c\u7528C\u6765\u66ff\u4ee3D\uff0c\u4e0d\u662f\u76f4\u63a5\u5220\u9664D\uff0c\u56e0\u4e3aD\u88abC\u66ff\u4ee3\u4e86\uff0c\u76f4\u63a5\u5220\u9664C\u5373\u53ef\u3002\u6240\u4ee5\u8fd9\u91cc\u5c31\u5c06\u5220\u9664\u7236\u8282\u70b9D\u7684\u4e8b\u60c5\u8f6c\u53d8\u4e3a\u4e86\u5220\u9664\u5b50\u8282\u70b9C\u7684\u4e8b\u60c5\uff0c\u8fd9\u6837\u5904\u7406\u5c31\u5c06\u590d\u6742\u7684\u5220\u9664\u4e8b\u4ef6\u7b80\u5355\u5316\u4e86\u3002\u5b50\u8282\u70b9C\u7684\u89c4\u5219\u662f\uff1a\u53f3\u5206\u652f\u6700\u5de6\u8fb9\uff0c\u6216\u8005 \u5de6\u5206\u652f\u6700\u53f3\u8fb9\u7684\u3002<\/p>\n<p>\u7ea2-\u9ed1\u4e8c\u53c9\u6811\u5220\u9664\u8282\u70b9\uff0c\u6700\u5927\u7684\u9ebb\u70e6\u662f\u8981\u4fdd\u6301 \u5404\u5206\u652f\u9ed1\u8272\u8282\u70b9\u6570\u76ee\u76f8\u7b49\u3002 \u56e0\u4e3a\u662f\u5220\u9664\uff0c\u6240\u4ee5\u4e0d\u7528\u62c5\u5fc3\u5b58\u5728\u989c\u8272\u51b2\u7a81\u95ee\u9898\u2014\u2014\u63d2\u5165\u624d\u4f1a\u5f15\u8d77\u989c\u8272\u51b2\u7a81\u3002<\/p>\n<p>\u7ea2\u9ed1\u6811\u5220\u9664\u8282\u70b9\u540c\u6837\u4f1a\u5206\u6210\u51e0\u79cd\u60c5\u51b5\uff0c\u8fd9\u91cc\u662f\u6309\u7167\u5f85\u5220\u9664\u8282\u70b9\u6709\u51e0\u4e2a\u513f\u5b50\u7684\u60c5\u51b5\u6765\u8fdb\u884c\u5206\u7c7b\uff1a<\/p>\n<p>1\u3001\u6ca1\u6709\u513f\u5b50\uff0c\u5373\u4e3a\u53f6\u7ed3\u70b9\u3002\u76f4\u63a5\u628a\u7236\u7ed3\u70b9\u7684\u5bf9\u5e94\u513f\u5b50\u6307\u9488\u8bbe\u4e3aNULL\uff0c\u5220\u9664\u513f\u5b50\u7ed3\u70b9\u5c31OK\u4e86\u3002<\/p>\n<p>2\u3001\u53ea\u6709\u4e00\u4e2a\u513f\u5b50\u3002\u90a3\u4e48\u628a\u7236\u7ed3\u70b9\u7684\u76f8\u5e94\u513f\u5b50\u6307\u9488\u6307\u5411\u513f\u5b50\u7684\u72ec\u751f\u5b50\uff0c\u5220\u9664\u513f\u5b50\u7ed3\u70b9\u4e5fOK\u4e86\u3002<\/p>\n<p>3\u3001\u6709\u4e24\u4e2a\u513f\u5b50\u3002\u8fd9\u79cd\u60c5\u51b5\u6bd4\u8f83\u590d\u6742\uff0c\u4f46\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u3002\u4e0a\u9762\u63d0\u5230\u8fc7\u7528\u5b50\u8282\u70b9C\u66ff\u4ee3\u4ee3\u66ff\u5f85\u5220\u9664\u8282\u70b9D\uff0c\u7136\u540e\u5220\u9664\u5b50\u8282\u70b9C\u5373\u53ef\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u8bba\u5404\u79cd\u5220\u9664\u60c5\u51b5\u6765\u8fdb\u884c\u56fe\u4f8b\u8bb2\u89e3\uff0c\u4f46\u662f\u5728\u8bb2\u89e3\u4e4b\u524d\u8bf7\u5141\u8bb8\u6211\u518d\u6b21\u5570\u55e6\u4e00\u53e5\uff0c\u8bf7\u65f6\u523b\u7262\u8bb0\u7ea2\u9ed1\u6811\u76845\u70b9\u89c4\u5b9a\uff1a<\/p>\n<p>1\u3001\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ea\u80fd\u662f\u7ea2\u8272\u6216\u8005\u9ed1\u8272<\/p>\n<p>2\u3001\u6839\u8282\u70b9\u662f\u9ed1\u8272<\/p>\n<p>3\u3001\u6bcf\u4e2a\u53f6\u8282\u70b9\uff08NIL\u8282\u70b9\uff0c\u7a7a\u8282\u70b9\uff09\u662f\u9ed1\u8272\u7684\u3002<\/p>\n<p>4\u3001\u5982\u679c\u4e00\u4e2a\u7ed3\u70b9\u662f\u7ea2\u7684\uff0c\u5219\u5b83\u4e24\u4e2a\u5b50\u8282\u70b9\u90fd\u662f\u9ed1\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u5728\u4e00\u6761\u8def\u5f84\u4e0a\u4e0d\u80fd\u51fa\u73b0\u76f8\u90bb\u7684\u4e24\u4e2a\u7ea2\u8272\u7ed3\u70b9\u3002<\/p>\n<p>5\u3001\u4ece\u4efb\u4e00\u8282\u70b9\u5230\u5176\u6bcf\u4e2a\u53f6\u5b50\u7684\u6240\u6709\u8def\u5f84\u90fd\u5305\u542b\u76f8\u540c\u6570\u76ee\u7684\u9ed1\u8272\u8282\u70b9\u3002<\/p>\n<p>\u8bda\u7136\uff0c\u65e2\u7136\u5220\u9664\u8282\u70b9\u6bd4\u8f83\u590d\u6742\uff0c\u90a3\u4e48\u5728\u8fd9\u91cc\u6211\u4eec\u5c31\u7ea6\u5b9a\u4e00\u4e0b\u89c4\u5219\uff1a<\/p>\n<p>1\u3001\u4e0b\u9762\u8981\u8bb2\u89e3\u7684\u5220\u9664\u8282\u70b9\u4e00\u5b9a\u662f\u5b9e\u9645\u8981\u5220\u9664\u8282\u70b9\u7684\u540e\u7ee7\u8282\u70b9\uff08N\uff09\uff0c\u5982\u524d\u9762\u63d0\u5230\u7684C\u3002<\/p>\n<p>2\u3001\u4e0b\u9762\u63d0\u5230\u7684\u5220\u9664\u8282\u70b9\u7684\u6811\u90fd\u662f\u5982\u4e0b\u7ed3\u6784\uff0c\u8be5\u7ed3\u6784\u6240\u9009\u53d6\u7684\u8282\u70b9\u662f\u5f85\u5220\u9664\u8282\u70b9\u7684\u53f3\u6811\u7684\u6700\u5de6\u8fb9\u5b50\u8282\u70b9\u3002\u8fd9\u91cc\u6211\u4eec\u89c4\u5b9a\u771f\u5b9e\u5220\u9664\u8282\u70b9\u4e3aN\u3001\u7236\u8282\u70b9\u4e3aP\u3001\u5144\u5f1f\u8282\u70b9\u4e3aW\u5144\u5f1f\u8282\u70b9\u7684\u4e24\u4e2a\u5b50\u8282\u70b9\u4e3aX1\u3001X2\u3002<\/p>\n<p>\u5220\u9664\u5143\u7d20\u7684\u8fc7\u7a0b\u548c\u666e\u901a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u641c\u7d22\u8fc7\u7a0b\u5927\u4f53\u4e5f\u6bd4\u8f83\u7c7b\u4f3c\uff0c\u9996\u5148\u662f\u6839\u636e\u5f85\u5220\u9664\u8282\u70b9\u7684\u60c5\u51b5\u8fdb\u884c\u5206\u6790\uff1a<\/p>\n<p>1. \u5f85\u5220\u9664\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c \u5219\u76f4\u63a5\u5220\u9664\u8be5\u8282\u70b9\u3002\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124230155109-1930464163.png\" alt=\"\" \/><\/p>\n<p>2. \u5f85\u5220\u9664\u8282\u70b9\u6709\u4e00\u4e2a\u5b50\u8282\u70b9\uff0c\u5219\u7528\u8be5\u5b50\u8282\u70b9\u66ff\u6362\u5b83\u7684\u7236\u8282\u70b9\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124230357265-752652354.png\" alt=\"\" \/><\/p>\n<p>3. \u5f85\u5220\u9664\u8282\u70b9\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\uff0c\u5219\u53d6\u5b83\u7684\u540e\u7ee7\u8282\u70b9\u66ff\u6362\u5b83\uff0c\u5e76\u5220\u9664\u8fd9\u4e2a\u540e\u7ee7\u8282\u70b9\u539f\u6765\u7684\u4f4d\u7f6e\u3002\u5b83\u53ef\u80fd\u6709\u79cd\u60c5\u51b5<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124230439140-453103889.png\" alt=\"\" \/><\/p>\n<h3>\u5220\u9664\u540e\u7684\u8c03\u6574<\/h3>\n<p>\u5220\u9664\u5143\u7d20\u4e4b\u540e\u7684\u8c03\u6574\u548c\u524d\u9762\u7684\u63d2\u5165\u5143\u7d20\u8c03\u6574\u7684\u8fc7\u7a0b\u6bd4\u8d77\u6765\u66f4\u590d\u6742\u3002\u5b83\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u539f\u6765\u8fc7\u7a0b\u4e2d\u53d6\u53cd\u3002\u6211\u4eec\u5148\u4ece\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u70b9\u5f00\u59cb\u5165\u624b\u3002\u9996\u5148\u4e00\u4e2a\uff0c\u6211\u4eec\u8981\u8fdb\u884c\u8c03\u6574\u7684\u8fd9\u4e2a\u70b9\u80af\u5b9a\u662f\u56e0\u4e3a\u6211\u4eec\u8981\u5220\u9664\u7684\u8fd9\u4e2a\u70b9\u7834\u574f\u4e86\u7ea2\u9ed1\u6811\u7684\u672c\u8d28\u7279\u6027\u3002\u800c\u5982\u679c\u6211\u4eec\u5220\u9664\u7684\u8fd9\u4e2a\u70b9\u662f\u7ea2\u8272\u7684\uff0c\u5219\u5b83\u80af\u5b9a\u4e0d\u4f1a\u7834\u574f\u91cc\u9762\u7684\u5c5e\u6027\u3002\u56e0\u4e3a\u4ece\u524d\u9762\u5220\u9664\u7684\u8fc7\u7a0b\u6765\u770b\uff0c\u6211\u4eec\u8fd9\u4e2a\u8981\u5220\u9664\u7684\u70b9\u662f\u5df2\u7ecf\u5728\u6fd2\u4e34\u53f6\u8282\u70b9\u7684\u9644\u8fd1\u4e86\uff0c\u5b83\u8981\u4e48\u6709\u4e00\u4e2a\u5b50\u8282\u70b9\uff0c\u8981\u4e48\u5c31\u662f\u4e00\u4e2a\u53f6\u8282\u70b9\u3002\u5982\u679c\u5b83\u662f\u7ea2\u8272\u7684\uff0c\u5220\u9664\u4e86\uff0c\u4ece\u4e0a\u9762\u7684\u8282\u70b9\u5230\u53f6\u8282\u70b9\u6240\u7ecf\u5386\u7684\u9ed1\u8272\u8282\u70b9\u6ca1\u6709\u53d8\u5316\u3002\u6240\u4ee5\uff0c\u8fd9\u91cc\u7684\u4e00\u4e2a\u524d\u7f6e\u6761\u4ef6\u5c31\u662f\u5f85\u5220\u9664\u7684\u8282\u70b9\u662f\u9ed1\u8272\u7684\u3002<\/p>\n<p>\u5728\u524d\u9762\u7684\u90a3\u4e2a\u524d\u63d0\u4e0b\uff0c\u6211\u4eec\u8981\u8c03\u6574\u7ea2\u9ed1\u6811\u7684\u76ee\u7684\u5c31\u662f\u8981\u4fdd\u8bc1\uff0c\u8fd9\u4e2a\u539f\u6765\u662f\u9ed1\u8272\u7684\u8282\u70b9\u88ab\u5220\u9664\u540e\uff0c\u6211\u4eec\u8981\u901a\u8fc7\u4e00\u5b9a\u7684\u53d8\u5316\uff0c\u4f7f\u5f97\u4ed6\u4eec\u4ecd\u7136\u662f\u5408\u6cd5\u7684\u7ea2\u9ed1\u6811\u3002\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u5728\u4e00\u4e2a\u9ed1\u8272\u8282\u70b9\u88ab\u5220\u9664\u540e\uff0c\u4ece\u4e0a\u9762\u7684\u8282\u70b9\u5230\u5b83\u6240\u5728\u7684\u53f6\u8282\u70b9\u8def\u5f84\u6240\u7ecf\u5386\u7684\u9ed1\u8272\u8282\u70b9\u5c31\u5c11\u4e86\u4e00\u4e2a\u3002\u6211\u4eec\u9700\u8981\u505a\u4e00\u4e9b\u8c03\u6574\uff0c\u4f7f\u5f97\u5b83\u5c11\u7684\u8fd9\u4e2a\u5728\u540e\u9762\u67d0\u4e2a\u5730\u65b9\u80fd\u591f\u8865\u4e0a\u3002<\/p>\n<p>ok\uff0c\u6709\u4e86\u8fd9\u4e00\u90e8\u5206\u7684\u7406\u89e3\uff0c\u6211\u4eec\u518d\u6765\u770b\u8c03\u6574\u8282\u70b9\u7684\u51e0\u79cd\u60c5\u51b5\u3002<\/p>\n<p>1. \u5f53\u524d\u8282\u70b9\u548c\u5b83\u7684\u7236\u8282\u70b9\u662f\u9ed1\u8272\u7684\uff0c\u800c\u5b83\u7684\u5144\u5f1f\u8282\u70b9\u662f\u7ea2\u8272\u7684\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124230633937-319334034.png\" alt=\"\" \/><\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\u65e2\u7136\u5b83\u7684\u5144\u5f1f\u8282\u70b9\u662f\u7ea2\u8272\u7684\uff0c\u4ece\u7ea2\u9ed1\u6811\u7684\u5c5e\u6027\u6765\u770b\uff0c\u5b83\u7684\u5144\u5f1f\u8282\u70b9\u5fc5\u7136\u6709\u4e24\u4e2a\u9ed1\u8272\u7684\u5b50\u8282\u70b9\u3002\u8fd9\u91cc\u5c31\u901a\u8fc7\u8282\u70b9x\u7684\u7236\u8282\u70b9\u5de6\u65cb\uff0c\u7136\u540e\u7236\u8282\u70b9B\u989c\u8272\u53d8\u6210\u7ea2\u8272\uff0c\u800c\u539f\u6765\u7684\u5144\u5f1f\u8282\u70b9D\u53d8\u6210\u9ed1\u8272\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u5c06\u6811\u8f6c\u53d8\u6210\u7b2c\u4e8c\u79cd\u60c5\u5f62\u4e2d\u7684\u67d0\u4e00\u79cd\u60c5\u51b5\u3002\u5728\u505a\u540e\u7eed\u53d8\u5316\u524d\uff0c\u8fd9\u68f5\u6811\u8fd9\u4e48\u7684\u53d8\u5316\u8fd8\u662f\u4fdd\u6301\u7740\u539f\u6765\u7684\u5e73\u8861\u3002<\/p>\n<p>2. 1\uff09 \u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9\u4e3a\u7ea2\u8272\uff0c\u800c\u5b83\u7684\u5144\u5f1f\u8282\u70b9\uff0c\u5305\u62ec\u5144\u5f1f\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9\u90fd\u662f\u9ed1\u8272\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124230741812-1836079901.png\" alt=\"\" \/><\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u5b83\u7684\u5144\u5f1f\u8282\u70b9\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff0c\u7136\u540ex\u8282\u70b9\u6307\u5411\u5b83\u7684\u7236\u8282\u70b9\u3002\u8fd9\u91cc\u6709\u4e2a\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u7684\u5730\u65b9\uff0c\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u8fd9\u4e48\u4e00\u53d8\u4e4b\u540e\u5b83\u5c31\u5e73\u8861\u4e86\u5462\uff1f\u56e0\u4e3a\u6211\u4eec\u5047\u5b9aA\u8282\u70b9\u662f\u8981\u8c03\u6574\u7684\u8282\u70b9\u4e00\u8def\u8c03\u6574\u8fc7\u6765\u7684\u3002\u56e0\u4e3a\u539f\u6765\u90a3\u4e2a\u8981\u8c03\u6574\u7684\u8282\u70b9\u4e3a\u9ed1\u8272\uff0c\u5b83\u4e00\u65e6\u88ab\u5220\u9664\u5c31\u8def\u5f84\u4e0a\u7684\u9ed1\u8272\u8282\u70b9\u5c11\u4e861.\u6240\u4ee5\u8fd9\u91ccA\u6240\u5728\u7684\u8def\u5f84\u90fd\u662f\u9ed1\u8272\u8282\u70b9\u5c111.\u8fd9\u91cc\u5c06A\u7684\u5144\u5f1f\u8282\u70b9\u53d8\u6210\u7ea2\u8272\u540e\uff0c\u4ece\u5b83\u7684\u7236\u8282\u70b9\u5230\u4e0b\u9762\u7684\u6240\u6709\u8def\u5f84\u5c31\u90fd\u7edf\u4e00\u5c11\u4e861.\u4fdd\u8bc1\u6700\u540e\u53c8\u90fd\u5e73\u8861\u4e86\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5927\u5bb6\u8fd8\u4f1a\u6709\u4e00\u4e2a\u62c5\u5fe7\uff0c\u5c31\u662f\u5f53\u524d\u8c03\u6574\u7684\u6bd5\u7adf\u53ea\u662f\u4e00\u68f5\u6811\u4e2d\u95f4\u7684\u5b57\u6570\uff0c\u8fd9\u91cc\u5934\u7684\u8282\u70b9B\u53ef\u80fd\u8fd8\u6709\u7236\u8282\u70b9\uff0c\u8fd9\u4e48\u4e00\u76f4\u5f80\u4e0a\u5230\u6839\u8282\u70b9\u3002\u4f60\u8fd9\u4e48\u4e00\u68f5\u5b57\u6570\u5c11\u4e86\u4e00\u4e2a\u9ed1\u8272\u8282\u70b9\uff0c\u8981\u4fdd\u8bc1\u6574\u7406\u5408\u683c\u8fd8\u662f\u4e0d\u591f\u7684\u3002\u8fd9\u91cc\u5728\u4ee3\u7801\u91cc\u6709\u4e86\u4e00\u4e2a\u4fdd\u8bc1\u3002\u5047\u8bbe\u8fd9\u91ccB\u5df2\u7ecf\u662f\u7ea2\u8272\u7684\u4e86\u3002\u90a3\u4e48\u4ee3\u7801\u91cc\u90a3\u4e2a\u5faa\u73af\u5757\u5c31\u8df3\u51fa\u6765\u4e86\uff0c\u6700\u540e\u7684\u90e8\u5206\u8fd8\u662f\u4f1a\u5bf9B\u8282\u70b9\uff0c\u4e5f\u5c31\u662fx\u6240\u6307\u5411\u7684\u8fd9\u4e2a\u8282\u70b9\u7f6e\u6210\u9ed1\u8272\u3002\u8fd9\u6837\u4fdd\u8bc1\u524d\u9762\u4e8f\u7684\u90a3\u4e00\u4e2a\u9ed1\u8272\u8282\u70b9\u5c31\u8865\u56de\u6765\u4e86\u3002<\/p>\n<p>2\uff09 \u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9\u4e3a\u9ed1\u8272\uff0c\u800c\u5b83\u7684\u5144\u5f1f\u8282\u70b9\uff0c\u5305\u62ec\u5144\u5f1f\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9\u90fd\u662f\u9ed1\u8272\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u548c\u524d\u9762\u6bd4\u8f83\u7c7b\u4f3c\u3002\u5982\u679c\u63a5\u7740\u524d\u9762\u7684\u8ba8\u8bba\u6765\uff0c\u5728\u505a\u4e86\u90a3\u4e2a\u5c06\u5144\u5f1f\u8282\u70b9\u7f6e\u6210\u7ea2\u8272\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u4ece\u7236\u8282\u70b9B\u5f00\u59cb\u7684\u6240\u6709\u5b50\u8282\u70b9\u90fd\u5c11\u4e861.\u90a3\u4e48\u8fd9\u91cc\u4ece\u4ee3\u7801\u4e2d\u95f4\u770b\u7684\u8bdd\uff0c\u7531\u4e8ex\u6307\u5411\u4e86\u7236\u8282\u70b9\uff0c\u4ecd\u7136\u662f\u9ed1\u8272\u3002\u5219\u8fd9\u4e2a\u65f6\u5019\u4ee5\u7236\u8282\u70b9B\u4f5c\u4e3a\u57fa\u51c6\u7684\u5b50\u6811\u4e0b\u9762\u90fd\u5c11\u4e86\u9ed1\u8282\u70b91. \u6211\u4eec\u5c31\u63a5\u7740\u4ee5\u8fd9\u4e48\u4e00\u79cd\u60c5\u51b5\u5411\u4e0a\u9762\u63a8\u8fdb\u3002<\/p>\n<p>3. \u00a0\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9\u4e3a\u7ea2\u8272\uff0c\u800c\u5b83\u7684\u5144\u5f1f\u8282\u70b9\u662f\u9ed1\u8272\uff0c\u540c\u65f6\u5144\u5f1f\u8282\u70b9\u6709\u4e00\u4e2a\u8282\u70b9\u662f\u7ea2\u8272\u3002<img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124231113218-303819138.png\" alt=\"\" \/><\/p>\n<p>\u8fd9\u91cc\u6240\u505a\u7684\u64cd\u4f5c\u5c31\u662f\u5148\u5c06\u5144\u5f1f\u8282\u70b9\u505a\u4e00\u4e2a\u53f3\u65cb\u64cd\u4f5c\uff0c\u8f6c\u53d8\u6210\u7b2c4\u79cd\u60c5\u51b5\u3002\u5f53\u7136\uff0c\u524d\u9762\u7684\u524d\u63d0\u662fB\u4e3a\u7ea2\u8272\uff0c\u5728B\u4e3a\u9ed1\u8272\u7684\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u540c\u6837\u7684\u5904\u7406\u3002<\/p>\n<p>4. \u5728\u5f53\u524d\u5144\u5f1f\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u662f\u7ea2\u8272\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/401339\/201711\/401339-20171124231156437-937263510.png\" alt=\"\" \/><\/p>\n<p>\u8fd9\u91cc\u662f\u4e00\u79cd\u6bd4\u8f83\u7406\u60f3\u7684\u5904\u7406\u60c5\u51b5\uff0c\u6211\u4eec\u5c06\u7236\u8282\u70b9\u505a\u4e00\u4e2a\u5de6\u65cb\u64cd\u4f5c\uff0c\u540c\u65f6\u5c06\u7236\u8282\u70b9B\u53d8\u6210\u9ed1\u8272\uff0c\u800c\u5c06\u539f\u6765\u7684\u5144\u5f1f\u8282\u70b9D\u53d8\u6210\u7ea2\u8272\uff0c\u5e76\u5c06D\u7684\u53f3\u5b50\u8282\u70b9\u53d8\u6210\u9ed1\u8272\u3002\u8fd9\u6837\u4fdd\u8bc1\u4e86\u65b0\u7684\u5b50\u6811\u4e2d\u95f4\u6839\u8282\u70b9\u5230\u5404\u53f6\u5b50\u8282\u70b9\u7684\u8def\u5f84\u4f9d\u7136\u662f\u5e73\u8861\u7684\u3002\u5927\u5bb6\u770b\u5230\u8fd9\u91cc\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u6709\u70b9\u5947\u602a\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u4e00\u6b65\u8c03\u6574\u7ed3\u675f\u540e\u5c31\u76f4\u63a5x = T.root\u4e86\u5462\uff1f\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u4e00\u8d70\u5b8c\u8fd9\u4e2a\u5c31\u53ef\u4ee5\u628ax\u76f4\u63a5\u8df3\u5230\u6839\u8282\u70b9\uff0c\u5176\u4ed6\u7684\u90fd\u4e0d\u9700\u8981\u770b\u4e86\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u524d\u9762\u7684\u4e00\u4e2a\u524d\u63d0\uff0cA\u8282\u70b9\u5411\u4e0a\u6240\u5728\u7684\u8def\u5f84\u90fd\u662f\u9ed1\u8272\u8282\u70b9\u5c11\u4e86\u4e00\u4e2a\u7684\uff0c\u8fd9\u91cc\u6211\u4eec\u4ee5\u8c03\u6574\u4e4b\u540e\u76f8\u5f53\u4e8e\u7ed9\u5b83\u589e\u52a0\u4e86\u4e00\u4e2a\u9ed1\u8272\u8282\u70b9\uff0c\u540c\u65f6\u5bf9\u5176\u4ed6\u5b50\u6811\u7684\u8282\u70b9\u6ca1\u6709\u4efb\u4f55\u53d8\u5316\u3002\u76f8\u5f53\u4e8e\u6211\u5185\u90e8\u5df2\u7ecf\u7ed9\u5b83\u8865\u507f\u4e0a\u6765\u4e86\u3002\u6240\u4ee5\u540e\u7eed\u5c31\u4e0d\u9700\u8981\u518d\u5f80\u4e0a\u53bb\u8c03\u6574\u3002<\/p>\n<p>\u524d\u9762\u8ba8\u8bba\u7684\u8fd94\u79cd\u60c5\u51b5\u662f\u5728\u5f53\u524d\u8282\u70b9\u662f\u7236\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u6761\u4ef6\u4e0b\u8fdb\u884c\u7684\u3002\u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u7236\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\uff0c\u5219\u53ef\u4ee5\u5bf9\u5e94\u7684\u505a\u5bf9\u79f0\u7684\u64cd\u4f5c\u5904\u7406\uff0c\u8fc7\u7a0b\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/p>\n<h2>\u5176\u4ed6<\/h2>\n<p>TreeMap\u7684\u7ea2\u9ed1\u6811\u5b9e\u73b0\u5f53\u7136\u4e5f\u5305\u542b\u5176\u4ed6\u90e8\u5206\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u5982\u7528\u4e8e\u67e5\u627e\u5143\u7d20\u7684getEntry\u65b9\u6cd5\uff0c\u53d6\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684getFirstEntry, getLastEntry\u65b9\u6cd5\u4ee5\u53ca\u6c42\u524d\u9a71\u548c\u540e\u7ee7\u7684predecesor, successor\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5b9e\u73b0\u548c\u666e\u901a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5b9e\u73b0\u6ca1\u4ec0\u4e48\u660e\u663e\u5dee\u522b\u3002\u8fd9\u91cc\u5c31\u5ffd\u7565\u4e0d\u8ba8\u8bba\u4e86\u3002\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u6709\u610f\u601d\u7684\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5c31\u662fbuildFromSorted\u65b9\u6cd5\u3002\u5b83\u7684\u5b9e\u73b0\u8fc7\u7a0b\u5e76\u4e0d\u590d\u6742\uff0c\u4e0d\u8fc7\u7ecf\u5e38\u88ab\u4f5c\u4e3a\u9762\u8bd5\u7684\u95ee\u9898\u6765\u8ba8\u8bba\u3002<\/p>\n<p><strong>\u516d\u3001\u5c0f\u8282<\/strong><\/p>\n<p>\u8fd9\u7bc7\u535a\u6587\u786e\u5b9e\u662f\u6709\u70b9\u513f\u957f\uff0c\u5728\u8fd9\u91cc\u975e\u5e38\u611f\u8c22\u5404\u4f4d\u770b\u5ba2\u80fd\u591f\u9759\u4e0b\u5fc3\u6765\u8bfb\u5b8c\uff0c\u6211\u60f3\u4f60\u901a\u8fc7\u8bfb\u5b8c\u8fd9\u7bc7\u535a\u6587\u4e00\u5b9a\u6536\u83b7\u4e0d\u5c0f\u3002<br \/>\n\u540c\u65f6\u8fd9\u7bc7\u535a\u6587\u6211\u5199\u7684\u8fc7\u7a0b\u4e2d\uff0c\u770b\u4e86\u3001\u53c2\u8003\u4e86\u5927\u91cf\u7684\u535a\u6587\u3002\u540c\u65f6\u4e0d\u514d\u4f1a\u6709\u4e9b\u5730\u65b9\u5b58\u5728\u501f\u9274\u4e4b\u5904\uff0c\u5728\u8fd9\u91cc\u5bf9\u5176\u8868\u793a\u611f\u8c22\u3002<\/p>\n<p><strong>\u53e6\u5916\uff0c\u6211\u60f3\u8bf4\u7684\u662f\uff0c\u91cd\u70b9\u8981\u638c\u63e1\u7684\u662fput\u65b9\u6cd5\u5373\u53ef\uff0c\u5176\u5b83\u5982\u679c\u7cbe\u529b\u8db3\u591f\u53ef\u4ee5\u7ec6\u7ec6\u54c1\u8bfb\u3002<\/strong><\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p>1\u3001\u7ea2\u9ed1\u6811\u6570\u636e\u7ed3\u6784\u5256\u6790\uff1a<a title=\"http:\/\/www.cnblogs.com\/fanzhidongyzby\/p\/3187912.html\" href=\"http:\/\/www.cnblogs.com\/fanzhidongyzby\/p\/3187912.html\">http:\/\/www.cnblogs.com\/fanzhidongyzby\/p\/3187912.html<\/a><\/p>\n<p>2\u3001\u7ea2\u9ed1\u4e8c\u53c9\u6811\u8be6\u89e3\u53ca\u7406\u8bba\u5206\u6790 \uff1a<a title=\"http:\/\/blog.csdn.net\/kartorz\/article\/details\/8865997\" href=\"http:\/\/blog.csdn.net\/kartorz\/article\/details\/8865997\">http:\/\/blog.csdn.net\/kartorz\/article\/details\/8865997<\/a><\/p>\n<p>3\u3001<a href=\"http:\/\/blog.csdn.net\/v_july_v\/article\/details\/6105630\">\u6559\u4f60\u900f\u5f7b\u4e86\u89e3\u7ea2\u9ed1\u6811\u00a0<\/a>\uff1a<a href=\"http:\/\/cmsblogs.com\/blog.csdn.net\/v_july_v\/article\/details\/6105630\">blog.csdn.net\/v_july_v\/article\/details\/6105630<\/a><\/p>\n<p>4\u3001\u7ecf\u5178\u7b97\u6cd5\u7814\u7a76\u7cfb\u5217\uff1a\u4e94\u3001\u7ea2\u9ed1\u6811\u7b97\u6cd5\u7684\u5b9e\u73b0\u4e0e\u5256\u6790 \uff1a<a title=\"http:\/\/blog.csdn.net\/v_JULY_v\/article\/details\/6109153\" href=\"http:\/\/blog.csdn.net\/v_JULY_v\/article\/details\/6109153\">http:\/\/blog.csdn.net\/v_JULY_v\/article\/details\/6109153<\/a><\/p>\n<p>5\u3001\u793a\u4f8b\uff0c\u7ea2\u9ed1\u6811\u63d2\u5165\u548c\u5220\u9664\u8fc7\u7a0b\uff1a<a title=\"http:\/\/saturnman.blog.163.com\/blog\/static\/557611201097221570\/\" href=\"http:\/\/saturnman.blog.163.com\/blog\/static\/557611201097221570\/\">http:\/\/saturnman.blog.163.com\/blog\/static\/557611201097221570\/<\/a><\/p>\n<p>6\u3001\u7ea2\u9ed1\u4e8c\u53c9\u6811\u8be6\u89e3\u53ca\u7406\u8bba\u5206\u6790 \uff1a<a title=\"http:\/\/blog.csdn.net\/kartorz\/article\/details\/8865997\" href=\"http:\/\/blog.csdn.net\/kartorz\/article\/details\/8865997\">http:\/\/blog.csdn.net\/kartorz\/article\/details\/8865997<\/a><\/p>\n<p>7\u3001\u7ea2\u9ed1\u6811\u6982\u5ff5\u3001\u7ea2\u9ed1\u6811\u7684\u63d2\u5165\u53ca\u65cb\u8f6c\u64cd\u4f5c\uff1a<a href=\"http:\/\/www.cnblogs.com\/xrq730\/p\/6867924.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.cnblogs.com\/xrq730\/p\/6867924.html<\/a><\/p>\n<p>8\u3001treemap:<a href=\"http:\/\/blog.csdn.net\/chenssy\/article\/details\/26668941\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/blog.csdn.net\/chenssy\/article\/details\/26668941<\/a><\/p>\n<\/div>\n<div class=\"clear\"><\/div>\n<div id=\"blog_post_info_block\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\u81ea\uff1ahttps:\/\/www.cnblogs.com\/pony1223\/p\/7892401.htm &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"series":[],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-programmers"],"_links":{"self":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/507"}],"collection":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=507"}],"version-history":[{"count":1,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":508,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions\/508"}],"wp:attachment":[{"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=507"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}