{"id":94,"date":"2025-06-28T16:10:02","date_gmt":"2025-06-28T08:10:02","guid":{"rendered":"http:\/\/120.76.99.214\/?p=94"},"modified":"2025-11-24T16:42:54","modified_gmt":"2025-11-24T08:42:54","slug":"%e4%ba%8c%e5%8f%89%e6%90%9c%e7%b4%a2%e6%a0%91","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=94","title":{"rendered":"\u4e8c\u53c9\u641c\u7d22\u6811"},"content":{"rendered":"\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\uff08Binary Rearch Tree\uff09\u662f\u4e8c\u53c9\u6811\u7684\u4e00\u79cd\uff0c\u4ed6\u9700\u8981\u6ee1\u8db3\u6761\u4ef6\u5de6\u5b50\u6811\u7684key\u5927\u4e8e\u53f3\u5b50\u6811\u7684key\u7684\u6761\u4ef6.\u9996\u5148\u6211\u4eec\u786e\u5b9a\u4e00\u4e2a\u6570\u7ec4arr[6]={3,6,2,1,8,4},\u5c06\u5176\u63d2\u5165\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5982\u56fe\u6240\u793a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"454\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/06\/image-2.png\" alt=\"\" class=\"wp-image-95\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/06\/image-2.png 686w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/06\/image-2-300x199.png 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/figure>\n\n\n\n<p>\u5148\u63d2\u51653\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u540e\u9762\u63d2\u51656,6>3\u4f5c\u4e3a3\u7684\u5b50\u8282\u70b9\uff0c\u540e\u9762\u63d2\u51652,2<3\u4f5c\u4e3a3\u7684\u53f3\u5b50\u8282\u70b9\uff0c\u4f9d\u6b21\u7c7b\u63a8\uff0c\u4e00\u9897\u4e8c\u53c9\u641c\u7d22\u6811\u5c31\u5b8c\u6210\u4e86\uff01<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4ee3\u7801\u5b9e\u73b0\uff1a<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C++:\ntemplate<class K>\nstruct BSTNode {\n\tK _key;\n\tBSTNode<K>* _left;\n\tBSTNode<K>* _right;\n\tBSTNode(const K& key) :_key(key)\n\t\t, _left(nullptr), _right(nullptr) {\n\t}\n};\ntemplate<class K>\nclass BSTree {\n\ttypedef BSTNode<K> Node;\nprivate:\n\t\n\tNode* _root = nullptr;\npublic:\n\tbool Insert(const K& key) {\n\t\tif (_root == nullptr) {\n\t\t\t_root = new Node(key);\n\t\t\treturn true;\n\t\t}\n\t\telse {\n\t\t\tNode* parent = nullptr;\n\t\t\tNode* cur = _root;\n\t\t\twhile (cur != NULL) {\n\t\t\t\tif (cur->_key < key) {\n\t\t\t\t\tparent = cur;\n\t\t\t\t\tcur = cur->_right;\n\t\t\t\t}\n\t\t\t\telse if (cur->_key > key) {\n\t\t\t\t\tparent = cur;\n\t\t\t\t\tcur = cur->_left;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"\u9519\u8bef\uff0c\u63d2\u5165\u76f8\u7b49\u7684\u503c\" << endl;\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tNode* newNode = new Node(key);\n\t\t\tif (parent->_key > key) {\n\t\t\t\tparent->_left = newNode;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tparent->_right = newNode;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t}\n}\n\n<\/code><\/pre>\n\n\n\n<p>\u67e5\u627e\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u5f88\u7b80\u5355\uff0c\u9996\u5148\u662f\u6839\u636ekey\u7684\u503c\u6bd4\u8f83\u5927\u5c0f\uff0c\u8981\u662f\u5927\u5c31\u5f80\u53f3\u67e5\u627e\uff0c\u8981\u662f\u5c0f\u5c31\u5f80\u5de6\u67e5\u627e\uff0c\u8981\u662f\u5faa\u73af\u5230\u6700\u540e\u8fd8\u662f\u4e3a\u7a7a\u5c31\u6ca1\u6709\u627e\u5230\u8fd4\u56de\u4e3a\u7a7a\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c++:\nbool Find(const K& key) {\n\tNode* cur = _root;\n\twhile (cur) {\n\t\tif (cur->_key < key) {\n\t\t\tcur = cur->_right;\n\t\t}\n\t\telse if (cur->_key > key) {\n\t\t\tcur = cur->_left;\n\t\t}\n\t\telse {\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>\u5220\u9664\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<p>\u641c\u7d22\u4e8c\u53c9\u6811\u7684\u5220\u9664\u64cd\u4f5c\u662f\u6700\u96be\u7684\uff0c\u5b83\u5206\u4e3a\u56db\u79cd\u60c5\u51b5<\/p>\n\n\n\n<p>1.\u5220\u9664\u7684\u8282\u70b9\u7684\u5de6\u53f3\u5b50\u6811\u90fd\u4e3a\u7a7a\uff1a\u6b64\u65f6\u6b64\u8282\u70b9\u4e3a\u53f6\u5b50\u8282\u70b9\uff0c\u76f4\u63a5\u5220\u9664\u5373\u53ef<\/p>\n\n\n\n<p>2.\u5220\u9664\u7684\u8282\u70b9\u5de6\u4e3a\u7a7a\uff1a\u6b64\u65f6\u5c06\u5220\u9664\u8282\u70b9\u7684\u7236\u8282\u70b9\u6307\u5411\u5220\u9664\u8282\u70b9\u7684\u53f3\u5b50\u6811<\/p>\n\n\n\n<p>3.\u5220\u9664\u7684\u8282\u70b9\u53f3\u4e3a\u7a7a:\u6b64\u65f6\u5c06\u5220\u9664\u8282\u70b9\u7684\u7236\u8282\u70b9\u6307\u5411\u5220\u9664\u8282\u70b9\u7684\u5de6\u5b50\u6811<\/p>\n\n\n\n<p>4.\u5220\u9664\u8282\u70b9\u5de6\u53f3\u5b50\u6811\u90fd\u4e0d\u4e3a\u7a7a\uff1a\u6b64\u65f6\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u9700\u8981\u4f7f\u7528\u66ff\u4ee3\u6cd5\uff0c\u9996\u5148\u53bb\u5373\u627e\u5230\u8be5\u8282\u70b9\u7684\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u503c\/\u5de6\u5b50\u6811\u7684\u6700\u5927\u503c\uff0c\u4e24\u4e2a\u8282\u70b9\u7684\u503c\u76f8\u4e92\u4ea4\u6362\uff0c\u518d\u53bb\u5220\u9664\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u56fe\u6f14\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"439\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/06\/image-3.png\" alt=\"\" class=\"wp-image-96\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/06\/image-3.png 669w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/06\/image-3-300x197.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/figure>\n\n\n\n<p>\u5f53\u5220\u96646\u65f6\uff0c\u4e24\u8fb9\u90fd\u6709\u8282\u70b9\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u5c068\u548c6\u4ea4\u6362\u624d\u80fd\u5b9e\u73b0\u8282\u70b9\u7684\u5220\u9664\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u5b9e\u4e60\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool ERase(const K& key) {\n\tNode* parent = nullptr;\n\tNode* cur = _root;\n\twhile (cur != NULL) {\n\t\tif (cur->_key < key) {\n\t\t\tparent = cur;\n\t\t\tcur = cur->_right;\n\t\t}\n\t\telse if (cur->_key > key) {\n\t\t\tparent = cur;\n\t\t\tcur = cur->_left;\n\t\t}\n\t\telse {\n\t\t\t\/\/\u5220\u9664\n\t\t\t\/\/\u5de6\u4e3a\u7a7a\n\t\t\tif (cur->_left == nullptr) {\n\t\t\t\tif (cur == _root) {\n\t\t\t\t\t_root = cur->_right;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (parent->_left == cur) {\n\t\t\t\t\t\tparent->_left = cur->_right;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tparent->_right = cur->_right;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdelete cur;\n\t\t\t}\n\t\t\telse if (cur->_right == nullptr) {\/\/\u53f3\u4e3a\u7a7a\n\t\t\t\tif (cur == _root) {\n\t\t\t\t\t_root = cur->_left;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (parent->_right == cur) {\n\t\t\t\t\t\tparent->_right = cur->_left;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tparent->_left = cur->_left;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdelete cur;\n\t\t\t}\n\t\t\telse {\t\t\t\t\/\/\u90fd\u4e0d\u4e3a\u7a7a\n\t\t\t\tNode* replaceparent = cur;\n\t\t\t\tNode* replace = cur->_right;    \/\/\u66ff\u4ee3\u8282\u70b9 \n\t\t\t\twhile (replace->_left) {\n\t\t\t\t\treplaceparent = replace;\n\t\t\t\t\treplace = replace->_left;\n\t\t\t\t}\n\t\t\t\tcur->_key = replace->_key;\n\t\t\t\tif (replaceparent->_left == replace) {\n\t\t\t\t\treplaceparent->_left = replace->_left;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treplaceparent->_right = replace->_right;\n\t\t\t\t}\n\t\t\t\tdelete replace;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t}\n\treturn false;\n}<\/code><\/pre>\n\n\n\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff1a\u5f53\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u9700\u8981\u989d\u5916\u63d0\u51fa\u6765\uff1a\u5982<\/p>\n\n\n\n<p>                               if (cur == _root) {<br>\t\t\t\t\t_root = cur->_right;<br>\t\t\t\t  }<\/p>\n\n\n\n<p>\u800c\u4e14\u5f53\u4e24\u8fb9\u90fd\u6709\u8282\u70b9\u65f6\u6211\u4eec\u4e5f\u9700\u8981\u5206\u60c5\u51b5\uff1a\u5982\u679c\u66ff\u4ee3\u8282\u70b9\u662f\u5176\u7236\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\uff0c\u66f4\u65b0\u7236\u8282\u70b9\u7684\u5de6\u6307\u9488\uff1b\u5426\u5219\u66f4\u65b0\u7236\u8282\u70b9\u7684\u53f3\u6307\u9488\u3002<\/p>\n\n\n\n<p>\u4fbf\u5229\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<p>\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u904d\u5386\u5f88\u7b80\u5355\uff0c\u4e3b\u8981\u662f\u901a\u8fc7\u9012\u5f52\u904d\u5386\uff0c\u535a\u4e3b\u8fd9\u91cc\u5c31\u4e0d\u4e00\u4e00\u5217\u4e3e\uff0c\u5c31\u4ee5\u4e2d\u5e8f\u904d\u5386\u4e3a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tvoid Inorder(Node* root) {\n\t\tif (root == nullptr) {\n\t\t\treturn;\n\t\t}\n\t\telse {\n\t\t\tInorder(root->_left);\n\t\t\tcout << root->_key << \" \";\n\t\t\tInorder(root->_right);\n\t\t}\n\t}\n};<\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u535a\u5ba2\u5c31\u5199\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u95ee\u9898\u8bf7\u5728\u5e16\u5b50\u4e0b\u9762\u7559\u8a00\u8c22\u8c22\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u53c9\u641c\u7d22\u6811\uff08Binary Rearch Tree\uff09\u662f\u4e8c\u53c9\u6811\u7684\u4e00\u79cd\uff0c\u4ed6\u9700\u8981\u6ee1\u8db3\u6761\u4ef6\u5de6\u5b50\u6811\u7684key\u5927\u4e8e\u53f3\u5b50\u6811\u7684ke&#8230;<\/p>\n","protected":false},"author":1,"featured_media":95,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":886,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/886"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/95"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}