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

    开发人员经常会碰到老板或上头安排的项目或需求,是自己完全陌生的领域,这个时候就会非常头痛,搜索引擎能解决大部分这些方面的问题,而有时因为自身问题或干脆找不到解决方案而非常抓狂......虽然干开发有1 ...

  2. Dropout正则化和其他方法减少神经网络中的过拟合

    1. 什么是Dropout(随机失活) 就是在神经网络的Dropout层,为每个神经元结点设置一个随机消除的概率,对于保留下来的神经元,我们得到一个节点较少,规模较小的网络进行训练. 标准网络和dro ...

  3. python SMTP

    一.一开始,相信SMTP服务,所以在本机安装了一个 apt-get install sendmail apt-get install sendmail-cf apt-get install squir ...

  4. Dart async proc

    //dart import 'dart:io';import 'dart:async'; Future printDailyNewsDigest1() { print('A:'); File file ...

  5. centos7 cpanm安装,及perl模块安装

    1. cpan安装 yum安装 yum install perl-App-cpanminus.noarch 注意:安装完成后,root及非root用户都可以使用cpanm安装模块,root用户直接用c ...

  6. iOS 新浪微博-5.0 首页微博列表

    首页显示微博列表,是微博的核心部分,这一章节,我们主要是显示出微博的列表. 导入第三方类库 pod 'SDWebImage', '~> 3.7.3' pod 'MJRefresh', '~> ...

  7. ES6 变量的解构

    默认值 let [foo = true] = []; foo // true let [x, y = 'b'] = ['a']; // x='a', y='b' let [x, y = 'b'] = ...

  8. 点击地面时,若鼠标点击的偶数次使得Cube向点击点移动,并且点击奇数次Cube变色

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class ray10 : ...

  9. node.js中ws模块创建服务端和客户端,网页WebSocket客户端

    首先下载websocket模块,命令行输入 npm install ws 1.node.js中ws模块创建服务端 // 加载node上websocket模块 ws; var ws = require( ...

  10. Java Socket入门

    Java Socket底层采用TCP/IP协议通信,通信细节被封装,我们仅仅需要指定IP.端口,便能轻易地创建TCP或UDP连接,进行网络通信.数据的读写,可以使用我们熟悉的stream进行操作. T ...