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. python date time

    //test.py import time ticks = time.time()print tickslocaltime = time.localtime(time.time())print loc ...

  2. Cocos Creator 计时器的延时循环试用方法

    *****计时器的一些运用***** //计算1次的计时器,2秒后执行 this.scheduleOnce(function(){ this.doSomething(); },2); //每隔5秒执行 ...

  3. 32.js 判断当前页面是否被浏览

    可以通过document.hidden属性判断当前页面是否是激活状态. 兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+ 兼容性写法示例: va ...

  4. PHP socket通信之UDP

    服务端: //服务器信息 $server = 'udp://127.0.0.1:9998'; //消息结束符号 $msg_eof = "\n"; $socket = stream_ ...

  5. ideal使用eclipse快捷键

    1.修改使用Eclipse风格的快捷键目的是习惯了使用eclipse的快捷键,在使用IDEA时不想重头记一套新的快捷键.按照下面的顺序操作File --> settings --> key ...

  6. python多线程,多进程编程。

    进程,是目前计算机中为应用程序分配资源的最小单位: 线程,是目前计算机中运行应用程序的最小单位: 在实际系统中,其实进程都是被分为线程来实现的,所以参与时间片轮转的是线程: 但是管理应用程序的资源的单 ...

  7. 17. Letter Combinations of a Phone Number(bfs)

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  8. jQuery文档操作--empty()和remove()

    empty() 概述 删除匹配的元素集合中所有的子节点 <!DOCTYPE html> <html> <head> <meta charset="U ...

  9. 关于git上的一些错误信息

    如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...

  10. python mmap对象

    ----使用内存映射的原因 为了随机访问文件的内容,使用mmap将文件映射到内存中是一个高效和优雅的方法.例如,无需打开一个文件并执行大量的seek(),read(),write()调用,只需要简单的 ...