二叉搜索树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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/298682.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c语言输入一个数存数组,//从键盘上输入若干整数,并将其存入数组中,并统计输入数据的个...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼//从键盘上输入若干整数&#xff0c;并将其存入数组中&#xff0c;并统计输入数据的个数。#include#include#include#includeint sum_num(char [],int []);int cou_num(char [][81],int,int[]);int txt(char [][81],int);//主函数m…

你吃的瓜子仁,真是老奶奶磕出来的?!

全世界只有3.14 % 的人关注了爆炸吧知识本文转载自微信公众号一只学霸&#xff08;ID&#xff1a;bajie203&#xff09;萌萌不是挺爱吃瓜子仁吗有次他吃的时候大毛在旁边看着看着突然想到一个问题吓得我反思了一下自己是怎么和他们考上同个学校的没思考出来吓得我给大家写了这篇…

ASP.NET GridView控件匯出EXCEL-移除控件,只是顯示文本

ASP.NET GridView控件匯出EXCEL-移除控件&#xff0c;只是顯示文本下午 05:10 2011/2/22 將GridView中的TextBox&#xff0c;DropDownList&#xff0c;LinkButton去掉&#xff0c;顯示文本。 public void ClearGridControls(ref Control sourceControl) { for (int i…

android脚步---不同activity之间参数传递

现在有两个activity&#xff0c;一个是mainactivity&#xff0c;一个是detectactivity 后者需要调用前者的一个参数&#xff0c;这里用到了intent getextras(); putextras();参数传递, 看代码&#xff0c;首先两个activity之间有关联&#xff0c;第一个activity里面有一个Butt…

持续20年,一场威胁Linux存亡的诉讼终结束

文 | 局长出品 | OSC开源社区&#xff08;ID&#xff1a;oschina2013&#xff09;一场持续将近 20 年、曾被认为会威胁 Linux 存亡的诉讼终于迎来了尾声。这场诉讼开始于 2003 年&#xff0c;不过其背后的事件最早可追溯到 1998 年。当时 IBM 和 Santa Cruz Operation&#xff…

shell grep 变量_老司机给出的关于 shell 脚本的8个建议,必收!

这八个建议&#xff0c;来源于键者几年来编写 shell 脚本的一些经验和教训。事实上开始写的时候还不止这几条&#xff0c;后来思索再三&#xff0c;去掉几条无关痛痒的&#xff0c;最后剩下八条。毫不夸张地说&#xff0c;每条都是精挑细选的&#xff0c;虽然有几点算是老生常谈…

c语言两个长整数相加,二个超长正整数的相加

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼看看这个,定义数组的长度为251,可以实现最长250位的两个整数相加.#include#includemain(){int i;char str1[251]{0};char str2[251]{0};char str_rut[252]{0};int len_str1,len_str2,len_rut0;int flag0; /*定义初始变量*/clrscr()…

python input函数无法输入字符串_Python手把手教程之用户输入input函数

函数input() 函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。 例如,下面的程序让用户输入一些文本,再将这些文本呈现给用户: message = input("Tell me something, and I will repeat it back to you: &…

修改SDE中自动生成的web.xml文件

SDE中的web.xml文件是自动生成&#xff0c;所以&#xff0c;不能直接修改&#xff0c;只能修改SDE的模版文件。<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />模版文件的位置如下&#xff1a;C:\SDE4\Java\tools\settings\prfr…

不是说好一起长大的吗?

1 仿佛闻到了嫉妒的味道&#xff01;2 阿拉&#xff1a;不是说好一起长大的吗&#xff1f;3 还有这么小的菠萝蜜&#xff1f;&#xff1f;4 你以为它是个橘子其实它并不是5 当我吃到自己喜欢吃的东西时……6 以后吃完小龙虾&#xff0c;千万别扔&#xff0c;有妙用7 这是啥玩意…

javascript:设置URL参数的方法,适合多条件查询

适用场景&#xff1a;多条件查询情况&#xff0c;如下图所示&#xff1a; 通过设置URL参数&#xff0c;再结合数据源控件设置的RUL参数&#xff0c;就能进行简单的多条件查询了。 javascript函数&#xff1a; <mce:script type"text/javascript"><!-- //设置…

SQL点滴19—T-SQL中的透视和逆透视

原文:SQL点滴19—T-SQL中的透视和逆透视透视 今天抽一点时间来看看透视和逆透视语句&#xff0c;简单的说就是行列转换。假设一个销售表中存放着产品号&#xff0c;产品折扣&#xff0c;产品价格三个列&#xff0c;每一种产品号可能有多种折扣&#xff0c;每一种折扣只对应一个…

Magicodes.IE 2.5.5.3发布

2.5.5.32021.08.27修复Append方式导出多个sheet时&#xff0c;发生“Tablename is not unique”错误&#xff0c;具体见#299。2.5.5.22021.08.24添加对Abp模块的包装&#xff0c;具体见#318。Magicodes.IE.Excel.Abp&#xff08;MagicodesIEExcelModule&#xff09;注册IExcelE…

C语言阿斯码,木叶四位上忍设定各不相同,网红负责秀操作,她只需要美就够了...

原标题&#xff1a;木叶四位上忍设定各不相同&#xff0c;网红负责秀操作&#xff0c;她只需要美就够了木叶四位上忍设定各不相同&#xff0c;网红负责秀操作&#xff0c;她只需要美就够了说道忍界网红&#xff0c;那一定就是卡卡西了。卡卡西在《火影》当中的表现俘获了大批小…

80岁COBOL码农:扶我起来,这个bug我会修!

95&#xff05;的 ATM 交易通过 COBOL 程序&#xff0c;80&#xff05;的现场交易依赖于它们&#xff0c;超过 40&#xff05;的银行仍然使用 COBOL 作为其系统的基础。由于年轻人懂 COBOL 的比较少&#xff0c;美国康涅狄格州劳工部正在召回经验丰富的退休 COBOL 人员。来源&a…

小心使用宏

开发过程中&#xff0c;会经常使用宏定义&#xff0c;偶尔还会碰到重复定义的宏&#xff0c;有些时候会造成不良影响。 见如下例子&#xff1a; Test.h #ifndef GUARD_TEST_H #define GUARD_TEST_H class CTest { public: CTest(); virtual ~CTest(); void Display(void); publ…

DMZ区

DMZ是英文“demilitarized zone”的缩写&#xff0c;中文名称为“隔离区”&#xff0c;也称“非军事化区”。它是为了解决安装防火墙后外部网络不能访问内部网络服务器的问题&#xff0c;而设立的一个非安全系统与安全系统之间的缓冲区&#xff0c;这个缓冲区位于企业内部网络和…

果园机器人是什么文体_果园机器人课文原文

秋天到了&#xff0c;果农们又高兴又发愁。高兴的是水果又丰收啦&#xff0c;发愁的是需要做的事太多了。要把果子从树上摘下来&#xff0c;要把它们运到很远的地方去卖&#xff0c;实在忙不过来。你会想&#xff0c;可以让机器人来帮忙呀。是的&#xff0c;现在已经有了会干农…

json_encode ajaxReturn getJSON

之前我在页面中getJSON("") $.getJSON("/index.php/Shopcar/getspcar",function(data){ spcars.splistdata; console.log(data); }); 在访问的方法中这样写的 $this->scres $sc->query($sql); $data json_encode($this->scres); $this->aja…

数据资产纳入国资保值增值考核

首先是国资云近期横空出世&#xff0c;国资云的推广预示着党政及国企未来将坚持私有云技术路线。从天津、四川等省市国资云平台的建设方式来看&#xff0c;未来党政及国企部门的业务系统上云将坚持私有云的技术路线&#xff0c;由此可能对未来国内云计算市场带来深远影响。国资…