#coding=utf8
node_list=[5,3,6,2,4,None,8,1,None,None,None,7,9] class Node:
def __init__(self,item):
self.item = item
self.child1 = None
self.child2 = None class Tree:
def __init__(self):
self.root = None def add(self, item):
node = Node(item)
if self.root is None:
self.root = node
else:
q = [self.root] while True:
pop_node = q.pop(0)
if pop_node.child1 is None:
pop_node.child1 = node
return
elif pop_node.child2 is None:
pop_node.child2 = node
return
else:
q.append(pop_node.child1)
q.append(pop_node.child2) def traverse(self): # 层次遍历
if self.root is None:
return None
q = [self.root]
res = [self.root.item]
while q != []:
pop_node = q.pop(0)
if pop_node.child1 is not None:
q.append(pop_node.child1)
res.append(pop_node.child1.item) if pop_node.child2 is not None:
q.append(pop_node.child2)
res.append(pop_node.child2.item)
return res def preorder(self,root): # 先序遍历
if root is None:
return []
result = [root.item]
left_item = self.preorder(root.child1)
right_item = self.preorder(root.child2)
return result + left_item + right_item def inorder(self,root): # 中序序遍历
if root is None:
return []
result = [root.item]
left_item = self.inorder(root.child1)
right_item = self.inorder(root.child2)
return left_item + result + right_item def postorder(self,root): # 后序遍历
if root is None:
return []
result = [root.item]
left_item = self.postorder(root.child1)
right_item = self.postorder(root.child2)
return left_item + right_item + result t = Tree()
for i in node_list:
t.add(i)
print('层序遍历:',t.traverse())
#print('先序遍历:',t.preorder(t.root))
#print('中序遍历:',t.inorder(t.root))
print('后序遍历:',t.postorder(t.root))

  

python二叉树练习的更多相关文章

  1. Python --- 二叉树的层序建立与三种遍历

    二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...

  2. Python - 二叉树, 堆, headq 模块

    二叉树 概念 二叉树是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树), 或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树组成. 特点 每个结点最多有两颗子树,所 ...

  3. python 二叉树实现带括号的四则运算(自学的孩子好可怜,不对的地方请轻责)

    #!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...

  4. python二叉树递归算法之后序遍历,前序遍历,中序遍历

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2016-11-18 08:53:45 # @Author : why_not_try ...

  5. python 二叉树实现

    二叉树实现思想 1.把每个节点都看作是一个对象包含以下特征: 节点的当前值 节点的左孩子(存储比当前节点值小的节点对象) 节点右孩子(存储比当前节点值大的节点对象) 2.二叉树就是以根节点开始的连续的 ...

  6. python 二叉树实现带括号的四则运算

    #!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...

  7. python二叉树简单实现

    二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...

  8. python二叉树染色-有严重BUG

    #coding:utf-8 ''' 二叉树涂黑 输入: 5 2 1 -1 4 2 -1 5 4 -1 3 1 1 2 输出: 3 第二题是:斗地主 ''' import sys b=list() cl ...

  9. python 二叉树计算器

    例子:计算1+2+3+4的值 代码: class Buffer(object): """字符串处理函数""" def __init__(se ...

  10. python二叉树的遍历,递归和非递归及相关其它

    # encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...

随机推荐

  1. Idea使用Maven创建Java Web项目

    最近学到了Java Web项目,使用Idea和Maven创建Java Web的时候遇到了诸多问题,最多的还是404问题.现在记录一下解决方案. 一.使用maven创建一个web项目,这一步网上都有,下 ...

  2. RelativeLayout中include 控件覆盖重叠的问题

    RelativeLayout直接include另一个layout是会把include中的控件与当前layout中的控件覆盖重叠,经过查资料 其中的include标签一定要加上(因为include中不指 ...

  3. threading.local学习

    多线程抢占问题 import time import threading obj = 5 def task(arg): global obj obj = arg time.sleep(1) print ...

  4. 无法定位程序输入点 InitializeCriticalSectionEx、GetTickCount64

    (1)方法一:在vc项目中把对应的方法名改为 InitializeCriticalSection.GetTickCount. (2)方法二:添加如下定义#define WINVER           ...

  5. 20155324 《Java程序设计》实验五 网络编程与安全

    20155324 <Java程序设计>实验五 网络编程与安全 实验内容 任务一 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上面功能中获取的表达式中 ...

  6. asp.net mvc 多文件上传

    @{ ViewBag.Title = "多文件上传测试"; } <h2>多文件上传测试</h2> <form action="/Demo/I ...

  7. C++ 函数的重载和参数默认值

    函数的重载和参数默认值视频教程 函数的重载注意事项: 只会根据三项内容进行重载:参数的个数.参数的类型.参数的顺序 参数默认值: 参数的默认值可以在函数的定义中也可以在函数的声明中,但不能同时有 从第 ...

  8. qt5下载与安装,VS2017的环境配置

    下载地址  http://download.qt.io/archive/qt/ 安装 选择安装路径 ,只有没有中文即可 二.Qt与VS2017相关联 1.打开VS2017,选择工具 - 拓展和更新 2 ...

  9. 总结PHP如何获取当前主机、域名、网址、路径、端口和参数等

    //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br />"; //获取网页地址 echo $_SERVER['PHP_SELF']. ...

  10. Hbase思维导图之数据存储