class Node(object):
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right class BTree(object):
def __init__(self, root=None):
self.root = root def is_empty(self):
if self.root is None:
return True
else:
return False # 先序遍历(递归,recursion)
def pre_order(self, node):
if node is None:
return
print(node.data)
self.pre_order(node.left)
self.pre_order(node.right) # 中序遍历(递归)
def in_order(self, node):
if node is None:
return
self.in_order(node.left)
print(node.data)
self.in_order(node.right) # 后序遍历(递归)
def post_order(self, node):
if node is None:
return
self.post_order(node.left)
self.post_order(node.right)
print(node.data) # 先序遍历(非递归)
def preorder(self, node):
stack = []
while node or stack:
if node is not None:
print(node.data)
stack.append(node)
node = node.left
else:
node = stack.pop()
node = node.right # 中序遍历(非递归)
def inorder(self, node):
stack = []
while node or stack:
if node:
stack.append(node)
node = node.left
else:
node = stack.pop()
print(node.data)
node = node.right # 后序遍历(非递归)
def postorder(self, node):
stack = []
queue = []
queue.append(node)
while queue:
node = queue.pop()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
stack.append(node)
while stack:
print(stack.pop().data) # 水平遍历
def levelorder(self, node):
if node is None:
return
queue = [node]
while queue:
node = queue.pop(0)
print(node.data)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right) # 根据前序遍历和中序遍历,求后序遍历
def findTree(self, preList, midList, afterList):
'''
preList = list('DBACEGF')
midList = list('ABCDEFG') afterList = ['A', 'C', 'B', 'F', 'G', 'E', 'D']
'''
if len(preList)==0:
return
if len(preList)==1:
afterList.append(preList[0])
return
root = preList[0]
n = midList.index(root)
self.findTree(preList[1:n+1], midList[:n], afterList)
self.findTree(preList[n+1:], midList[n+1:], afterList)
afterList.append(root) #return afterList '''
n1 = Node(data=1)
n2 = Node(2,n1,None)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5,n3,n4)
n6 = Node(6,n2,n5)
n7 = Node(7,n6,None)
n8 = Node(8)
root = Node(0,n7,n8)
'''
root = Node(0, Node(7, Node(6, Node(2, Node(1)), Node(5, Node(3), Node(4)))), Node(8)) bt = BTree(root)
print('pre_order......')
print(bt.pre_order(bt.root))
print('in_order......')
print(bt.in_order(bt.root))
print('post_order.....')
print(bt.post_order(bt.root)) preList = list('DBACEGF')
midList = list('ABCDEFG')
afterList = [] bt.findTree(preList, midList, afterList)
print(afterList)

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. Redis客户端开发包:Jedis学习-高级应用

    事务 Jedis中事务的写法是将redis操作写在事物代码块中,如下所示,multi与exec之间为具体的事务. jedis.watch (key1, key2, ...); Transaction ...

  2. Linux high memory 学习总结

    在free命令中有个参数l,它表示 show detailed low and high memory statistics.其实最先是对High Memory总是为零有些不解(Linux是64为). ...

  3. 随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    public class Testlvyou extends Thread{ @Override public void run() { test(); } private void test() { ...

  4. (转,有改动)测试网页响应时间的shell脚本[需要curl支持]

    用法及返回结果如下: [root@myserver01 tmp]# sh test_web.sh -n500 http://www.baidu.com Request url: http://www. ...

  5. cat,tac,more

    cat VS tac cat是查看文本文件的内容,tac是cat反过来,反向查看文件 $cat 1.txt ls: cannot access ee: No such file or director ...

  6. 模块module

    python中的Module相当于C++中头文件和命名空间的组合体,便于代码的组织,任何一个python代码的文件都是一个Module,都可以被其他模块import import,from...imp ...

  7. linux系统的7种运行级别

    Linux系统有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多 ...

  8. 实例浅析epoll的水平触发和边缘触发,以及边缘触发为什么要使用非阻塞IO

    一.基本概念                                                          我们通俗一点讲: Level_triggered(水平触发):当被监控的 ...

  9. SCCM 客户端的修复

    1. Stopping the SMS Agent Host service (net stop ccmexec) 2. Stopping the WMI service (net stop winm ...

  10. [转]simple sample to create and use widget for nopcommerce

    本文转自:http://badpaybad.info/simple-sample-to-create-and-use-widget-for-nopcommerce Here is very simpl ...