日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

二叉搜索树python,代表python中的二叉搜索树

發布時間:2023/12/4 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉搜索树python,代表python中的二叉搜索树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

how do i represent binary search trees in python?

解決方案class Node(object):

def __init__(self, payload):

self.payload = payload

self.left = self.right = 0

# this concludes the "how to represent" asked in the question. Once you

# represent a BST tree like this, you can of course add a variety of

# methods to modify it, "walk" over it, and so forth, such as:

def insert(self, othernode):

"Insert Node `othernode` under Node `self`."

if self.payload <= othernode.payload:

if self.left: self.left.insert(othernode)

else: self.left = othernode

else:

if self.right: self.right.insert(othernode)

else: self.right = othernode

def inorderwalk(self):

"Yield this Node and all under it in increasing-payload order."

if self.left:

for x in self.left.inorderwalk(): yield x

yield self

if self.right:

for x in self.right.inorderwalk(): yield x

def sillywalk(self):

"Tiny, silly subset of `inorderwalk` functionality as requested."

if self.left:

self.left.sillywalk()

print(self.payload)

if self.right:

self.right.sillywalk()

etc, etc -- basically like in any other language which uses references rather than pointers (such as Java, C#, etc).

Edit:

Of course, the very existence of sillywalk is silly indeed, because exactly the same functionality is a singe-liner external snippet on top of the walk method:

for x in tree.walk(): print(x.payload)

and with walk you can obtain just about any other functionality on the nodes-in-order stream, while, with sillywalk, you can obtain just about diddly-squat. But, hey, the OP says yield is "intimidating" (I wonder how many of Python 2.6's other 30 keywords deserve such scare words in the OP's judgment?-) so I'm hoping print isn't!

This is all completely beyond the actual question, on representing BSTs: that question is entirely answered in the __init__ -- a payload attribute to hold the node's payload, left and right attribute to hold either None (meaning, this node has no descendants on that side) or a Node (the top of the sub-tree of descendants on the appropriate side). Of course, the BST constraint is that every left descendant of each node (if any) has a payload less or equal than that of the node in question, every right one (again, if any) has a greater payload -- I added insert just to show how trivial it is to maintain that constraint, walk (and now sillywalk) to show how trivial it is to get all nodes in increasing order of payloads. Again, the general idea is just identical to the way you'd represent a BST in any language which uses references rather than pointers, like, for example, C# and Java.

總結

以上是生活随笔為你收集整理的二叉搜索树python,代表python中的二叉搜索树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。