{"id":1073,"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 Tree\u904d\u5386","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1073.html","title":{"rendered":"Python Tree\u904d\u5386"},"content":{"rendered":"
\n
Python - Tree Tree\u904d\u5386\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u6709\u5e8f\u904d\u5386<\/span> <\/body>
\n \u524d\u5e8f\u904d\u5386<\/span>
\n \u540e\u5e8f\u904d\u5386<\/span> <\/p>\n\u6709\u5e8f\u904d\u5386<\/h2>\n
\n
\u5728\u4e0b\u9762\u7684python\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Node\u7c7b\u4e3a\u6839\u8282\u70b9\u4ee5\u53ca\u5de6\u53f3\u8282\u70b9\u521b\u5efa\u5360\u4f4d\u7b26\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u63d2\u5165\u51fd\u6570\u4ee5\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u6811\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u5e76\u9996\u5148\u6dfb\u52a0\u5de6\u8282\u70b9\uff0c\u7136\u540e\u518d\u6dfb\u52a0\u6839\u8282\u70b9\u6216\u7236\u8282\u70b9\u6765\u5b9e\u73b0\u6709\u5e8f\u904d\u5386\u903b\u8f91\u3002\u6700\u540e\uff0c\u6dfb\u52a0\u5de6\u8282\u70b9\u4ee5\u5b8c\u6210\u987a\u5e8f\u904d\u5386\u3002\u8bf7\u6ce8\u610f\uff0c\u5bf9\u6bcf\u4e2a\u5b50\u6811\u91cd\u590d\u6b64\u8fc7\u7a0b\uff0c\u76f4\u5230\u904d\u5386\u6240\u6709\u8282\u70b9\u4e3a\u6b62\u3002\n <\/div>\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
# Insert Node
<\/span> def <\/span>insert(self, data):
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>()
# Inorder traversal
<\/span> # Left -> Root -> Right
<\/span> def <\/span>inorderTraversal(self, root):
res = []
if <\/span>root:
res = self.inorderTraversal<\/span>(root.left)
res.append<\/span>(root.data)
res = res + self.inorderTraversal<\/span>(root.right)
return <\/span>res
root = Node(27)
root.insert<\/span>(14)
root.insert<\/span>(35)
root.insert<\/span>(10)
root.insert<\/span>(19)
root.insert<\/span>(31)
root.insert<\/span>(42)
print(root.inorderTraversal<\/span>(root))
<\/span><\/code><\/pre>\n<\/p><\/div>\n [10, 14, 19, 27, 31, 35, 42]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u524d\u5e8f\u904d\u5386<\/h2>\n
\n
\u5728\u4e0b\u9762\u7684python\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Node\u7c7b\u4e3a\u6839\u8282\u70b9\u4ee5\u53ca\u5de6\u53f3\u8282\u70b9\u521b\u5efa\u5360\u4f4d\u7b26\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u63d2\u5165\u51fd\u6570\u4ee5\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u6811\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u5e76\u5148\u6dfb\u52a0\u6839\u8282\u70b9\uff0c\u7136\u540e\u518d\u6dfb\u52a0\u5de6\u8282\u70b9\u6765\u5b9e\u73b0\u9884\u904d\u5386\u903b\u8f91\u3002\u6700\u540e\uff0c\u6dfb\u52a0\u53f3\u8282\u70b9\u4ee5\u5b8c\u6210\u9884\u904d\u5386\u3002\u8bf7\u6ce8\u610f\uff0c\u5bf9\u6bcf\u4e2a\u5b50\u6811\u90fd\u91cd\u590d\u6b64\u8fc7\u7a0b\uff0c\u76f4\u5230\u904d\u5386\u6240\u6709\u8282\u70b9\u4e3a\u6b62\u3002\n <\/div>\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
# Insert Node
<\/span> def <\/span>insert(self, data):
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>()
# Preorder traversal
<\/span> # Root -> Left ->Right
<\/span> def <\/span>PreorderTraversal(self, root):
res = []
if <\/span>root:
res.append<\/span>(root.data)
res = res + self.PreorderTraversal<\/span>(root.left)
res = res + self.PreorderTraversal<\/span>(root.right)
return <\/span>res
root = Node(27)
root.insert<\/span>(14)
root.insert<\/span>(35)
root.insert<\/span>(10)
root.insert<\/span>(19)
root.insert<\/span>(31)
root.insert<\/span>(42)
print(root.PreorderTraversal<\/span>(root))
<\/span><\/code><\/pre>\n<\/p><\/div>\n[27, 14, 10, 19, 35, 31, 42]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u540e\u5e8f\u904d\u5386<\/h2>\n
\n
\u5728\u4e0b\u9762\u7684python\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Node\u7c7b\u4e3a\u6839\u8282\u70b9\u4ee5\u53ca\u5de6\u53f3\u8282\u70b9\u521b\u5efa\u5360\u4f4d\u7b26\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u63d2\u5165\u51fd\u6570\u4ee5\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u6811\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u5e76\u5148\u6dfb\u52a0\u5de6\u8282\u70b9\uff0c\u518d\u6dfb\u52a0\u53f3\u8282\u70b9\u6765\u5b9e\u73b0\u540e\u904d\u5386\u903b\u8f91\u3002\u6700\u540e\uff0c\u6dfb\u52a0\u6839\u8282\u70b9\u6216\u7236\u8282\u70b9\u4ee5\u5b8c\u6210\u540e\u987a\u5e8f\u904d\u5386\u3002\u8bf7\u6ce8\u610f\uff0c\u5bf9\u6bcf\u4e2a\u5b50\u6811\u90fd\u91cd\u590d\u6b64\u8fc7\u7a0b\uff0c\u76f4\u5230\u904d\u5386\u6240\u6709\u8282\u70b9\u4e3a\u6b62\u3002\n <\/div>\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
# Insert Node
<\/span> def <\/span>insert(self, data):
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>()
# Postorder traversal
<\/span> # Left ->Right -> Root
<\/span> def <\/span>PostorderTraversal(self, root):
res = []
if <\/span>root:
res = self.PostorderTraversal<\/span>(root.left)
res = res + self.PostorderTraversal<\/span>(root.right)
res.append<\/span>(root.data)
return <\/span>res
root = Node(27)
root.insert<\/span>(14)
root.insert<\/span>(35)
root.insert<\/span>(10)
root.insert<\/span>(19)
root.insert<\/span>(31)
root.insert<\/span>(42)
print(root.PostorderTraversal<\/span>(root))
<\/span><\/code><\/pre>\n<\/p><\/div>\n[10, 19, 14, 31, 42, 35, 27]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python Tree\u904d\u5386zh-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-1073","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1073"}],"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=1073"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1073\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}