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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【Spark机器学习速成宝典】模型篇05决策树【Decision Tree】(Python版)

    目录 决策树原理 决策树代码(Spark Python) 决策树原理 详见博文:http://www.cnblogs.com/itmorn/p/7918797.html 返回目录 决策树代码(Spar ...

  4. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  5. leetcode Binary Tree Postorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  6. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  7. [leetcode]Binary Tree Preorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...

  8. naive cube implementation in python

    这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=.= 输入大约长这样,依次是 index userid country state city topic cat ...

  9. 文件目录tree显示,python

    #/usr/bin/python import os def travelTree(currentPath, count=0): if not os.path.exists(currentPath): ...

随机推荐

  1. Dotfuscator代码混淆工具的使用

    关于Dotfuscator,VS自带了一个社区版的源代码保护工具,不过它只有基本的混淆命名的功能.专业版功能很多,可以混淆流程,密字符串,加入水印,程序签名等. 工具/原料   Dotfuscator ...

  2. 关于RBAC的文章

    权限系统与RBAC模型概述 RBAC权限管理模型 摘自慕课网的RBAC

  3. HDU 2254 奥运(矩阵+二分等比求和)

    奥运 [题目链接]奥运 [题目类型]矩阵+二分等比求和 &题解: 首先离散化城市,之后就是矩阵快速幂了,但让求的是A^(t1)+A^(t1+1)+...+A^(t2),我先想的是打表,但时间真 ...

  4. ES6class

    类的方法都定义在prototype对象上面,所以类的新方法可以添加在prototype对象上面.Object.assign方法可以很方便地一次向类添加多个方法. 类的内部所有定义的方法,都是不可枚举的 ...

  5. Tomcat 9 和tomcat 8区别以及 tomcat9 新特性

    1.Tomcat 9.0.0.M1 (alpha) 版本的主要特点 详细信息请点击:Tomcat 9.0.0.M1 其他版本信息:详细参见官网,传送门 注明:当前版本要求最低的Java环境为 1.8+ ...

  6. html5-textarea元素

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  7. html5-嵌入图片

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. 20165305 苏振龙《Java程序设计》第三周学习总结

    面向对象和面向过程: 面向对象是相对面向过程而言的,面向过程强调的是功能行为,面向对象是将过程封装进对象,强调具备了功能的对象,面向对象是基于面向过程的. 面向对象的三个特征: 封装,继承,多态: 对 ...

  9. 使用Hive读取ElasticSearch中的数据

    本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员.本文使用的 ...

  10. impala与hive的比较以及impala的有缺点

    最近读的几篇关于impala的文章,这篇良心不错:https://www.biaodianfu.com/impala.html(本文截取部分内容) Impala是Cloudera公司主导开发的新型查询 ...