{"id":93,"date":"2025-06-21T22:29:45","date_gmt":"2025-06-21T14:29:45","guid":{"rendered":"http:\/\/116.62.30.57\/?p=93"},"modified":"2025-06-21T22:52:56","modified_gmt":"2025-06-21T14:52:56","slug":"%e4%ba%8c%e5%8f%89%e6%8e%92%e5%ba%8f%e6%a0%91%ef%bc%88bst%ef%bc%89","status":"publish","type":"post","link":"https:\/\/qiuqiqaq.fun\/index.php\/2025\/06\/21\/%e4%ba%8c%e5%8f%89%e6%8e%92%e5%ba%8f%e6%a0%91%ef%bc%88bst%ef%bc%89\/","title":{"rendered":"\u4e8c\u53c9\u6392\u5e8f\u6811\uff08BST\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49<\/h2>\n\n\n\n<p>\u4e8c\u53c9\u6392\u5e8f\u6811\uff08Binary Search Tree\uff0cBST\uff09\uff0c\u53c8\u79f0\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\u6570\u636e\u7ed3\u6784\uff0c\u8981\u4e48\u4e3a<strong>\u7a7a\u6811<\/strong>\uff0c\u8981\u4e48\u4e3a\u5177\u6709\u4e0b\u9762\u7279\u6027\u7684\u4e8c\u53c9\u6811<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u6811\u4e2d\u7684\u4efb\u610f\u8282\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u82e5\u5de6\u5b50\u6811\u975e\u7a7a\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\u90fd\u5c0f\u4e8e\u8be5\u8282\u70b9\u7684\u503c<\/li>\n\n\n\n<li>\u82e5\u53f3\u5b50\u6811\u975e\u7a7a\uff0c\u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\u90fd\u5927\u4e8e\u8be5\u8282\u70b9\u7684\u503c<\/li>\n\n\n\n<li>\u5de6\u53f3\u5b50\u6811\u4e5f\u90fd\u662f\u4e8c\u53c9\u6392\u5e8f\u6811<\/li>\n<\/ul>\n\n\n\n<p>\u6839\u636e\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u5b9a\u4e49\uff0c\u5de6\u5b50\u6811\u8282\u70b9\u503c&lt;\u6839\u8282\u70b9\u503c&lt;\u53f3\u5b50\u6811\u8282\u70b9\u503c\uff0c\u56e0\u6b64\u5bf9\u4e8c\u53c9\u6392\u5e8f\u6811\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\uff0c\u5c06\u5f97\u5230\u4e00\u4e2a\u9012\u589e\u6709\u5e8f\u5e8f\u5217\uff0c\u5982\u4e0b\u56fe\uff0c\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u5c06\u5f97\u5230\u5e8f\u5217\uff1a8,10,11,12,13,15,16,17,18,19,22,25<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/116.62.30.57\/wp-content\/uploads\/2025\/06\/image-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"332\" data-original=\"http:\/\/116.62.30.57\/wp-content\/uploads\/2025\/06\/image-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-94\"  sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/div><\/figure>\n\n\n\n<p>\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u53c9\u6392\u5e8f\u6811\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef int DataType;\ntypedef struct BST_Node {\n    DataType data;\n    struct BST_Node *lchild, *rchild;\n}BST_T, *BST_P;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u6784\u9020<\/h2>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void CreateBST(BST_P *T, int a&#91;], int n)\n{\n    int i;\n    for (i = 0; i &lt; n; i++)\n    {\n        Insert_BST(T, a&#91;i]);\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u67e5\u627e<\/h2>\n\n\n\n<p>\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u67e5\u627e\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u5229\u7528BST\u7684\u6709\u5e8f\u6027\u8d28\uff0c\u8fdb\u884c\u9012\u5f52\u67e5\u627e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u76ee\u6807\u503c\u7b49\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u627e\u5230\u76ee\u6807<\/li>\n\n\n\n<li>\u5982\u679c\u76ee\u6807\u503c\u5c0f\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u5728\u5de6\u5b50\u6811\u67e5\u627e<\/li>\n\n\n\n<li>\u5982\u679c\u76ee\u6807\u503c\u5927\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u5728\u53f3\u5b50\u6811\u67e5\u627e<\/li>\n<\/ul>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e73\u5747O(log<sub>2<\/sub>n)\uff0c\u6700\u574f\u60c5\u51b5\u67e5\u627e\u7684\u503c\u5728\u6700\u53f3\u4e0b\u65b9\uff0c\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n\n\n\n<p>\u9012\u5f52\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BST_P Search_BST(BST_P root, DataType key)\n{\n    if (root == NULL)\n        return NULL;\n    if (key &gt; root-&gt;data) \/\/\u67e5\u627e\u53f3\u5b50\u6811  \n        return Search_BST(root-&gt;rchild, key);\n    else if (key &lt; root-&gt;data) \/\/\u67e5\u627e\u5de6\u5b50\u6811  \n        return Search_BST(root-&gt;lchild, key);\n    else\n        return root;\n}<\/code><\/pre>\n\n\n\n<p>\u975e\u9012\u5f52\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BST_P Search_BST(BST_P root, DataType key)\n{\n    while (root != NULL &amp;&amp; root-&gt;data != key) \u82e5\u6811\u7a7a\u6216\u7b49\u4e8e\u6839\u8282\u70b9\u503c\uff0c\u9000\u51fa\u5faa\u73af\n    {       \n       if(key &lt; T-&gt;data)  T = T-&gt;lchild; \/\/\u5c0f\u4e8e\uff0c\u67e5\u627e\u5de6\u5b50\u6811\n       else T = T-&gt;rchild; \/\/\u5927\u4e8e\uff0c\u67e5\u627e\u53f3\u5b50\u6811\n    }\n    return NULL;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u63d2\u5165<\/h2>\n\n\n\n<p>\u63d2\u5165\u65b0\u8282\u70b9\u65f6\uff0c\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u6bd4\u8f83\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u82e5\u5f53\u524d\u8282\u70b9\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d2\u5165<\/li>\n\n\n\n<li>\u5982\u679c\u65b0\u503c\u5c0f\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u5411\u5de6\u5b50\u6811\u9012\u5f52<\/li>\n\n\n\n<li>\u5982\u679c\u65b0\u503c\u5927\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u5411\u53f3\u5b50\u6811\u9012\u5f52<\/li>\n\n\n\n<li>\u627e\u5230\u7a7a\u4f4d\u7f6e\u540e\u63d2\u5165\u65b0\u8282\u70b9<\/li>\n<\/ul>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e73\u5747O(log<sub>2<\/sub>n)\uff0c\u6700\u574fO(n)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/116.62.30.57\/wp-content\/uploads\/2025\/06\/image-3-1024x495.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"495\" data-original=\"http:\/\/116.62.30.57\/wp-content\/uploads\/2025\/06\/image-3-1024x495.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-96\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int BST_Insert(BST_P T, DataType key){\n    if(T == NULL){      \/\/ \u539f\u6811\u4e3a\u7a7a\uff0c\u63d2\u5165\u7684\u503c\u4e3a\u7a7a\u8282\u70b9\n        T = (BST_P)malloc(sizeof(BST_Node));\n        T-&gt;data = key;\n        T-&gt;lchild = T-&gt;rchild = NULL;\n        return 1;     \/\/ \u8fd4\u56de\uff0c\u63d2\u5165\u6210\u529f\n    }\n    else if(key == T-&gt;data)\n        return 0;   \/\/ \u5f53\u524d\u8282\u70b9\u7684\u503c\u4e0e\u8981\u63d2\u5165\u7684\u503c\u76f8\u540c\uff0c\u63d2\u5165\u5931\u8d25\n    else if(key &lt; T-&gt;data)  \/\/ \u63d2\u5165\u5de6\u5b50\u6811\n        return BST_Insert(T-&gt;lchild, key);\n    else  \/\/ \u63d2\u5165\u53f3\u5b50\u6811\n        return BST_Insert(T-&gt;rchild, key);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u5220\u9664<\/h2>\n\n\n\n<p>\u5220\u9664\u662f\u6700\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u9700\u8981\u8003\u8651\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u60c5\u51b51\uff1a\u5220\u9664\u53f6\u5b50\u8282\u70b9<\/strong> \u76f4\u63a5\u5220\u9664\uff0c\u5c06\u7236\u8282\u70b9\u5bf9\u5e94\u7684\u6307\u9488\u8bbe\u4e3a\u7a7a\u3002<\/li>\n\n\n\n<li><strong>\u60c5\u51b52\uff1a\u5220\u9664\u53ea\u6709\u4e00\u4e2a\u5b50\u8282\u70b9\u7684\u8282\u70b9<\/strong> \u7528\u8be5\u8282\u70b9\u7684\u5b50\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002<\/li>\n\n\n\n<li><strong>\u60c5\u51b53\uff1a\u5220\u9664\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u8282\u70b9<\/strong> \u627e\u5230\u8be5\u8282\u70b9\u7684\u4e2d\u5e8f\u540e\u7ee7\uff08\u53f3\u5b50\u6811\u4e2d\u7684\u6700\u5c0f\u503c\uff09\u6216\u4e2d\u5e8f\u524d\u9a71\uff08\u5de6\u5b50\u6811\u4e2d\u7684\u6700\u5927\u503c\uff09\uff0c\u7528\u540e\u7ee7\/\u524d\u9a71\u7684\u503c\u66ff\u6362\u8981\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u7136\u540e\u5220\u9664\u540e\u7ee7\/\u524d\u9a71\u8282\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0\uff08\u8003\u7684\u6982\u7387\u5f88\u5c0f\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void DeleteBSTNode(BST_P *root, DataType data)\n{\n    BST_P p = *root, parent = NULL, s = NULL;\n\n    if (!p) return;\n\n    if (p-&gt;data == data) \/\/\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\u4e86\n    {\n        \/* It's a leaf node *\/\n        if (!p-&gt;rchild &amp;&amp; !p-&gt;lchild) \n            *root = NULL;\n\n        \/\/ \u53ea\u6709\u4e00\u4e2a\u5de6\u8282\u70b9\n        else if (!p-&gt;rchild&amp;&amp;p-&gt;lchild) \n            *root = p-&gt;lchild;\n\n        \/\/ \u53ea\u6709\u4e00\u4e2a\u53f3\u8282\u70b9\n        else if (!p-&gt;lchild&amp;&amp;p-&gt;rchild) \n            *root = p-&gt;rchild;\n\n        \/\/\u5de6\u53f3\u8282\u70b9\u90fd\u4e0d\u7a7a\n        else \n        {\n            s = p-&gt;rchild;\n            \/* the s without left child *\/\n            if (!s-&gt;lchild)\n                s-&gt;lchild = p-&gt;lchild;\n            \/* the s have left child *\/\n            else \n            {\n                \/* find the smallest node in the left subtree of s *\/\n                while (s-&gt;lchild) \n                {\n                    \/* record the parent node of s *\/\n                    parent = s;\n                    s = s-&gt;lchild;\n                }\n                parent-&gt;lchild = s-&gt;rchild;\n                s-&gt;lchild = p-&gt;lchild;\n                s-&gt;rchild = p-&gt;rchild;\n            }\n            *root = s;\n        }\n        free(p);\n    }\n    else if (data &gt; p-&gt;data) \/\/\u5411\u53f3\u627e\n        DeleteBSTNode(&amp;(p-&gt;rchild), data);\n    else if (data &lt; p-&gt;data) \/\/\u5411\u5de6\u627e\n        DeleteBSTNode(&amp;(p-&gt;lchild), data);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u64cd\u4f5c<\/td><td>\u5e73\u5747\u60c5\u51b5<\/td><td>\u6700\u574f\u60c5\u51b5<\/td><\/tr><tr><td>\u67e5\u627e<\/td><td>O(log n)<\/td><td>O(n)<\/td><\/tr><tr><td>\u63d2\u5165<\/td><td>O(log n)<\/td><td>O(n)<\/td><\/tr><tr><td>\u5220\u9664<\/td><td>O(log n)<\/td><td>O(n)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u904d\u5386\u64cd\u4f5c<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e2d\u5e8f\u904d\u5386<\/strong>\uff1a\u5de6\u2192\u6839\u2192\u53f3\uff0c\u5f97\u5230\u6709\u5e8f\u5e8f\u5217<\/li>\n\n\n\n<li> <strong>\u524d\u5e8f\u904d\u5386<\/strong>\uff1a\u6839\u2192\u5de6\u2192\u53f3\uff0c\u7528\u4e8e\u590d\u5236\u6811\u7ed3\u6784 <\/li>\n\n\n\n<li><strong>\u540e\u5e8f\u904d\u5386<\/strong>\uff1a\u5de6\u2192\u53f3\u2192\u6839\uff0c\u7528\u4e8e\u5220\u9664\u6811<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u67e5\u627e\u6548\u7387<\/h2>\n\n\n\n<p>\u4e8c\u53c9\u6392\u5e8f\u6811\u7684\u67e5\u627e\u6548\u7387\u4e3b\u8981\u53d6\u51b3\u4e8e\u6811\u7684\u9ad8\u5ea6\uff0c\u6700\u4f18\u60c5\u51b5\u4e0b\uff0c\u5f53\u5de6\u53f3\u5b50\u6811\u9ad8\u5ea6\u4e4b\u5dee\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71\u65f6\uff08\u5373\u5e73\u8861\u4e8c\u53c9\u6811\uff09\uff0c\u5e73\u5747\u67e5\u627e\u957f\u5ea6\u4e0eO(log<sub>2<\/sub>n)\uff0c\u6700\u574f\u60c5\u51b5 \uff0c\u9000\u5316\u4e3a\u94fe\u8868\uff0c\u5373\u5f53\u63d2\u5165\u7684\u5143\u7d20\u6709\u5e8f\u65f6\uff0c\u5f62\u6210\u4e00\u4e2a\u53ea\u6709\u53f3\u5b69\u5b50\u7684\u5355\u679d\u6570\uff0c\u5e73\u5747\u67e5\u627e\u957f\u5ea6\u4e3a(n+1)\/2<\/p>\n\n\n\n<p>\u6587\u5b57\u53ef\u80fd\u7565\u663e\u62bd\u8c61\uff0c\u4e0d\u59a8\u770b\u770b\u89c6\u9891\uff1a<a href=\"https:\/\/www.bilibili.com\/video\/BV1uK421x7JJ\/\">https:\/\/www.bilibili.com\/video\/BV1uK421x7JJ\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49 \u4e8c\u53c9\u6392\u5e8f\u6811\uff08Binary Search Tree\uff0cBST\uff09\uff0c\u53c8\u79f0\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\u6570\u636e\u7ed3\u6784\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-datastructure"],"_links":{"self":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":5,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":101,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts\/93\/revisions\/101"}],"wp:attachment":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}