Python多叉树的构造及取出节点数据(treelib)的方法
作者:qxqxqzz 时间:2021-11-07 05:00:16
项目:
基于Pymysql的专家随机抽取系统
引入库函数:
>>> import treelib
>>> from treelib import Tree, Node
构造节点类:
>>> class Nodex(object): \
def __init__(self, num): \
self.num = num
构造多叉树:(注意节点的第2个属性已标红,它是节点ID,为str类型,不能与其他节点重复,否则构建节点失败)
>>> tree1 = Tree()
>>> tree1.create_node('Root', 'root', data = Nodex('3'));\
tree1.create_node('Child1', 'child1', parent = 'root', data =Nodex('4'));\
tree1.create_node('Child2', 'child2', parent = 'root', data =Nodex('5'));\
tree1.create_node('Child3', 'child3', parent = 'root', data =Nodex('6'));\
构造结果:
>>> tree1.show()
Root
├── Child1
├── Child2
└── Child3
>>> tree1.show(data_property = 'num')
3
├── 4
├── 5
└── 6
打印节点信息:(其实节点是以字典的形式存储的)
>>> tree1.nodes
{'root': Node(tag=Root, identifier=root, data=<__main__.Nodex object at 0x000002265C6A9550>), 'child1': Node(tag=Child1, identifier=child1, data=<__main__.Nodex object at 0x000002265C6A9E10>)}
取出child1节点存储的数据:
>>> tree1.nodes['child1'].data.num
'4'
来源:https://blog.csdn.net/qxqxqzzz/article/details/81415843
标签:Python,多叉树,构造,节点
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用蜜罐来阻止垃圾评论
2007-11-06 12:35:00
![](https://img.aspxhome.com/file/UploadPic/200711/6/200711612588568s.gif)
SQL 2008邮件故障排除:发送测试电子邮件
2008-12-02 14:28:00
python使用pandas处理excel文件转为csv文件的方法示例
2021-09-13 07:15:52
python实现定时发送邮件到指定邮箱
2023-05-02 05:04:02
![](https://img.aspxhome.com/file/2023/3/76933_0s.png)
python3发送邮件需要经过代理服务器的示例代码
2023-07-27 01:00:44
CodeIgniter分页类pagination使用方法示例
2023-11-24 10:33:05
PyCharm 2020.2.2 x64 下载并安装的详细教程
2023-11-29 13:36:32
![](https://img.aspxhome.com/file/2023/4/89164_0s.jpg)
FSO组件之文件操作(中)
2010-05-03 11:05:00
BluePrint CSS框架0.9版发布
2009-06-03 21:02:00
python如何编写win程序
2022-12-09 11:48:38
![](https://img.aspxhome.com/file/2023/6/76856_0s.png)
python3实现163邮箱SMTP发送邮件
2021-02-28 07:59:19
![](https://img.aspxhome.com/file/2023/8/63538_0s.jpg)
怎样正确的解决 MySQL 中文模糊检索问题
2008-12-19 17:26:00
避免使用滤镜
2009-10-13 20:30:00
约瑟夫问题的Python和C++求解方法
2023-12-05 15:09:28
python 读取DICOM头文件的实例
2021-12-03 19:30:48
![](https://img.aspxhome.com/file/2023/2/68362_0s.jpg)
Python通过m3u8文件下载合并ts视频的操作
2023-07-19 04:36:51
![](https://img.aspxhome.com/file/2023/3/75243_0s.jpg)
解决python 读取npy文件太大不能完全显示的问题
2021-08-20 12:07:19
![](https://img.aspxhome.com/file/2023/4/82974_0s.jpg)
PHP中str_split()函数的用法讲解
2023-06-05 09:43:15
![](https://img.aspxhome.com/file/2023/5/55375_0s.png)
PHP简单实现正则匹配省市区的方法
2023-11-14 22:24:09
浅谈Python中chr、unichr、ord字符函数之间的对比
2022-02-27 16:02:58