{"id":1067,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u4e8c\u53c9\u6811","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1067.html","title":{"rendered":"Python \u4e8c\u53c9\u6811"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u4e8c\u53c9\u6811<\/h1>\n

Python \u4e8c\u53c9\u6811\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u6811\u8868\u793a\u901a\u8fc7\u8fb9\u8fde\u63a5\u7684\u8282\u70b9\u3002\u5b83\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\u3002\n <\/div>\n

\u4e00\u4e2a\u8282\u70b9\u88ab\u6807\u8bb0\u4e3a\u6839\u8282\u70b9\u3002<\/span>
\n \u9664\u6839\u8282\u70b9\u5916\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u4e0e\u4e00\u4e2a\u7236\u8282\u70b9\u5173\u8054\u3002<\/span>
\n \u6bcf\u4e2a\u8282\u70b9\u53ef\u4ee5\u5177\u6709chib\u8282\u70b9\u7684\u7f16\u53f7\u3002<\/span> <\/p>\n

\n \u6211\u4eec\u4f7f\u7528\u524d\u9762\u8ba8\u8bba\u7684\u6982\u5ff5os\u8282\u70b9\u5728python\u4e2d\u521b\u5efa\u6811\u6570\u636e\u7ed3\u6784\u3002\u6211\u4eec\u5c06\u4e00\u4e2a\u8282\u70b9\u6307\u5b9a\u4e3a\u6839\u8282\u70b9\uff0c\u7136\u540e\u6dfb\u52a0\u66f4\u591a\u8282\u70b9\u4f5c\u4e3a\u5b50\u8282\u70b9\u3002\u4e0b\u9762\u662f\u521b\u5efa\u6839\u8282\u70b9\u7684\u7a0b\u5e8f\u3002\n <\/div>\n

\u521b\u5efa\u6839<\/h3>\n
\n \u6211\u4eec\u53ea\u521b\u5efa\u4e00\u4e2aNode\u7c7b\uff0c\u5e76\u4e3a\u8be5\u8282\u70b9\u6dfb\u52a0\u4e00\u4e2a\u8d4b\u503c\u3002\u8fd9\u53d8\u6210\u53ea\u6709\u6839\u8282\u70b9\u7684\u6811\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
class <\/span>Node:
    def <\/span>__init__(self, data):
        self.left = None
        self.right = None
        self.data = data
    def <\/span>PrintTree(self):
        print(self.data)
root = Node(10)
root.PrintTree<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 10
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u63d2\u5165\u6811\u4e2d<\/h3>\n
\n \u4e3a\u4e86\u63d2\u5165\u5230\u6811\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e0e\u4e0a\u9762\u521b\u5efa\u7684\u76f8\u540c\u7684\u8282\u70b9\u7c7b\uff0c\u5e76\u5411\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u63d2\u5165\u7c7b\u3002\u63d2\u5165\u7c7b\u5c06\u8282\u70b9\u7684\u503c\u4e0e\u7236\u8282\u70b9\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u51b3\u5b9a\u5c06\u5176\u6dfb\u52a0\u4e3a\u5de6\u4fa7\u8282\u70b9\u8fd8\u662f\u53f3\u4fa7\u8282\u70b9\u3002\u6700\u540e\uff0cPrintTree\u7c7b\u7528\u4e8e\u6253\u5370\u6811\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
class <\/span>Node:
    def <\/span>__init__(self, data):
        self.left = None
        self.right = None
        self.data = data
    def <\/span>insert(self, data):
# Compare the new value with <\/span>the parent node
<\/span>         if <\/span>self.data:
            if <\/span>data < self.data:
                if <\/span>self.left is <\/span>None:
                    self.left = Node<\/span>(data)
                else:<\/span>
                    self.left.insert<\/span>(data)
            elif <\/span><\/span>data > self.data:
                if <\/span>self.right is <\/span>None:
                    self.right = Node<\/span>(data)
                else:<\/span>
                    self.right.insert<\/span>(data)
        else:<\/span>
            self.data = data
# print <\/span>the tree
<\/span>     def <\/span>PrintTree(self):
        if <\/span>self.left:
            self.left.PrintTree<\/span>()
        print( self.data),
        if <\/span>self.right:
            self.right.PrintTree<\/span>()
# Use the insert method to add nodes
<\/span> root = Node(12)
root.insert<\/span>(6)
root.insert<\/span>(14)
root.insert<\/span>(3)
root.PrintTree<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
3 6 12 14
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u904d\u5386\u4e00\u68f5\u6811<\/h2>\n
\n \u53ef\u4ee5\u901a\u8fc7\u786e\u5b9a\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u7684\u987a\u5e8f\u6765\u904d\u5386\u6811\u3002\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u7136\u540e\u9996\u5148\u8bbf\u95ee\u5de6\u5b50\u6811\uff0c\u7136\u540e\u518d\u8bbf\u95ee\u53f3\u5b50\u6811\u3002\u6216\u8005\u6211\u4eec\u4e5f\u53ef\u4ee5\u5148\u8bbf\u95ee\u53f3\u8fb9\u7684\u5b50\u6811\uff0c\u7136\u540e\u518d\u8bbf\u95ee\u5de6\u8fb9\u7684\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6811\u904d\u5386\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u540d\u79f0\u3002\u6211\u4eec\u5c06\u5728\u6b64\u5904\u5b9e\u73b0\u6811\u904d\u5386\u7b97\u6cd5\u7684\u7ae0\u8282\u4e2d\u8be6\u7ec6\u7814\u7a76\u5b83\u4eec\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4e8c\u53c9\u6811zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1067","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1067"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1067"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1067\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}