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中的二叉搜索树相关推荐

  1. python在excel中插入二维码

    场景 需要在excel中的barcode列中插入二维码图片,方便运营人员打印和数据录入. 下面是python代码生成后的结果: 代码 代码实现分为两个步骤: 1.生成二维码svg图片: 2.将图片插入 ...

  2. python 二重积分_python中求二维积分的方法

    python中一般求解微积分可以使符号积分求出解析解,使用数值积分求出数值解.在计算机的处理当中,数值解往往更有意义.本文介绍python中利用数值积分例程和微分方程求解器scipy.integrat ...

  3. 使用python在pycharm中制作二维码

    1.需要安装MyQR包 在cmd中,安装命令为pip install myqr 在pycharm中,在file-setting-project-project interpreter中添加MyQR M ...

  4. matlab 二维数组转一维数组中,将二维数组映射到一维数组上

    C语言使用multidimensional array的行顺序 要用单维数组来模拟这个,可以将行索引乘以宽度,然后添加列索引: int array[width * height]; int SetEl ...

  5. matlab二维图形函数,在MATLAB中,画二维图形的常用函数为( )。

    [单选题]( )至今仍未走出困境,美欧与俄罗斯由此而展开的战略博弈也仍在持续. [多选题]信号跟踪的主要目的有: [判断题]You have the power to develop the sale ...

  6. [转载] 如何使用 Python 生成酷炫的二维码?

    参考链接: Python | 使用pyqrcode模块生成QR码 新时代,人们有新的追求,自然而然会有新发明的诞生.去年,在"一带一路"国际合作高峰论坛举行期间,20 国青年投票选 ...

  7. 请用python代码表示什么_深度解析什么是二维码?用Python 5行代码生成个性二维码...

    二维码满天飞, 随便扫一扫就能扫到不一样的内容. 有没有好奇什么是二维码? 又是怎么生成的呢? 今天我们就用python 5行代码 生成一个二维码,并且是个性的二维码,想你所想的,先看效果图,准备好微 ...

  8. Python 5行代码生成个性二维码,要不要试一下?

    二维码满天飞, 随便扫一扫就能扫到不一样的内容. 有没有好奇什么是二维码? 又是怎么生成的呢? 今天我们就用python 5行代码 生成一个二维码,并且是个性的二维码,想你所想的,先看效果图,准备好微 ...

  9. c语言字符串二维数组的动态分配应,C语言中动态分配二维数组复习过程.doc

    C语言中动态分配二维数组复习过程.doc C语言中动态分配二维数组在C中动态分配内存的,对于单个变量,字符串,一维数组等,都是很容易的.C中动态分配二维数组的方法,很少有C语言书中描述,我查找了有的C ...

最新文章

  1. 2021年web前端发展方向有哪些
  2. html false插件,解析webpack插件html-webpack-plugin
  3. HTTP权威指南阅读笔记五:Web服务器
  4. Pinnacle Studio Ultimate中文版
  5. win7怎么看服务器文件管理,Win7库文件管理器怎么用 Win7库功能及其使用方法详解...
  6. Visual Studio 2017 15.9预览版3支持ARM64 for UWP
  7. Hitv8 文件批量重命名工具
  8. 【es】使用ElasticSearch的44条建议 性能优化
  9. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化dp
  10. 刷程序对车危害_刷ecu对车有影响吗?会伤车吗?
  11. Docker默认存储路径修改
  12. BZOJ4141 THUSC2013 魔塔 贪心
  13. System Center App Controller 2012 Service Pack 1系列文章
  14. 现金支票打印模板excel_好用的支票打印软件
  15. 05Oracle P6培训系列:05创建项目
  16. SQL Server BI Step by step 1 AdventureWorks数据库的安装
  17. CTC算法论文阅读笔记:Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurren
  18. 免费把自己写的网页上传到网上(腾讯云)
  19. HDU5960 可持久化左偏树 k短路问题
  20. uni-app 评论五星

热门文章

  1. Visual Studio 2019 使用 Live Share
  2. 中国到底有多少个.NET 程序员?都在哪个城市写代码?
  3. 【WEB API项目实战干货系列】- API访问客户端(WebApiClient适用于MVC/WebForms/WinForm)(四)
  4. Excel VBA窗体上打印系统时间print now出错原因及解决方案
  5. 【ArcGIS遇上Python】ArcGIS Python获取Shapefile矢量数据字段名称
  6. linux之彻底卸载mysql
  7. windows之如何知道C盘目录下的大文件路径
  8. 剑指offer之分行从上到下打印二叉树
  9. Android之运行Java.lang.UnsatisfiedLinkError的错误解决办法
  10. C++之‘malloc’ was not declared in this scope和invalid conversion from ‘void*’ to ‘char*’