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): ...
随机推荐
- 移动端--touch事件与点透问题
也来说说touch事件与点击穿透问题: http://blog.csdn.net/alex8046/article/details/52299785
- url请求老是有 之前的部分url
url请求的时候老是会有之前的部分url <form action="shiro/register" method="POST"> 解决方法,加上本 ...
- django项目mysql中文编码问题
在做django+mysql项目的时候,遇到中文报错问题. 问题分析:是由于mysql数据库,字符集的问题 在cmd命令行模式进入mysql mysql -uroot -p以root身份进入mysql ...
- 网络编程之Socket详解
在说socket之前.我们先了解下相关的网络知识: 端口 在Internet上有很多这样的主机,这些主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket,并绑定到一个端口上 ...
- Oracle SPA取报告阶段xml解析失败解决方案
SPA的整体测试过程可以参考: 记录一则完整的SPA(10g->11g)测试过程 故障描述:数据库字符集:US7ASCII,在SPA分析阶段正常,但在取报告阶段xml解析失败,具体现象如下: S ...
- Spark Streaming 002 统计单词的例子
1.准备 事先在hdfs上创建两个目录: 保存上传数据的目录:hdfs://alamps:9000/library/SparkStreaming/data checkpoint的目录:hdfs://a ...
- 002-golang安装配置
1.安装位置: 2.环境变量. path的值如下: 3.工作目录.
- C++中位运算
简介 1 位逻辑运算符: & (位 “与”) and ----------------- 2个都为1 才是1-----------0^0 = 0 , 0^1 = 0, 1^0 = 0 ...
- CSS position &居中(水平,垂直)
css position是个很重要的知识点: 知乎Header部分: 知乎Header-inner部分: position属性值: fixed:生成绝对定位的元素,相对浏览器窗口进行定位(位置可通过: ...
- C# SQLite 数据库操作
C# SQLite 数据库操作学习 运行环境:Window7 64bit,.NetFramework4.61,C# 7.0 参考: SQLite 官网 SQL As Understood By SQL ...