{"id":10006651,"date":"2022-10-08T19:36:58","date_gmt":"2022-10-08T11:36:58","guid":{"rendered":"https:\/\/ityet.com\/?p=10006651"},"modified":"2022-10-08T19:36:58","modified_gmt":"2022-10-08T11:36:58","slug":"35%ef%bc%8c%e5%a6%82%e6%9e%9c%e9%9d%a2%e8%af%95%e8%ae%a9%e6%88%91%e6%89%8b%e5%86%99%e7%ba%a2%e9%bb%91%e6%a0%91%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006651","title":{"rendered":"35+\uff0c\u5982\u679c\u9762\u8bd5\u8ba9\u6211\u624b\u5199\u7ea2\u9ed1\u6811\uff01"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u5c0f\u5085\u54e5<br \/>\u535a\u5ba2\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Z%2FcbnDer3lXdXrdF9jyZlA%3D%3D.ObzikqzTmpQzuVAmJn6n6rdLVaAtg9ndNdVzM%2FCuB6w%3D\" rel=\"nofollow\">https:\/\/bugstack.cn<\/a><\/p>\n<blockquote><p>\u6c89\u6dc0\u3001\u5206\u4eab\u3001\u6210\u957f\uff0c\u8ba9\u81ea\u5df1\u548c\u4ed6\u4eba\u90fd\u80fd\u6709\u6240\u6536\u83b7\uff01\ud83d\ude04<\/p><\/blockquote>\n<h2>\u4e00\u3001\u524d\u8a00\uff1a\u6302\u5728\u6811\u4e0a\uff01<\/h2>\n<p><code>\u4e0d\u77e5\u9053\u4f60\u7ecf\u5386\u8fc7HashMap\u7684\u593a\u547d\u8fde\u73af\u95ee\uff01<\/code><\/p>\n<p>\u4e3a\u5565\uff0c\u9762\u8bd5\u5b98\u90a3\u4e48\u559c\u6b22\u8ba9\u4f60\u804a\u804a HashMap\uff1f\u56e0\u4e3a HashMap \u6d89\u53ca\u7684\u4e1c\u897f\u5e7f\uff0c\u7528\u5230\u7684\u6570\u636e\u7ed3\u6784\u591a\uff0c\u95ee\u9898\u5ef6\u5c55\u6027\u597d\uff0c\u4e00\u4e2a HashMap \u5c31\u80fd\u804a\u4e0b\u676580%\u7684\u6570\u636e\u7ed3\u6784\u4e86\u3002\u800c\u4e14\u9762\u8bd5 HashMap \u80fd\u901a\u8fc7\u4f60\u5bf9\u7ea2\u9ed1\u6811\u7684\u4e86\u89e3\u5b9a\u4f4d\u51fa\u4f60\u54ea\u4e2a\u7ea7\u522b\u7684\u7814\u53d1\u4eba\u5458\u3002<\/p>\n<p>\u800c\u7ea2\u9ed1\u6811\u7684\u77e5\u8bc6\u70b9\u53ef\u4ee5\u8bf4\u662f\u975e\u5e38\u5e9e\u5927\uff0c\u5728\u5b66\u4e60\u7ea2\u9ed1\u6811\u65f6\u4e5f\u4e0d\u80fd\u4e00\u4e0b\u5c31\u80fd\u638c\u63e1\uff0c\u751a\u81f3\u5f88\u7a0b\u5e8f\u5458\u538b\u6839\u5c31\u6ca1\u641e\u660e\u767d\u7ea2\u9ed1\u6811\uff0c\u53ea\u662f\u80cc\u4e0b\u6765\u5b83\u7684\u51e0\u6761\u9650\u5b9a\u89c4\u5219\u800c\u5df2\u3002<\/p>\n<p><strong>\u5176\u5b9e\u4e00\u5f00\u59cb\u6211\u4e5f\u662f\u8fd9\u6837\uff01<\/strong> \u4e0d\u8fc7\u603b\u611f\u89c9\u8fd9\u5757\u7684\u77e5\u8bc6\u70b9\u4e0d\u641e\u4e2a\u660e\u660e\u767d\u767d\uff0c\u5c31\u95f9\u5fc3\u3002\u56e0\u4e3a\u4e0d\u53ef\u80fd\u4e00\u4e2a\u7406\u79d1\u7684\u4e1c\u897f\uff0c\u662f\u9700\u8981\u6b7b\u8bb0\u786c\u80cc\u641e\u4e0b\u6765\u7684\u3002\u6240\u4ee5\u5728\u7ffb\u9605\u8d44\u6599\u3001\u6587\u6863\u3001\u5386\u53f2\u3001\u5178\u7c4d\uff0c\u627e\u5230\u7ea2\u9ed1\u6811\u7684\u6f14\u5316\u8fc7\u7a0b\uff0c\u5b83\u662f\u4ece2-3\u6811\u6f14\u53d8\u800c\u6765\uff0c\u800c2-3\u6811\u3001AVL\u6811\uff0c\u8fd9\u7c7bB-\u6811\uff0c\u4e5f\u5c31\u662f BalancedTree \u5e73\u8861\u6811\u3002\u5b83\u4eec\u90fd\u662f\u4e3a\u4e86\u89e3\u51b3 BST \u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u81ea\u5e73\u8861\u800c\u6765\u7684\u4ea7\u7269\u3002<\/p>\n<p>\u90a3\u4e48\u73b0\u5728\u6e05\u695a\u4e86\uff0c\u8981\u60f3\u641e\u5b9a\u7ea2\u9ed1\u6811\uff0c\u8ba9\u61c2\u4e86\u5c31\u662f\u771f\u7684\u61c2\uff0c\u5c31\u9700\u8981\u628a\u524d\u9762\u8fd9\u4e9b\u77e5\u8bc6\u641e\u5b9a\uff0c\u5e76\u4e14\u9664\u4e86\u7406\u8bba\u8fd8\u80fd\u7528\u843d\u5730\u7684\u6848\u4f8b\u4ee3\u7801\u7f16\u5199\u51fa\u6765\uff0c\u624d\u662f\u609f\u900f\u3002<strong>\u597d\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\uff0c\u5c0f\u5085\u54e5\u5c31\u5e26\u7740\u4f60\u4e00\u8d77\u641e\u5b9a\u8fd9\u70b9\u4e8b<\/strong><\/p>\n<h2>\u4e8c\u3001BST \u6811<\/h2>\n<p><code>Binary Search Tree\u5386\u53f2<\/code><\/p>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u7b97\u6cd5\u662f\u7531\u5305\u62ec PF Windley\u3001Andrew Donald Booth\u3001Andrew Colin\u3001Thomas N. Hibbard \u5728\u5185\u7684\u51e0\u4f4d\u7814\u7a76\u4eba\u5458\u72ec\u7acb\u53d1\u73b0\u7684\u3002\u8be5\u7b97\u6cd5\u5f52\u529f\u4e8e Conway Berners-Lee \u548c David Wheeler \uff0c\u4ed6\u4eec\u5728 1960 \u5e74\u4f7f\u7528\u5b83\u5728\u78c1\u5e26\u4e2d\u5b58\u50a8\u6807\u8bb0\u6570\u636e\u3002 \u6700\u65e9\u548c\u6d41\u884c\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u7b97\u6cd5\u4e4b\u4e00\u662f Hibbard \u7b97\u6cd5\u3002<\/p>\n<h3>1. \u4e8c\u53c9\u641c\u7d22\u6811\u6570\u636e\u7ed3\u6784<\/h3>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\uff08Binary Search Tree\uff09\uff0c\u4e5f\u79f0\u4e8c\u53c9\u67e5\u627e\u6811\u3002\u5982\u679c\u4f60\u770b\u89c1\u6709\u5e8f\u4e8c\u53c9\u6811\uff08Ordered Binary tree\uff09\u3001\u6392\u5e8f\u4e8c\u53c9\u6811\uff08Sorted Binary Tree\uff09\u90a3\u4e48\u8bf4\u7684\u90fd\u662f\u4e00\u4e2a\u4e1c\u897f\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-bst-01.png?raw=true&quot; width=&quot;400px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<ul>\n<li>\u82e5\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u4e0d\u7a7a\uff0c\u5219\u5de6\u5b50\u6811\u4e0a\u6240\u6709\u8282\u70b9\u7684\u503c\u5747\u5c0f\u4e8e\u5b83\u7684\u6839\u8282\u70b9\u7684\u503c\uff1b<\/li>\n<li>\u82e5\u4efb\u610f\u8282\u70b9\u7684\u53f3\u5b50\u6811\u4e0d\u7a7a\uff0c\u5219\u53f3\u5b50\u6811\u4e0a\u6240\u6709\u8282\u70b9\u7684\u503c\u5747\u5927\u4e8e\u5b83\u7684\u6839\u8282\u70b9\u7684\u503c\uff1b<\/li>\n<li>\u4efb\u610f\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u6811\u4e5f\u5206\u522b\u4e3a\u4e8c\u53c9\u67e5\u627e\u6811\uff1b<\/li>\n<\/ul>\n<hr>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u4f7f\u7528\u7684\u573a\u666f\u8fd8\u662f\u6bd4\u8f83\u591a\u7684\uff0c\u4f8b\u5982\u57fa\u4e8e\u7ec4\u5408\u6a21\u5f0f\u5b9e\u73b0\u7684\u89c4\u5219\u5f15\u64ce\uff0c\u5b83\u5c31\u662f\u4e00\u9897\u4e8c\u53c9\u641c\u7d22\u6811\u3002\u4f46\u7c7b\u4f3c\u8fd9\u6837\u7684\u5f00\u53d1\u4e2d\u7528\u5230\u7684\u4e8c\u53c9\u6811\u573a\u666f\uff0c\u90fd\u662f\u57fa\u4e8e\u914d\u7f6e\u751f\u6210\uff0c\u6240\u4ee5\u7ec4\u5408\u51fa\u6765\u7684\u8282\u70b9\u4e5f\u66f4\u52a0\u65b9\u4fbf\u63a7\u5236\u6811\u9ad8\u548c\u5e73\u8861\u6027\u3002\u8fd9\u4e0e Java API HashMap \u4e2d\u7684\u7ea2\u9ed1\u6811\u8fd9\u6837\u4e3a\u4e86\u89e3\u51b3\u63d2\u5165\u8282\u70b9\u540e\u4ecd\u4fdd\u6301\u6811\u7684\u5e73\u8861\u6027\u662f\u6709\u6240\u4e0d\u540c\u7684\u3002<\/p>\n<p>\u6240\u4ee5\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u4e00\u9897\u6ca1\u6709\u7ecf\u8fc7\u8c03\u8861\u7684\u57fa\u7840\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5728\u4e00\u5b9a\u6982\u7387\u4e0a\u5b83\u5b8c\u6210\u6709\u53ef\u80fd\u9000\u5316\u6210\u94fe\u8868\uff0c\u4e5f\u5c31\u662f\u4ece\u8fd1\u4f3cO(logn)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u5230O(n)\u3002\u5173\u4e8e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5e73\u8861\u89e3\u51b3\u65b9\u6848\uff0c\u5305\u62ec\uff1bAVL\u6811\u30012-3\u6811\u3001\u7ea2\u9ed1\u6811\u7b49\uff0c\u5c0f\u5085\u54e5\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u7ee7\u7eed\u5b9e\u73b0\u3002<\/p>\n<h3>2. \u4e8c\u53c9\u641c\u7d22\u6811\u7ed3\u6784\u5b9e\u73b0<\/h3>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u6574\u4e2a\u6811\u7ed3\u6784\u4e2d\u6700\u57fa\u672c\u7684\u6811\uff0c\u540c\u65f6\u4e5f\u662f\u6811\u8fd9\u4e2a\u4f53\u7cfb\u4e2d\u5b9e\u73b0\u8d77\u6765\u6700\u5bb9\u6613\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f46\u4e4b\u6240\u4ee5\u8981\u4f7f\u7528\u57fa\u4e8e\u4e8c\u53c9\u641c\u7d22\u6811\u4e4b\u4e0a\u7684\u5176\u4ed6\u6811\u7ed3\u6784\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u5c31\u662f\u5bf9\u6570\u636e\u7684\u5b58\u653e\u548c\u8bfb\u53d6\u3002\u90a3\u4e48\u4e3a\u4e86\u63d0\u9ad8\u541e\u5410\u6548\u7387\uff0c\u5219\u9700\u8981\u5c3d\u53ef\u80fd\u7684\u5e73\u8861\u5143\u7d20\u7684\u6392\u5e8f\uff0c\u4f53\u73b0\u5728\u6811\u4e0a\u5219\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u5217\u64cd\u4f5c\uff0c\u6240\u4ee5\u4f1a\u6709\u4e0d\u540c\u7684\u7ed3\u6784\u6811\u5b9e\u73b0\u3002<\/p>\n<p>\u800c\u5b9e\u73b0\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u6700\u597d\u7684\u57fa\u7840\u5b66\u4e60\uff0c\u4e86\u89e3\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\u540e\u624d\u66f4\u5bb9\u6613\u6269\u5c55\u5b66\u4e60\u5176\u4ed6\u6811\u7ed3\u6784\u3002<\/p>\n<ul>\n<li>\u6e90\u7801\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=X5ao%2FwuNRGOaVLe%2Fy0xGXg%3D%3D.tErdcStdwLiVVjle1RLlWElE1q4RyHiFNgO0Auri91%2BxB2fP4%2BAfRqZNkd8i54TG\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms<\/a><\/li>\n<li>\u672c\u7ae0\u6e90\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=RMJvwm0TIon%2FQxL9iYMWCg%3D%3D.0SQDrHmeweQVpY52wjnkSbbZoqQEAEyeLUSe1qa6AdIlCfN1%2Fph9MnCXXPCMbNFgSIzJ4MdtvMr0RtyazkSuJnSbNS2mZccNQYYr2X04zvO%2BSZ%2B2%2BQWyEhFniF5y2S9f\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms\/tree\/main\/data-structures\/src\/main\/java\/tree<\/a><\/li>\n<\/ul>\n<h4>2.1 \u6811\u679d\u5b9a\u4e49<\/h4>\n<pre><code class=\"java\">public Integer value;\npublic Node parent;\npublic Node left;\npublic Node right;<\/code><\/pre>\n<ul>\n<li>\u7528\u4e8e\u7ec4\u6210\u4e00\u9897\u6811\u7684\u8282\u70b9\uff0c\u5219\u9700\u8981\u5305\u62ec\uff1b\u503c\u548c\u4e0e\u4e4b\u5173\u8054\u7684\u4e09\u89d2\u7ed3\u6784\uff0c\u4e00\u4e2a\u7236\u8282\u70b9\u3001\u4e24\u4e2a\u5b69\u5b50\u8282\u70b9\u3002\u5982\u679c\u662fAVL\u6811\u8fd8\u9700\u8981\u6811\u9ad8\uff0c\u7ea2\u9ed1\u6811\u8fd8\u9700\u8981\u67d3\u8272\u6807\u8bb0\u3002<\/li>\n<\/ul>\n<h4>2.2 \u63d2\u5165\u8282\u70b9<\/h4>\n<pre><code class=\"java\">public Node insert(int e) {\n    if (null == root) {\n        root = new Node(e, null, null, null);\n        size++;\n        return root;\n    }\n    \n    \/\/ \u7d22\u5f15\u51fa\u5f85\u63d2\u5165\u5143\u7d20\u4f4d\u7f6e\uff0c\u4e5f\u5c31\u662f\u63d2\u5165\u5230\u54ea\u4e2a\u7236\u5143\u7d20\u4e0b\n    Node parent = root;\n    Node search = root;\n    while (search != null &amp;&amp; search.value != null) {\n        parent = search;\n        if (e &lt; search.value) {\n            search = search.left;\n        } else {\n            search = search.right;\n        }\n    }\n    \n    \/\/ \u63d2\u5165\u5143\u7d20\n    Node newNode = new Node(e, parent, null, null);\n    if (parent.value &gt; newNode.value) {\n        parent.left = newNode;\n    } else {\n        parent.right = newNode;\n    }\n    size++;\n    return newNode;\n}<\/code><\/pre>\n<ul>\n<li>\u9996\u5148\u5224\u65ad\u63d2\u5165\u5143\u7d20\u65f6\u5019\u662f\u5426\u6709\u6811\u6839\uff0c\u6ca1\u6709\u5219\u4f1a\u628a\u5f53\u524d\u8282\u70b9\u521b\u5efa\u51fa\u4e00\u9897\u6811\u6839\u6765\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u6811\u662f\u6709\u6811\u6839\u7684\uff0c\u5219\u5bf9\u63d2\u5165\u5143\u7d20\u4e0e\u5f53\u524d\u6811\u8fdb\u884c\u4e00\u4e2a\u8282\u70b9\u904d\u5386\u64cd\u4f5c\uff0c\u627e\u5230\u5143\u7d20\u53ef\u4ee5\u63d2\u5165\u7684\u7d22\u5f15\u4f4d\u7f6e parent\uff08\u6302\u5230\u8fd9\u4e2a\u7236\u8282\u70b9\u4e0b\uff09\u3002\u4e5f\u5c31\u662f search \u641c\u7d22\u8fc7\u7a0b\u3002<\/li>\n<li>\u6700\u540e\u5c31\u662f\u63d2\u5165\u5143\u7d20\uff0c\u901a\u8fc7\u7ed9\u63d2\u5165\u503c\u521b\u5efa\u4e00\u4e2a Node \u8282\u70b9\uff0c\u5e76\u7ed1\u5b9a\u5b83\u7684\u7236\u5143\u7d20\uff0c\u4ee5\u53ca\u628a\u65b0\u5143\u7d20\u6302\u5230\u7d22\u5f15\u5230\u7684 parent \u8282\u70b9\u4e0b\u3002<\/li>\n<\/ul>\n<h4>2.3 \u7d22\u5f15\u8282\u70b9<\/h4>\n<pre><code class=\"java\">public Node search(int e) {\n    Node node = root;\n    while (node != null &amp;&amp; node.value != null &amp;&amp; node.value != e) {\n        if (e &lt; node.value) {\n            node = node.left;\n        } else {\n            node = node.right;\n        }\n    }\n    return node;\n}<\/code><\/pre>\n<ul>\n<li>\u503c\u67e5\u627e\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u5bf9\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u904d\u5386\uff0c\u4e0d\u65ad\u7684\u5faa\u73af\u8282\u70b9\uff0c\u6309\u7167\u8282\u70b9\u503c\u7684\u5de6\u53f3\u5339\u914d\uff0c\u627e\u51fa\u6700\u7ec8\u76f8\u5f53\u7684\u503c\u8282\u70b9\u3002<\/li>\n<\/ul>\n<h4>2.4 \u5220\u9664\u8282\u70b9<\/h4>\n<pre><code class=\"java\">public Node delete(int e) {\n    Node delNode = search(e);\n    if (null == delNode) return null;\n    return delete(delNode);\n}\n\nprivate Node delete(Node delNode) {\n    if (delNode == null) return null;\n    Node result = null;\n    if (delNode.left == null) {\n        result = transplant(delNode, delNode.right);\n    } else if (delNode.right == null) {\n        result = transplant(delNode, delNode.left);\n    } else {\n        \/\/ \u56e0\u4e3a\u5220\u9664\u7684\u8282\u70b9\uff0c\u67092\u4e2a\u5b69\u5b50\u8282\u70b9\uff0c\u8fd9\u4e2a\u65f6\u5019\u627e\u5230\u8fd9\u6761\u5206\u652f\u4e0b\uff0c\u6700\u5de6\u4fa7\u505a\u5c0f\u7684\u8282\u70b9\u3002\u7528\u5b83\u6765\u66ff\u6362\u5220\u9664\u7684\u8282\u70b9\n        Node miniNode = getMiniNode(delNode.right);\n        if (miniNode.parent != delNode) {\n            \/\/ \u4ea4\u6362\u4f4d\u7f6e\uff0c\u7528miniNode\u53f3\u8282\u70b9\uff0c\u66ff\u6362miniNode\n            transplant(miniNode, miniNode.right);\n            \/\/ \u628aminiNode \u63d0\u5347\u7236\u8282\u70b9\uff0c\u8bbe\u7f6e\u53f3\u5b50\u6811\u5e76\u8fdb\u884c\u6302\u94fe\u3002\u66ff\u4ee3\u5f85\u5220\u8282\u70b9\n            miniNode.right = delNode.right;\n            miniNode.right.parent = miniNode;\n        }\n        \/\/ \u4ea4\u6362\u4f4d\u7f6e\uff0c\u5220\u9664\u8282\u70b9\u548cminiNode \u53ef\u6253\u5370\u6d4b\u8bd5\u89c2\u5bdf\uff1bSystem.out.println(this);\n        transplant(delNode, miniNode);\n        \/\/ \u628aminiNode \u63d0\u5347\u5230\u7236\u8282\u70b9\uff0c\u8bbe\u7f6e\u5de6\u5b50\u6811\u5e76\u6302\u94fe\n        miniNode.left = delNode.left;\n        miniNode.left.parent = miniNode;\n        result = miniNode;\n    }\n    size--;\n    return result;\n}\nprivate Node getMinimum(Node node) {\n    while (node.left != null) {\n        node = node.left;\n    }\n    return node;\n}\n\nprivate Node transplant(Node delNode, Node addNode) {\n    if (delNode.parent == null) {\n        this.root = addNode;\n    }\n    \/\/ \u5224\u65ad\u5220\u9664\u5143\u7d20\u662f\u5de6\/\u53f3\u8282\u70b9\n    else if (delNode.parent.left == delNode) {\n        delNode.parent.left = addNode;\n    } else {\n        delNode.parent.right = addNode;\n    }\n    \/\/ \u8bbe\u7f6e\u7236\u8282\u70b9\n    if (null != addNode) {\n        addNode.parent = delNode.parent;\n    }\n    return addNode;\n}<\/code><\/pre>\n<h5>2.4.1 \u5220\u9664\u5355\u8282\u70b9<\/h5>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-bst-02.png?raw=true&quot; width=&quot;400px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<ul>\n<li>\u5f85\u5220\u9664\u8282\u70b914\uff0c\u5224\u65ad\u6b64\u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u5b69\u5b50\u8282\u70b9\uff0c\u54ea\u4e2a\u7b49\u4e8e14\uff0c\u627e\u51fa\u5de6\u53f3<\/li>\n<li>\u628a\u5f85\u5220\u8282\u70b9\u7684\u53f3\u5b69\u5b50\u8282\u70b9\uff0c\u6302\u5230\u5220\u9664\u8282\u70b9\u7684\u53f3\u8282\u70b9<\/li>\n<li>\u7ed9\u5f85\u5220\u8282\u70b9\u7684\u53f3\u5b69\u5b50\u8282\u70b9\uff0c\u8bbe\u7f6e\u4e0a\u7236\u8282\u70b9<\/li>\n<\/ul>\n<h5>2.4.2 \u5220\u9664\u53cc\u8282\u70b9<\/h5>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-bst-03.png?raw=true&quot; width=&quot;400px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<ul>\n<li>\u5f85\u5220\u9664\u8282\u70b964\uff0c\u542b\u6709\u53cc\u5b50\u8282\u70b9\uff0c\u5219\u9700\u8981\u6839\u636e\u7b2c\u4e00\u4e2a\u53f3\u5b50\u8282\u70b9\u67e5\u627e\u6700\u5c0f\u5de6\u5b50\u8282\u70b9\u3002\u4ece89\u523072\uff0c\u5982\u679c\u6709\u6bd472\u8fd8\u5c0f\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u7ee7\u7eed\u6392\u67e5\u3002<\/li>\n<li>\u6392\u67e5\u5230\u8282\u70b972\uff0c\u5c0672\u8fd9\u4e2a\u51c6\u5907\u66ff\u6362\u5f85\u5220\u5143\u7d20\u7684\u8282\u70b9\uff0c\u4e0e\u53f3\u5b50\u8282\u70b973\u8fdb\u884c\u4f4d\u7f6e\u4ea4\u6362\uff0c\u8fc7\u7a0b\u4e0e 4.1 \u76f8\u540c\u3002\u4f7f\u7528\u4ea4\u6362\u51fd\u6570 transplant<\/li>\n<li>\u6700\u540e\u662f\u8fdb\u884c\u8282\u70b972\u4e0e\u5f85\u5220\u8282\u70b964\u7684\u4ea4\u6362\u8fc7\u7a0b\uff0c\u66f4\u6362\u4e09\u89d2\u5173\u7cfb\uff0c\u7236\u8282\u70b9\u3001\u5de6\u5b50\u8282\u70b9\u3001\u53f3\u5b50\u8282\u70b9\u3002<\/li>\n<\/ul>\n<h3>3. \u4e8c\u53c9\u641c\u7d22\u6811\u529f\u80fd\u6d4b\u8bd5<\/h3>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u89c2\u5bdf\u6811\u7ed3\u6784\u7684\u53d8\u5316\uff0c\u8fd9\u91cc\u5c0f\u5085\u54e5\u627e\u4e86\u4e00\u4e9b\u8d44\u6599\u8d44\u6599\uff0c\u4e00\u79cd\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7a0b\u5e8f\u6765\u6253\u5370\uff08\u7c7b\u4f3c\u5927\u5bb6\u4e4b\u524d\u6253\u537099\u4e58\u6cd5\u8868\uff0c\u53e6\u5916\u662f\u4f7f\u7528\u7ebf\u4e0a\u7684\u53ef\u89c6\u5316\u56fe\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Xgjm9jmaTT5tm7z0afU5Jg%3D%3D.diN4Hmj5JG4PkyijhiPtTdjoAAV0YEJ38lS70psdTunNF5wRcJs6QmZ7NkgblSpO\" rel=\"nofollow\">https:\/\/visualgo.net\/zh\/bst?slide=1<\/a>\uff09<\/p>\n<h4>3.1 \u968f\u673a\u63d2\u5165\u5143\u7d20<\/h4>\n<pre><code class=\"java\">@Test\npublic void test_binary_search_tree() {\n    BinarySearchTree tree = new BinarySearchTree();\n    for (int i = 0; i &lt; 10; i++) {\n        tree.insert(new Random().nextInt(100));\n    }\n    System.out.println(tree);\n}<\/code><\/pre>\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n<pre><code class=\"java\">         \/----- 91\n         |       \/----- 78\n \/----- 74\n |       \/----- 67\n61\n |       \/----- 51\n \/----- 40\n         |       \/----- 28\n         \/----- 14\n                 \/----- 7\n                 \nProcess finished with exit code 0<\/code><\/pre>\n<ul>\n<li>\u56e0\u4e3a\u4f60\u6d4b\u8bd5\u65f6\u7684\u968f\u673a\u6570\u4e0d\u540c\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5f88\u591a\u4e0d\u540c\u7ed3\u6784\u7684\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u4f3c\u94fe\u8868\u7ed3\u6784\u7684\u9000\u5316\u6811\u3002<\/li>\n<\/ul>\n<h4>3.2 \u63d2\u5165\u5e76\u4e14\u5220\u9664<\/h4>\n<pre><code class=\"java\">@Test\npublic void test_insert_delete(){\n    BinarySearchTree tree = new BinarySearchTree();\n    tree.insert(32);\n    tree.insert(7);\n    tree.insert(64);\n    tree.insert(63);\n    tree.insert(89);\n    tree.insert(72);\n    tree.insert(94);\n    tree.insert(6);\n    tree.insert(14);\n    tree.insert(18);\n    tree.insert(73);\n    System.out.println(tree);\n    \n    \/\/ \u5220\u9664\u5355\u8282\u70b9\uff0c\u53ea\u6709\u4e00\u4e2a\u5b69\u5b50\u7684\u7236\u8282\u70b9\n    \/\/ tree.delete(14);\n    \n    \/\/ \u5220\u9664\u53cc\u8282\u70b9\uff0c\u62e5\u6709\u4e8c\u4e2a\u5b69\u5b50\u7684\u7236\u8282\u70b9\n    tree.delete(64);\n    System.out.println(tree);\n}<\/code><\/pre>\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n<pre><code class=\"java\">                 \/----- 94\n         \/----- 89\n         |       |       \/----- 73\n         |       \/----- 72\n \/----- 64\n |       \/----- 63\n32\n |               \/----- 18\n |       \/----- 14\n \/----- 7\n         \/----- 6\n\n                 \/----- 94\n         \/----- 89\n         |       \/----- 73\n \/----- 72\n |       \/----- 63\n32\n |               \/----- 18\n |       \/----- 14\n \/----- 7\n         \/----- 6\n\n\nProcess finished with exit code 0<\/code><\/pre>\n<ul>\n<li>\u8fd9\u4e2a\u6848\u4f8b\u5c31\u662f <a href=\"https:\/\/segmentfault.com\/a\/#\">\u5220\u9664\u53cc\u8282\u70b9<\/a> \u7684\u6848\u4f8b\uff0c\u5220\u9664\u4e86\u8282\u70b964\u4ee5\u540e\uff0c\u8282\u70b972\u88ab\u63d0\u53d6\u4e0a\u6765\u4f7f\u7528\u3002<em>\u8bfb\u8005\u4f19\u4f34\u4e5f\u53ef\u4ee5\u5c1d\u8bd5\u5220\u9664\u5176\u4ed6\u8282\u70b9\u6d4b\u8bd5\u9a8c\u8bc1<\/em><\/li>\n<\/ul>\n<h2>\u4e09\u3001AVL \u6811<\/h2>\n<p><code>AVL\u6811\u5386\u53f2<\/code><\/p>\n<p>\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0cAVL \u6811\u4ee5\u5176\u4e24\u4f4d\u82cf\u8054\u53d1\u660e\u5bb6Georgy Adelson-Velsky\u548c Evgenii Landis\u7684\u540d\u5b57\u547d\u540d\uff0c\u4ed6\u4eec\u5728 1962 \u5e74\u7684\u8bba\u6587\u201c\u4fe1\u606f\u7ec4\u7ec7\u7b97\u6cd5\u201d\u4e2d\u53d1\u8868\u4e86\u5b83\u3002\u5b83\u662f\u4e00\u79cd\u81ea\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811(BST)\uff0c\u8fd9\u662f\u53d1\u660e\u7684\u7b2c\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<h3>1. AVL\u6811\u6570\u636e\u7ed3\u6784<\/h3>\n<p>AVL \u81ea\u5e73\u8861\u4e8c\u53c9\u6811\u7684\u51fa\u73b0\uff0c\u5176\u76ee\u7684\u5728\u4e8e\u89e3\u51b3\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u6210\u94fe\u8868\u7684\u95ee\u9898\u3002\u5f53\u6211\u4eec\u5411BST\u4e8c\u53c9\u641c\u7d22\u6811\u987a\u5e8f\u5b58\u5165<code>1\u30012\u30013\u30014\u30015\u30016\u30017<\/code>\u4e2a\u5143\u7d20\u65f6\uff0c\u5b83\u4f1a\u9000\u5316\u6210\u4e00\u6761\u94fe\u8868\uff0c\u56e0\u800c\u5931\u53bb\u6811\u67e5\u8be2\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981AVL\u6811\u5e73\u8861\u6811\u9ad8\u3002\u5982\u56fe\u6240\u793a<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-avl-01.png?raw=true&quot; width=&quot;500px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<p>\u90a3\u4e48AVL\u6811\u662f\u600e\u4e48\u5e73\u8861\u6811\u9ad8\u7684\u5462\uff1f<\/p>\n<p>\u5f53\u4e8c\u53c9\u6811\u7684\u5de6\u53f3\u5206\u652f\u6811\u9ad8\u5dee\u4e0d\u4e3a1\u65f6\uff0c\u9700\u8981\u8fdb\u884c\u5de6\u65cb\u6216\u8005\u53f3\u65cb\uff0c\u6765\u8c03\u8861\u6811\u9ad8\u3002\u8fd9\u6709\u70b9\u50cf\u5f00\u8f66\u7684\u65f6\u5019\uff0c\u5982\u679c\u8f66\u5934\u504f\u5de6\u5c31\u5f80\u53f3\u6253\u65b9\u5411\u76d8\uff0c\u8f66\u5934\u504f\u53f3\u5c31\u5f80\u5de6\u6253\u65b9\u5411\u76d8\u662f\u4e00\u4e2a\u9053\u7406\u3002\u90a3\u8fd9\u4e2a\u65b9\u5411\u76d8(\u5de6\u65cb\u3001\u53f3\u65cb)\u662f\u600e\u4e48\u6253\u7684\u5462\uff0c\u4e3b\u8981\u5206\u4ee5\u4e0b\u56db\u79cd\u60c5\u51b5\uff1b<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">\u5de6\u65cb\uff08\u65b0\u589e\u8282\u70b96\uff09<\/th>\n<th align=\"left\">\u53f3\u65cb\uff08\u65b0\u589e\u8282\u70b91\uff09<\/th>\n<th align=\"left\">\u5de6\u65cb+\u53f3\u65cb\uff08\u65b0\u589e\u8282\u70b94\uff09<\/th>\n<th align=\"left\">\u53f3\u65cb+\u5de6\u65cb\uff08\u65b0\u589e\u8282\u70b93\uff09<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581818\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<td align=\"left\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581819\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<td align=\"left\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581820\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<td align=\"left\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581821\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><strong>\u6761\u4ef6<\/strong>\uff1a\u8282\u70b94\uff0c\u5e73\u8861\u56e0\u5b50\u4e3a-2\uff0c\u5de6\u65cb<\/td>\n<td align=\"left\"><strong>\u6761\u4ef6<\/strong>\uff1a\u8282\u70b93\uff0c\u5e73\u8861\u56e0\u5b50\u4e3a2\uff0c\u53f3\u65cb<\/td>\n<td align=\"left\"><strong>\u6761\u4ef6<\/strong>\uff1a\u8282\u70b93\uff0c\u5e73\u8861\u56e0\u5b50\u4e3a2\uff0c\u53f3\u65cb\u3002\u4f46\u5f53\u8282\u70b92\u5e73\u8861\u56e0\u5b50-1\u5148\u5de6\u65cb\u3002<\/td>\n<td align=\"left\"><strong>\u6761\u4ef6<\/strong>\uff1a\u8282\u70b92\uff0c\u5e73\u8861\u56e0\u5b50\u4e3a-2\uff0c\u5de6\u65cb\u3002\u4f46\u5f53\u8282\u70b95\u5e73\u8861\u56e0\u5b501\u5148\u53f3\u65cb\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u8282\u70b9\u6811\u9ad8\uff1a\u4ee5\u8282\u70b94\u4e3a\u8bf4\u660e\uff0c\u6700\u957f\u7684\u5de6\u53f3\u5206\u652f\u8282\u70b9\u4e2a\u6570\uff0c\u5c31\u662f\u8282\u70b94\u7684\u6700\u5927\u6811\u9ad8\u3002\u8fd9\u91cc\u8282\u70b94\u5de6\u53f3\u5b69\u5b50\u8282\u70b9\u6700\u957f\u8def\u5f84\u90fd\u4e3a2\uff0c\u6240\u4ee5\u5b83\u7684\u6811\u9ad8\u4e3a2\u3002\u540c\u7406\u53ef\u8ba1\u7b97\u5176\u4ed6\u8282\u70b9\u6811\u9ad8\u3002<\/li>\n<li>\u5e73\u8861\u56e0\u5b50\uff1a\u901a\u8fc7\u5f53\u524d\u8282\u70b9\u7684\u5de6\u53f3\u5b50\u8282\u70b9\u4f5c\u5dee\u8ba1\u7b97\u5e73\u8861\u56e0\u5b50\uff0c\u4e4b\u540eAVL\u6811\u901a\u8fc7\u5e73\u8861\u56e0\u5b50\uff0c\u5b9a\u4e49\u4e86\u4ec0\u4e48\u65f6\u5019\u8fdb\u884c\u5de6\u65cb\u548c\u53f3\u65cb\u3002<\/li>\n<li>\u6e90\u7801\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=nuNtiKPIbKAFVtgdwuTx5Q%3D%3D.7VyIY0%2BrkqJp16lhjxSBObN8JJUMTcdyaHK72DEBns2tc8QhjhftDtYdQcWIvvnq\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms<\/a><\/li>\n<li>\u672c\u7ae0\u6e90\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=DMHTYMGXjvuO46kOHo6uEQ%3D%3D.uf7kfn%2FEbPTz5MOo1MRzQTPrXxpk6xOdNBcD64GLXleJtuclbRLAN3Up4HYySu%2FMf4dxHZtOLYDeubGt6SvWxQsCcQl5oKxlxCirhmJfovkCrRodg6xvXcxoCXy%2F8CJQ\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms\/tree\/main\/data-structures\/src\/main\/java\/tree<\/a><\/li>\n<\/ul>\n<h3>2. AVL\u6811\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u5bf9\u4e8e AVL \u6811\u7684\u5b9e\u73b0\u4e0e BST \u4e8c\u53c9\u641c\u7d22\u6811\u76f8\u6bd4\uff0c\u5728\u6811\u7684\u8282\u70b9\u5b9a\u4e49\u4e0a\u591a\u4e86\u4e00\u4e2a\u6811\u9ad8\u7684\u5c5e\u6027\u3002\u4e5f\u6709\u4e9bAVL\u6811\u4f7f\u7528\u7684\u662f\u5e73\u8861\u56e0\u5b50\u7684\u5c5e\u6027\uff0c\u5c31\u662f\u901a\u8fc7\u6811\u9ad8\u8ba1\u7b97\u540e\u7684\u7ed3\u679c\u3002\u6811\u8282\u70b9\u4ee3\u7801\u7ed3\u6784\u5982\u4e0b\uff1b<\/p>\n<pre><code class=\"java\">public class Node {\n\n    public Class&lt;?&gt; clazz;\n    public Integer value;\n    public Node parent;\n    public Node left;\n    public Node right;\n    \/\/ AVL \u6811\u6240\u9700\u5c5e\u6027\n    public int height;\n    \n}    <\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u5c0f\u5085\u54e5\u5c31\u5206\u522b\u901a\u8fc7\u4ee3\u7801\u8bb2\u89e3\u4e0b\u4e00\u9897AVL\u6811\u7684\u5de6\u65cb\u3001\u53f3\u65cb\u3001\u5de6\u65cb+\u53f3\u65cb\u3001\u53f3\u65cb+\u5de6\u65cb\u7684\u4ee3\u7801\u64cd\u4f5c\u3002\u4e0d\u8981\u62c5\u5fc3\u8fd9\u6ca1\u6709\u591a\u590d\u6742\uff0c\u53ea\u8981\u4f60\u80fd\u641e\u6e05\u695a\u5de6\u65cb\uff0c\u5c31\u80fd\u641e\u6e05\u695a\u53f3\u65cb\u3002\u4e24\u65cb\u5f04\u61c2\u7ec4\u5408\u5c31\u6ca1\u5565\u96be\u5ea6\u4e86\u3002<\/p>\n<ul>\n<li>\u6e90\u7801\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=mILjqnS7jnVq9h4ptQfz1w%3D%3D.DpxMAFAdqWeWHhhHEGZDgCc7w6DYDLkW35E19MhbWLjbhbUZKuNyKfaZ5GEa9U2n\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms<\/a><\/li>\n<li>\u672c\u7ae0\u6e90\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Pfo9fhrYme%2F0x9vDB1LROQ%3D%3D.t8jE1v8PuT9bqXZafDmuB9mixoH1vNH9iOHXX%2FvkfJ2cLuNhszk0hakupZGYpz7B06GI0Pq%2FE%2BPjorL%2FzmnhYkuSTGhifgIM4%2BhozgqGVkGMoXDKNXZz0LusnafQ3c00\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms\/tree\/main\/data-structures\/src\/main\/java\/stack<\/a><\/li>\n<li>\u52a8\u753b\u6f14\u793a\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=GYIRmI44cLQcfu4jaqEiiQ%3D%3D.ByPQmrqd1%2BaEz8jo1yRykL0Q0%2Fud6UFpchDLAk2kXAi3A9tGn42PBZKka7UZ57MJ\" rel=\"nofollow\">https:\/\/visualgo.net\/zh\/bst?slide=1<\/a> \u2014\u2014 AVL\u6811\u521d\u6b21\u7406\u89e3\u8fd8\u662f\u6bd4\u8f83\u56f0\u96be\u7684\uff0c\u53ef\u4ee5\u7ed3\u5408\u5b66\u4e60\u5185\u5bb9\u7684\u540c\u65f6\u505a\u4e00\u4e9b\u52a8\u753b\u6f14\u793a\u3002<\/li>\n<\/ul>\n<h4>2.1 \u5de6\u65cb<\/h4>\n<p>\u56fe\u89e3\u5de6\u65cb\u64cd\u4f5c\uff1b\u5b83\u5c31\u662f\u4e00\u79cd\u6458\u94fe\u66f4\u6362\u8c03\u6574\u8282\u70b9\u7684\u5904\u7406\u8fc7\u7a0b\uff0c\u5c0f\u5085\u54e5\u628a\u5b83\u5206\u89e3\u5c55\u793a\uff0c\u6574\u4e2a\u8fc7\u7a0b\u5982\u4e0b\uff1b<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-avl-06.png?raw=true&quot; width=&quot;500px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/p>\n<pre><code class=\"java\">protected Node rotateLeft(Node node) {\n    Node temp = node.right;\n    temp.parent = node.parent;\n  \n    node.right = temp.left;\n    if (node.right != null) {\n        node.right.parent = node;\n    }\n  \n    temp.left = node;\n    node.parent = temp;\n  \n    if (temp.parent == null) {\n        root = temp;\n    } else {\n        if (temp.parent.left == node) {\n            temp.parent.left = temp;\n        } else {\n            temp.parent.right = temp;\n        }\n    }\n    return temp;\n}<\/code><\/pre>\n<ol>\n<li>\u5de6\u65cb\u7684\u4f5c\u7528\uff0c\u76f8\u5f53\u4e8e\u901a\u8fc7\u5411\u4e0a\u8fc1\u79fb\u6811\u9ad8\u5dee\u5927\u4e8e1\u7684\u53f3\u5b50\u8282\u70b9\u6765\u964d\u4f4e\u6811\u9ad8\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u901a\u8fc7\u8282\u70b94\u62ff\u5230\u7236\u8282\u70b92\u548c\u53f3\u5b50\u8282\u70b95\uff0c\u628a\u7236\u8282\u70b92\u548c\u53f3\u5b50\u8282\u70b95\u5efa\u7acb\u5173\u8054<\/li>\n<li>\u8282\u70b95\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u76f8\u5f53\u4e8e\u662f\u5927\u4e8e4\u5c0f\u4e8e4\u7684\u90a3\u4e48\u4e00\u4e2a\u503c\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u4e0d\u4f53\u73b0\u3002\u90a3\u4e48\u8fd9\u4e2a\u8282\u70b94\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u5e94\u8be5\u88ab\u8fc1\u79fb\u5230\u8282\u70b93\u7684\u53f3\u5b50\u8282\u70b9\u4e0a\u3002<\/li>\n<li>\u6574\u7406\u8282\u70b95\u7684\u5173\u7cfb\uff0c\u5de6\u5b50\u8282\u70b9\u4e3a4\u3002\u5de6\u5b50\u8282\u70b94\u7684\u7236\u8282\u70b9\u4e3a5<\/li>\n<li>\u5982\u679c\u8bf4\u8fc1\u79fb\u4e0a\u6765\u7684\u8282\u70b95\u65e0\u7236\u8282\u70b9\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7236\u8282\u70b9 root = temp<\/li>\n<li>\u8fc1\u79fb\u4e0a\u6765\u7684\u8282\u70b95\uff0c\u627e\u5230\u539f\u8282\u70b94\u662f\u5bf9\u5e94\u7236\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u8fd8\u662f\u53f3\u5b50\u8282\u70b9\uff0c\u5bf9\u5e94\u7684\u8bbe\u7f6e\u8282\u70b95\u7684\u5de6\u53f3\u4f4d\u7f6e<\/li>\n<\/ol>\n<h4>2.2 \u53f3\u65cb<\/h4>\n<p>\u56fe\u89e3\u53f3\u65cb\u64cd\u4f5c\uff1b\u5b83\u5c31\u662f\u4e00\u79cd\u6458\u94fe\u66f4\u6362\u8c03\u6574\u8282\u70b9\u7684\u5904\u7406\u8fc7\u7a0b\uff0c\u5c0f\u5085\u54e5\u628a\u5b83\u5206\u89e3\u5c55\u793a\uff0c\u6574\u4e2a\u8fc7\u7a0b\u5982\u4e0b\uff1b<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-avl-07.png?raw=true&quot; width=&quot;500px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/p>\n<pre><code class=\"java\">protected Node rotateRight(Node node) {\n    Node temp = node.left;\n    temp.parent = node.parent;\n    node.left = temp.right;\n    if (node.left != null) {\n        node.left.parent = node;\n    }\n    temp.right = node;\n    node.parent = temp;\n    if (temp.parent == null) {\n        root = temp;\n    } else {\n        if (temp.parent.left == node) {\n            temp.parent.left = temp;\n        } else {\n            temp.parent.right = temp;\n        }\n    }\n    return temp;\n}<\/code><\/pre>\n<ol>\n<li>\u53f3\u65cb\u7684\u4f5c\u7528\uff0c\u76f8\u5f53\u4e8e\u901a\u8fc7\u5411\u4e0a\u8fc1\u79fb\u6811\u9ad8\u5dee\u5927\u4e8e1\u7684\u53f3\u5b50\u8282\u70b9\u6765\u964d\u4f4e\u6811\u9ad8\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u901a\u8fc7\u8282\u70b93\u62ff\u5230\u7236\u8282\u70b94\u548c\u5de6\u5b50\u8282\u70b92\uff0c\u628a\u7236\u8282\u70b97\u548c\u5de6\u5b50\u8282\u70b92\u5efa\u7acb\u5173\u8054<\/li>\n<li>\u8282\u70b92\u7684\u53f3\u5b50\u8282\u70b9\uff0c\u76f8\u5f53\u4e8e\u662f\u5927\u4e8e2\u5c0f\u4e8e3\u7684\u90a3\u4e48\u4e00\u4e2a\u503c\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u4e0d\u4f53\u73b0\u3002\u90a3\u4e48\u8fd9\u4e2a\u8282\u70b92\u7684\u53f3\u5b50\u8282\u70b9\uff0c\u5e94\u8be5\u88ab\u8fc1\u79fb\u5230\u8282\u70b93\u7684\u5de6\u5b50\u8282\u70b9\u4e0a\u3002<\/li>\n<li>\u6574\u7406\u8282\u70b92\u7684\u5173\u7cfb\uff0c\u53f3\u5b50\u8282\u70b9\u4e3a3\u3002\u53f3\u5b50\u8282\u70b93\u7684\u7236\u8282\u70b9\u4e3a2<\/li>\n<li>\u5982\u679c\u8bf4\u8fc1\u79fb\u4e0a\u6765\u7684\u8282\u70b92\u65e0\u7236\u8282\u70b9\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7236\u8282\u70b9 root = temp<\/li>\n<li>\u8fc1\u79fb\u4e0a\u6765\u7684\u8282\u70b92\uff0c\u627e\u5230\u539f\u8282\u70b93\u662f\u5bf9\u5e94\u7236\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u8fd8\u662f\u53f3\u5b50\u8282\u70b9\uff0c\u5bf9\u5e94\u7684\u8bbe\u7f6e\u8282\u70b92\u7684\u5de6\u53f3\u4f4d\u7f6e<\/li>\n<\/ol>\n<h4>2.3 \u5de6\u65cb + \u53f3\u65cb<\/h4>\n<p>\u4e4b\u6240\u4ee5\u4f1a\u6709\u5de6\u65cb + \u53f3\u65cb\uff0c\u662f\u56e0\u4e3a\u4e00\u6b21\u53f3\u65cb\u64cd\u4f5c\u6ca1\u6cd5\u5e73\u8861\u6811\u9ad8\uff0c\u800c\u8fd9\u79cd\u6811\u7684\u4e0d\u5e73\u8861\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u8fc7\u957f\uff0c\u6240\u4ee5\u8981\u628a\u4e0d\u5e73\u8861\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u5411\u5de6\u65cb\u8f6c\u4e00\u6b21\uff0c\u4e4b\u540e\u518d\u8fdb\u884c\u53f3\u65cb\u64cd\u4f5c\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-avl-08.png?raw=true&quot; width=&quot;800px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/p>\n<pre><code class=\"java\">if (factor(node.left) &gt;= 0) {\n    Node temp = super.rotateRight(node);\n    refreshHeight(temp.right);\n    refreshHeight(temp);\n} else {\n    Node temp = super.rotateLeft(node.left);\n    refreshHeight(temp.left);\n    refreshHeight(temp);\n    node.left = temp;\n    \n    temp = super.rotateRight(node);\n    refreshHeight(temp.right);\n    refreshHeight(temp);\n}<\/code><\/pre>\n<h4>2.4 \u53f3\u65cb + \u5de6\u65cb<\/h4>\n<p>\u4e4b\u6240\u4ee5\u4f1a\u6709\u53f3\u65cb + \u5de6\u65cb\uff0c\u662f\u56e0\u4e3a\u4e00\u6b21\u5de6\u65cb\u64cd\u4f5c\u6ca1\u6cd5\u5e73\u8861\u6811\u9ad8\uff0c\u800c\u8fd9\u79cd\u6811\u7684\u4e0d\u5e73\u8861\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u8fc7\u957f\uff0c\u6240\u4ee5\u8981\u628a\u4e0d\u5e73\u8861\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u5411\u53f3\u65cb\u8f6c\u4e00\u6b21\uff0c\u4e4b\u540e\u518d\u8fdb\u884c\u5de6\u65cb\u64cd\u4f5c\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-avl-09.png?raw=true&quot; width=&quot;800px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/p>\n<pre><code class=\"java\">if (factor(node.right) &lt;= 0) {\n    Node temp = super.rotateLeft(node);\n    refreshHeight(temp.left);\n    refreshHeight(temp);\n} else {\n    Node temp = super.rotateRight(node.right);\n    refreshHeight(temp.right);\n    refreshHeight(temp);\n    node.right = temp;\n    \n    temp = super.rotateLeft(node);\n    refreshHeight(temp.left);\n    refreshHeight(temp);\n}<\/code><\/pre>\n<h3>3. AVL\u6811\u529f\u80fd\u6d4b\u8bd5<\/h3>\n<p>\u4e3a\u4e86\u9a8c\u8bc1AVL\u6811\u7684\u5b9e\u73b0\u6b63\u786e\u4e0e\u5426\uff0c\u8fd9\u91cc\u6211\u4eec\u505a\u4e00\u4e0b\u968f\u673a\u8282\u70b9\u7684\u63d2\u5165\uff0c\u5982\u679c\u5b83\u80fd\u4e00\u76f4\u4fdd\u6301\u5e73\u8861\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u4e00\u9897\u53ef\u9760 AVL \u5e73\u8861\u6811\u3002<\/p>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<pre><code class=\"java\">@Test\npublic void test_random() {\n    AVLTree tree = new AVLTree();\n    for (int i = 0; i &lt; 30; i++) {\n        tree.insert(new Random().nextInt(100));\n    }\n    System.out.println(tree);\n}<\/code><\/pre>\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n<pre><code class=\"java\">\u8f93\u5165\u8282\u70b9\uff1a61,3,34,82,1,75,56,65,87,18,3,96,53,50,42,24,69,11,95,69,1,1,84,22,5,70,28,55,38,92\n\n                         \/----- 96(0)\n                 \/----- 95(1)\n                 |       \/----- 92(0)\n         \/----- 87(2)\n         |       |       \/----- 84(0)\n         |       \/----- 82(1)\n \/----- 75(3)\n |       |               \/----- 70(0)\n |       |       \/----- 69(1)\n |       \/----- 69(2)\n |               \/----- 65(0)\n61(5)\n |               \/----- 56(1)\n |               |       \/----- 55(0)\n |       \/----- 53(2)\n |       |       |       \/----- 50(0)\n |       |       \/----- 42(1)\n |       |               \/----- 38(0)\n \/----- 34(4)\n         |                       \/----- 28(0)\n         |               \/----- 24(1)\n         |               |       \/----- 22(0)\n         |       \/----- 18(2)\n         |       |       \/----- 11(1)\n         |       |               \/----- 5(0)\n         \/----- 3(3)\n                 |       \/----- 3(1)\n                 |       |       \/----- 1(0)\n                 \/----- 1(2)\n                         \/----- 1(0)\n\n\nProcess finished with exit code 0<\/code><\/pre>\n<ul>\n<li>\u968f\u673a\u63d2\u516530\u4e2a\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u987a\u5e8f\u5df2\u7ecf\u6253\u5370\uff0c\u7ecf\u8fc7AVL\u5de6\u53f3\u65cb\u8c03\u8861\u540e\uff0c\u4e8c\u53c9\u7ed3\u6784\u59cb\u7ec8\u4fdd\u6301\u6811\u9ad8\u5e73\u8861\u56e0\u5b50\u4e0d\u8d85\u8fc71\uff0c\u90a3\u4e48\u9a8c\u8bc1\u901a\u8fc7\u3002<\/li>\n<\/ul>\n<h2>\u56db\u30012-3\u6811<\/h2>\n<p>\u8fd9\u65f6\u5019\u5927\u90e8\u5206\u8d44\u6599\u4f1a\u7528<strong>2-3\u6811<\/strong>\u6765\u8bb2\u89e3<strong>\u7ea2\u9ed1\u6811<\/strong>\uff0c\u4e0d\u8fc7\u53c8\u4e0d\u53bb\u5b9e\u73b0\u4e00\u4e2a<strong>2-3\u6811<\/strong>\uff0c\u53ea\u662f\u7528\u4e86\u4e00\u4e2a\u7406\u8bba\u5957\u53e6\u5916\u4e00\u4e2a\u7406\u8bba\u3002\u867d\u7136\u80fd\u4ece\u7406\u89e3\u4e0a\u591a\u4e00\u4e9b\u53c2\u8003\uff0c\u4f46\u59cb\u7ec8\u611f\u89c9\u6ca1\u6709\u6293\u624b\u5440\u3002\u5bf9\u4e8e\u7406\u79d1\u601d\u7ef4\u6765\u8bf4\uff0c\u4f60\u5f97\u7ed9\u6211\u4e1c\u897f\u5440\u3002\u8001\u662f\u6574\u8fd9\u60ac\u5f97\u695e\u7684\ud83e\udd76\u8c01\u80fd\u53d7\u4e86\u3002<strong>\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u5148\u6765\u7528Java\u5b9e\u73b0\u4e00\u4e2a2-3\u6811\uff0c\u6709\u4e86\u57fa\u7840\u518d\u5b66\u4e60\u7ea2\u9ed1\u6811<\/strong><\/p>\n<h3>1. 2-3\u6811\u6570\u636e\u7ed3\u6784<\/h3>\n<p>2\u20133\u6811\u662f\u4e00\u79cd\u6811\u578b\u6570\u636e\u7ed3\u6784\uff0c\u7531\u7ea6\u7ff0\u00b7\u970d\u666e\u514b\u6d1b\u592b\u7279\u4e8e1970\u5e74\u53d1\u660e\u3002\u5b83\u901a\u8fc7\u5728\u4e00\u4e2a\u8282\u70b9\u5b58\u653e1-2\u4e2a\u5143\u7d20\u6765\u5e73\u8861\u6811\u9ad8\u3002\u4ece\u800c\u4e5f\u4f7f2-3\u6811\u5b58\u57282\u53c9\u8282\u70b9\u548c3\u53c9\u8282\u70b9\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-23-01.png?raw=true&quot; width=&quot;400px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<p>\u8fd9\u91cc\u8981\u63d0\u5230\u4e00\u70b9\uff0c\u5728BST\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u9000\u5316\u6210\u94fe\u8868\u7684\u57fa\u7840\u4e0a\u3002\u5f15\u51fa\u4e86\u81ea\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u4e5f\u5c31\u662f\u5305\u62ec\u4e0a\u4e00\u7ae0\u5b9e\u73b0\u7684AVL\u6811\u548cJava API HashMap\u4e2d\u7528\u5230\u7684\u7ea2\u9ed1\u6811\uff0c\u5b83\u4eec\u90fd\u5c5e\u4e8eBalancedTree\uff0c\u4e5f\u7edf\u79f0\u4e3aB\u6811\uff0c\u5e73\u8861\u7684\u610f\u601d\u3002<\/p>\n<p>\u800c\u672c\u7ae0\u5b9e\u73b0\u76842-3\u6811\u4e5f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u5e73\u8861\u6811\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5177\u6709\u5b50\u8282\u70b9\uff08\u5185\u90e8\u8282\u70b9\uff09\u7684\u8282\u70b9\u8981\u4e48\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\uff082 \u8282\u70b9\uff09\u548c\u4e00\u4e2a\u6570\u636e\u5143\u7d20\uff0c\u8981\u4e48\u6709\u4e09\u4e2a\u5b50\u8282\u70b9\uff083 \u8282\u70b9\uff09\u548c\u4e24\u4e2a\u6570\u636e\u5143\u7d20\u3002\u53e6\u5916 2-3 \u6811\u662f3\u9636B \u6811\uff0c2-3-4 \u6811\u662f4\u9636B\u6811\u3002<\/p>\n<hr>\n<p>\u5728\u5b9e\u73b02-3\u6811\u4e4b\u524d\uff0c\u5148\u901a\u8fc7\u56fe\u7a3f\u6f14\u793a\u4e0b\u57282-3\u6811\u4e2d\u987a\u5e8f\u63d2\u51651\u30012\u30013\u30014\u30015\u30016\u30017\uff0c\u4e03\u4e2a\u5143\u7d20\u65f6\uff0c2-3\u6811\u7684\u8c03\u8861\u5904\u7406\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-23-02.png?raw=true&quot; width=&quot;600px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<ul>\n<li>2-3 \u6811\u7684\u63d2\u5165\u8fc7\u7a0b\u4e0e BST \u6811\u7c7b\u4f3c\uff0c\u4f1a\u901a\u8fc7\u6811\u7684\u5de6\u53f3\u8282\u70b9\u5927\u5c0f\uff0c\u627e\u5230\u81ea\u5df1\u7684\u63d2\u5165\u4f4d\u7f6e\u3002<\/li>\n<li>\u4e00\u4e2a\u8282\u70b9\u53ef\u4ee5\u53f31-3\u4e2a\u5143\u7d20\uff0c\u4f46\u5f53\u5143\u7d20\u4e2a\u6570\u4e3a3\u65f6\uff0c\u5219\u9700\u8981\u8c03\u8861\u3002\u628a\u4e09\u4e2a\u8282\u70b9\u7684\u4e2d\u95f4\u8282\u70b9\u664b\u5347\u4e0a\u6765\uff0c\u5176\u4f59\u4e24\u4e2a\u8282\u70b9\u4e3a\u5b50\u8282\u70b9\u3002<\/li>\n<li>\u5982\u679c\u8fdb\u884c\u4e00\u6b21\u8c03\u8861\u540e\uff0c\u4e0a\u4e00\u5c42\u7236\u8282\u70b9\u8fbe\u52303\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u89812\u6b21\u8c03\u8861\uff0c\u6765\u6ee1\u8db32-3\u6811\u7684\u89c4\u5219\u3002<\/li>\n<\/ul>\n<p><strong>\u548b\u6837<\/strong>\uff0c\u662f\u4e0d\u770b\u8fc7\u8fd9\u4e2a\u56fe\u4e4b\u540e\u5bf9\u4e8e2-3\u6811\u7684\u5b9e\u73b0\u5df2\u7ecf\u6709\u611f\u89c9\u4e86\uff0c\u60f3\u52a8\u624b\u5199\u5199\u8bd5\u8bd5\u4e86\uff1f<\/p>\n<ul>\n<li>\u6e90\u7801\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=t29yUJ7Ib4Bzbw8KBB0%2FFA%3D%3D.DS%2BFddexLD58ATNP4BAaCF0gKb0ownfBVKWu2mx9zpgg9ufzRE8Va2SW7FH%2Fa0lL\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms<\/a><\/li>\n<li>\u672c\u7ae0\u6e90\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=tOy%2BtWlQ5YIK%2BlSGeUKDIA%3D%3D.GwQWiP8wv8PFeKAs%2FSmNtFXgbtBwSX6b8ydXFTlRvumlbYKFoaF4ncUe9o596%2BgmjEaIqy1mJXSmebbigONJfj36KS0HFU3rmvh%2BnzdxyyAJu4SHCNR6SMWxYpDDwCyj\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms\/tree\/main\/data-structures\/src\/main\/java\/tree<\/a><\/li>\n<\/ul>\n<h3>2. 2-3\u6811\u7ed3\u6784\u5b9e\u73b0<\/h3>\n<p>2-3 \u6811\u7684\u5b9e\u73b0\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u5728\u5b9e\u73b0\u524d\u8981\u601d\u8003\ud83e\udd14\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1b<\/p>\n<ul>\n<li>Node \u8282\u70b9\u5c5e\u6027\u4fe1\u606f\u90fd\u5305\u62ec\u4ec0\u4e48\uff1f<\/li>\n<li>\u63d2\u5165\u503c\uff0c\u662f\u5426\u9700\u8981\u521b\u5efa\u65b0\u7684 Node\uff1f<\/li>\n<li>\u63d2\u5165\u540e\uff0c\u8282\u70b9\u5185\u67093\u4e2a\u5143\u7d20\u540e\uff0c\u600e\u4e48\u8fc1\u79fb\u5143\u7d20\uff1f<\/li>\n<\/ul>\n<h4>2.1 \u8282\u70b9\u5b9a\u4e49<\/h4>\n<pre><code class=\"java\">public class Node_2_3 {\n\n    \/\/ \u5143\u7d20\n    public int[] items;\n    \/\/ \u5e8f\u53f7\n    public int number;\n    \/\/ \u5b69\u5b50\n    public Node_2_3[] children;\n    \/\/ \u7236\u4eb2\u3010\u975e\u5fc5\u987b\u3011\n    public Node_2_3 parent;\n\n    public Node_2_3() {\n        this.items = new int[3];\n        this.number = 0;\n        this.children = new Node_2_3[4];\n        this.parent = null;\n    }\n    \n    public void insert(int e) {\n        int idx = this.number - 1;\n        while (idx &gt;= 0) {\n            if (this.items[idx] &lt; e) break;\n            this.items[idx + 1] = this.items[idx];\n            --idx;\n        }\n        this.items[idx + 1] = e;\n        ++this.number;\n    }\n    \n    \/\/ ... \u7701\u7565\u90e8\u5206\u4ee3\u7801\n}<\/code><\/pre>\n<ul>\n<li>2-3\u6811\u7684\u51e0\u70b9\u5143\u7d20\u9700\u8981\u5305\u62ec\uff1b\u4e00\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\u96c6\u5408\u3001\u5143\u7d20\u7684\u5e8f\u53f7\u3001\u5b69\u5b50\u5143\u7d20\u3002\u56e0\u4e3a\u4e00\u4e2a\u8282\u70b9\u6700\u591a\u53ef\u4e34\u65f6\u653e\u51653\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u5c31\u4f1a\u6700\u591a\u67094\u4e2a\u5b69\u5b50\u5143\u7d20\uff0c\u6240\u4ee5\u5b69\u5b50\u5143\u7d20\u4e5f\u662f\u4e00\u4e2a\u6570\u7ec4\u5e76\u4e14\u5728\u6784\u9020\u51fd\u6570\u4e2d\u6309\u71674\u4e2a\u5143\u7d20\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n<li>\u7531\u4e8e\u672c\u8eab2-3\u6811\u63d2\u5165\u5143\u7d20\u7684\u5f00\u59cb\u9636\u6bb5\uff0c\u5e76\u4e0d\u662f\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8282\u70b9\uff0c\u800c\u662f\u5728\u521d\u59cb\u5316\u7684\u6570\u7ec4\u7a7a\u95f4\u4e2d\u5b58\u5165\u5143\u7d20\u3002\u6240\u4ee5\u5728\u8282\u70b9\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u63d2\u5165\u5143\u7d20\u7684\u65b9\u6cd5 insert \u6765\u5904\u7406\u65b0\u589e\u5143\u7d20\u3002<\/li>\n<li>\u53e6\u59162-3\u6811\u7684\u8282\u70b9\u7c7b\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u67e5\u8be2\u7684\u65b9\u6cd5\u3002\u5305\u62ec\uff1a\u83b7\u53d6\u5de6\u8fb9\u5143\u7d20\u3001\u4e2d\u95f4\u5143\u7d20\u3001\u53f3\u8fb9\u5143\u7d20\uff0c\u4ee5\u53ca\u6700\u5c0f\u503c\u3001\u6700\u5927\u503c\u548c\u5224\u65ad\u662f\u5426\u6709\u5b69\u5b50\u8282\u70b9\u3002\u8fd9\u4e9b\u5185\u5bb9\u53ef\u4ee5\u6e90\u7801\u3002<\/li>\n<\/ul>\n<h4>2.2 \u62c6\u5206\u8282\u70b9<\/h4>\n<p>\u5f53\u4e00\u4e2a\u8282\u70b9\u5185\u67093\u4e2a\u5143\u7d20\u7684\u65f6\u5019\uff0c\u5c31\u8981\u53d1\u8d77\u62c6\u5206\u4e1c\u897f\uff0c\u62c6\u5206\u7684\u8fc7\u7a0b\u5206\u4e3a\uff1b<\/p>\n<ol>\n<li>\u5bf93\u4e2a\u8282\u70b9\u7684\u4e2d\u95f4\u8282\u70b9\uff0c\u63d2\u5165\u5230\u7236\u8282\u70b9\u4e0a\u3002<\/li>\n<li>\u5269\u4f592\u4e2a\u8282\u70b9\u521b\u5efa\u51fa\u65b0\u7684\u8282\u70b9\u3002<\/li>\n<li>\u5efa\u7acb\u7236\u8282\u70b9\u548c\u65b0\u521b\u5efa\u76842\u4e2a\u8282\u70b9\u95f4\u5173\u7cfb\u3002<\/li>\n<\/ol>\n<p>\u6574\u4e2a\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe\u6240\u793a<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-23-03.png?raw=true&quot; width=&quot;480px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<h5>2.1 \u63d2\u5165\u7236\u8282\u70b9<\/h5>\n<pre><code class=\"java\">private Node_2_3 split(Node_2_3 node, Node_2_3 parent) {\n    if (parent == null) {\n        parent = new Node_2_3(node);\n    }\n    \n    parent.insert(node.getMiddleItem());\n    \n    Node_2_3[] newNodes = this.triangle(node);\n    this.replaceChild(parent, node, newNodes[0], newNodes[1]);\n    return parent;\n}<\/code><\/pre>\n<ul>\n<li>\u6574\u4e2a2-3\u6811\u62c6\u5206\u7684\u8fc7\u7a0b\u5c31\u662f\u5728 split \u8fd9\u4e2a\u65b9\u6cd5\u91cc\uff0c\u7b2c\u4e00\u6b65\u89e3\u51b3\u4e86\u662f\u5426\u6709\u7236\u8282\u70b9\uff0c\u6ca1\u6709\u5219\u521b\u5efa\u3002<\/li>\n<li>\u4e4b\u540e\u5c06\u539f\u8282\u70b9\u7684\u4e2d\u95f4\u503c\u63d2\u5165\u5230\u7236\u8282\u70b9\u4e2d\u3002\u63a5\u4e0b\u6765\u7684\u64cd\u4f5c\u5c31\u662f\u62c6\u5206\u65b0\u8282\u70b9\u548c\u66f4\u6362\u5b69\u5b50\u8282\u70b9\u5efa\u7acb\u65b0\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<h5>2.2 \u62c6\u5206\u65b0\u8282\u70b9<\/h5>\n<pre><code class=\"java\">private Node_2_3[] triangle(Node_2_3 node) {\n    Node_2_3[] newNodes = new Node_2_3[2];\n    newNodes[0] = new Node_2_3(node.items[0]);\n    newNodes[1] = new Node_2_3(node.items[2]);\n    if (!node.isLeaf()) {\n        \/\/ \u5de6\u5b69\u5b50\n        newNodes[0].children[0] = node.children[0];\n        newNodes[0].children[1] = node.children[1];\n        \/\/ \u53f3\u5b69\u5b50\n        newNodes[1].children[0] = node.children[2];\n        newNodes[1].children[1] = node.children[3];\n    }\n    return newNodes;\n}<\/code><\/pre>\n<ul>\n<li>\u57fa\u4e8e\u4f20\u9012\u8fdb\u6765\u7684\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u7684\u5de6\u53f3\u5b69\u5b50\u521b\u5efa\u65b0\u8282\u70b9\uff0c\u5982\u679c\u8fd9\u4e2a\u5b69\u5b50\u8282\u70b9\u8fd8\u6709\u5206\u652f\u8282\u70b9\uff0c\u5219\u4e00\u5e76\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<h5>2.3 \u5efa\u7acb\u65b0\u8fde\u63a5<\/h5>\n<pre><code class=\"java\">private void replaceChild(Node_2_3 parent, Node_2_3 oldChild, Node_2_3 child01, Node_2_3 child02) {\n    if (oldChild == parent.children[0]) {\n        parent.children[3] = parent.children[2];\n        parent.children[2] = parent.children[1];\n        parent.children[1] = child02;\n        parent.children[0] = child01;\n    } else if (oldChild == parent.children[1]) {\n        parent.children[3] = parent.children[2];\n        parent.children[2] = child02;\n        parent.children[1] = child01;\n    } else {\n        parent.children[3] = child02;\n        parent.children[2] = child01;\n    }\n}<\/code><\/pre>\n<ul>\n<li>\u5efa\u7acb\u65b0\u8fde\u63a5\u9700\u8981\u5224\u65ad\u8fd9\u4e2a\u8282\u70b9 oldChild \u662f\u7236\u8282\u70b9\u7684\u5de6\u3001\u4e2d\u3001\u53f3\uff0c\u4e4b\u540e\u8fdb\u884c\u4f9d\u6b21\u7684\u66f4\u6362\u3002<\/li>\n<li>\u5982\u62c6\u5206\u8282\u70b9\u7684\u4ecb\u7ecd\u56fe\u4e2d\uff0c\u7528\u5230\u7684\u5c31\u662f <code>parent.children[1] = child02;parent.children[0] = child01;<\/code> \u4e24\u6b65\u64cd\u4f5c\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<h4>2.3 \u65b0\u589e\u8282\u70b9<\/h4>\n<pre><code class=\"java\">public void insert(int e) {\n    \/\/ \u8bb0\u5f55\u5143\u7d20\n    elementList.add(e);\n    \/\/ \u63d2\u5165\u5143\u7d20\n    if (root == null) {\n        root = new Node_2_3(e);\n    } else {\n        root = insert(e, root);\n        if (root.number == 3) {\n            root = split(root, null);\n        }\n    }\n}\n\nprivate Node_2_3 insert(int e, Node_2_3 parent) {\n    if (parent.isLeaf()) {\n        parent.insert(e);\n        return parent;\n    }\n    \n    Node_2_3 child = null;\n    if (parent.number == 1) {\n        if (e &lt; parent.getMinItem()) {\n            child = insert(e, parent.getLeft());\n        } else {\n            child = insert(e, parent.getMiddle());\n        }\n    } else {\n        if (e &lt; parent.getMinItem()) {\n            child = insert(e, parent.getLeft());\n        } else if (e &gt; parent.getMiddleItem()) {\n            child = insert(e, parent.getRight());\n        } else {\n            child = insert(e, parent.getMiddle());\n        }\n    }\n    \n    if (child.number == 3) {\n        return this.split(child, parent);\n    }\n    \n    return parent;\n}<\/code><\/pre>\n<ul>\n<li>\u65b0\u589e\u8282\u70b9\u7684\u8fc7\u7a0b\u5c31\u6bd4\u8f83\u7b80\u5355\u4e86\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u9012\u5f52\u627e\u5230\u53ef\u4ee5\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u53e6\u5916\u4e00\u79cd\u5c31\u662f where \u5faa\u73af\u3002\u6211\u4eec\u518dBST\u3001AVL\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u79cd\u90fd\u662f\u7528\u4e86 where \u5faa\u73af\u3002<\/li>\n<li>\u57282-3\u6811\u4e2d insert \u65b9\u6cd5\u9012\u5f52\u5230\u5bf9\u5e94\u7684\u63d2\u5165\u4f4d\u7f6e\u540e\uff0c\u5f00\u59cb\u63d2\u5165\u5143\u7d20\u3002\u5f53\u63d2\u5165\u5143\u7d20\u7ed3\u675f\u540e\u5224\u65ad\u8fd9\u4e2a\u8282\u70b9\u662f\u5426\u5df2\u7ecf\u8fbe\u5230\u4e863\u4e2a\u8282\u70b9\uff0c\u5982\u679c\u662f\u5219\u8fdb\u884c\u62c6\u5206\u3002<em>\u62c6\u5206\u5c31\u8c03\u7528\u4e86\u4e0a\u9762\u7684\u6b65\u9aa4<\/em><\/li>\n<\/ul>\n<h3>3. 2-3\u6811\u7ed3\u6784\u6d4b\u8bd5<\/h3>\n<p>\u4e3a\u4e86\u8ba9\u8bfb\u8005\u66f4\u597d\u7684\u7406\u89e32-3\u6811\u7684\u7ed3\u6784\uff0c\u5c0f\u5085\u54e5\u5728\u7a0b\u5e8f\u7684\u63a7\u5236\u53f0\u6253\u5370\u4e86\u63d2\u5165\u7684\u8fc7\u7a0b\u3002<em>\u7f51\u4e0a\u6ca1\u67092-3\u6811\u5728\u7ebf\u7684\u52a8\u753b\u6f14\u793a\uff0c\u5982\u679c\u8bfb\u8005\u770b\u5230\u4e5f\u53ef\u4ee5\u7559\u8a00\u7ed9\u5c0f\u5085\u54e5<\/em><\/p>\n<pre><code class=\"java\">@Test\npublic void test_insert_incr() {\n    Tree_2_3 tree = new Tree_2_3();\n    for (int i = 1; i &lt;= 10; i++) {\n        tree.insert(i);\n        System.out.println(tree);\n    }\n}<\/code><\/pre>\n<ul>\n<li>\u987a\u5e8f\u63d2\u516510\u4e2a\u8282\u70b9\uff0c\u5982\u679c\u8fd9\u662f\u4e00\u9897BST\u6811\uff0c\u5b83\u5c06\u4f1a\u9000\u5316\u6210\u94fe\u8868\u3002\u90a3\u4e48\u6211\u4eec\u4f7f\u7528\u81ea\u5e73\u8861\u76842-3\u6811\uff0c\u6765\u770b\u770b\u5b83\u7684\u63d2\u5165\u6548\u679c\u3002<\/li>\n<\/ul>\n<p><strong>\u6d4b\u8bd5\u6548\u679c<\/strong><\/p>\n<pre><code class=\"java\">\u8f93\u5165\u8282\u70b9(1\u4e2a)\uff1a1\n \n[1]\n\n\u8f93\u5165\u8282\u70b9(2\u4e2a)\uff1a1,2\n \n[1,2]\n\n\u8f93\u5165\u8282\u70b9(3\u4e2a)\uff1a1,2,3\n \n \/----- [3]\n[2]\n \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(4\u4e2a)\uff1a1,2,3,4\n \n \/----- [3,4]\n[2]\n \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(5\u4e2a)\uff1a1,2,3,4,5\n \n \/----- [5]\n[2,4]---- [3]\n \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(6\u4e2a)\uff1a1,2,3,4,5,6\n \n \/----- [5,6]\n[2,4]---- [3]\n \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(7\u4e2a)\uff1a1,2,3,4,5,6,7\n \n         \/----- [7]\n \/----- [6]\n |       \/----- [5]\n[4]\n |       \/----- [3]\n \/----- [2]\n         \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(8\u4e2a)\uff1a1,2,3,4,5,6,7,8\n \n         \/----- [7,8]\n \/----- [6]\n |       \/----- [5]\n[4]\n |       \/----- [3]\n \/----- [2]\n         \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(9\u4e2a)\uff1a1,2,3,4,5,6,7,8,9\n \n         \/----- [9]\n \/----- [6,8]---- [7]\n |       \/----- [5]\n[4]\n |       \/----- [3]\n \/----- [2]\n         \/----- [1]\n\n\u8f93\u5165\u8282\u70b9(10\u4e2a)\uff1a1,2,3,4,5,6,7,8,9,10\n \n         \/----- [9,10]\n \/----- [6,8]---- [7]\n |       \/----- [5]\n[4]\n |       \/----- [3]\n \/----- [2]\n         \/----- [1]\n\n\nProcess finished with exit code 0<\/code><\/pre>\n<ul>\n<li>\u6709\u4e86\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\u793a\u610f\uff0c\u662f\u4e0d\u662f\u518d\u6765\u770b2-3\u6811\u5c31\u975e\u5e38\u6e05\u6670\u4e86\u3002\u2014\u2014 \u6211\u8bf4\u8fc7\uff0c\u7406\u79d1\u751f + \u6280\u672f\uff0c\u4e0d\u8981\u53ea\u629b\u7406\u8bba\uff0c\u8981\u770b\u6548\u679c\u7684\uff01\u4e1c\u897f\u5230\u624b\u4e86\uff0c\u80fd\u62ff\u634f\u4e86\uff0c\u518d\u8865\u5145\u7406\u8bba\u3002<\/li>\n<\/ul>\n<h2>\u4e94\u3001\u7ea2\u9ed1\u6811<\/h2>\n<p><code>\u7ea2\u9ed1\u6811\u7684\u5386\u53f2<\/code><\/p>\n<p>\u7ea2\u9ed1\u6811\uff08Red Black Tree\uff09\u662f\u4e00\u79cd\u81ea\u5e73\u8861\u4e8c\u53c9\u67e5\u627e\u6811\uff0c\u4e8e 1972 \u5e74\u7531 Rudolf Bayer \u53d1\u660e\u7684\u5bf9\u79f0\u4e8c\u53c9B\u6811\u6f14\u5316\u800c\u6765\uff0c\u5e76\u4ee52-3-4\u6811\u30012-3\u6811\u6d41\u884c\u3002\u6700\u7ec8\u5728 1978 \u5e74\u7531  Leonidas J. Guibas \u548c Robert Sedgewick \u4ece\u5bf9\u79f0\u4e8c\u53c9 B \u6811\u4e2d\u63a8\u5bfc\u51fa\u7ea2\u9ed1\u6811\u3002PS\uff1a\u4e4b\u6240\u4ee5\u9009\u62e9\u201c\u7ea2\u8272\u201d\uff0c\u662f\u56e0\u4e3a\u5b83\u662f\u4f5c\u8005\u5728Xerox PARC\u5de5\u4f5c\u65f6\u53ef\u7528\u7684\u5f69\u8272\u6fc0\u5149\u6253\u5370\u673a\u4ea7\u751f\u7684\u6700\u597d\u770b\u7684\u989c\u8272\u3002<\/p>\n<h3>1. \u7ea2\u9ed1\u6811\u6570\u636e\u7ed3\u6784<\/h3>\n<p>\u5efa\u7acb\u5728 BST \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u57fa\u7840\u4e0a\uff0cAVL\u30012-3\u6811\u3001\u7ea2\u9ed1\u6811\u90fd\u662f\u81ea\u5e73\u8861\u4e8c\u53c9\u6811\uff08\u7edf\u79f0B-\u6811\uff09\u3002\u4f46\u76f8\u6bd4\u4e8eAVL\u6811\uff0c\u9ad8\u5ea6\u5e73\u8861\u6240\u5e26\u6765\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7ea2\u9ed1\u6811\u5bf9\u5e73\u8861\u7684\u63a7\u5236\u8981\u5bbd\u677e\u4e00\u4e9b\uff0c\u7ea2\u9ed1\u6811\u53ea\u9700\u8981\u4fdd\u8bc1\u9ed1\u8272\u8282\u70b9\u5e73\u8861\u5373\u53ef\u3002\u4e5f\u6b63\u56e0\u7ea2\u9ed1\u6811\u5728\u63d2\u5165\u548c\u5220\u9664\u65f6\u4e0d\u9700\u8981\u592a\u591a\u7684\u5e73\u8861\u64cd\u4f5c\uff0c\u4e5f\u8ba9\u5b83\u6210\u4e3a\uff1bJava\u4e2dHashMap\u7684\u5143\u7d20\u78b0\u649e\u540e\u7684\u8f6c\u6362\u3001Linux\u7684CFS\u8fdb\u884c\u8c03\u5ea6\u7b97\u6cd5\u3001\u591a\u8def\u590d\u7528\u6280\u672f\u7684Epoll\u7b49\u5404\u7c7b\u5e95\u5c42\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u3002<\/p>\n<p>\u4f46\u7ea2\u9ed1\u6811\u5e76\u4e0d\u662f\u4e00\u4e2a\u90a3\u4e48\u5bb9\u6613\u7406\u89e3\u7684\u77e5\u8bc6\u70b9\uff0c\u751a\u81f3\u5f88\u591a\u8d44\u6599\u90fd\u53ea\u662f\u7ed9\u51fa\u7ea2\u9ed1\u6811\u7684\u7406\u8bba\uff0c\u4f46\u4e3a\u4ec0\u4e48\u8981\u67d3\u8272\u3001\u4e3a\u4ec0\u4e48\u8981\u5de6\u65cb\u3001\u4e3a\u4ec0\u4e48\u8fd8\u8981\u5de6\u65cb\u63a5\u53f3\u65cb\u3002\u8fd9\u6837\u7684\u77e5\u8bc6\u70b9\u672c\u5c31\u4e0d\u5e94\u8be5\u662f\u8003\u6b7b\u8bb0\u786c\u80cc\u6765\u5b66\u4e60\u7684\uff0c\u8fd9\u6839\u672c\u4e0d\u662f\u5b66\u4e60\u7f16\u7a0b\u7684\u201d\u5957\u8def\u201c\u3002\u2014\u2014 \u4f60\u80cc\u7684\u518d\u6e9c\uff0c\u4e5f\u6ca1\u6cd5\u7406\u89e3\u6838\u5fc3\u672c\u8d28\uff0c\u5fd8\u4e5f\u53ea\u662f\u65f6\u95f4\u7684\u95ee\u9898\uff01<\/p>\n<p>\u5176\u5b9e\u6839\u636e\u7ea2\u9ed1\u6811\u7684\u5386\u53f2\u6765\u770b\uff0c\u6700\u65e9\u7ea2\u9ed1\u6811\u5c31\u662f\u6765\u81ea\u4e8e2-3\u6811\u7684\u7ed3\u6784\uff0c\u6240\u4ee5\u8981\u5b66\u4e60\u6e05\u695a\u7684\u7ed3\u6784\u5c31\u8981\u5b66\u4e60 2-3\u6811\u3002\u4f46\u540c\u65f6\u5bf9\u4e8e 2-3\u6811\u7684\u5b66\u4e60\u4e5f\u4e0d\u80fd\u53ea\u662f\u4f9d\u9760\u4e00\u4efd\u7406\u8bba\uff0c\u5426\u5219\u5bf9\u4e8e\u7ea2\u9ed1\u7684\u5b66\u4e60\u6765\u770b\uff0c\u5c31\u662f\u7528\u4e0d\u592a\u7406\u89e3\u7684 2-3\u6811\u7406\u8bba\u5957\u7ea2\u9ed1\u6811\u7406\u8bba\uff0c\u4f9d\u65e7\u6ca1\u6cd5\u7406\u89e3\u3002\u6240\u4ee5\u5c0f\u5085\u54e5\u5728\u4e0a\u4e00\u7ae0\u4e13\u95e8\u8bb2\u89e3\u4e86 2-3\u6811\uff0c\u5e76\u505a\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<p>\u73b0\u5728\u6765\u672c\u7ae0\uff0c\u6211\u4eec\u5728\u6765\u770b\u770b\u7ea2\u9ed1\u6811\u4e0e2-3\u6811\u7684\u5173\u7cfb\uff1b<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">\u7ea2\u9ed1\u6811<\/th>\n<th align=\"center\">\u7ea2\u9ed1\u6811<\/th>\n<th align=\"center\">2-3\u6811<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581822\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<td align=\"center\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581823\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<td align=\"center\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042581824\" alt=\"\" title=\"\" loading=\"lazy\"><\/td>\n<\/tr>\n<tr>\n<td align=\"center\">\u4e00\u68f5\u6807\u51c6\u4e8c\u53c9\u7ea2\u9ed1\u6811<\/td>\n<td align=\"center\">\u7ea2\u9ed1\u6811\u6f14\u5316\uff08\u7ea2\u8272\u8282\u70b9\u62c9\u5e73\uff09<\/td>\n<td align=\"center\">\u6700\u7ec8\u6062\u590d\u52302-3\u6811<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ea2\u9ed1\u6811\u4e00\u68f5\u57282-3\u6811\u57fa\u7840\u4e0a\u7684\u5de6\u503e\u7ea2\u9ed1\u6811\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u628a\u7ea2\u8272\u8282\u70b9\u4e0e\u5bf9\u5e94\u7684\u7236\u8282\u70b9\u62c9\u5e73\uff0c\u518d\u628a\u4e24\u4e2a\u62c9\u5e73\u7684\u8282\u70b9\u653e\u5230\u4e00\u4e2a\u8282\u70b9\u4e2d\u3002\u5c31\u662f\u6211\u4eec\u719f\u6089\u76842-3\u6811\u4e86\u3002<em>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b66\u4e60\u8fc72-3\u6811\uff0c\u6700\u597d\u5148\u770b\u4e0b\u5c0f\u5085\u54e5\u7684<a href=\"https:\/\/segmentfault.com\/a\/#\">2-3\u6811<\/a>\uff0c\u5426\u5219\u4f60\u4f1a\u770b\u7684\u5f88\u5403\u529b<\/em><\/p>\n<p>\u73b0\u5728\u518d\u6765\u770b\u4e0b\u7ea2\u9ed1\u6811\u7684\u4e94\u6761\u5b9a\u4e49\uff1b<\/p>\n<ol>\n<li>\n<p><strong>\u6bcf\u4e2a\u8282\u70b9\u4e0d\u662f\u7ea2\u8272\u5c31\u662f\u9ed1\u8272\u3002<\/strong><\/p>\n<ul>\n<li>\u9ed1\u8272\u51b3\u5b9a\u5e73\u8861\uff0c\u7ea2\u8272\u4e0d\u51b3\u5b9a\u5e73\u8861\u3002\u8fd9\u5bf9\u5e94\u4e862-3\u6811\u4e2d\u4e00\u4e2a\u8282\u70b9\u5185\u53ef\u4ee5\u5b58\u653e1~2\u4e2a\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6839\u662f\u9ed1\u8272\u7684\u3002<\/strong><\/p>\n<ul>\n<li>\u8fd9\u6761\u89c4\u5219\u6709\u65f6\u4f1a\u88ab\u7701\u7565\u3002\u7531\u4e8e\u6839\u603b\u662f\u53ef\u4ee5\u4ece\u7ea2\u8272\u53d8\u4e3a\u9ed1\u8272\uff0c\u4f46\u4e0d\u4e00\u5b9a\u76f8\u53cd\uff0c\u56e0\u6b64\u8be5\u89c4\u5219\u5bf9\u5206\u6790\u51e0\u4e4e\u6ca1\u6709\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6240\u6709\u53f6\u5b50 (NIL) \u90fd\u662f\u9ed1\u8272\u7684\u3002<\/strong><\/p>\n<ul>\n<li>\u8fd9\u91cc\u6307\u7684\u662f\u7ea2\u9ed1\u6811\u90fd\u4f1a\u6709\u4e00\u4e2a\u7a7a\u7684\u53f6\u5b50\u8282\u70b9\uff0c\u662f\u7ea2\u9ed1\u6811\u81ea\u5df1\u7684\u89c4\u5219\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5982\u679c\u4e00\u4e2a\u8282\u70b9\u662f\u7ea2\u8272\u7684\uff0c\u90a3\u4e48\u5b83\u7684\u4e24\u4e2a\u5b50\u8282\u70b9\u90fd\u662f\u9ed1\u8272\u7684\u3002<\/strong><\/p>\n<ul>\n<li>\u901a\u5e38\u8fd9\u6761\u89c4\u5219\u4e5f\u53eb\u4e0d\u4f1a\u6709\u8fde\u7eed\u7684\u7ea2\u8272\u8282\u70b9\u3002\u8fd9\u4f53\u73b0\u57282-3\u6811\u4e2d\uff0c\u4e00\u4e2a\u8282\u70b9\u6700\u591a\u4e34\u65f6\u4f1a\u67093\u4e2a\u8282\u70b9\uff0c\u4e2d\u95f4\u662f\u9ed1\u8272\u8282\u70b9\uff0c\u5de6\u53f3\u662f\u7ea2\u8272\u8282\u70b9\u30022-3\u6811\u4e2d\u51fa\u73b0\u8fd9\u6837\u7684\u60c5\u51b5\u540e\uff0c\u4f1a\u8fdb\u884c\u8282\u70b9\u8fc1\u79fb\uff0c\u4e2d\u95f4\u8282\u70b9\u6210\u4e3a\u7236\u8282\u70b9\uff0c\u5de6\u53f3\u8282\u70b9\u6210\u4e3a\u5b50\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4ece\u7ed9\u5b9a\u8282\u70b9\u5230\u5176\u4efb\u4f55\u540e\u4ee3 NIL \u8282\u70b9\u7684\u6bcf\u6761\u8def\u5f84\u90fd\u5305\u542b\u76f8\u540c\u6570\u91cf\u7684\u9ed1\u8272\u8282\u70b9\u3002<\/strong><\/p>\n<ul>\n<li>\u5bf9\u5e942-3\u6811\u4e2d\uff0c\u6bcf\u4e00\u5c42\u90fd\u53ea\u662f\u6709\u4e00\u4e2a\u8282\u70b9\u8d21\u732e\u4e86\u6811\u9ad8\u51b3\u5b9a\u5e73\u8861\u6027\uff0c\u4e5f\u5c31\u662f\u5bf9\u5e94\u7ea2\u9ed1\u6811\u4e2d\u7684\u9ed1\u8272\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u597d\u5566\uff0c\u73b0\u5728\u518d\u770b\u8fd95\u6761\u7406\u8bba\u662f\u4e0d\u5c31\u4e0d\u9700\u8981\u518d\u6b7b\u8bb0\u786c\u80cc\u4e86\u3002\u56e0\u4e3a\u7f16\u7a0b\u672c\u5c31\u662f\u5bf9\u6570\u5b66\u903b\u8f91\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u53ea\u8981\u628a\u6838\u5fc3\u903b\u8f91\u7406\u987a\u5176\u5b9e\u5f88\u597d\u7406\u89e3\u3002\u63a5\u4e0b\u6765\u5c0f\u5085\u54e5\u5c31\u5e26\u7740\u5927\u5bb6\u52a8\u624b\u5b9e\u73b0\u4e00\u4e0b\u7ea2\u9ed1\u6811\u3002<\/p>\n<h3>2. \u7ea2\u9ed1\u6811\u7ed3\u6784\u5b9e\u73b0<\/h3>\n<p>\u57fa\u4e8e BST \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u57fa\u7840\u4e0a\uff0cAVL\u6811\u6dfb\u52a0\u4e86\u6811\u9ad8\u4f5c\u4e3a\u8ba1\u7b97\u5e73\u8861\u56e0\u5b50\u7684\u6761\u4ef6\uff0c\u90a3\u4e48\u7ea2\u9ed1\u6811\u4e5f\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u989c\u8272\u5c5e\u6027\uff0c\u7528\u4e8e\u5904\u7406\u5e73\u8861\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"java\">public class Node {\n\n    public Class&lt;?&gt; clazz;\n    public Integer value;\n    public Node parent;\n    public Node left;\n    public Node right;\n\n    \/\/ AVL \u6811\u6240\u9700\u5c5e\u6027\n    public int height;\n    \/\/ \u7ea2\u9ed1\u6811\u6240\u9700\u5c5e\u6027\n    public Color color = Color.RED;\n    \n}    <\/code><\/pre>\n<p>\u76f8\u6bd4\u4e8eAVL\u6811\u901a\u8fc7\u5de6\u53f3\u65cb\u8f6c\u5e73\u8861\u6811\u9ad8\uff0c\u7ea2\u9ed1\u6811\u5219\u662f\u57282-3\u6811\u7684\u57fa\u7840\u4e0a\uff0c\u53ea\u5bf9\u9ed1\u8272\u8282\u70b9\u7ef4\u62a4\u6811\u9ad8\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u5230\u67d3\u8272\u548c\u5de6\u53f3\u65cb\u6765\u5bf9\u6811\u9ad8\u8c03\u8861\u3002<em>\u67d3\u8272\u4e0e\u5de6\u53f3\u65cb\u76f8\u6bd4\uff0c\u51cf\u5c11\u4e86\u5e73\u8861\u64cd\u4f5c<\/em><\/p>\n<ul>\n<li>\u6e90\u7801\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=kWbnNQ3rF0HVPsIgf%2BTzbw%3D%3D.HSqiv%2Fi89moWfoSiMoHmd6TtDZ%2FDm%2B%2Bi3CgiZHYxpzWqi%2FISkXWHoTM3iXUeoth4\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms<\/a><\/li>\n<li>\u672c\u7ae0\u6e90\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Ylz8qH44AQXTEnkmvRB75w%3D%3D.G5B6tAyVS2jx%2BttbvFjhVawMq0%2Fg2L1kXpLYpJeARiOF6hqaWfpq1ajF1i12ZwG2uNN8X45liQ2%2BhAVEojOZ%2BEBpaxOBGo%2FoA3%2F1c6D1jRr3xek8yVBCEOARRFDGWvyq\" rel=\"nofollow\">https:\/\/github.com\/fuzhengwei\/java-algorithms\/tree\/main\/data-structures\/src\/main\/java\/tree<\/a><\/li>\n<li>\u52a8\u753b\u6f14\u793a\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=bpTsvB70tGSrczSJ977Bgg%3D%3D.hAQqTe9oQO2DfEmpSL9%2F6wdtx%2BsI2YueR7%2FAKzQhEsKkFLMeNRjNDAgU829UNeJ7wTQSwJkgx6ygvYEW2cKozg%3D%3D\" rel=\"nofollow\">https:\/\/www.cs.usfca.edu\/~galles\/visualization\/RedBlack.html<\/a>\u2014\u2014 \u7ea2\u9ed1\u6811\u521d\u6b21\u7406\u89e3\u8fd8\u662f\u6bd4\u8f83\u56f0\u96be\u7684\uff0c\u53ef\u4ee5\u7ed3\u5408\u5b66\u4e60\u5185\u5bb9\u7684\u540c\u65f6\u505a\u4e00\u4e9b\u52a8\u753b\u6f14\u793a\u3002<\/li>\n<\/ul>\n<h4>2.1 \u5de6\u503e\u67d3\u8272<\/h4>\n<p>\u65b0\u589e\u8282\u70b91\uff0c\u76f8\u5f53\u4e8e2-3\u6811\u4e2d\u5728\u8282\u70b92\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u65f6\u5019\u5e76\u4e0d\u5f71\u54cd\u6811\u9ad8\uff0c\u53ea\u9700\u8981\u67d3\u8272\u4fdd\u6301\u7ea2\u9ed1\u6811\u7684\u89c4\u5219\u5373\u53ef\u3002\u67d3\u8272\u8fc7\u7a0b\u5982\u56fe\u6240\u793a\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-rbt-04.png?raw=true&quot; width=&quot;750px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<pre><code class=\"java\">Node uncle = grandParent.right;\n\/\/ \u67d3\u8272\nif (uncle.color == Node.Color.RED){\n    parent.color = Node.Color.BLACK;\n    uncle.color = Node.Color.BLACK;\n    grandParent.color = Node.Color.RED;\n    current = grandParent;\n}<\/code><\/pre>\n<ul>\n<li>\u67d3\u8272\u65f6\u6839\u636e\u5f53\u524d\u8282\u70b9\u7684\u7237\u7237\u8282\u70b9\uff0c\u627e\u5230\u5f53\u524d\u8282\u70b9\u7684\u53d4\u53d4\u8282\u70b9\u3002<\/li>\n<li>\u518d\u628a\u7236\u8282\u70b9\u67d3\u9ed1\u3001\u53d4\u53d4\u8282\u70b9\u67d3\u9ed1\uff0c\u7237\u7237\u8282\u70b9\u67d3\u7ea2\u3002\u4f46\u7237\u7237\u8282\u70b9\u67d3\u7ea2\u662f\u4e34\u65f6\u7684\uff0c\u5f53\u5e73\u8861\u6811\u9ad8\u64cd\u4f5c\u540e\u4f1a\u628a\u6839\u8282\u70b9\u67d3\u9ed1\u3002<em>\u5177\u4f53\u53c2\u8003\u6e90\u7801<\/em><\/li>\n<\/ul>\n<h4>2.2 \u53f3\u503e\u67d3\u8272<\/h4>\n<p>\u65b0\u589e\u8282\u70b94\uff0c\u76f8\u5f53\u4e8e2-3\u6811\u4e2d\u5728\u8282\u70b93\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u65f6\u5019\u5e76\u4e0d\u5f71\u54cd\u6811\u9ad8\uff0c\u53ea\u9700\u8981\u67d3\u8272\u4fdd\u6301\u7ea2\u9ed1\u6811\u7684\u89c4\u5219\u5373\u53ef\u3002\u67d3\u8272\u8fc7\u7a0b\u5982\u56fe\u6240\u793a\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-rbt-05.png?raw=true&quot; width=&quot;750px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<pre><code class=\"java\">Node uncle = grandParent.left;\n\/\/ \u67d3\u8272\nif(uncle.color == Node.Color.RED){\n    parent.color = Node.Color.BLACK;\n    uncle.color = Node.Color.BLACK;\n    grandParent.color = Node.Color.RED;\n    current= grandParent;\n}<\/code><\/pre>\n<ul>\n<li>\u67d3\u8272\u65f6\u6839\u636e\u5f53\u524d\u8282\u70b9\u7684\u7237\u7237\u8282\u70b9\uff0c\u627e\u5230\u5f53\u524d\u8282\u70b9\u7684\u53d4\u53d4\u8282\u70b9\u3002<\/li>\n<li>\u518d\u628a\u7236\u8282\u70b9\u67d3\u9ed1\u3001\u53d4\u53d4\u8282\u70b9\u67d3\u9ed1\uff0c\u7237\u7237\u8282\u70b9\u67d3\u7ea2\u3002\u4f46\u7237\u7237\u8282\u70b9\u67d3\u7ea2\u662f\u4e34\u65f6\u7684\uff0c\u5f53\u5e73\u8861\u6811\u9ad8\u64cd\u4f5c\u540e\u4f1a\u628a\u6839\u8282\u70b9\u67d3\u9ed1\u3002<em>\u5177\u4f53\u53c2\u8003\u6e90\u7801<\/em><\/li>\n<\/ul>\n<h4>2.3 \u5de6\u65cb\u8c03\u8861<\/h4>\n<h5>2.3.1 \u4e00\u6b21\u5de6\u65cb<\/h5>\n<p>\u5bf9\u71672-3\u6811\uff0c\u53ea\u6709\u5f53\u4e00\u4e2a\u8282\u70b9\u5185\u67093\u4e2a\u8282\u70b9\u7684\u65f6\u5019\uff0c\u624d\u9700\u8981\u8c03\u8861\u3002\u90a3\u4e48\u7ea2\u9ed1\u6811\u5219\u662f\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u53d4\u53d4\u8282\u70b9\u662f\u5426\u4e3a\u7ea2\u8272\u8282\u70b9\uff0c\u5982\u679c\u4e0d\u662f\u5219\u6ca1\u6cd5\u901a\u8fc7\u67d3\u8272\u8c03\u8861\uff0c\u4e5f\u5c31\u662f\u9700\u8981\u9009\u62e9\u8fdb\u884c\u8c03\u8861\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-rbt-06.png?raw=true&quot; width=&quot;750px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<pre><code class=\"java\">parent.color = Node.Color.BLACK;\ngrandParent.color = Node.Color.RED;\nsuper.rotateLeft(grandParent);<\/code><\/pre>\n<ul>\n<li>\u5f53\u4f60\u628a\u7ea2\u9ed1\u6811\u5bf9\u7167\u7406\u89e3\u62102-3\u6811\uff0c\u5982\u56fe\u4e2d\u7b2c1\u6b65\u9aa4\u4e0b\u7684\u5de6\u4fa7\u5c0f\u56fe\uff0c\u65b0\u589e\u7684\u8282\u70b95\u5012\u7f6e2-3\u6811\u4e0d\u5e73\u8861\u3002<\/li>\n<li>\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u628a2-3\u6811\u4e2d\u8282\u70b94\u63d0\u8d77\u6765\uff0c\u800c\u5bf9\u5e94\u7ea2\u9ed1\u6811\u5219\u9700\u8981\u5148\u8fdb\u884c\u67d3\u8272\uff0c\u5f85\u64cd\u4f5c\u7684\u8282\u70b94\u4e3a\u9ed1\u8272\uff0c\u4e24\u4e2a\u5b69\u5b50\u8282\u70b9\u4e3a\u7ea2\u8272\u3002<\/li>\n<li>\u6700\u540e\u662f\u628a\u8282\u70b93\u8fdb\u884c\u4e00\u6b21\u5de6\u65cb\u64cd\u4f5c\uff0c\u5b8c\u6210\u6811\u7684\u5e73\u8861\u3002\u5bf9\u5e94\u6b65\u9aa43\u4e2d\u7684\u5de6\u4fa7\u5c0f\u56fe\u662f2-3\u6811\u8c03\u8861\u540e\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<h5>2.3.2 \u53f3\u65cb + \u5de6\u65cb<\/h5>\n<p>\u5f53\u4e00\u6b21\u5de6\u65cb\u6ca1\u6cd5\u8c03\u8861\uff0c\u9700\u8981\u53f3\u65cb+\u5de6\u65cb\u7684\u60c5\u51b5\uff0c\u5728AVL\u6811\u4e2d\u6709\u540c\u6837\u7684\u573a\u666f\u3002\u672c\u8eab\u6811\u9700\u8981\u5de6\u65cb\u64cd\u4f5c\uff0c\u4f46\u6574\u4f53\u5206\u652f\u6811\u8282\u70b9\u504f\u5de6\uff0c\u6b64\u65f6\u9700\u8981\u53f3\u65cb\u8c03\u6574\u6811\u7ed3\u6784\u518d\u5de6\u65cb\u3002<em>\u6b64\u5904\u53ef\u53c2\u8003\u5c0f\u5085\u54e5\u7f16\u5199\u7684<a href=\"https:\/\/link.segmentfault.com\/?enc=NBnG4DUySxSJ0OpNWmayJw%3D%3D.Yhqel93uzwLB1ingj367qGvGSN4fhXPss1JbXJ90uoF%2BTEGZshViVasGX2inWFh2%2BMBgczje3KukHZYF%2FJPWDltDVGfzcnNeHI9qMmQo16ggxA3w8hBYd%2FfiHXYuGkV4HnYfDmHKqdvEpZtghfMhVDY4GOUhFNwNyLG%2BCs9Sd1s%3D\" rel=\"nofollow\">AVL\u6811<\/a><\/em><\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-rbt-07.png?raw=true&quot; width=&quot;750px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<pre><code class=\"java\">\/\/ \u504f\u5de6\u2199\uff0c\u5148\u53f3\u65cb\u4e00\u6b21\u8c03\u8861\nif (current == parent.left){\n    current = parent;\n    super.rotateRight(current);\n    parent = current.parent;\n}\nparent.color = Node.Color.BLACK;\ngrandParent.color = Node.Color.RED;\nsuper.rotateLeft(grandParent);<\/code><\/pre>\n<ul>\n<li>\u7ea2\u9ed1\u6811\u65b0\u589e\u8282\u70b94\u4ee5\u540e\uff0c4\u21995 \u7ed3\u6784\u504f\u5de6\uff0c\u9700\u8981\u5148\u8fdb\u884c\u53f3\u65cb\u8c03\u8861\u6811\u7ed3\u6784\uff0c\u518d\u8fdb\u884c\u5de6\u65cb\u3002\u5176\u5b9e\u8fd9\u4e2a\u65f6\u5019\u518d\u8fdb\u884c\u7684\u5de6\u65cb\u5c31\u548c\u4e0a\u9762\u4e00\u6b21\u5de6\u65cb\u64cd\u4f5c\u4e00\u81f4\u4e86\u3002<\/li>\n<\/ul>\n<h4>4. \u53f3\u65cb\u8c03\u8861<\/h4>\n<h5>2.4.1 \u4e00\u6b21\u53f3\u65cb<\/h5>\n<p>\u5bf9\u71672-3\u6811\uff0c\u53ea\u6709\u5f53\u4e00\u4e2a\u8282\u70b9\u5185\u67093\u4e2a\u8282\u70b9\u7684\u65f6\u5019\uff0c\u624d\u9700\u8981\u8c03\u8861\u3002\u90a3\u4e48\u7ea2\u9ed1\u6811\u5219\u662f\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u53d4\u53d4\u8282\u70b9\u662f\u5426\u4e3a\u7ea2\u8272\u8282\u70b9\uff0c\u5982\u679c\u4e0d\u662f\u5219\u6ca1\u6cd5\u901a\u8fc7\u67d3\u8272\u8c03\u8861\uff0c\u4e5f\u5c31\u662f\u9700\u8981\u9009\u62e9\u8fdb\u884c\u8c03\u8861\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-rbt-08.png?raw=true&quot; width=&quot;750px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<pre><code class=\"java\">parent.color = Node.Color.BLACK;\ngrandParent.color = Node.Color.RED;\nsuper.rotateRight(grandParent);<\/code><\/pre>\n<ul>\n<li>\u5f53\u4f60\u628a\u7ea2\u9ed1\u6811\u5bf9\u7167\u7406\u89e3\u62102-3\u6811\uff0c\u5982\u56fe\u4e2d\u7b2c1\u6b65\u9aa4\u4e0b\u7684\u53f3\u4fa7\u5c0f\u56fe\uff0c\u65b0\u589e\u7684\u8282\u70b91\u5012\u7f6e2-3\u6811\u4e0d\u5e73\u8861\u3002<\/li>\n<li>\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u628a2-3\u6811\u4e2d\u8282\u70b92\u63d0\u8d77\u6765\uff0c\u800c\u5bf9\u5e94\u7ea2\u9ed1\u6811\u5219\u9700\u8981\u5148\u8fdb\u884c\u67d3\u8272\uff0c\u5f85\u64cd\u4f5c\u7684\u8282\u70b92\u4e3a\u9ed1\u8272\uff0c\u4e24\u4e2a\u5b69\u5b50\u8282\u70b9\u4e3a\u7ea2\u8272\u3002<\/li>\n<li>\u6700\u540e\u662f\u628a\u8282\u70b92\u8fdb\u884c\u4e00\u6b21\u53f3\u65cb\u64cd\u4f5c\uff0c\u5b8c\u6210\u6811\u7684\u5e73\u8861\u3002\u5bf9\u5e94\u6b65\u9aa43\u4e2d\u7684\u53f3\u4fa7\u5c0f\u56fe\u662f2-3\u6811\u8c03\u8861\u540e\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<h5>2.4.2 \u5de6\u65cb + \u53f3\u65cb<\/h5>\n<p>\u5f53\u4e00\u6b21\u5de6\u65cb\u6ca1\u6cd5\u8c03\u8861\uff0c\u9700\u8981\u5de6\u65cb+\u53f3\u65cb\u7684\u60c5\u51b5\uff0c\u5728AVL\u6811\u4e2d\u6709\u540c\u6837\u7684\u573a\u666f\u3002\u672c\u8eab\u6811\u9700\u8981\u53f3\u65cb\u64cd\u4f5c\uff0c\u4f46\u6574\u4f53\u5206\u652f\u6811\u8282\u70b9\u504f\u53f3\uff0c\u6b64\u65f6\u9700\u8981\u5de6\u65cb\u8c03\u6574\u6811\u7ed3\u6784\u518d\u53f3\u65cb\u3002<\/p>\n<p>&lt;div align=&quot;center&quot;&gt;<\/p>\n<pre><code>&lt;img src=&quot;https:\/\/bugstack.cn\/images\/article\/algorithm\/tree-rbt-09.png?raw=true&quot; width=&quot;650px&quot;&gt;<\/code><\/pre>\n<p>&lt;\/div&gt;<\/p>\n<pre><code class=\"java\">\/\/ \u504f\u53f3\u2198\uff0c\u5148\u5de6\u65cb\u4e00\u6b21\u8c03\u8861\nif (current == parent.right){\n    current = parent;\n    super.rotateLeft(current);\n    parent = current.parent;\n}\nparent.color = Node.Color.BLACK;\ngrandParent.color = Node.Color.RED;\nsuper.rotateRight(grandParent);<\/code><\/pre>\n<ul>\n<li>\u7ea2\u9ed1\u6811\u65b0\u589e\u8282\u70b92\u4ee5\u540e\uff0c1\u21982 \u7ed3\u6784\u504f\u53f3\uff0c\u9700\u8981\u5148\u8fdb\u884c\u5de6\u65cb\u8c03\u8861\u6811\u7ed3\u6784\uff0c\u518d\u8fdb\u884c\u53f3\u65cb\u3002\u5176\u5b9e\u8fd9\u4e2a\u65f6\u5019\u518d\u8fdb\u884c\u7684\u53f3\u65cb\u5c31\u548c\u4e0a\u9762\u4e00\u6b21\u53f3\u65cb\u64cd\u4f5c\u4e00\u81f4\u4e86\u3002<\/li>\n<\/ul>\n<h3>3. \u7ea2\u9ed1\u6811\u5b9e\u73b0\u6d4b\u8bd5<\/h3>\n<p>\u4e3a\u4e86\u9a8c\u8bc1\u7ea2\u9ed1\u6811\u7684\u5b9e\u73b0\u6b63\u786e\u4e0e\u5426\uff0c\u8fd9\u91cc\u6211\u4eec\u505a\u4e00\u4e0b\u968f\u673a\u8282\u70b9\u7684\u63d2\u5165\uff0c\u5982\u679c\u5b83\u80fd\u4e00\u76f4\u4fdd\u6301\u5e73\u8861\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u4e00\u9897\u53ef\u9760\u7ea2\u9ed1\u5e73\u8861\u6811\u3002<\/p>\n<pre><code class=\"java\">@Test\npublic void test_binary_search_tree() {\n    Tree tree = new RedBlackTree();\n    for (int i = 0; i &lt; 20; i++) {\n        tree.insert(new Random().nextInt(100));\n    }\n    System.out.println(tree);\n}<\/code><\/pre>\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n<pre><code class=\"java\">RedBlackTree\uff0c\u8f93\u5165\u8282\u70b9\uff1a79,92,36,35,72,22,11,66,98,28,30,39,56,26,1,25,33,80,22,23\n\n                         \/----- &lt;NIL&gt;\n                 \/----- 98(\u7ea2)\n                 |       \/----- &lt;NIL&gt;\n         \/----- 92(\u9ed1)\n         |       |       \/----- &lt;NIL&gt;\n         |       \/----- 80(\u7ea2)\n         |               \/----- &lt;NIL&gt;\n \/----- 79(\u9ed1)\n |       |               \/----- &lt;NIL&gt;\n |       |       \/----- 72(\u9ed1)\n |       |       |       \/----- &lt;NIL&gt;\n |       \/----- 66(\u7ea2)\n |               |               \/----- &lt;NIL&gt;\n |               |       \/----- 56(\u7ea2)\n |               |       |       \/----- &lt;NIL&gt;\n |               \/----- 39(\u9ed1)\n |                       \/----- &lt;NIL&gt;\n36(\u9ed1)\n |                       \/----- &lt;NIL&gt;\n |               \/----- 35(\u9ed1)\n |               |       |       \/----- &lt;NIL&gt;\n |               |       \/----- 33(\u7ea2)\n |               |               \/----- &lt;NIL&gt;\n |       \/----- 30(\u7ea2)\n |       |       |       \/----- &lt;NIL&gt;\n |       |       \/----- 28(\u9ed1)\n |       |               \/----- &lt;NIL&gt;\n \/----- 26(\u9ed1)\n         |                       \/----- &lt;NIL&gt;\n         |               \/----- 25(\u7ea2)\n         |               |       \/----- &lt;NIL&gt;\n         |       \/----- 23(\u9ed1)\n         |       |       |       \/----- &lt;NIL&gt;\n         |       |       \/----- 22(\u7ea2)\n         |       |               \/----- &lt;NIL&gt;\n         \/----- 22(\u7ea2)\n                 |       \/----- &lt;NIL&gt;\n                 \/----- 11(\u9ed1)\n                         |       \/----- &lt;NIL&gt;\n                         \/----- 1(\u7ea2)\n                                 \/----- &lt;NIL&gt;\n\n\u5bf9\u71672-3\u6811\u7ed3\u6784\n                 \/----- [98]\n         \/----- [92]\n         |       \/----- [80]\n \/----- [79]\n |       |       \/----- [72]\n |       \/----- [66]\n |               \/----- [39,56]\n[36]\n |               \/----- [33,35]\n |       \/----- [30]\n |       |       \/----- [28]\n \/----- [26]\n         |       \n         |       \/----- [25]\n         \/----- [22,23]----- [22]\n                 \/----- [1,11]                                 <\/code><\/pre>\n<ul>\n<li>\u968f\u673a\u63d2\u516520\u4e2a\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u987a\u5e8f\u5df2\u7ecf\u6253\u5370\uff0c\u7ecf\u8fc7\u7ea2\u9ed1\u6811\u7684\u67d3\u8272\u548c\u5de6\u53f3\u65cb\u8c03\u8861\u540e\uff0c\u4e8c\u53c9\u7ed3\u6784\u59cb\u7ec8\u4fdd\u6301\u6811\u4fdd\u6301\u5e73\u8861\uff0c\u90a3\u4e48\u9a8c\u8bc1\u901a\u8fc7\u3002<\/li>\n<li>\u53e6\u5916\u672c\u6587\u51fa\u73b0\u7684\u6848\u4f8b\u5df2\u7ecf\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u90fd\u6709\u7f16\u5199\uff0c\u8bfb\u8005\u53ef\u4ee5\u5728\u6e90\u7801\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042581816\">https:\/\/segmentfault.com\/a\/1190000042581816<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u5c0f\u5085\u54e5\u535a\u5ba2\uff1ahttps:\/\/bugstack.cn \u6c89\u6dc0\u3001\u5206\u4eab\u3001\u6210\u957f\uff0c\u8ba9\u81ea\u5df1\u548c\u4ed6\u4eba\u90fd\u80fd\u6709\u6240\u6536\u83b7\uff01\ud83d\ude04 \u4e00\u3001 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006651","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006651"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006651\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}