Tree Implementation with Python
Tree Implementation with Python
List of List

代码如下:
def binary_tree(val):
return [val, [], []]
def insert_left(root, val):
root[1] = binary_tree(val)
def insert_right(root, val):
root[2] = binary_tree(val)
def get_root_val(root):
return root[0]
def set_root_val(root, val):
root[0] = val
def get_left_child(root):
return root[1]
def get_right_child(root):
return root[2]
def preorder(root):
if root:
print(get_root_val(root))
preorder(get_left_child(root))
preorder(get_right_child(root))
def inorder(root):
if root:
inorder(get_left_child(root))
print(get_root_val(root))
inorder(get_right_child(root))
def postorder(root):
if root:
postorder(get_left_child(root))
postorder(get_right_child(root))
print(get_root_val(root))
if __name__ == '__main__':
root = binary_tree('a')
insert_left(root, 'b')
insert_right(root, 'c')
insert_right(get_left_child(root), 'd')
insert_left(get_right_child(root), 'e')
insert_right(get_right_child(root), 'f')
print(root)
# ['a',
# ['b',
# [],
# ['d', [], []]],
# ['c',
# ['e', [], []],
# ['f', [], []]]]
preorder(root) # a b d c e f
inorder(root) # b d a e c f
postorder(root) # d b e f c a
Tree Implementation with Python的更多相关文章
- Huffman Implementation with Python
Huffman Implementation with Python 码表 Token Frequency a 10 e 15 i 12 s 3 t 4 space 13 n 1 生成 Huffman ...
- [Data Structure] Stack Implementation in Python
We can realize a Stack as an adaptation of a Python List. S.push(e)=L.append(e) S.pop()=L.pop() S.to ...
- 【Spark机器学习速成宝典】模型篇05决策树【Decision Tree】(Python版)
目录 决策树原理 决策树代码(Spark Python) 决策树原理 详见博文:http://www.cnblogs.com/itmorn/p/7918797.html 返回目录 决策树代码(Spar ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode Binary Tree Inorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- [leetcode]Binary Tree Preorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...
- naive cube implementation in python
这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=.= 输入大约长这样,依次是 index userid country state city topic cat ...
- 文件目录tree显示,python
#/usr/bin/python import os def travelTree(currentPath, count=0): if not os.path.exists(currentPath): ...
随机推荐
- 查看Andorid应用是32位还是64位
adb shell cat /proc/进程pid/maps 查看linker位数即可
- DataGrip设置长sql语句自动换行
我这个DataGrip是英文版的. 在每个查询窗口(Console)的工具图标里,有个Settings.打开Settings弹出框,试图从Appearance和Editor里找到设置选项,却没有找到. ...
- Vue+webpack项目中实现跨域的http请求
目前Vue项目中对json数据的请求一般使用两个插件vue-resource和axios, 但vue-resource已经不再维护, 而axios是官方推荐的且npm下载量已经170多万,github ...
- 使用spring的特殊bean完成分散配置
1.在使用分散配置时,spring的配置文件applicationContext.xml中写法如下: <!-- 引入db.properties文件, --> <context:pro ...
- sqlserver恢复数据库被挂起
已测试过,直接执行此句后,数据库恢复原状态.数据不会丢失.具体是什么意思,暂时没来得及搞明白 RESTORE database dbname with norecovery
- Rpgmakermv(12) gacha插件系列
很有趣的插件,可以做扭蛋啦,抽奖啦之类的东西.... 简单的示范: a.开始抽奖画面: b.抽奖中 c.随机得到物品 d.查看收集图鉴 e.图鉴内容 1.gacha 作用: get the item ...
- GBDT:梯度提升决策树
http://www.jianshu.com/p/005a4e6ac775 综述 GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Ad ...
- RobotFrameWork(十三)RobotFramework与loadrunner性能测试结合(基于Remote库)
一般我们进行完功能测试,都需要进行下性能测试,那么这章我来介绍下,RobotFramework与loadrunner性能测试的融合,即运行完自动化功能测试,借助RobotFramework的Remot ...
- jQuery效果--show([speed,[easing],[fn]])和hide([speed,[easing],[fn]])
hide([speed,[easing],[fn]]) 概述 隐藏显示的元素 这个就是 'hide( speed, [callback] )' 的无动画版.如果选择的元素是隐藏的,这个方法将不会改变任 ...
- jQuery选择器--:selected和:checked
:selected 概述 匹配所有选中的option元素 <!DOCTYPE html> <html> <head> <meta charset=" ...