{"id":1098421,"date":"2025-01-08T15:21:03","date_gmt":"2025-01-08T07:21:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098421.html"},"modified":"2025-01-08T15:21:05","modified_gmt":"2025-01-08T07:21:05","slug":"python%e5%a6%82%e4%bd%95%e6%9e%84%e9%80%a0%e4%b8%80%e6%a3%b5%e6%a0%91-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098421.html","title":{"rendered":"Python\u5982\u4f55\u6784\u9020\u4e00\u68f5\u6811"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212743\/6db64c54-2940-446f-9c90-3feaa338dc25.webp\" alt=\"Python\u5982\u4f55\u6784\u9020\u4e00\u68f5\u6811\" \/><\/p>\n<p><p> <strong>Python\u6784\u9020\u4e00\u68f5\u6811\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7c7b\u4e0e\u5bf9\u8c61\u3001\u5b57\u5178\u3001\u5217\u8868\u7b49\u6570\u636e\u7ed3\u6784\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u5b9a\u4e49\u6811\u8282\u70b9\u7c7b\u3001\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u8868\u793a\u6811\u3001\u4ee5\u53ca\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u8868\u793a\u6811\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u7c7b\u4e0e\u5bf9\u8c61\u6765\u6784\u9020\u548c\u64cd\u4f5c\u4e00\u68f5\u6811\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6811\u8282\u70b9\u7c7b<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u6811\u8282\u70b9\u7c7b\uff0c\u6811\u8282\u70b9\u7c7b\u662f\u6811\u7ed3\u6784\u7684\u57fa\u672c\u6784\u5efa\u6a21\u5757\u3002\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u6570\u636e\u548c\u6307\u5411\u5b50\u8282\u70b9\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.children = []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c<code>value<\/code>\u5c5e\u6027\u5b58\u50a8\u8282\u70b9\u7684\u503c\uff0c<code>children<\/code>\u5c5e\u6027\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6dfb\u52a0\u5b50\u8282\u70b9<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u6811\u8282\u70b9\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5411\u8282\u70b9\u6dfb\u52a0\u5b50\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.children = []<\/p>\n<p>    def add_child(self, child_node):<\/p>\n<p>        self.children.append(child_node)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>add_child<\/code>\u65b9\u6cd5\uff0c\u5c06\u4e00\u4e2a\u5b50\u8282\u70b9\u6dfb\u52a0\u5230\u5f53\u524d\u8282\u70b9\u7684<code>children<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6784\u5efa\u6811<\/h3>\n<\/p>\n<p><p>\u6709\u4e86\u6811\u8282\u70b9\u7c7b\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u7c7b\u6765\u6784\u5efa\u6811\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u6839\u8282\u70b9\uff0c\u5e76\u4e3a\u5176\u6dfb\u52a0\u5b50\u8282\u70b9\u548c\u5b59\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6839\u8282\u70b9<\/p>\n<p>root = TreeNode(&quot;root&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u8282\u70b9<\/strong><\/h2>\n<p>child1 = TreeNode(&quot;child1&quot;)<\/p>\n<p>child2 = TreeNode(&quot;child2&quot;)<\/p>\n<h2><strong>\u5c06\u5b50\u8282\u70b9\u6dfb\u52a0\u5230\u6839\u8282\u70b9<\/strong><\/h2>\n<p>root.add_child(child1)<\/p>\n<p>root.add_child(child2)<\/p>\n<h2><strong>\u521b\u5efa\u5b59\u8282\u70b9<\/strong><\/h2>\n<p>grandchild1 = TreeNode(&quot;grandchild1&quot;)<\/p>\n<p>grandchild2 = TreeNode(&quot;grandchild2&quot;)<\/p>\n<h2><strong>\u5c06\u5b59\u8282\u70b9\u6dfb\u52a0\u5230\u5b50\u8282\u70b9<\/strong><\/h2>\n<p>child1.add_child(grandchild1)<\/p>\n<p>child2.add_child(grandchild2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u904d\u5386\u6811<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u9a8c\u8bc1\u6211\u4eec\u6784\u5efa\u7684\u6811\u7ed3\u6784\u662f\u5426\u6b63\u786e\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u6811\u7684\u904d\u5386\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u6811\u904d\u5386\u65b9\u6cd5\u6709\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u548c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h4>1. \u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff08DFS\uff09<\/h4>\n<\/p>\n<p><p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dfs(node):<\/p>\n<p>    print(node.value)<\/p>\n<p>    for child in node.children:<\/p>\n<p>        dfs(child)<\/p>\n<h2><strong>\u8c03\u7528\u6df1\u5ea6\u4f18\u5148\u904d\u5386<\/strong><\/h2>\n<p>dfs(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\uff08BFS\uff09<\/h4>\n<\/p>\n<p><p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ef\u4ee5\u901a\u8fc7\u961f\u5217\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>def bfs(node):<\/p>\n<p>    queue = deque([node])<\/p>\n<p>    while queue:<\/p>\n<p>        current_node = queue.popleft()<\/p>\n<p>        print(current_node.value)<\/p>\n<p>        for child in current_node.children:<\/p>\n<p>            queue.append(child)<\/p>\n<h2><strong>\u8c03\u7528\u5e7f\u5ea6\u4f18\u5148\u904d\u5386<\/strong><\/h2>\n<p>bfs(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6811\u7684\u5176\u4ed6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u67e5\u627e\u8282\u70b9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\u6765\u67e5\u627e\u6811\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_node(node, value):<\/p>\n<p>    if node.value == value:<\/p>\n<p>        return node<\/p>\n<p>    for child in node.children:<\/p>\n<p>        result = find_node(child, value)<\/p>\n<p>        if result:<\/p>\n<p>            return result<\/p>\n<p>    return None<\/p>\n<h2><strong>\u67e5\u627e\u8282\u70b9<\/strong><\/h2>\n<p>result = find_node(root, &quot;grandchild1&quot;)<\/p>\n<p>if result:<\/p>\n<p>    print(f&quot;Node found: {result.value}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Node not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5220\u9664\u8282\u70b9<\/h4>\n<\/p>\n<p><p>\u5220\u9664\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5148\u627e\u5230\u8be5\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u7136\u540e\u4ece\u7236\u8282\u70b9\u7684<code>children<\/code>\u5217\u8868\u4e2d\u5220\u9664\u8be5\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_node(node, value):<\/p>\n<p>    for child in node.children:<\/p>\n<p>        if child.value == value:<\/p>\n<p>            node.children.remove(child)<\/p>\n<p>            return True<\/p>\n<p>        result = delete_node(child, value)<\/p>\n<p>        if result:<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<h2><strong>\u5220\u9664\u8282\u70b9<\/strong><\/h2>\n<p>delete_node(root, &quot;grandchild1&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u6811\u8282\u70b9\u7c7b\u3001\u6dfb\u52a0\u5b50\u8282\u70b9\u3001\u6784\u5efa\u6811\u3001\u904d\u5386\u6811\uff0c\u4ee5\u53ca\u5b9e\u73b0\u67e5\u627e\u548c\u5220\u9664\u8282\u70b9\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728Python\u4e2d\u6784\u9020\u548c\u64cd\u4f5c\u4e00\u68f5\u6811\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u5177\u6709\u5f88\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\uff0c\u53ef\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u6811\u8282\u70b9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u6811\u8282\u70b9\u7c7b\u6765\u5b9a\u4e49\u6811\u7684\u7ed3\u6784\u3002\u901a\u5e38\uff0c\u8fd9\u4e2a\u7c7b\u4f1a\u5305\u542b\u8282\u70b9\u7684\u503c\u4ee5\u53ca\u6307\u5411\u5176\u5b50\u8282\u70b9\u7684\u5f15\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a<code>TreeNode<\/code>\u7c7b\uff0c\u5305\u542b<code>value<\/code>\u5c5e\u6027\u548c\u4e00\u4e2a\u5b50\u8282\u70b9\u5217\u8868<code>children<\/code>\u3002\u8fd9\u6837\u7684\u7ed3\u6784\u4f7f\u5f97\u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6709\u591a\u4e2a\u5b50\u8282\u70b9\uff0c\u4ece\u800c\u5f62\u6210\u6811\u7684\u5c42\u6b21\u7ed3\u6784\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u6811\u7ed3\u6784\u5b9e\u73b0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u89c1\u7684\u6811\u7ed3\u6784\u5305\u62ec\u4e8c\u53c9\u6811\u3001N\u53c9\u6811\u548c AVL \u6811\u7b49\u3002\u6bcf\u79cd\u7ed3\u6784\u90fd\u6709\u4e0d\u540c\u7684\u7279\u6027\u548c\u4f7f\u7528\u573a\u666f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\uff0c\u9002\u5408\u8fdb\u884c\u6392\u5e8f\u548c\u67e5\u627e\u64cd\u4f5c\uff1bN\u53c9\u6811\u5219\u5141\u8bb8\u6bcf\u4e2a\u8282\u70b9\u6709\u4efb\u610f\u6570\u91cf\u7684\u5b50\u8282\u70b9\uff0c\u5e38\u7528\u4e8e\u8868\u793a\u5c42\u6b21\u5173\u7cfb\uff1bAVL\u6811\u662f\u4e00\u79cd\u81ea\u5e73\u8861\u7684\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u904d\u5386\u4e00\u68f5\u6811\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6216\u8fed\u4ee3\u7684\u65b9\u5f0f\u904d\u5386\u6811\u3002\u5e38\u89c1\u7684\u904d\u5386\u65b9\u6cd5\u6709\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002\u524d\u5e8f\u904d\u5386\u4f1a\u5148\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u7136\u540e\u9012\u5f52\u8bbf\u95ee\u6bcf\u4e2a\u5b50\u6811\uff1b\u4e2d\u5e8f\u904d\u5386\u5219\u4f1a\u5148\u8bbf\u95ee\u5de6\u5b50\u6811\uff0c\u518d\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u6700\u540e\u8bbf\u95ee\u53f3\u5b50\u6811\uff1b\u540e\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u662f\u5148\u8bbf\u95ee\u5b50\u6811\uff0c\u6700\u540e\u8bbf\u95ee\u6839\u8282\u70b9\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\uff08\u5c42\u5e8f\u904d\u5386\uff09\u6765\u6309\u5c42\u8bbf\u95ee\u6811\u4e2d\u7684\u8282\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6784\u9020\u4e00\u68f5\u6811\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7c7b\u4e0e\u5bf9\u8c61\u3001\u5b57\u5178\u3001\u5217\u8868\u7b49\u6570\u636e\u7ed3\u6784\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u5b9a\u4e49\u6811\u8282\u70b9\u7c7b\u3001\u4f7f\u7528\u5d4c [&hellip;]","protected":false},"author":3,"featured_media":1098429,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098421"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1098421"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098421\/revisions"}],"predecessor-version":[{"id":1098432,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098421\/revisions\/1098432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098429"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}