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. 修改easydialog标题

    使用easyui作为前台框架极大的节省了项目资源,easyui官网文档中基本上囊括了所有的方法,但一些灵活性的方法文档中是找不到的,比如说动态替换窗口的属性,下边简单介绍些如何快速替换窗体的title ...

  2. es6proxy

    Proxy 支持的拦截操作一览. 对于可以设置.但没有设置拦截的操作,则直接落在目标对象上,按照原先的方式产生结果. (1)get(target, propKey, receiver) 拦截对象属性的 ...

  3. shadow一键安装

    https://blog.csdn.net/qq_4278923/article/details/80909686

  4. github上删除一个项目或者reposity

    1,点击github的头像,选择如下操作. 2.选择要删除的reposity 3.选择settings 4.复制reposity名字,然后下滑鼠标到底部,选择delete this reposity ...

  5. 6.Daemon线程

    1.如下代码: package com.bawei.multithread; public class Recursive { private static int counter = 0; publ ...

  6. Sqrt Bo (水题)

    #include<bits/stdc++.h> using namespace std; ], comp; ]; int main(){ arr[] = 1LL; ; i < ; i ...

  7. python 内置函数format

    Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数 ...

  8. 转:【专题五】TCP编程

    前言 前面专题的例子都是基于应用层上的HTTP协议的介绍, 现在本专题来介绍下传输层协议——TCP协议,主要介绍下TCP协议的工作过程和基于TCP协议的一个简单的通信程序,下面就开始本专题的正文了. ...

  9. WebStorm: The Smartest JavaScript IDE by JetBrains

    WebStorm: The Smartest JavaScript IDE by JetBrains https://www.jetbrains.com/webstorm/?fromMenu

  10. 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明

    以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明 为了让你的Ðapp运行上以太坊,一种选择是使用web3.js library提供的web3.对象.底层实 ...