一、二叉树

from collections import deque

class BiTreeNode:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None a = BiTreeNode('A')
b = BiTreeNode('B')
c = BiTreeNode('C')
d = BiTreeNode('D')
e = BiTreeNode('E')
f = BiTreeNode('F')
g = BiTreeNode('G') e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f root = e def pre_order(root):
if root:
print(root.data, end='')
pre_order(root.lchild)
pre_order(root.rchild) def in_order(root):
if root:
in_order(root.lchild)
print(root.data, end='')
in_order(root.rchild) def post_order(root):
if root:
post_order(root.lchild)
post_order(root.rchild)
print(root.data, end='') def level_order(root):
queue = deque()
queue.append(root)
while len(queue) > 0:
node = queue.popleft()
print(node.data,end='')
if node.lchild:
queue.append(node.lchild)
if node.rchild:
queue.append(node.rchild) pre_order(root)
print("")
in_order(root)
print("")
post_order(root)
print("")
level_order(root)

前序,中序,后序,层次遍历

        

        

class BiTreeNode:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None class BST:
def __init__(self, li=None):
self.root = None
if li:
self.root = self.insert(self.root, li[0])
for val in li[1:]:
self.insert(self.root, val) def insert(self, root, val):
if root is None:
root = BiTreeNode(val)
elif val < root.data:
root.lchild = self.insert(root.lchild, val)
else:
root.rchild = self.insert(root.rchild, val)
return root def insert_no_rec(self, val):
p = self.root
if not p:
self.root = BiTreeNode(val)
return
while True:
if val < p.data:
if p.lchild:
p = p.lchild
else:
p.lchild = BiTreeNode(val)
break
else:
if p.rchild:
p = p.rchild
else:
p.rchild = BiTreeNode(val)
break def query(self, root, val):
if not root:
return False
if root.data == val:
return True
elif root.data > val:
return self.query(root.lchild, val)
else:
return self.query(root.rchild, val) def query_no_rec(self, val):
p = self.root
while p:
if p.data == val:
return True
elif p.data > val:
p = p.lchild
else:
p = p.rchild
return False def in_order(self, root):
if root:
self.in_order(root.lchild)
print(root.data, end=',')
self.in_order(root.rchild) tree = BST()
for i in [1,5,9,8,7,6,4,3,2]:
tree.insert_no_rec(i)
tree.in_order(tree.root)
#print(tree.query_no_rec(12))

Python算法——二叉树的更多相关文章

  1. python算法-二叉树广度优先遍历

    广度优先遍历:优先遍历兄弟节点,再遍历子节点 算法:通过队列实现-->先进先出 广度优先遍历的结果: 50,20,60,15,30,70,12 程序遍历这个二叉树: # encoding=utf ...

  2. Python算法-二叉树深度优先遍历

    二叉树 组成: 1.根节点  BinaryTree:root 2.每一个节点,都有左子节点和右子节点(可以为空)  TreeNode:value.left.right 二叉树的遍历: 遍历二叉树:深度 ...

  3. GitHub标星2.6万!Python算法新手入门大全

    今天推荐一个Python学习的干货. 几个印度小哥,在GitHub上建了一个各种Python算法的新手入门大全,现在标星已经超过2.6万.这个项目主要包括两部分内容:一是各种算法的基本原理讲解,二是各 ...

  4. 安装Python算法库

    安装Python算法库 主要包括用NumPy和SciPy来处理数据,用Matplotlib来实现数据可视化.为了适应处理大规模数据的需求,python在此基础上开发了Scikit-Learn机器学习算 ...

  5. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

  6. 【DataStructure In Python】Python模拟二叉树

    使用Python模拟二叉树的基本操作,感觉写起来很别扭.最近做编译的优化,觉得拓扑排序这种东西比较强多.近期刷ACM,发现STL不会用实在太伤了.决定花点儿时间学习一下STL.Boost其实也很强大. ...

  7. python算法(一)

    python算法(一) 一.求数x的因子 x=100 divisors=()#初始化空的元组 for i in range(1,x): if x%i==0: divisors=divisors+(i, ...

  8. Python实现二叉树的四种遍历

    对于一个没学过数据结构这门课程的编程菜鸟来说,自己能理解数据结构中的相关概念,但是自己动手通过Python,C++来实现它们却总感觉有些吃力.递归,指针,类这些知识点感觉自己应用的不够灵活,这是自己以 ...

  9. Python算法与数据结构--求所有子数组的和的最大值

    Python算法与数据结构--求所有子数组的和的最大值 玄魂工作室-玄魂 玄魂工作室秘书 玄魂工作室 昨天 题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个 ...

随机推荐

  1. PHP获取DHCP分配的本机IP

    在搭建本地环境的时候,需要用到多个设备,有服务器.打印机连接接设备等.因为DHCP动态分配IP,所以每次重连都会发生IP地址的变更. 解决办法就是将每个设备的本机IP上传到统一的地方保存.因为使用RE ...

  2. 用Windbg来分析.Net程序的dump

    介绍 1. 什么是Windbg WinDbg是微软发布的一款相当优秀的源码级(source-level)调试工具,可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件. WinDbg是微 ...

  3. 副本机制与副本同步------《Designing Data-Intensive Applications》读书笔记6

    进入到第五章了,来到了分布式系统之中最核心与复杂的内容:副本与一致性.通常分布式系统会通过网络连接的多台机器上保存相同数据的副本,所以在本篇之中,我们来展开看看如何去管理和维护这些副本,以及这个过程之 ...

  4. JavaScript判断对象类型及节点类型、节点名称和节点值

    一.JavaScript判断对象类型 1.可以使用typeof函数判断对象类型 function checkObject1(){ var str="str"; console.lo ...

  5. HUST 1583 长度单位

    1583 - 长度单位 时间限制:1秒 内存限制:128兆 536 次提交 103 次通过 题目描述 我们生活中常用的长度单位有英尺.英寸和厘米,众所周知它们之间的换算关系每英寸等于3厘米,而每英尺等 ...

  6. Codeforces 839E Mother of Dragons【__builtin_popcount()的使用】

    E. Mother of Dragons time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

  7. bzoj 1705;poj 3612:[Usaco2007 Nov]Telephone Wire 架设电话线

    Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= ...

  8. Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)

    A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...

  9. BC#64 4.Tree

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5589 对于u,v的xor和就是u到根的xor和 xor上 v到根的xor和.看到n<=5w,考虑莫队 ...

  10. [国嵌攻略][065][DM9000驱动程序设计]

    移植代码:通过已有的可用的代码修改到新环境下运行. 代码编写: 初始化网卡 1.选中网卡 nLAN_CS BWSCON(0x48000000) DW4:01 16bit BANKCON4(0x4800 ...