{"id":78117,"date":"2026-02-26T08:11:32","date_gmt":"2026-02-26T00:11:32","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78117.html"},"modified":"2026-02-26T08:11:32","modified_gmt":"2026-02-26T00:11:32","slug":"avl%e6%a0%91%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78117.html","title":{"rendered":"AVL\u6811\u7684\u5b9e\u73b0"},"content":{"rendered":"<h2 style=\"background-color:transparent\">\u4e00.AVL\u6811\u7684\u6982\u5ff5\u4e0e\u7ed3\u6784<\/h2>\n<h2 style=\"background-color:transparent\">1.AVL\u6811\u7684\u6982\u5ff5<\/h2>\n<p>1.1.\u4e3a\u4ec0\u4e48\u7528AVL\u6811<\/p>\n<p>\u539f\u56e0&#xff1a;\u666e\u901a\u7684\u4e8c\u53c9\u641c\u7d22\u6811&#xff08;BST&#xff09;\u5728\u63d2\u5165\u6709\u5e8f\u6570\u636e&#xff08;\u5982 1&#xff0c; 2&#xff0c; 3&#xff0c; 4&#xff0c; 5&#xff09;\u65f6&#xff0c;\u4f1a\u53d8\u6210\u4e00\u4e2a\u53ea\u6709\u53f3\u5b69\u5b50\u7684\u957f\u94fe&#xff0c;\u6b64\u65f6\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u6548\u7387\u5c31\u4eceO(log N)\u9000\u5316\u5230O(N)&#xff0c;\u4f46\u662fAVL \u6811&#xff1a;\u901a\u8fc7\u65cb\u8f6c\u673a\u5236&#xff0c;\u5f3a\u5236\u4fdd\u8bc1\u5de6\u53f3\u5b50\u6811\u9ad8\u5ea6\u5dee\u4e0d\u8d85\u8fc7 1&#xff0c;\u4f7f\u5f97\u6811\u9ad8\u59cb\u7ec8\u7ef4\u6301\u5728O(log N)\u3002<\/p>\n<p>1.2.AVL\u6811\u7684\u6982\u5ff5<\/p>\n<p>\u6982\u5ff5&#xff1a;AVL\u662f\u2f00\u9897\u7a7a\u6811&#xff0c;\u6216\u8005\u5177\u5907\u4e0b\u5217\u6027\u8d28\u7684\u2f06\u53c9\u641c\u7d22\u6811&#xff1a;\u5b83\u7684\u5de6\u53f3\u2f26\u6811\u90fd\u662fAVL\u6811&#xff0c;\u4e14\u5de6\u53f3\u2f26\u6811\u7684\u9ad8\u5ea6\u5dee&#xff08;\u5e73\u8861\u56e0\u5b50&#xff09;\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71\u3002<\/p>\n<p style=\"text-align:center\">\u4f8b\u5982&#xff1a;<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb0100aa.png\" \/><\/p>\n<p style=\"text-align:center\">\u6ce8\u610f&#xff1a;\u4e0a\u9762\u7684\u6bcf\u4e00\u4e2a\u6570\u4e0a\u7684\u6570\u5b57\u5c31\u662f\u5e73\u8861\u56e0\u5b50<\/p>\n<p>\u7279\u70b9&#xff1a;AVL\u6811\u5b9e\u73b0\u8fd9\u2fa5\u6211\u4eec\u5f15\u2f0a\u2f00\u4e2a\u5e73\u8861\u56e0\u2f26(balancefactor)\u7684\u6982\u5ff5&#xff0c;\u6bcf\u4e2a\u7ed3\u70b9\u90fd\u6709\u2f00\u4e2a\u5e73\u8861\u56e0\u2f26&#xff0c;\u4efb\u4f55\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7b49\u4e8e\u53f3\u2f26\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u4efb\u4f55\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u2f26\u7b49\u4e8e0\/1\/-1&#xff0c; AVL\u6811\u5e76\u4e0d\u662f\u5fc5\u987b\u8981\u5e73\u8861\u56e0\u2f26&#xff0c;\u4f46\u662f\u6709\u4e86\u5e73\u8861\u56e0\u2f26\u53ef\u4ee5\u66f4\u2f45\u4fbf\u6211\u4eec\u53bb\u8fdb\u2f8f\u89c2\u5bdf\u548c\u63a7\u5236\u6811\u662f\u5426\u5e73\u8861&#xff0c; \u5c31\u50cf\u2f00\u4e2a\u2edb\u5411\u6807\u2f00\u6837\u3002<\/p>\n<p>\u95ee&#xff1a;\u4e3a\u4ec0\u4e48AVL\u6811\u662f\u9ad8\u5ea6\u5e73\u8861\u641c\u7d22\u2f06\u53c9\u6811&#xff0c;\u8981\u6c42\u9ad8\u5ea6\u5dee\u4e0d\u8d85\u8fc71&#xff0c;\u2f7d\u4e0d\u662f\u9ad8\u5ea6\u5dee\u662f0\u5462&#xff1f;0\u4e0d\u662f\u66f4\u597d\u7684\u5e73\u8861\u5417&#xff1f;<\/p>\n<p>\u539f\u56e0&#xff1a;0\u4e0d\u662f\u66f4\u597d\u7684\u5e73\u8861\u5417&#xff1f;\u753b\u753b\u56fe\u5206\u6790\u6211\u4eec\u53d1\u73b0&#xff0c;\u4e0d\u662f\u4e0d\u60f3\u8fd9\u6837\u8bbe\u8ba1&#xff0c;\u800c\u662f\u6709\u4e9b\u60c5\u51b5\u662f\u505a\u4e0d\u5230\u9ad8\u5ea6\u5dee\u662f0\u7684&#xff0c;\u6bd4\u5982\u2f00\u68f5\u6811\u662f2\u4e2a\u7ed3\u70b9&#xff0c;4\u4e2a\u7ed3\u70b9\u7b49\u60c5\u51b5\u4e0b&#xff0c;\u9ad8\u5ea6\u5dee\u6700\u597d\u5c31\u662f1&#xff0c;\u65e0\u6cd5\u505a\u5230\u9ad8\u5ea6\u5dee\u662f0\u3002<\/p>\n<p>\u6548\u7387&#xff1a;AVL\u6811\u6574\u4f53\u7ed3\u70b9\u6570\u91cf\u548c\u5206\u5e03\u548c\u5b8c\u5168\u4e8c\u53c9\u6811\u7c7b\u4f3c&#xff0c;\u9ad8\u5ea6\u53ef\u4ee5\u63a7\u5236\u5728 &#xff0c;\u90a3\u4e48\u589e\u5220\u67e5\u6539\u7684\u6548\u7387\u4e5f\u53ef \u4ee5\u63a7\u5236\u5728O(logN) &#xff0c;\u76f8\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u6709\u4e86\u672c\u8d28\u7684\u63d0\u5347\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb02c770.png\" \/><\/p>\n<p>\u4f46\u662f\u5982\u679c\u6811\u957f\u6210\u8fd9\u6837&#xff0c;\u5c31\u4e0d\u662fAVL\u6811&#xff0c;\u6b64\u65f6\u5c31\u9700\u8981\u901a\u8fc7\u65cb\u8f6c\u4f7f\u5176\u53d8\u6210AVL\u6811\u3002<\/p>\n<h3>2.AVL\u6811\u7684\u7ed3\u6784<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u5b9a\u4e49\u4e24\u4e2a\u6587\u4ef6&#xff0c;\u4e00\u4e2a\u4e3aAVLTree.h&#xff0c;\u53e6\u4e00\u4e2a\u4e3aAVL_Test.cpp<\/p>\n<p>\u5728AVLTree.h\u4e2d&#xff0c;\u6211\u4eec\u5148\u5b9e\u73b0\u6bcf\u4e00\u4e2a\u8282\u70b9&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;assert.h&gt;<br \/>\n#include&lt;vector&gt;<br \/>\nusing namespace std;<br \/>\ntemplate&lt;class K,class V&gt;<br \/>\nstruct AVLTNode<br \/>\n{<br \/>\npair&lt;K, V&gt; _kv;<br \/>\nAVLTNode&lt;K, V&gt;* _left;<br \/>\nAVLTNode&lt;K, V&gt;* _right;<br \/>\n\/\/parent\u6307\u9488\u7528\u4e8e\u540e\u7eed\u66f4\u65b0\u5e73\u8861\u56e0\u5b50<br \/>\nAVLTNode&lt;K, V&gt;* _parent;<br \/>\nint _bf;<br \/>\n\/\/\u6784\u9020\u51fd\u6570<br \/>\nAVLTNode(const pair&lt;K,V&gt;&amp; kv)<br \/>\n:_kv(kv)<br \/>\n,_left(nullptr)<br \/>\n,_right(nullptr)<br \/>\n,_parent(nullptr)<br \/>\n,_bf(0)<br \/>\n{}<br \/>\n};<\/p>\n<p>\u89e3\u91ca&#xff1a;\u5176\u4e2d\u7684pair&lt;K, V&gt; _kv;\u4e0d\u9700\u8981\u6211\u4eec\u53bb\u5b9e\u73b0&#xff0c;AVLTNode&lt;K, V&gt;* _left;AVLTNode&lt;K, V&gt;* _right;\u4e0e\u641c\u7d22\u4e8c\u53c9\u6811\u7c7b\u4f3c&#xff0c;AVLTNode&lt;K, V&gt;* _parent;\u4e2d\u7684parent\u6307\u9488\u7528\u4e8e\u540e\u7eed\u66f4\u65b0\u5e73\u8861\u56e0\u5b50<\/p>\n<p>_bf\u7528\u4e8e\u8bb0\u5f55\u5e73\u8861\u56e0\u5b50<\/p>\n<p>template&lt;class K, class V&gt;<br \/>\nclass AVLTree<br \/>\n{<br \/>\n        typedef AVLTreeNode&lt;K, V&gt; Node;<br \/>\npublic:<br \/>\n        \/\/&#8230;<br \/>\nprivate:<br \/>\n    Node* _root &#061; nullptr;<br \/>\n};<\/p>\n<p>\u8fd9\u4e2a\u5c31\u4e3a\u6811\u7684\u5927\u6846\u67b6<\/p>\n<h3>\u4e8c.AVL\u6811\u7684\u63d2\u5165&#xff08;\u96be\u70b9&#xff09;<\/h3>\n<h2 style=\"background-color:transparent\">1.\u63d2\u5165\u5927\u6982\u8fc7\u7a0b<\/h2>\n<p>1.1.\u8fc7\u7a0b<\/p>\n<p>&lt;1&gt;. \u63d2\u5165\u2f00\u4e2a\u503c\u6309\u2f06\u53c9\u641c\u7d22\u6811\u89c4\u5219\u8fdb\u884c\u63d2\u2f0a\u3002 <\/p>\n<p>&lt;2&gt;. \u65b0\u589e\u7ed3\u70b9\u4ee5\u540e&#xff0c;\u53ea\u4f1a\u5f71\u54cd\u7956\u5148\u7ed3\u70b9\u7684\u9ad8\u5ea6&#xff0c;\u4e5f\u5c31\u662f\u53ef\u80fd\u4f1a\u5f71\u54cd\u90e8\u5206\u7956\u5148\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50&#xff0c;\u6240\u4ee5\u66f4\u65b0 \u4ece\u65b0\u589e\u7ed3\u70b9-&gt;\u6839\u7ed3\u70b9\u8def\u5f84\u4e0a\u7684\u5e73\u8861\u56e0\u5b50&#xff0c;\u5b9e\u9645\u4e2d\u6700\u574f\u60c5\u51b5\u4e0b\u8981\u66f4\u65b0\u5230\u6839&#xff0c;\u6709\u4e9b\u60c5\u51b5\u66f4\u65b0\u5230\u4e2d\u95f4\u5c31\u53ef\u4ee5\u505c\u6b62\u4e86&#xff0c;\u5177\u4f53\u60c5\u51b5\u6211\u4eec\u4e0b\u9762\u518d\u8be6\u7ec6\u5206\u6790\u3002 <\/p>\n<p>&lt;3&gt;. \u66f4\u65b0\u5e73\u8861\u56e0\u5b50\u8fc7\u7a0b\u4e2d\u6ca1\u6709\u51fa\u73b0\u95ee\u9898&#xff0c;\u5219\u63d2\u5165\u7ed3\u675f\u3002<\/p>\n<p>&lt;4&gt;. \u66f4\u65b0\u5e73\u8861\u56e0\u5b50\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4e0d\u5e73\u8861&#xff0c;\u5bf9\u4e0d\u5e73\u8861\u5b50\u6811\u65cb\u8f6c&#xff0c;\u65cb\u8f6c\u540e\u672c\u8d28\u8c03\u5e73\u8861\u7684\u540c\u65f6&#xff0c;\u672c\u8d28\u964d\u4f4e\u4e86\u5b50\u6811 \u7684\u9ad8\u5ea6&#xff0c;\u4e0d\u4f1a\u518d\u5f71\u54cd\u4e0a\u2f00\u5c42&#xff0c;\u6240\u4ee5\u63d2\u5165\u7ed3\u675f\u3002<\/p>\n<p>1.2.\u5e73\u8861\u56e0\u5b50\u66f4\u65b0<\/p>\n<p>1.2.1.\u66f4\u65b0\u539f\u5219&#xff1a;<\/p>\n<p>\u2022 \u5e73\u8861\u56e0\u5b50&#061;\u5de6\u5b50\u6811\u9ad8\u5ea6-\u5de6\u5b50\u6811\u9ad8\u5ea6&#xff08;\u4e5f\u53ef\u4ee5\u5de6\u5b50\u6811\u9ad8\u5ea6-\u53f3\u5b50\u6811\u9ad8\u5ea6&#xff0c;\u53d6\u51b3\u4e8e\u81ea\u5df1&#xff09;\u3002<\/p>\n<p>\u2022 \u53ea\u6709\u5b50\u6811\u9ad8\u5ea6\u53d8\u5316\u624d\u4f1a\u5f71\u54cd\u5f53\u524d\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u3002<\/p>\n<p>\u2022 \u63d2\u2f0a\u7ed3\u70b9&#xff0c;\u4f1a\u589e\u52a0\u9ad8\u5ea6&#xff0c;\u6240\u4ee5\u65b0\u589e\u7ed3\u70b9\u5728parent\u7684\u53f3\u5b50\u6811&#xff0c;parent\u7684\u5e73\u8861\u56e0\u5b50&#043;&#043;&#xff0c;\u65b0\u589e\u7ed3\u70b9\u5728 parent\u7684\u5de6\u5b50\u6811&#xff0c;parent\u5e73\u8861\u56e0\u5b50&#8211; <\/p>\n<p>\u2022 parent\u6240\u5728\u5b50\u6811\u7684\u9ad8\u5ea6\u662f\u5426\u53d8\u5316\u51b3\u5b9a\u4e86\u662f\u5426\u4f1a\u7ee7\u7eed\u5f80\u4e0a\u66f4\u65b0\u3002<\/p>\n<p>1.2.2.\u66f4\u65b0\u505c\u6b62\u6761\u4ef6&#xff1a; <\/p>\n<p>\u2022 \u66f4\u65b0\u540eparent\u7684\u5e73\u8861\u56e0\u5b50\u7b49\u4e8e0&#xff0c;\u8bf4\u660e\u66f4\u65b0\u4e2dparent\u7684\u5e73\u8861\u56e0\u5b50\u53d8\u5316\u4e3a-1-&gt;0\u6216\u80051-&gt;0&#xff0c;\u8bf4\u660e\u66f4\u65b0\u524d parent\u5b50\u6811\u2f00\u8fb9\u9ad8\u2f00\u8fb9\u4f4e&#xff0c;\u65b0\u589e\u7684\u7ed3\u70b9\u63d2\u5165\u5728\u4f4e\u7684\u90a3\u8fb9&#xff0c;\u63d2\u5165\u540eparent\u6240\u5728\u7684\u5b50\u6811\u9ad8\u5ea6\u4e0d\u53d8&#xff0c;\u4e0d\u4f1a\u5f71\u54cdparent\u7684\u7236\u4eb2\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50&#xff0c;\u66f4\u65b0\u7ed3\u675f\u3002<\/p>\n<p style=\"text-align:center\">\u4f8b\u5982&#xff1a;3\u4e3a\u6839\u7684\u5b50\u6811\u9ad8\u5ea6\u4e0d\u53d8&#xff0c;\u4e0d\u4f1a\u5f71\u54cd\u4e0a\u2f00\u5c42&#xff0c;\u66f4\u65b0\u7ed3\u675f<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb046a0f.png\" \/><\/p>\n<p style=\"text-align:center\">\u2022 \u66f4\u65b0\u540eparent\u7684\u5e73\u8861\u56e0\u5b50\u7b49\u4e8e1\u6216-1&#xff0c;\u66f4\u65b0\u524d\u66f4\u65b0\u4e2dparent\u7684\u5e73\u8861\u56e0\u5b50\u53d8\u5316\u4e3a0-&gt;1\u6216\u80050-&gt;-1&#xff0c;\u8bf4 \u660e\u66f4\u65b0\u524dparent\u5b50\u6811\u4e24\u8fb9\u2f00\u6837\u9ad8&#xff0c;\u65b0\u589e\u7684\u63d2\u2f0a\u7ed3\u70b9\u540e&#xff0c;parent\u6240\u5728\u7684\u5b50\u6811\u2f00\u8fb9\u9ad8\u2f00\u8fb9\u4f4e&#xff0c;parent\u6240 \u5728\u7684\u5b50\u6811\u7b26\u5408\u5e73\u8861\u8981\u6c42&#xff0c;\u4f46\u662f\u9ad8\u5ea6\u589e\u52a0\u4e861&#xff0c;\u4f1a\u5f71\u54cdparent\u7684\u7236\u4eb2\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u2f26&#xff0c;\u6240\u4ee5\u8981\u7ee7\u7eed\u5411\u4e0a\u66f4\u65b0\u3002<\/p>\n<p>\u4f8b\u5982&#xff1a;1\u4e3a\u6839\u7684\u5b50\u6811\u9ad8\u5ea6\u6539\u53d8&#xff0c;\u5f71\u54cd\u4e0a\u2f00\u5c42&#xff0c;\u66f4\u65b0\u7ee7\u7eed&#xff0c;\u4f7fcur&#061;parent&#xff0c;parent&#061;parent-&gt;_parent<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb0643f8.png\" \/><\/p>\n<p style=\"text-align:center\">\u2022 \u66f4\u65b0\u540eparent\u7684\u5e73\u8861\u56e0\u2f26\u7b49\u4e8e2\u6216-2&#xff0c;\u66f4\u65b0\u524d\u66f4\u65b0\u4e2dparent\u7684\u5e73\u8861\u56e0\u2f26\u53d8\u5316\u4e3a1-&gt;2\u6216\u8005-1-&gt;-2&#xff0c;\u8bf4 \u660e\u66f4\u65b0\u524dparent\u5b50\u6811\u2f00\u8fb9\u9ad8\u2f00\u8fb9\u4f4e&#xff0c;\u65b0\u589e\u7684\u63d2\u5165\u7ed3\u70b9\u5728\u9ad8\u7684\u90a3\u8fb9&#xff0c;parent\u6240\u5728\u7684\u5b50\u6811\u9ad8\u7684\u90a3\u8fb9\u66f4\u9ad8\u4e86&#xff0c;\u7834\u574f\u4e86\u5e73\u8861&#xff0c;parent\u6240\u5728\u7684\u5b50\u6811\u4e0d\u7b26\u5408\u5e73\u8861\u8981\u6c42&#xff0c;\u9700\u8981\u65cb\u8f6c\u5904\u7406&#xff0c;\u65cb\u8f6c\u7684\u76ee\u6807\u6709\u4e24\u4e2a&#xff1a;1\u3001\u628a parent\u5b50\u6811\u65cb\u8f6c\u5e73\u8861\u30022\u3001\u964d\u4f4eparent\u2f26\u6811\u7684\u9ad8\u5ea6&#xff0c;\u6062\u590d\u5230\u63d2\u2f0a\u7ed3\u70b9\u4ee5\u524d\u7684\u9ad8\u5ea6\u3002\u6240\u4ee5\u65cb\u8f6c\u540e\u4e5f\u4e0d \u9700\u8981\u7ee7\u7eed\u5f80\u4e0a\u66f4\u65b0&#xff0c;\u63d2\u5165\u7ed3\u675f\u3002<\/p>\n<p>\u4f8b\u5982&#xff1a;\u66f4\u65b0\u523010\u7ed3\u70b9&#xff0c;\u5e73\u8861\u56e0\u5b50\u4e3a2&#xff0c;10\u6240\u5728\u7684\u5b50\u6811\u5df2\u7ecf\u4e0d\u5e73\u8861&#xff0c;\u9700\u8981\u65cb\u8f6c\u5904\u7406<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb083715.png\" \/><\/p>\n<p>\u2022 \u4e0d\u65ad\u66f4\u65b0&#xff0c;\u66f4\u65b0\u5230\u6839&#xff0c;\u6839\u7684\u5e73\u8861\u56e0\u5b50\u662f1\u6216-1\u4e5f\u505c\u6b62\u4e86\u3002<\/p>\n<p style=\"text-align:center\">\u4f8b\u5982&#xff1a;<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb09f470.png\" \/><\/p>\n<p>\u90a3\u4e48\u63d2\u5165\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u8fd9\u6837\u5199&#xff1a;<\/p>\n<p>bool Insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nif (_root &#061;&#061; nullptr)<br \/>\n{<br \/>\n_root &#061; new Node(kv);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>Node* parent &#061; nullptr;<br \/>\nNode* cur &#061; _root;<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (cur-&gt;_kv.first &lt; kv.first)<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_right;<br \/>\n}<br \/>\nelse if (cur-&gt;_kv.first &gt; kv.first)<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_left;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn false;<br \/>\n}<br \/>\n}<br \/>\ncur &#061; new Node(kv);<br \/>\nif (parent-&gt;_kv.first &lt; kv.first)<br \/>\n{<br \/>\nparent-&gt;_right &#061; cur;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nparent-&gt;_left &#061; cur;<br \/>\n}<br \/>\ncur-&gt;_parent &#061; parent;<\/p>\n<p>while (parent)<br \/>\n{<br \/>\n        \/\/\u66f4\u65b0\u5e73\u8861\u56e0\u5b50<br \/>\nif (parent-&gt;_left &#061;&#061; cur)<br \/>\n{<br \/>\nparent-&gt;_bf&#8211;;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nparent-&gt;_bf&#043;&#043;;<br \/>\n}<br \/>\n        \/\/\u67e5\u627e\u66f4\u65b0\u5e73\u8861\u56e0\u5b50\u540e&#xff0c;\u8981\u65cb\u8f6c\u7684\u8282\u70b9<br \/>\nif (parent-&gt;_bf &#061;&#061; 0)<br \/>\n{<br \/>\nbreak;<br \/>\n}<br \/>\nelse if (parent-&gt;_bf &#061;&#061; 1 || parent-&gt;_bf &#061;&#061; -1)<br \/>\n{<br \/>\ncur &#061; parent;<br \/>\nparent &#061; parent-&gt;_parent;<br \/>\n}<br \/>\nelse if (parent-&gt;_bf &#061;&#061; 2 || parent-&gt;_bf &#061;&#061; -2)<br \/>\n{<br \/>\nif (parent-&gt;_bf &#061;&#061; -2 &amp;&amp; cur-&gt;_bf &#061;&#061; -1)<br \/>\n{<br \/>\nRotate_Right(parent);<br \/>\n}<br \/>\nelse if (parent-&gt;_bf &#061;&#061; 2 &amp;&amp; cur-&gt;_bf &#061;&#061; 1)<br \/>\n{<br \/>\nRotate_Left(parent);<br \/>\n}<br \/>\nelse if (parent-&gt;_bf &#061;&#061; -2 &amp;&amp; cur-&gt;_bf &#061;&#061; 1)<br \/>\n{<br \/>\nRotate_Left_Right(parent);<br \/>\n}<br \/>\nelse if (parent-&gt;_bf &#061;&#061; 2 &amp;&amp; cur-&gt;_bf &#061;&#061; -1)<br \/>\n{<br \/>\nRotate_Right_Left(parent);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nassert(false);<br \/>\n}<br \/>\nbreak;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nassert(false);<br \/>\n}<br \/>\n}<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>\u89e3\u91ca&#xff1a;\u5176\u5b9e\u524d\u9762\u7684\u4ee3\u7801\u5c31\u662f\u5bfb\u627e\u8981\u63d2\u5165\u8282\u70b9\u7684\u5730\u65b9&#xff0c;\u6ca1\u6709\u627e\u5230&#xff0c;\u5c31\u8fd4\u56defalse&#xff0c;\u627e\u5230\u4e86&#xff0c;\u5c31\u63d2\u5165\u8282\u70b9&#xff0c;\u800ccur-&gt;_parent &#061; parent;\u5c31\u7528\u4e8e\u6307\u5411cur\u81ea\u5df1\u7684\u7236\u4eb2\u8282\u70b9&#xff0c;\u7136\u540e\u5728parent\u5faa\u73af\u4e2d&#xff0c;\u5148\u66f4\u65b0\u5e73\u8861\u56e0\u5b50&#xff0c;\u518d\u5224\u65ad\u66f4\u65b0\u540e\u7684\u5e73\u8861\u56e0\u5b50\u662f\u5426\u6ee1\u8db3\u66f4\u65b0\u505c\u6b62\u6761\u4ef6&#xff0c;\u6ee1\u8db3\u505c\u6b62\u6761\u4ef6\u5c31\u505c\u6b62\u66f4\u65b0&#xff0c;\u4e0d\u6ee1\u8db3\u505c\u6b62\u6761\u4ef6\u5c31\u7ee7\u7eed\u5411\u4e0a\u66f4\u65b0&#xff0c;\u5982\u679c\u9047\u5230parent-&gt;_bf\u7684\u7edd\u5bf9\u503c\u5927\u4e8e1&#xff0c;\u5c31\u9700\u8981\u65cb\u8f6c&#xff08;Rotate&#xff09;\u3002<\/p>\n<h3 style=\"background-color:transparent\">2.\u65cb\u8f6c<\/h3>\n<p>\u89c4\u5219&#xff1a;<\/p>\n<p>&lt;1&gt;. \u4fdd\u6301\u641c\u7d22\u6811\u7684\u89c4\u5219 <\/p>\n<p>&lt;2&gt;. \u8ba9\u65cb\u8f6c\u7684\u6811\u4ece\u4e0d\u6ee1\u8db3\u53d8\u5e73\u8861&#xff0c;\u5176\u6b21\u964d\u4f4e\u65cb\u8f6c\u6811\u7684\u9ad8\u5ea6<\/p>\n<p>\u65cb\u8f6c\u603b\u5171\u5206\u4e3a\u56db\u79cd&#xff0c;\u5de6\u5355\u65cb\/\u53f3\u5355\u65cb\/\u5de6\u53f3\u53cc\u65cb\/\u53f3\u5de6\u53cc\u65cb\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;\u4e0b\u9762\u7684\u56fe\u4e2d&#xff0c;\u6709\u4e9b\u7ed3\u70b9\u6211\u4eec\u7ed9\u7684\u662f\u5177\u4f53\u503c&#xff0c;\u598210\u548c5\u7b49\u7ed3\u70b9&#xff0c;\u8fd9\u91cc\u662f\u4e3a\u4e86\u65b9\u4fbf\u8bb2\u89e3&#xff0c;\u5b9e\u9645\u4e2d\u662f\u4ec0 \u4e48\u503c\u90fd\u53ef\u4ee5&#xff0c;\u53ea\u8981\u5927\u5c0f\u5173\u7cfb\u7b26\u5408\u641c\u7d22\u6811\u7684\u6027\u8d28\u5373\u53ef\u3002<\/p>\n<h2 style=\"background-color:transparent\">2.1.\u53f3\u5355\u65cb<\/h2>\n<p>\u2022 \u672c\u56fe1\u5c55\u793a\u7684\u662f10\u4e3a\u6839\u7684\u6811&#xff0c;\u6709a\/b\/c\u62bd\u8c61\u4e3a\u4e09\u68f5\u9ad8\u5ea6\u4e3ah\u7684\u5b50\u6811(h&gt;&#061;0)&#xff0c;a\/b\/c\u5747\u7b26\u5408AVL\u6811\u7684\u8981 \u6c42\u300210\u53ef\u80fd\u662f\u6574\u68f5\u6811\u7684\u6839&#xff0c;\u4e5f\u53ef\u80fd\u662f\u2f00\u4e2a\u6574\u68f5\u6811\u4e2d\u5c40\u90e8\u7684\u5b50\u6811\u7684\u6839\u3002\u8fd9\u91cca\/b\/c\u662f\u9ad8\u5ea6\u4e3ah\u7684\u2f26\u6811&#xff0c; \u662f\u2f00\u79cd\u6982\u62ec\u62bd\u8c61\u8868\u793a&#xff0c;\u4ed6\u4ee3\u8868\u4e86\u6240\u6709\u53f3\u5355\u65cb\u7684\u573a\u666f&#xff0c;\u5b9e\u9645\u53f3\u5355\u65cb\u5f62\u6001\u6709\u5f88\u591a\u79cd&#xff0c;\u5177\u4f53\u56fe2\/\u56fe3\/\u56fe4\/ \u56fe5\u8fdb\u2f8f\u4e86\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<p>\u2022 \u5728a\u5b50\u6811\u4e2d\u63d2\u5165\u2f00\u4e2a\u65b0\u7ed3\u70b9&#xff0c;\u5bfc\u81f4a\u5b50\u6811\u7684\u9ad8\u5ea6\u4eceh\u53d8\u6210h&#043;1&#xff0c;\u4e0d\u65ad\u5411\u4e0a\u66f4\u65b0\u5e73\u8861\u56e0\u2f26&#xff0c;\u5bfc\u81f410\u7684\u5e73\u8861\u56e0\u5b50\u4ece-1\u53d8\u6210-2&#xff0c;10\u4e3a\u6839\u7684\u6811\u5de6\u53f3\u9ad8\u5ea6\u5dee\u8d85\u8fc71&#xff0c;\u8fdd\u53cd\u5e73\u8861\u89c4\u5219\u300210\u4e3a\u6839\u7684\u6811\u5de6\u8fb9\u592a\u9ad8\u4e86&#xff0c;\u9700\u8981 \u5f80\u53f3\u8fb9\u65cb\u8f6c&#xff0c;\u63a7\u5236\u4e24\u68f5\u6811\u7684\u5e73\u8861\u3002 <\/p>\n<p>\u2022 \u65cb\u8f6c\u6838\u5fc3\u6b65\u9aa4&#xff0c;\u56e0\u4e3a5&lt;b\u5b50\u6811\u7684\u503c&lt;10&#xff0c;\u4e3a\u5c06b\u53d8\u621010\u7684\u5de6\u5b50\u6811&#xff0c;10\u53d8\u62105\u7684\u53f3\u5b50\u6811&#xff0c;5\u53d8\u6210\u8fd9\u68f5\u6811\u65b0 \u7684\u6839&#xff0c;\u7b26\u5408\u641c\u7d22\u6811\u7684\u89c4\u5219&#xff0c;\u63a7\u5236\u4e86\u5e73\u8861&#xff0c;\u540c\u65f6\u8fd9\u68f5\u7684\u9ad8\u5ea6\u6062\u590d\u5230\u4e86\u63d2\u5165\u4e4b\u524d\u7684h&#043;2&#xff0c;\u7b26\u5408\u65cb\u8f6c\u539f \u5219\u3002\u5982\u679c\u63d2\u5165\u4e4b\u524d10\u6574\u68f5\u6811\u7684\u2f00\u4e2a\u5c40\u90e8\u5b50\u6811&#xff0c;\u65cb\u8f6c\u540e\u4e0d\u4f1a\u518d\u5f71\u54cd\u4e0a\u4e00\u5c42&#xff0c;\u63d2\u5165\u7ed3\u675f\u4e86\u3002<\/p>\n<p style=\"text-align:center\">\u4f8b\u5982&#xff1a;\u8fd9\u4e2a\u62bd\u8c61\u4f8b\u5b50<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb0c20d3.png\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;\u5728\u8fd9\u4e2a\u56fe\u4e2d&#xff0c;a,b,c\u7684\u9ad8\u5ea6\u5fc5\u987b\u5b58\u5728\u4e14\u76f8\u7b49&#xff0c;\u5982\u679cb\u7684\u9ad8\u5ea6\u6bd4a\u5c0f1&#xff0c;\u5c31\u4f1a\u5bfc\u81f4\u5728a\u5904\u63d2\u5165\u65f6&#xff0c;5\u7684\u5e73\u8861\u56e0\u5b50\u53d8\u4e3a-2&#xff0c;\u90a3\u4e48\u8981\u65cb\u8f6c\u7684\u5c31\u4e0d\u662f10\u8fd9\u4e2a\u8282\u70b9&#xff0c;\u5982\u679cc\u7684\u9ad8\u5ea6\u6bd4a\u548cb\u5c0f1&#xff0c;\u5c31\u4f1a\u5bfc\u81f4\u63d2\u5165\u524d\u5c31\u4e0d\u6ee1\u8db3AVL\u6811&#xff0c;\u6240\u4ee5\u4e5f\u4e0d\u6ee1\u8db3\u63d2\u5165\u6761\u4ef6&#xff0c;\u6ee1\u8db3a,b,c\u7684\u9ad8\u5ea6\u5fc5\u987b\u5b58\u5728&#xff08;\u9ad8\u5ea6&gt;&#061;0&#xff09;\u4e14\u76f8\u7b49\u540e&#xff0c;\u6211\u4eec\u57285&#xff08;parent&#xff09;\u7684\u53f3\u8fb9a\u63d2\u5165\u4e00\u4e2a\u8282\u70b9&#xff0c;\u4f1a\u5bfc\u81f45\u7684\u5e73\u8861\u56e0\u5b50\u75310-&gt;-1,parent\u5411\u4e0a\u66f4\u65b0&#xff0c;\u6b64\u65f610\u8fd9\u4e2aparent\u5e73\u8861\u56e0\u5b50\u53d8\u4e3a-2&#xff0c;\u90a3\u4e48\u6211\u4eec\u5c31\u8fdb\u884c\u5bf910\u8fdb\u884c\u53f3\u5355\u65cb&#xff0c;\u964d\u4f4e10\u7684\u9ad8\u5ea6&#xff0c;\u5c31\u53ef\u4ee5\u5c06\u9ad8\u5ea6\u4e3ah&#043;1\u7684a\u7ed9subL&#xff0c;subLR\u53d8\u4e3aparent\u7684\u5de6\u8fb9&#xff0c;\u6b64\u65f6subL\u4e0eparent\u7684\u5de6\u53f3\u4e24\u8fb9\u9ad8\u5ea6\u5747\u76f8\u7b49&#xff0c;\u5e73\u8861\u56e0\u5b50\u4e5f\u5c31\u53d8\u4e3a\u4e860\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u5177\u4f53\u7684\u4f8b\u5b50&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb0f2eb6.png\" \/><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001129-699f8fb126a35.png\" \/><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001129-699f8fb15b64b.png\" \/><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001129-699f8fb18af50.png\" \/><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0a\u7684\u4f8b\u5b50&#xff0c;\u4e86\u89e3\u53f3\u5355\u65cb\u7684\u62bd\u8c61\u5b9e\u4f8b\u3002<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>void Rotate_Right(Node* parent)<br \/>\n{<br \/>\nNode* subL &#061; parent-&gt;_left;<br \/>\nNode* subLR &#061; subL-&gt;_right;<br \/>\nNode* Pparent &#061; parent-&gt;_parent;<\/p>\n<p>subL-&gt;_right &#061; parent;<br \/>\nparent-&gt;_left &#061; subLR;<br \/>\nparent-&gt;_parent &#061; subL;<br \/>\nif (subLR)<br \/>\n{<br \/>\nsubLR-&gt;_parent &#061; parent;<br \/>\n}<\/p>\n<p>\/\/parent\u4e3a\u6839\u8282\u70b9\u7684\u60c5\u51b5<br \/>\nif (_root &#061;&#061; parent)<br \/>\n{<br \/>\n_root &#061; subL;<br \/>\nsubL-&gt;_parent &#061; nullptr;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (Pparent-&gt;_left &#061;&#061; parent)<br \/>\n{<br \/>\nPparent-&gt;_left &#061; subL;<br \/>\n}<br \/>\n\/\/Pparent-&gt;_right &#061;&#061; parent<br \/>\nelse<br \/>\n{<br \/>\nPparent-&gt;_right &#061; subL;<br \/>\n}<br \/>\nsubL-&gt;_parent &#061; Pparent;<br \/>\n}<\/p>\n<p>\/\/\u66f4\u65b0\u5e73\u8861\u56e0\u5b50<br \/>\nsubL-&gt;_bf &#061; parent-&gt;_bf &#061; 0;<br \/>\n}<\/p>\n<p>\u89e3\u91ca&#xff1a;\u6211\u4eec\u5148\u5b9a\u4e49subL\u4e0esubLR&#xff0c;\u5206\u522b\u4e3aparent\u7684\u5de6\u8fb9&#xff0c;subL\u7684\u53f3\u8fb9&#xff0c;\u6211\u4eec\u4e0d\u5149\u8981\u8981\u6539\u53d8subL\u7684\u53f3\u6307\u5411parent&#xff0c;parent\u7684\u5de6\u6307\u5411subLR&#xff0c;\u8fd8\u8981\u8003\u8651subLR\u4e0d\u4e3anullptr\u65f6&#xff0c;\u4f7fsubLR\u7684_parent\u6307\u5411parent&#xff0c;parent\u7684_parent\u6307\u5411subL&#xff0c;\u800c\u4e14\u8fd8\u8981\u8003\u8651parent\u4e3a_root\u65f6&#xff0c;\u6539\u53d8_root\u4e0esubL\u7684parent&#xff0c;\u4ee5\u53caparent\u4e0d\u4e3a_root\u65f6&#xff0c;\u8bb0\u5f55\u4e00\u4e2aPparent\u4e3aparent\u7684\u7236\u4eb2\u8282\u70b9&#xff0c;\u4f7fPParent\u7684\u5de6\u8fb9\/\u53f3\u8fb9\u6307\u5411subL&#xff0c;subL\u7684_parent\u6307\u5411Pparent&#xff0c;\u6700\u540e\u6539\u53d8subL-&gt;_bf \u4e0e\u00a0parent-&gt;_bf \u7684\u5e73\u8861\u56e0\u5b50\u3002<\/p>\n<h3>2.2.\u5de6\u5355\u65cb<\/h3>\n<p id=\"p-rc_fa7c0f1c2594b6ce-19\">\u672c\u56fe6\u5c55\u793a\u7684\u662f10\u4e3a\u6839\u7684\u6811&#xff0c;\u6709a\/b\/c\u62bd\u8c61\u4e3a\u4e09\u68f5\u9ad8\u5ea6\u4e3ah\u7684\u5b50\u6811&#xff08;h&gt;&#061;0&#xff09;&#xff0c;a\/b\/c\u5747\u7b26\u5408AVL\u6811\u7684\u8981\u6c42\u300210\u53ef\u80fd\u662f\u6574\u68f5\u6811\u7684\u6839&#xff0c;\u4e5f\u53ef\u80fd\u662f\u2f00\u4e2a\u6574\u68f5\u6811\u4e2d\u5c40\u90e8\u7684\u2f26\u6811\u7684\u6839\u3002\u8fd9\u2fa5a\/b\/c\u662f\u2fbc\u5ea6\u4e3ah\u7684\u2f26\u6811&#xff0c; \u662f\u2f00\u79cd\u6982\u62ec\u62bd\u8c61\u8868\u2f70&#xff0c;\u4ed6\u4ee3\u8868\u4e86\u6240\u6709\u53f3\u5355\u65cb\u7684\u573a\u666f&#xff0c;\u5b9e\u9645\u53f3\u5355\u65cb\u5f62\u6001\u6709\u5f88\u591a\u79cd&#xff0c;\u5177\u4f53\u8ddf\u4e0a\u2faf\u5de6\u65cb\u7c7b \u4f3c\u3002<\/p>\n<p id=\"p-rc_fa7c0f1c2594b6ce-20\">\u2022 \u5728a\u5b50\u6811\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u7ed3\u70b9&#xff0c;\u5bfc\u81f4a\u5b50\u6811\u7684\u9ad8\u5ea6\u4eceh\u53d8\u6210h&#043;1&#xff0c;\u4e0d\u65ad\u5411\u4e0a\u66f4\u65b0\u5e73\u8861\u56e0\u5b50&#xff0c;\u5bfc\u81f410\u7684\u5e73\u8861\u56e0\u5b50\u4ece1\u53d8\u62102,10\u4e3a\u6839\u7684\u6811\u5de6\u53f3\u9ad8\u5ea6\u5dee\u8d85\u8fc71&#xff0c;\u8fdd\u53cd\u5e73\u8861\u89c4\u5219\u300210\u4e3a\u6839\u7684\u6811\u53f3\u8fb9\u592a\u2fbc\u4e86&#xff0c;\u9700\u8981\u5f80 \u5de6\u8fb9\u65cb\u8f6c&#xff0c;\u63a7\u5236\u4e24\u68f5\u6811\u7684\u5e73\u8861\u3002<\/p>\n<p>\u2022 \u65cb\u8f6c\u6838\u2f3c\u6b65\u9aa4&#xff0c;\u56e0\u4e3a10&lt;b\u5b50\u6811\u7684\u503c&lt;15&#xff0c;\u5c06b\u53d8\u621010\u7684\u53f3\u2f26\u6811&#xff0c;10\u53d8\u621015\u7684\u5de6\u2f26\u6811&#xff0c;15\u53d8\u6210\u8fd9\u68f5 \u6811\u65b0\u7684\u6839&#xff0c;\u7b26\u5408\u641c\u7d22\u6811\u7684\u89c4\u5219&#xff0c;\u63a7\u5236\u4e86\u5e73\u8861&#xff0c;\u540c\u65f6\u8fd9\u68f5\u7684\u2fbc\u5ea6\u6062\u590d\u5230\u4e86\u63d2\u2f0a\u4e4b\u524d\u7684h&#043;2&#xff0c;\u7b26\u5408\u65cb\u8f6c \u539f\u5219\u3002\u5982\u679c\u63d2\u2f0a\u4e4b\u524d10\u6574\u68f5\u6811\u7684\u2f00\u4e2a\u5c40\u90e8\u2f26\u6811&#xff0c;\u65cb\u8f6c\u540e\u4e0d\u4f1a\u518d\u5f71\u54cd\u4e0a\u2f00\u5c42&#xff0c;\u63d2\u2f0a\u7ed3\u675f\u4e86\u3002<\/p>\n<p style=\"text-align:center\">\u4f8b\u5982&#xff1a;<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001129-699f8fb1ba002.png\" \/><\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>void Rotate_Left(Node* parent)<br \/>\n{<br \/>\nNode* subR &#061; parent-&gt;_right;<br \/>\nNode* subRL &#061; subR-&gt;_left;<br \/>\nNode* Pparent &#061; parent-&gt;_parent;<\/p>\n<p>subR-&gt;_left &#061; parent;<br \/>\nparent-&gt;_right &#061; subRL;<br \/>\nparent-&gt;_parent &#061; subR;<br \/>\nif (subRL)<br \/>\n{<br \/>\nsubRL-&gt;_parent &#061; parent;<br \/>\n}<br \/>\nif (parent &#061;&#061; _root)<br \/>\n{<br \/>\n_root &#061; subR;<br \/>\nsubR-&gt;_parent &#061; nullptr;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (Pparent-&gt;_left &#061;&#061; parent)<br \/>\n{<br \/>\nPparent-&gt;_left &#061; subR;<br \/>\n}<br \/>\n\/\/Pparent-&gt;_right &#061;&#061; parent<br \/>\nelse<br \/>\n{<br \/>\nPparent-&gt;_right &#061; subR;<br \/>\n}<br \/>\nsubR-&gt;_parent &#061; Pparent;<br \/>\n}<\/p>\n<p>\/\/\u66f4\u65b0\u5e73\u8861\u56e0\u5b50<br \/>\nsubR-&gt;_bf &#061; parent-&gt;_bf &#061; 0;<br \/>\n}<\/p>\n<h4>2.3.\u5de6\u53f3\u53cc\u65cb<\/h4>\n<p>\u901a\u8fc7\u56fe1\u548c\u56fe2\u53ef\u4ee5\u770b\u5230&#xff0c;\u5de6\u8fb9\u2fbc\u65f6&#xff0c;\u5982\u679c\u63d2\u2f0a\u4f4d\u7f6e\u4e0d\u662f\u5728a\u2f26\u6811&#xff0c;\u2f7d\u662f\u63d2\u2f0a\u5728b\u2f26\u6811&#xff0c;b\u2f26\u6811\u2fbc\u5ea6\u4eceh\u53d8 \u6210h&#043;1&#xff0c;\u5f15\u53d1\u65cb\u8f6c&#xff0c;\u53f3\u5355\u65cb\u2f46\u6cd5\u89e3\u51b3\u95ee\u9898&#xff0c;\u53f3\u5355\u65cb\u540e&#xff0c;\u6211\u4eec\u7684\u6811\u4f9d\u65e7\u4e0d\u5e73\u8861\u3002\u53f3\u5355\u65cb\u89e3\u51b3\u7684\u7eaf\u7cb9\u7684\u5de6\u8fb9 \u2fbc&#xff0c;\u4f46\u662f\u63d2\u2f0a\u5728b\u2f26\u6811\u4e2d&#xff0c;10\u4e3a\u8ddf\u7684\u2f26\u6811\u4e0d\u518d\u662f\u5355\u7eaf\u7684\u5de6\u8fb9\u2fbc&#xff0c;\u5bf9\u4e8e10\u662f\u5de6\u8fb9\u2fbc&#xff0c;\u4f46\u662f\u5bf9\u4e8e5\u662f\u53f3\u8fb9 \u2fbc&#xff0c;\u9700\u8981\u2f64\u4e24\u6b21\u65cb\u8f6c\u624d\u80fd\u89e3\u51b3&#xff0c;\u4ee55\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u2f00\u4e2a\u5de6\u5355\u65cb&#xff0c;\u4ee510\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u2f00\u4e2a\u53f3\u5355\u65cb&#xff0c;\u8fd9\u68f5\u6811 \u8fd9\u68f5\u6811\u5c31\u5e73\u8861\u4e86\u3002<\/p>\n<p style=\"text-align:center\">\u5982\u56fe1&#xff1a;<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001129-699f8fb1ddd1f.png\" \/><\/p>\n<p style=\"text-align:center\">\u5982\u56fe2&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001130-699f8fb20de5a.png\" \/><\/p>\n<p>\u2022 \u56fe1\u548c\u56fe2\u5206\u522b\u4e3a\u5de6\u53f3\u53cc\u65cb\u4e2dh&#061;&#061;0\u548ch&#061;&#061;1\u5177\u4f53\u573a\u666f\u5206\u6790&#xff0c;\u4e0b\u2faf\u6211\u4eec\u5c06a\/b\/c\u2f26\u6811\u62bd\u8c61\u4e3a\u2fbc\u5ea6h\u7684AVL \u2f26\u6811\u8fdb\u2f8f\u5206\u6790&#xff0c;\u53e6\u5916\u6211\u4eec\u9700\u8981\u628ab\u2f26\u6811\u7684\u7ec6\u8282\u8fdb\u2f00\u6b65\u5c55\u5f00\u4e3a8\u548c\u5de6\u2f26\u6811\u2fbc\u5ea6\u4e3ah-1\u7684e\u548cf\u2f26\u6811&#xff0c;\u56e0\u4e3a \u6211\u4eec\u8981\u5bf9b\u7684\u2f57\u4eb25\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u5de6\u5355\u65cb&#xff0c;\u5de6\u5355\u65cb\u9700\u8981\u52a8b\u6811\u4e2d\u7684\u5de6\u2f26\u6811\u3002b\u2f26\u6811\u4e2d\u65b0\u589e\u7ed3\u70b9\u7684\u4f4d\u7f6e \u4e0d\u540c&#xff0c;\u5e73\u8861\u56e0\u2f26\u66f4\u65b0\u7684\u7ec6\u8282\u4e5f\u4e0d\u540c&#xff0c;\u901a\u8fc7\u89c2\u5bdf8\u7684\u5e73\u8861\u56e0\u2f26\u4e0d\u540c&#xff0c;\u8fd9\u2fa5\u6211\u4eec\u8981\u5206\u4e09\u4e2a\u573a\u666f\u8ba8\u8bba\u3002\u00a0<\/p>\n<p>\u2022 \u573a\u666f1&#xff1a;h&gt;&#061;1\u65f6&#xff0c;\u65b0\u589e\u7ed3\u70b9\u63d2\u2f0a\u5728e\u2f26\u6811&#xff0c;e\u2f26\u6811\u2fbc\u5ea6\u4eceh-1\u5e76\u4e3ah\u5e76\u4e0d\u65ad\u66f4\u65b08-&gt;5-&gt;10\u5e73\u8861\u56e0\u2f26&#xff0c; \u5f15\u53d1\u65cb\u8f6c&#xff0c;\u5176\u4e2d8\u7684\u5e73\u8861\u56e0\u2f26\u4e3a-1&#xff0c;\u65cb\u8f6c\u540e8\u548c5\u5e73\u8861\u56e0\u2f26\u4e3a0&#xff0c;10\u5e73\u8861\u56e0\u2f26\u4e3a1\u3002<\/p>\n<p>\u2022 \u573a\u666f2&#xff1a;h&gt;&#061;1\u65f6&#xff0c;\u65b0\u589e\u7ed3\u70b9\u63d2\u2f0a\u5728f\u2f26\u6811&#xff0c;f\u2f26\u6811\u2fbc\u5ea6\u4eceh-1\u53d8\u4e3ah\u5e76\u4e0d\u65ad\u66f4\u65b08-&gt;5-&gt;10\u5e73\u8861\u56e0\u2f26&#xff0c;\u5f15 \u53d1\u65cb\u8f6c&#xff0c;\u5176\u4e2d8\u7684\u5e73\u8861\u56e0\u2f26\u4e3a1&#xff0c;\u65cb\u8f6c\u540e8\u548c10\u5e73\u8861\u56e0\u2f26\u4e3a0&#xff0c;5\u5e73\u8861\u56e0\u2f26\u4e3a-1\u3002<\/p>\n<p>\u2022 \u573a\u666f3&#xff1a;h&#061;&#061;0\u65f6&#xff0c;a\/b\/c\u90fd\u662f\u7a7a\u6811&#xff0c;b\u2f83\u2f30\u5c31\u662f\u2f00\u4e2a\u65b0\u589e\u7ed3\u70b9&#xff0c;\u4e0d\u65ad\u66f4\u65b05-&gt;10\u5e73\u8861\u56e0\u2f26&#xff0c;\u5f15\u53d1\u65cb \u8f6c&#xff0c;\u5176\u4e2d8\u7684\u5e73\u8861\u56e0\u2f26\u4e3a0&#xff0c;\u65cb\u8f6c\u540e8\u548c10\u548c5\u5e73\u8861\u56e0\u2f26\u5747\u4e3a0\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001130-699f8fb239c33.png\" \/><\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>void Rotate_Left_Right(Node* parent)<br \/>\n{<br \/>\nNode* subL &#061; parent-&gt;_left;<br \/>\nNode* subLR &#061; subL-&gt;_right;<br \/>\nint bf &#061; subLR-&gt;_bf;<\/p>\n<p>Rotate_Left(parent-&gt;_left);<br \/>\nRotate_Right(parent);<br \/>\nif (bf &#061;&#061; 0)<br \/>\n{<br \/>\nparent-&gt;_bf &#061; 0;<br \/>\nsubL-&gt;_bf &#061; 0;<br \/>\nsubLR-&gt;_bf &#061; 0;<br \/>\n}<br \/>\nelse if (bf &#061;&#061; -1)<br \/>\n{<br \/>\nparent-&gt;_bf &#061; 1;<br \/>\nsubL-&gt;_bf &#061; 0;<br \/>\nsubLR-&gt;_bf &#061; 0;<br \/>\n}<br \/>\nelse if (bf &#061;&#061; 1)<br \/>\n{<br \/>\nparent-&gt;_bf &#061; 0;<br \/>\nsubL-&gt;_bf &#061; -1;<br \/>\nsubLR-&gt;_bf &#061; 0;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nassert(false);<br \/>\n}<br \/>\n}<\/p>\n<h5>2.4.\u53f3\u5de6\u53cc\u65cb<\/h5>\n<p>\u2022 \u8ddf\u5de6\u53f3\u53cc\u65cb\u7c7b\u4f3c&#xff0c;\u4e0b\u2faf\u6211\u4eec\u5c06a\/b\/c\u2f26\u6811\u62bd\u8c61\u4e3a\u2fbc\u5ea6h\u7684AVL\u2f26\u6811\u8fdb\u2f8f\u5206\u6790&#xff0c;\u53e6\u5916\u6211\u4eec\u9700\u8981\u628ab\u2f26\u6811\u7684 \u7ec6\u8282\u8fdb\u2f00\u6b65\u5c55\u5f00\u4e3a12\u548c\u5de6\u2f26\u6811\u2fbc\u5ea6\u4e3ah-1\u7684e\u548cf\u2f26\u6811&#xff0c;\u56e0\u4e3a\u6211\u4eec\u8981\u5bf9b\u7684\u2f57\u4eb215\u4e3a\u65cb\u8f6c\u70b9\u8fdb\u2f8f\u53f3\u5355 \u65cb&#xff0c;\u53f3\u5355\u65cb\u9700\u8981\u52a8b\u6811\u4e2d\u7684\u53f3\u2f26\u6811\u3002b\u2f26\u6811\u4e2d\u65b0\u589e\u7ed3\u70b9\u7684\u4f4d\u7f6e\u4e0d\u540c&#xff0c;\u5e73\u8861\u56e0\u2f26\u66f4\u65b0\u7684\u7ec6\u8282\u4e5f\u4e0d\u540c&#xff0c;\u901a \u8fc7\u89c2\u5bdf12\u7684\u5e73\u8861\u56e0\u2f26\u4e0d\u540c&#xff0c;\u8fd9\u2fa5\u6211\u4eec\u8981\u5206\u4e09\u4e2a\u573a\u666f\u8ba8\u8bba\u3002<\/p>\n<p>\u2022 \u573a\u666f1&#xff1a;h&gt;&#061;1\u65f6&#xff0c;\u65b0\u589e\u7ed3\u70b9\u63d2\u2f0a\u5728e\u2f26\u6811&#xff0c;e\u2f26\u6811\u2fbc\u5ea6\u4eceh-1\u53d8\u4e3ah\u5e76\u4e0d\u65ad\u66f4\u65b012-&gt;15-&gt;10\u5e73\u8861\u56e0 \u2f26&#xff0c;\u5f15\u53d1\u65cb\u8f6c&#xff0c;\u5176\u4e2d12\u7684\u5e73\u8861\u56e0\u2f26\u4e3a-1&#xff0c;\u65cb\u8f6c\u540e10\u548c12\u5e73\u8861\u56e0\u2f26\u4e3a0&#xff0c;15\u5e73\u8861\u56e0\u2f26\u4e3a1\u3002<\/p>\n<p>\u2022 \u573a\u666f2&#xff1a;h&gt;&#061;1\u65f6&#xff0c;\u65b0\u589e\u7ed3\u70b9\u63d2\u2f0a\u5728f\u2f26\u6811&#xff0c;f\u2f26\u6811\u2fbc\u5ea6\u4eceh-1\u53d8\u4e3ah\u5e76\u4e0d\u65ad\u66f4\u65b012-&gt;15-&gt;10\u5e73\u8861\u56e0\u2f26&#xff0c; \u5f15\u53d1\u65cb\u8f6c&#xff0c;\u5176\u4e2d12\u7684\u5e73\u8861\u56e0\u2f26\u4e3a1&#xff0c;\u65cb\u8f6c\u540e15\u548c12\u5e73\u8861\u56e0\u2f26\u4e3a0&#xff0c;10\u5e73\u8861\u56e0\u2f26\u4e3a-1\u3002<\/p>\n<p>\u2022 \u573a\u666f3&#xff1a;h&#061;&#061;0\u65f6&#xff0c;a\/b\/c\u90fd\u662f\u7a7a\u6811&#xff0c;b\u2f83\u2f30\u5c31\u662f\u2f00\u4e2a\u65b0\u589e\u7ed3\u70b9&#xff0c;\u4e0d\u65ad\u66f4\u65b015-&gt;10\u5e73\u8861\u56e0\u2f26&#xff0c;\u5f15\u53d1\u65cb \u8f6c&#xff0c;\u5176\u4e2d12\u7684\u5e73\u8861\u56e0\u2f26\u4e3a0&#xff0c;\u65cb\u8f6c\u540e10\u548c12\u548c15\u5e73\u8861\u56e0\u2f26\u5747\u4e3a0\u3002<\/p>\n<p style=\"text-align:center\">\u5982\u56fe&#xff1a;<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001130-699f8fb2b685c.png\" \/><\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>void Rotate_Right_Left(Node* parent)<br \/>\n{<br \/>\nNode* subR &#061; parent-&gt;_right;<br \/>\nNode* subRL &#061; subR-&gt;_left;<br \/>\nint bf &#061; subRL-&gt;_bf;<\/p>\n<p>Rotate_Right(parent-&gt;_right);<br \/>\nRotate_Left(parent);<br \/>\nif (bf &#061;&#061; 0)<br \/>\n{<br \/>\nparent-&gt;_bf &#061; 0;<br \/>\nsubR-&gt;_bf &#061; 0;<br \/>\nsubRL-&gt;_bf &#061; 0;<br \/>\n}<br \/>\nelse if (bf &#061;&#061; 1)<br \/>\n{<br \/>\nparent-&gt;_bf &#061; -1;<br \/>\nsubR-&gt;_bf &#061; 0;<br \/>\nsubRL-&gt;_bf &#061; 0;<br \/>\n}<br \/>\nelse if (bf &#061;&#061; -1)<br \/>\n{<br \/>\nparent-&gt;_bf &#061; 0;<br \/>\nsubR-&gt;_bf &#061; 1;<br \/>\nsubRL-&gt;_bf &#061; 0;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nassert(false);<br \/>\n}<br \/>\n}<\/p>\n<h4>\u4e09.\u5176\u4ed6\u63a5\u53e3\u5b9e\u73b0<\/h4>\n<p>1.\u4e2d\u5e8f\u904d\u5386<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>void InOrder()<br \/>\n{<br \/>\n_InOrder(_root);<br \/>\n}<br \/>\nvoid _InOrder(Node* root)<br \/>\n{<br \/>\nif (root &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn;<br \/>\n}<br \/>\n_InOrder(root-&gt;_left);<br \/>\ncout &lt;&lt; root-&gt;_kv.first &lt;&lt; &#034;:&#034; &lt;&lt; root-&gt;_kv.second &lt;&lt; endl;<br \/>\n_InOrder(root-&gt;_right);<br \/>\n}<\/p>\n<p>2.\u67e5\u627e\u503c\u4e3akey\u63a5\u53e3<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>Node* Find(const K&amp; key)<br \/>\n{<br \/>\nreturn _Find(key);<br \/>\n}<br \/>\nNode* _Find(const K&amp; key)<br \/>\n{<br \/>\nNode* cur &#061; _root;<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (cur-&gt;_kv.first &lt; key)<br \/>\n{<br \/>\ncur &#061; cur-&gt;_right;<br \/>\n}<br \/>\nelse if (cur-&gt;_kv.first &gt; key)<br \/>\n{<br \/>\ncur &#061; cur-&gt;_left;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn cur;<br \/>\n}<br \/>\n}<br \/>\nreturn nullptr;<br \/>\n}<\/p>\n<p>3.\u8ba1\u7b97\u6811\u7684\u5bb9\u91cf<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>int Size()<br \/>\n{<br \/>\nreturn _Size(_root);<br \/>\n}<br \/>\nint _Size(Node* root)<br \/>\n{<br \/>\nif (root &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn 0;<br \/>\n}<br \/>\nreturn _Size(root-&gt;_left) &#043; _Size(root-&gt;_right) &#043; 1;<br \/>\n}<\/p>\n<p>4.\u8ba1\u7b97\u6811\u7684\u9ad8\u5ea6<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>int Height()<br \/>\n{<br \/>\nreturn _Height(_root);<br \/>\n}<br \/>\nint _Height(Node* root)<br \/>\n{<br \/>\nif (root &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn 0;<br \/>\n}<br \/>\nint Height_Left &#061; _Height(root-&gt;_left);<br \/>\nint Height_Right &#061; _Height(root-&gt;_right);<br \/>\nreturn Height_Left &gt; Height_Right ? Height_Left &#043; 1 : Height_Right &#043; 1;<br \/>\n}<\/p>\n<p>5.\u68c0\u67e5\u662f\u5426\u4e3aAVL\u6811<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>bool IsBalanceTree()<br \/>\n{<br \/>\nreturn _IsBalanceTree(_root);<br \/>\n}<br \/>\nbool _IsBalanceTree(Node* root)<br \/>\n{<br \/>\nif (root &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn true;<br \/>\n}<br \/>\nint Height_Left &#061; _Height(root-&gt;_left);<br \/>\nint Height_Right &#061; _Height(root-&gt;_right);<br \/>\nint diff &#061; Height_Right &#8211; Height_Left;<\/p>\n<p>if (abs(diff) &gt;&#061; 2)<br \/>\n{<br \/>\ncout &lt;&lt; root-&gt;_kv.first &lt;&lt;&#034;:&#034; &lt;&lt; &#034;\u9ad8\u5ea6\u5dee\u5f02\u5e38&#034; &lt;&lt; endl;<br \/>\nreturn false;<br \/>\n}<br \/>\nif (root-&gt;_bf !&#061; diff)<br \/>\n{<br \/>\ncout &lt;&lt; root-&gt;_kv.first &lt;&lt; &#034;:&#034; &lt;&lt; &#034;\u5e73\u8861\u56e0\u5b50\u5f02\u5e38&#034; &lt;&lt; endl;<br \/>\nreturn false;<br \/>\n}<br \/>\nreturn _IsBalanceTree(root-&gt;_left) &amp;&amp; _IsBalanceTree(root-&gt;_right);<br \/>\n}<\/p>\n<p>6.\u6790\u6784\u51fd\u6570<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>~AVLTree()<br \/>\n{<br \/>\n_Destroy(_root);<br \/>\n_root &#061; nullptr;<br \/>\n}<br \/>\nvoid _Destroy(Node* root)<br \/>\n{<br \/>\nif (root &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn;<br \/>\n}<br \/>\n_Destroy(root-&gt;_left);<br \/>\n_Destroy(root-&gt;_right);<br \/>\ndelete root;<br \/>\nroot &#061; nullptr;<br \/>\n}<\/p>\n<p>\u6ce8\u610f&#xff1a;\u5176\u4ed6\u63a5\u53e3\u5b9e\u73b0\u65f6&#xff0c;\u5b9e\u73b0\u4ee3\u7801\u5e94\u6539\u653e\u5728private,\u5426\u5219\u53ef\u80fd\u4f1a\u56e0\u4e3a\u8c03\u7528\u63a5\u53e3\u65f6&#xff0c;\u8c03\u7528\u4e86\u542b\u6709_root\u7684\u63a5\u53e3&#xff0c;\u56e0\u4e3a_root\u4e3aprivate\u800c\u62a5\u9519\u3002<\/p>\n<h5>\u56db.AVL\u6811\u7684\u6d4b\u8bd5<\/h5>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>#include&#034;AVLTree.h&#034;<br \/>\nvoid TestAVLTree1()<br \/>\n{<br \/>\n    kong_AVLTree::AVLTree&lt;int, int&gt; t;<br \/>\n    \/\/ \u5e38\u89c4\u7684\u6d4b\u8bd5\u2f64\u4f8b<br \/>\n    \/\/int a[] &#061; { 16, 3, 7, 11, 9, 26, 18, 14, 15 };<br \/>\n    \/\/\u7279\u6b8a\u7684\u5e26\u6709\u53cc\u65cb\u573a\u666f\u7684\u6d4b\u8bd5\u2f64\u4f8b<br \/>\n    int a[] &#061; { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };<br \/>\n    for (auto e : a)<br \/>\n    {<br \/>\n        t.Insert({ e, e });<br \/>\n    }<br \/>\n    t.InOrder();<br \/>\n    cout &lt;&lt; t.IsBalanceTree() &lt;&lt; endl;<br \/>\n}<br \/>\nvoid TestAVLTree2()<br \/>\n{<br \/>\n    const int N &#061; 100000;<br \/>\n    vector&lt;int&gt; v;<br \/>\n    v.reserve(N);<br \/>\n    srand(time(0));<br \/>\n    for (size_t i &#061; 0; i &lt; N; i&#043;&#043;)<br \/>\n    {<br \/>\n        v.push_back(rand() &#043; i);<br \/>\n    }<br \/>\n    size_t begin2 &#061; clock();<br \/>\n    kong_AVLTree::AVLTree&lt;int, int&gt; t;<br \/>\n    for (auto e : v)<br \/>\n    {<br \/>\n        t.Insert(make_pair(e, e));<br \/>\n    }<br \/>\n    size_t end2 &#061; clock();<br \/>\n    cout &lt;&lt; &#034;Insert:&#034; &lt;&lt; end2 &#8211; begin2 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;Height:&#034; &lt;&lt; t.Height() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;Size:&#034; &lt;&lt; t.Size() &lt;&lt; endl;<br \/>\n    size_t begin1 &#061; clock();<br \/>\n    \/\/ \u786e\u5b9a\u5728\u7684\u503c<\/p>\n<p>   \/*for (auto e : v)<br \/>\n   {<br \/>\n      t.Find(e);<br \/>\n    }*\/<br \/>\n    \/\/ \u968f\u673a\u503c<br \/>\n    for (size_t i &#061; 0; i &lt; N; i&#043;&#043;)<br \/>\n    {<br \/>\n        t.Find((rand() &#043; i));<br \/>\n    }<br \/>\n    size_t end1 &#061; clock();<br \/>\n    cout &lt;&lt; &#034;Find:&#034; &lt;&lt; end1 &#8211; begin1 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u662f\u5426\u4e3a\u5e73\u8861\u6811:&#034;;<br \/>\n    cout &lt;&lt; t.IsBalanceTree() &lt;&lt; endl;<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n    TestAVLTree1();<br \/>\n    TestAVLTree2();<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u4ee3\u7801\u6d4b\u8bd5\u8fd9\u4e2a\u6811\u662f\u5426\u4e3aAVL\u6811&#xff0c;\u4ee5\u53ca\u6d4b\u8bd5AVL\u6811\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.AVL\u6811\u7684\u6982\u5ff5\u4e0e\u7ed3\u67841.AVL\u6811\u7684\u6982\u5ff51.1.\u4e3a\u4ec0\u4e48\u7528AVL\u6811\u539f\u56e0&#xff1a;\u666e\u901a\u7684\u4e8c\u53c9\u641c\u7d22\u6811&#xff08;BST&#xff09;\u5728\u63d2\u5165\u6709\u5e8f\u6570\u636e&#xff08;\u5982 1&#xff0c; 2&#xff0c; 3&#xff0c; 4&#xff0c; 5&#xff09;\u65f6&#xff0c;\u4f1a\u53d8\u6210\u4e00\u4e2a\u53ea\u6709\u53f3\u5b69\u5b50\u7684\u957f\u94fe&#xff0c;\u6b64\u65f6\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u6548\u7387\u5c31\u4eceO(log N)\u9000\u5316\u5230O(N)&#xff0c;\u4f46\u662fAVL \u6811&#xff1a;\u901a\u8fc7\u65cb\u8f6c\u673a\u5236&#xff0c;\u5f3a\u5236\u4fdd\u8bc1\u5de6\u53f3\u5b50\u6811<\/p>\n","protected":false},"author":2,"featured_media":78101,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,1813],"topic":[],"class_list":["post-78117","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-1813"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AVL\u6811\u7684\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/78117.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AVL\u6811\u7684\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00.AVL\u6811\u7684\u6982\u5ff5\u4e0e\u7ed3\u67841.AVL\u6811\u7684\u6982\u5ff51.1.\u4e3a\u4ec0\u4e48\u7528AVL\u6811\u539f\u56e0&#xff1a;\u666e\u901a\u7684\u4e8c\u53c9\u641c\u7d22\u6811&#xff08;BST&#xff09;\u5728\u63d2\u5165\u6709\u5e8f\u6570\u636e&#xff08;\u5982 1&#xff0c; 2&#xff0c; 3&#xff0c; 4&#xff0c; 5&#xff09;\u65f6&#xff0c;\u4f1a\u53d8\u6210\u4e00\u4e2a\u53ea\u6709\u53f3\u5b69\u5b50\u7684\u957f\u94fe&#xff0c;\u6b64\u65f6\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u6548\u7387\u5c31\u4eceO(log N)\u9000\u5316\u5230O(N)&#xff0c;\u4f46\u662fAVL \u6811&#xff1a;\u901a\u8fc7\u65cb\u8f6c\u673a\u5236&#xff0c;\u5f3a\u5236\u4fdd\u8bc1\u5de6\u53f3\u5b50\u6811\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78117.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-26T00:11:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb0100aa.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78117.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78117.html\",\"name\":\"AVL\u6811\u7684\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-26T00:11:32+00:00\",\"dateModified\":\"2026-02-26T00:11:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78117.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78117.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78117.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AVL\u6811\u7684\u5b9e\u73b0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AVL\u6811\u7684\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/78117.html","og_locale":"zh_CN","og_type":"article","og_title":"AVL\u6811\u7684\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00.AVL\u6811\u7684\u6982\u5ff5\u4e0e\u7ed3\u67841.AVL\u6811\u7684\u6982\u5ff51.1.\u4e3a\u4ec0\u4e48\u7528AVL\u6811\u539f\u56e0&#xff1a;\u666e\u901a\u7684\u4e8c\u53c9\u641c\u7d22\u6811&#xff08;BST&#xff09;\u5728\u63d2\u5165\u6709\u5e8f\u6570\u636e&#xff08;\u5982 1&#xff0c; 2&#xff0c; 3&#xff0c; 4&#xff0c; 5&#xff09;\u65f6&#xff0c;\u4f1a\u53d8\u6210\u4e00\u4e2a\u53ea\u6709\u53f3\u5b69\u5b50\u7684\u957f\u94fe&#xff0c;\u6b64\u65f6\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u6548\u7387\u5c31\u4eceO(log N)\u9000\u5316\u5230O(N)&#xff0c;\u4f46\u662fAVL \u6811&#xff1a;\u901a\u8fc7\u65cb\u8f6c\u673a\u5236&#xff0c;\u5f3a\u5236\u4fdd\u8bc1\u5de6\u53f3\u5b50\u6811","og_url":"https:\/\/www.wsisp.com\/helps\/78117.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-26T00:11:32+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226001128-699f8fb0100aa.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78117.html","url":"https:\/\/www.wsisp.com\/helps\/78117.html","name":"AVL\u6811\u7684\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-26T00:11:32+00:00","dateModified":"2026-02-26T00:11:32+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78117.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78117.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78117.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"AVL\u6811\u7684\u5b9e\u73b0"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=78117"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78117\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/78101"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78117"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}