{"id":81,"date":"2024-09-05T15:35:56","date_gmt":"2024-09-05T07:35:56","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=81"},"modified":"2024-09-05T15:35:56","modified_gmt":"2024-09-05T07:35:56","slug":"c%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e4%ba%8c%e5%8f%89%e6%a0%91%e7%9a%84%e6%a6%82%e5%bf%b5%e4%b8%8e%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=81","title":{"rendered":"C\u8bed\u8a00\u4e2d\u7684\u4e8c\u53c9\u6811\u7684\u6982\u5ff5\u4e0e\u5b9e\u73b0"},"content":{"rendered":"<p>\u4e8c\u53c9\u6811\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u8d77\u7740\u5173\u952e\u4f5c\u7528\u3002\u4e8c\u53c9\u6811\u5177\u6709\u5e7f\u6cdb\u7684\u7528\u9014\uff0c\u5982\u8868\u8fbe\u5f0f\u6811\u3001\u67e5\u627e\u6811\uff08\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u3001\u5806\uff08Heap\uff09\u7b49\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e8c\u53c9\u6811\u7684\u6982\u5ff5\u3001\u5728C\u8bed\u8a00\u4e2d\u7684\u5b9e\u73b0\u4ee5\u53ca\u5b83\u7684\u5e38\u89c1\u7528\u9014\u3002<\/p>\n<h3>\u4e00\u3001\u4e8c\u53c9\u6811\u7684\u6982\u5ff5<\/h3>\n<p>\u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u6811\u5f62\u7ed3\u6784\uff0c\u5b83\u7684\u7279\u70b9\u662f\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\uff0c\u901a\u5e38\u79f0\u4e3a\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>\u6839\u8282\u70b9\uff08Root\uff09\uff1a<\/strong> \u4e8c\u53c9\u6811\u7684\u9876\u70b9\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002<\/li>\n<li><strong>\u53f6\u5b50\u8282\u70b9\uff08Leaf\uff09\uff1a<\/strong> \u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u3002<\/li>\n<li><strong>\u5185\u8282\u70b9\uff08Internal Node\uff09\uff1a<\/strong> \u5177\u6709\u81f3\u5c11\u4e00\u4e2a\u5b50\u8282\u70b9\u7684\u8282\u70b9\u3002<\/li>\n<li><strong>\u5b50\u6811\uff08Subtree\uff09\uff1a<\/strong> \u6bcf\u4e2a\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5206\u522b\u4e3a\u4ee5\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u4e3a\u6839\u7684\u6811\u3002<\/li>\n<\/ul>\n<h3>\u4e8c\u3001\u4e8c\u53c9\u6811\u7684\u5b9e\u73b0<\/h3>\n<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u4e8c\u53c9\u6811\u901a\u5e38\u901a\u8fc7\u9012\u5f52\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u6570\u636e\u57df\u548c\u4e24\u4e2a\u6307\u5411\u5176\u5b50\u8282\u70b9\u7684\u6307\u9488\u3002<\/p>\n<h4>1. \u4e8c\u53c9\u6811\u8282\u70b9\u7684\u5b9a\u4e49<\/h4>\n<pre><code class=\"language-c\">typedef struct TreeNode {\n    int data;  \/\/ \u6570\u636e\u57df\n    struct TreeNode* left;  \/\/ \u5de6\u5b50\u8282\u70b9\n    struct TreeNode* right; \/\/ \u53f3\u5b50\u8282\u70b9\n} TreeNode;<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<code>TreeNode<\/code>\u7ed3\u6784\u4f53\u8868\u793a\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u5176\u4e2d<code>data<\/code>\u5b58\u50a8\u8282\u70b9\u7684\u6570\u636e\uff0c<code>left<\/code>\u548c<code>right<\/code>\u5206\u522b\u6307\u5411\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002<\/p>\n<h4>2. \u521b\u5efa\u8282\u70b9<\/h4>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u5e76\u521d\u59cb\u5316\u8282\u70b9\u7684\u5185\u5bb9\u3002<\/p>\n<pre><code class=\"language-c\">TreeNode* createNode(int data) {\n    TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));\n    if (newNode == NULL) {\n        printf(&quot;\u5185\u5b58\u5206\u914d\u5931\u8d25\\n&quot;);\n        return NULL;\n    }\n    newNode-&gt;data = data;\n    newNode-&gt;left = NULL;\n    newNode-&gt;right = NULL;\n    return newNode;\n}<\/code><\/pre>\n<h4>3. \u63d2\u5165\u8282\u70b9<\/h4>\n<p>\u63d2\u5165\u8282\u70b9\u53ef\u4ee5\u9012\u5f52\u5730\u5c06\u65b0\u8282\u70b9\u63d2\u5165\u5230\u5408\u9002\u7684\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u63d2\u5165\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-c\">TreeNode* insertNode(TreeNode* root, int data) {\n    if (root == NULL) {\n        return createNode(data);\n    }\n\n    if (data &lt; root-&gt;data) {\n        root-&gt;left = insertNode(root-&gt;left, data);\n    } else {\n        root-&gt;right = insertNode(root-&gt;right, data);\n    }\n\n    return root;\n}<\/code><\/pre>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7\u6bd4\u8f83\u6570\u636e\u6765\u51b3\u5b9a\u65b0\u8282\u70b9\u5e94\u63d2\u5165\u5230\u5de6\u5b50\u6811\u8fd8\u662f\u53f3\u5b50\u6811\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u63d2\u5165\u540e\u7684\u6811\u7684\u6839\u8282\u70b9\u3002<\/p>\n<h4>4. \u67e5\u627e\u8282\u70b9<\/h4>\n<p>\u4e8c\u53c9\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\uff0c\u4ee5\u4e0b\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-c\">TreeNode* searchNode(TreeNode* root, int data) {\n    if (root == NULL || root-&gt;data == data) {\n        return root;\n    }\n\n    if (data &lt; root-&gt;data) {\n        return searchNode(root-&gt;left, data);\n    }\n\n    return searchNode(root-&gt;right, data);\n}<\/code><\/pre>\n<h4>5. \u5220\u9664\u8282\u70b9<\/h4>\n<p>\u5220\u9664\u4e8c\u53c9\u6811\u4e2d\u7684\u8282\u70b9\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u5904\u7406\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>\u8282\u70b9\u662f\u53f6\u5b50\u8282\u70b9\uff1b<\/li>\n<li>\u8282\u70b9\u53ea\u6709\u4e00\u4e2a\u5b50\u8282\u70b9\uff1b<\/li>\n<li>\u8282\u70b9\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-c\">TreeNode* deleteNode(TreeNode* root, int data) {\n    if (root == NULL) return root;\n\n    if (data &lt; root-&gt;data) {\n        root-&gt;left = deleteNode(root-&gt;left, data);\n    } else if (data &gt; root-&gt;data) {\n        root-&gt;right = deleteNode(root-&gt;right, data);\n    } else {\n        \/\/ \u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\n        if (root-&gt;left == NULL) {\n            TreeNode* temp = root-&gt;right;\n            free(root);\n            return temp;\n        } else if (root-&gt;right == NULL) {\n            TreeNode* temp = root-&gt;left;\n            free(root);\n            return temp;\n        }\n\n        \/\/ \u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u60c5\u51b5\uff0c\u627e\u5230\u53f3\u5b50\u6811\u4e2d\u6700\u5c0f\u7684\u8282\u70b9\n        TreeNode* temp = findMin(root-&gt;right);\n        root-&gt;data = temp-&gt;data;\n        root-&gt;right = deleteNode(root-&gt;right, temp-&gt;data);\n    }\n    return root;\n}\n\nTreeNode* findMin(TreeNode* node) {\n    TreeNode* current = node;\n    while (current &amp;&amp; current-&gt;left != NULL) {\n        current = current-&gt;left;\n    }\n    return current;\n}<\/code><\/pre>\n<h3>\u4e09\u3001\u4e8c\u53c9\u6811\u7684\u904d\u5386<\/h3>\n<p>\u4e8c\u53c9\u6811\u7684\u904d\u5386\u65b9\u5f0f\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002\u8fd9\u4e09\u79cd\u904d\u5386\u65b9\u6cd5\u5206\u522b\u5bf9\u5e94\u4e0d\u540c\u7684\u8bbf\u95ee\u987a\u5e8f\u3002<\/p>\n<h4>1. \u524d\u5e8f\u904d\u5386<\/h4>\n<p>\u5728\u524d\u5e8f\u904d\u5386\u4e2d\uff0c\u9996\u5148\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u5de6\u5b50\u6811\uff0c\u6700\u540e\u904d\u5386\u53f3\u5b50\u6811\u3002<\/p>\n<pre><code class=\"language-c\">void preorderTraversal(TreeNode* root) {\n    if (root != NULL) {\n        printf(&quot;%d &quot;, root-&gt;data);\n        preorderTraversal(root-&gt;left);\n        preorderTraversal(root-&gt;right);\n    }\n}<\/code><\/pre>\n<h4>2. \u4e2d\u5e8f\u904d\u5386<\/h4>\n<p>\u4e2d\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u662f\uff1a\u5148\u904d\u5386\u5de6\u5b50\u6811\uff0c\u7136\u540e\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u6700\u540e\u904d\u5386\u53f3\u5b50\u6811\u3002<\/p>\n<pre><code class=\"language-c\">void inorderTraversal(TreeNode* root) {\n    if (root != NULL) {\n        inorderTraversal(root-&gt;left);\n        printf(&quot;%d &quot;, root-&gt;data);\n        inorderTraversal(root-&gt;right);\n    }\n}<\/code><\/pre>\n<h4>3. \u540e\u5e8f\u904d\u5386<\/h4>\n<p>\u5728\u540e\u5e8f\u904d\u5386\u4e2d\uff0c\u9996\u5148\u904d\u5386\u5de6\u5b50\u6811\uff0c\u7136\u540e\u904d\u5386\u53f3\u5b50\u6811\uff0c\u6700\u540e\u8bbf\u95ee\u6839\u8282\u70b9\u3002<\/p>\n<pre><code class=\"language-c\">void postorderTraversal(TreeNode* root) {\n    if (root != NULL) {\n        postorderTraversal(root-&gt;left);\n        postorderTraversal(root-&gt;right);\n        printf(&quot;%d &quot;, root-&gt;data);\n    }\n}<\/code><\/pre>\n<h3>\u56db\u3001\u4e8c\u53c9\u6811\u7684\u7528\u9014<\/h3>\n<p>\u4e8c\u53c9\u6811\u5728\u8bb8\u591a\u9886\u57df\u4e2d\u90fd\u5177\u6709\u91cd\u8981\u7528\u9014\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4e8c\u53c9\u641c\u7d22\u6811\uff08Binary Search Tree, BST\uff09\uff1a<\/strong><br \/>\nBST\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\uff0c\u5b83\u7684\u5de6\u5b50\u8282\u70b9\u5c0f\u4e8e\u6839\u8282\u70b9\uff0c\u53f3\u5b50\u8282\u70b9\u5927\u4e8e\u6839\u8282\u70b9\u3002\u8fd9\u79cd\u7ed3\u6784\u4f7f\u5f97\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(log n)\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5b9e\u73b0\u9ad8\u6548\u7684\u67e5\u627e\u548c\u6392\u5e8f\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5806\uff08Heap\uff09\uff1a<\/strong><br \/>\n\u5806\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5b83\u5e38\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u3002\u5728\u6700\u5927\u5806\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\u90fd\u5927\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u8282\u70b9\u7684\u503c\uff0c\u6700\u5c0f\u5806\u5219\u76f8\u53cd\u3002\u5806\u901a\u5e38\u7528\u4e8e\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5806\u6392\u5e8f\uff09\u548c\u56fe\u7b97\u6cd5\uff08\u5982Dijkstra\u7b97\u6cd5\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8868\u8fbe\u5f0f\u6811\uff08Expression Tree\uff09\uff1a<\/strong><br \/>\n\u8868\u8fbe\u5f0f\u6811\u662f\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u5e94\u7528\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u7b97\u672f\u8868\u8fbe\u5f0f\u7684\u7ed3\u6784\u3002\u53f6\u5b50\u8282\u70b9\u8868\u793a\u64cd\u4f5c\u6570\uff0c\u5185\u90e8\u8282\u70b9\u8868\u793a\u64cd\u4f5c\u7b26\u3002\u8fd9\u79cd\u6811\u7ed3\u6784\u4f7f\u5f97\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u970d\u592b\u66fc\u7f16\u7801\u6811\uff08Huffman Tree\uff09\uff1a<\/strong><br \/>\n\u970d\u592b\u66fc\u6811\u662f\u4e00\u79cd\u4e8c\u53c9\u6811\uff0c\u7528\u4e8e\u6570\u636e\u538b\u7f29\u7b97\u6cd5\u3002\u5b83\u901a\u8fc7\u6784\u9020\u5e26\u6743\u8def\u5f84\u957f\u5ea6\u6700\u5c0f\u7684\u4e8c\u53c9\u6811\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u65e0\u635f\u538b\u7f29\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u51b3\u7b56\u6811\uff08Decision Tree\uff09\uff1a<\/strong><br \/>\n\u51b3\u7b56\u6811\u662f\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff0c\u7528\u4e8e\u5206\u7c7b\u548c\u56de\u5f52\u4efb\u52a1\u3002\u5b83\u901a\u8fc7\u4e8c\u53c9\u6811\u7ed3\u6784\u6765\u5212\u5206\u6570\u636e\u7a7a\u95f4\uff0c\u6700\u7ec8\u505a\u51fa\u51b3\u7b56\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<p>\u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5728\u6570\u636e\u7ec4\u7ec7\u548c\u5904\u7406\u65b9\u9762\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u4e8c\u53c9\u6811\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e\u6307\u9488\u548c\u9012\u5f52\u64cd\u4f5c\uff0c\u7406\u89e3\u548c\u638c\u63e1\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u67e5\u627e\u3001\u5220\u9664\u548c\u904d\u5386\uff0c\u662f\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u7684\u91cd\u8981\u6b65\u9aa4\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u4e8c\u53c9\u6811\u53d8\u79cd\uff08\u5982\u5e73\u8861\u4e8c\u53c9\u6811\u3001\u7ea2\u9ed1\u6811\u3001AVL\u6811\u7b49\uff09\u53ef\u4ee5\u4f18\u5316\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u4f7f\u5176\u66f4\u9002\u5408\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u53c9\u6811\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u8d77\u7740\u5173\u952e\u4f5c\u7528\u3002\u4e8c\u53c9\u6811\u5177\u6709\u5e7f\u6cdb\u7684\u7528\u9014\uff0c\u5982\u8868\u8fbe\u5f0f\u6811\u3001\u67e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[14,25],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-25"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=81"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}