BinaryTree.py

 '''二叉树:是每个节点最多有两个子树(分别称为左子树和右子树)的树结构,二叉树的第i层最多有2**(i-1)个节点,常用于排序或查找'''
class BinaryTree:
def __init__(self,value):
self.__left = None
self.__right = None
self.__data = value #析构函数
def __del__(self):
del self.__data #创建左子树
def insertLeftChild(self,value):
if self.__left:
print('Left child tree already exists.')
else:
self.__left = BinaryTree(value)
return self.__left #创建右子树
def insertRightChild(self,value):
if self.__right:
print('Right child tree already exits.')
else:
self.__right = BinaryTree(value)
return self.__right def show(self):
print(self.__data) #前序遍历
def preOrder(self):
print(self.__data) #输出根节点的值
if self.__left:
self.__left.preOrder() #遍历左子树
if self.__right:
self.__right.preOrder() #遍历右子树 def postOrder(self):
if self.__left:
self.__left.postOrder()
if self.__right:
self.__right.postOrder()
print(self.__data) def inOrder(self): #中序遍历
if self.__left:
self.__left.inOrder()
print(self.__data)
if self.__right:
self.__right.inOrder() if __name__ == '__main__':
print('Please use me a module')

useBinaryTree.py

 from BinaryTree import BinaryTree
root = BinaryTree('root')
b=root.insertRightChild('B')
a=root.insertLeftChild('A')
c=a.insertLeftChild('C')
d=c.insertRightChild('D')
e=b.insertRightChild('E')
f=e.insertLeftChild('F')
root.inOrder()
# C
# D
# A
# root
# B
# F
# E
root.postOrder()
# D
# C
# A
# F
# E
# B
# root
b.inOrder()
# B
# F
# E

Python_二叉树的更多相关文章

  1. python_二叉树简单实现

    今日头条面试题,先做下: 二叉树代码实现 class Node: def __init__(self,item): self.item = item self.child1 = None self.c ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  4. c 二叉树的使用

    简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  5. Java 二叉树遍历右视图-LeetCode199

    题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...

  6. 数据结构:二叉树 基于list实现(python版)

    基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  9. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

随机推荐

  1. (NO.00001)iOS游戏SpeedBoy Lite成形记(七)

    因为我们之前在GameScene中建立的2个数组,分别为player和label的数组.大家可以注意到其中每个元素是一一对应的. 知道了这层关系,我们尝试来更新matchRun方法: CCAction ...

  2. 最新的App上架教程Object-C

    准备 开发者账号 完工的项目 上架步骤 一.创建App ID 二.创建证书请求文件 (CSR文件) 三.创建发布证书 (CER) 四.创建Provisioning Profiles配置文件 (PP文件 ...

  3. Java进阶(二十一)java 空字符串与null区别

    java 空字符串与null区别 1.类型 null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ; ""表示的是一个空字符串, ...

  4. C++中const的实现机制深入分析

    via:http://www.jb51.net/article/32336.htm C语言以及C++语言中的const究竟表示什么?其具体的实现机制又是如何实现的呢?本文将对这两个问题进行一些分析,需 ...

  5. LIRe 源代码分析 3:基本接口(ImageSearcher)

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  6. 插件化开发—动态加载技术加载已安装和未安装的apk

    首先引入一个概念,动态加载技术是什么?为什么要引入动态加载?它有什么好处呢?首先要明白这几个问题,我们先从 应用程序入手,大家都知道在Android App中,一个应用程序dex文件的方法数最大不能超 ...

  7. 基于hashchange导航管理

    想在五一放假的时候写出来,由于放假有点兴奋,心早就跑了,不废话了. 说一下基于hashchange导航管理: 浏览器的历史记录导航是用户非常常用的功能,除了点击前进后退按钮外,Window上的hist ...

  8. python socketserver框架解析

    socketserver框架是一个基本的socket服务器端框架, 使用了threading来处理多个客户端的连接, 使用seletor模块来处理高并发访问, 是值得一看的python 标准库的源码之 ...

  9. ASP.NET平台下从浏览器地址栏输入之后发生的事

    浏览器一般内嵌两个模块: Socket通信模块 → 浏览器将地址栏的数据及其他的数据放入http协议的请求头文件中,Socket将此http请求数据发送到远程服务器端 浏览器引擎渲染模块 → 浏览器接 ...

  10. 3 sum closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...