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. c# 利用MailKit.IMap 收取163邮件

    最近我要做一个爬虫.这个爬虫需要如下几个步骤: 1 填写注册内容(需要邮箱注册) 2 过拖拽验证码(geetest) 3 注册成功会给邮箱发一封确认邮箱 4 点击确认邮箱中的链接 完成注册 我这里就采 ...

  2. Python Shell 中敲击方向键显示「^[[C^[[D],问题解决

    碰到问题后,在网上搜索. 有帖子建议:yum install -y ncurses-devel,我这个2.7.13版本的在Linux下不行.估计是解决python3.x的方案. 尝试网上建议的,装了 ...

  3. IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)

    一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...

  4. summaryなな

    Word如果遇到有空白页不能删除的情况,将光标定位在空白页前一页的末尾,然后按Delete键就可以删除空白页了. 实时计算,强调的是实时.比如小明要查看他去年一年的消费总额度,那么当小明点下统计按钮的 ...

  5. DES加解密 cbc模式 的简单讲解 && C++用openssl库来实现的注意事项

    DES cbc是基于数据块加密的.数据块的长度为8字节64bit.以数据块为单位循环加密,再拼接.每个数据块加密的秘钥一样,IV向量不同.第一个数据快所需的IV向量,需要我们提供,从第二个数据块开始, ...

  6. Android -- 自定义ViewGroup实现FlowLayout效果

    1,在开发的时候,常在我们的需求中会有这种效果,添加一个商品的一些热门标签,效果图如下: 2,从上面效果可以看得出来,这是一个自定义的ViewGroup,然后实现换行效果,让我们一起来实现一下 自定义 ...

  7. [转]js刷新父窗体

    浮层内嵌iframe及frame集合窗口,刷新父页面的多种方法   <script language=JavaScript>       parent.location.reload(); ...

  8. sitecore系列教程之Sitecore个性化-配置文件,模式和角色

    这是利用Sitecore规则引擎实现数字化转换的三部分系列的第二部分.阅读上一篇文章,通过为您的个性化体验定义内容策略来设置基础.   Sitecore有一个非常强大的规则引擎,可以帮助推动个性化的用 ...

  9. SQL 中的连接查询

    关于SQL的应用,肯定离不开查询,而相对复杂的查询,总是离不开对表的连接,单个表操作的并不罕见,但是在应用环境大多数的查询都是针对2.3个表甚至更多的表7,至于连接,有内连接.外链接.交叉连接之分,每 ...

  10. django之admin源码解析

    解析admin的源码 第一步:项目启动,加载settings文件中的 INSTALLED_APPS 里边有几个app就加载几个,按照注册顺序来执行. 第二步:其中加载的是admin.py,加载每一个a ...