二叉树的python实现
1 ''' 2 Created on 2016/12/26 3 Created by freeol.cn 4 一些排序算法的Python实现 5 @author: 拽拽绅士 6 ''' 7 8 import sys 9 from _ast import While 10 from celery.bin.celery import result 11 12 '''顺序存储的二叉树实现(非完全存储)''' 13 class node1(object): 14 def __init__(self, S, L, R, V): 15 self.S = S#自 16 self.L = L#左子 17 self.R = R#右子 18 self.V = V#值 19 class tree1(object): 20 def createTree(self, a): 21 data = [] 22 for n in a: 23 data.append(node1(n[0], n[1], n[2], n[3])) 24 return data 25 def getTree(self, a): 26 return self.createTree(a) 27 28 '''链式存储的二叉树(非完全存储)''' 29 class tree2(object): 30 def __init__(self): 31 self.L = None#Left node 32 self.R = None#Right node 33 self.V = None#value 34 self.tmp = {} 35 def createTree(self, key, tree): 36 if key in self.tmp: 37 tmpN = self.tmp[key] 38 tree.V = tmpN[3] 39 Lkey = tmpN[1] 40 Rkey = tmpN[2] 41 if Lkey != None: 42 Ltree = tree2() 43 Ltree = self.createTree(Lkey, Ltree) 44 tree.L = Ltree 45 if Rkey != None: 46 Rtree = tree2() 47 Rtree = self.createTree(Rkey, Rtree) 48 tree.R = Rtree 49 return tree 50 def getTree(self, a): 51 for n in a: 52 self.tmp[n[0]] = n#收集各节点信息 53 tree = tree2() 54 return self.createTree('1', tree) 55 56 '''判断二叉树存储结构''' 57 def checkTree1orTree2(tree): 58 if type(tree) == list: 59 return 1#顺序存储 60 else: 61 return 2#链式存储 62 63 '''获取根节点''' 64 def root(tree): 65 chk = checkTree1orTree2(tree) 66 if chk == 1:#顺序存储 67 childKeys = {} 68 for t in tree: 69 if t.L != None: 70 childKeys[t.L] = None 71 if t.R != None: 72 childKeys[t.R] = None 73 for t in tree: 74 if t.S not in childKeys: 75 return t 76 else:#链式存储 77 return tree 78 79 '''获取二叉树的度''' 80 def degree(tree): 81 chk = checkTree1orTree2(tree) 82 if chk == 1:#顺序存储 83 return len(tree) 84 else:#链式存储 85 cnt = 1 86 if tree.L != None: 87 cnt += degree(tree.L) 88 if tree.R != None: 89 cnt += degree(tree.R) 90 return cnt 91 92 '''深度''' 93 def deepDegree(tree): 94 chk = checkTree1orTree2(tree) 95 if chk == 1:#顺序存储 96 cnt = 0 97 leafs = []#叶子集 98 branchs = []#枝干集 99 for t in tree: 100 if t.L==None and t.R == None: 101 leafs.append(t) 102 else: 103 branchs.append(t) 104 save_cnt = 0 105 for leaf in leafs:#回溯法 叶->枝->根 106 cnt = 1 107 key = leaf.S 108 tmpBranchs = branchs.copy() 109 i = 0 110 while i < len(tmpBranchs): 111 branch = tmpBranchs[i] 112 if branch.L == key or branch.R == key: 113 cnt+=1 114 key = branch.S 115 i = 0 116 else: 117 i+=1 118 if cnt > save_cnt: 119 save_cnt = cnt 120 return save_cnt 121 else:#链式存储 122 cnt = 1 123 Lcnt = 0 124 Rcnt = 0 125 if tree == None: 126 return 0 127 if tree.L != None: 128 Lcnt = deepDegree(tree.L) 129 if tree.R != None: 130 Rcnt = deepDegree(tree.R) 131 if Lcnt > Rcnt: 132 cnt += Lcnt 133 else: 134 cnt += Rcnt 135 return cnt 136 137 '''链式结构二叉树 138 前序遍历:根节点->左子树->右子树''' 139 def preorder(tree, m, result): 140 if m == 1:#非递归实现(栈) 141 static = []#栈 142 t = tree 143 '''法1 144 while t != None or static != []: 145 while t != None: 146 result.append(t) 147 static.append(t) 148 t=t.L 149 if static != []: 150 t = static.pop() 151 t = t.R 152 ''' 153 static.append(tree) 154 while static: 155 n = static.pop() 156 result.append(n) 157 if n.R: 158 static.append(n.R) 159 if n.L: 160 static.append(n.L) 161 162 else:#递归实现 163 if tree == None: 164 return result 165 result.append(tree) 166 result=preorder(tree.L, 2, result) 167 result=preorder(tree.R, 2, result) 168 return result 169 170 '''链式结构二叉树 171 中序遍历:左子树->根节点->右子树''' 172 def inorder(tree, m, result): 173 if m == 1:#非递归实现(栈) 174 static = []#栈 175 t = tree 176 '''法1 177 while t != None or static != []: 178 while t != None: 179 static.append(t) 180 t=t.L 181 if static != []: 182 t = static.pop() 183 result.append(t) 184 t = t.R 185 ''' 186 while t != None or static != []: 187 while t != None: 188 static.append(t) 189 t = t.L 190 t = static.pop() 191 result.append(t) 192 t = t.R 193 else:#递归实现 194 if tree == None: 195 return result 196 result=inorder(tree.L, 2, result) 197 result.append(tree) 198 result=inorder(tree.R, 2, result) 199 return result 200 201 '''链式结构二叉树 202 后序遍历:左子树->右子树->根节点''' 203 def postorder(tree, m, result): 204 if m == 1:#非递归实现(栈) 205 static = []#栈 206 t = tree 207 mk = None 208 while t != None or static != []: 209 while t != None: 210 static.append(t) 211 t = t.L 212 t = static.pop() 213 if t.R == None or t.R == mk: 214 result.append(t) 215 mk = t 216 t = None 217 else: 218 static.append(t) 219 t = t.R 220 else:#递归实现 221 if tree == None: 222 return result 223 result = postorder(tree.L, 2, result) 224 result = postorder(tree.R, 2, result) 225 result.append(tree) 226 return result 227 228 '''order value print''' 229 def resultPrintV(msg, rs): 230 v=[] 231 for t in rs: 232 v.append(t.V) 233 print(msg, v) 234 235 236 '''期望高度''' 237 238 239 def main(): 240 ''' 1 241 ∧ 242 2 3 243 ∧ ∧ 244 4 5 9 7 245 ∧ ∧ 246 8 6 10 11''' 247 data = [ #原始数据 248 ['1', '2', '3', 1],#Self key, Left key, Right key, Value 249 ['2', '4', '5', 2], 250 ['3', '9', '7', 3], 251 ['4', '8', '6', 4], 252 ['5', '10', '11', 5], 253 ['9', None, None, 9], 254 ['7', None, None, 7], 255 ['8', None, None, 8], 256 ['6', None, None, 6], 257 ['10', None, None, 10], 258 ['11', None, None, 11], 259 ] 260 print('原始数据大小', sys.getsizeof(data)) 261 print('预计二叉树根节点值', 1) 262 print('预计二叉树的度', 11) 263 print('预计二叉树的深度', 4) 264 print('预计前序遍历值的结果', [1, 2, 4, 8, 6, 5, 10, 11, 3, 9, 7]) 265 print('预计中序遍历值的结果', [8, 4, 6, 2, 10, 5, 11, 1, 9, 3, 7]) 266 print('预计后序遍历值的结果', [8, 6, 4, 10, 11, 5, 2, 9, 7, 3, 1]) 267 268 print('========>创建顺序结构二叉树') 269 t1 = tree1().getTree(data)#顺序结构 270 print('顺序结构二叉树大小', sys.getsizeof(t1)) 271 root1 = root(t1) 272 print('顺序结构二叉树根节点值', root1.V) 273 print('顺序结构二叉树的度', degree(t1)) 274 print('顺序结构二叉树的深度', deepDegree(t1)) 275 276 print('========>创建链式结构二叉树') 277 t2 = tree2().getTree(data)#链式结构 278 print('链式结构二叉树大小', sys.getsizeof(t2)) 279 root2 = root(t2) 280 print('链式结构二叉树根节点值', root2.V) 281 print('链式结构二叉树的度', degree(t2)) 282 print('链式结构二叉树的深度', deepDegree(t2)) 283 rs = [];resultPrintV('链式结构 前序遍历值的结果->非递归实现', preorder(t2, 1, rs)) 284 rs = [];resultPrintV('链式结构 前序遍历值的结果->递归实现', preorder(t2, 2, rs)) 285 rs = [];resultPrintV('链式结构 中序遍历值的结果->非递归实现', inorder(t2, 1, rs)) 286 rs = [];resultPrintV('链式结构 中序遍历值的结果->递归实现', inorder(t2, 2, rs)) 287 rs = [];resultPrintV('链式结构 后序遍历值的结果->非递归实现', postorder(t2, 1, rs)) 288 rs = [];resultPrintV('链式结构 后序遍历值的结果->递归实现', postorder(t2, 2, rs)) 289 290 if __name__ == '__main__': 291 main()
转载于:https://www.cnblogs.com/zzss/p/6222029.html
二叉树的python实现相关推荐
- 【数据结构】二叉树的python实现
[数据结构]二叉树的python实现 本博文描述的二叉树是任意二叉树,可以使完全二叉树,也可以是非完全二叉树. 首先声明一个二叉树节点的初始化类TNode,每一个节点都有三个组成部分,节点的元素值,节 ...
- LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法
题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...
- 【剑指offer 07】用迭代和递归两种方法重构二叉树(python实现)
本文讲解一个经典的面试题,使用 python 通过迭代和递归两种方法重构二叉树. 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字 ...
- python构建二叉树_BinaryTree:学习二叉树的Python库
Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 简介: 您是否在为考试.作业或技术面试学习二叉树? Binarytree是一个Python库,它通过一个简 ...
- 二叉树(python实现)
1. BinaryTree (二叉树) 二叉树是有限个元素的集合,该集合或者为空.或者有一个称为根节点(root)的元素及两个互不相交的.分别被称为左子树和右子树的二叉树组成. 二叉树的每个结点至多只 ...
- python 读取内存二叉树_二叉树类python
python中的树数据结构 线性数据中的典型顺序表和链表已经讲完: <顺序表数据结构在python中的应用> <python实现单向链表数据结构及其基本方法> <pyth ...
- BinaryTree-学习二叉树的Python库
2019独角兽企业重金招聘Python工程师标准>>> Binarytree: Python Library for Studying Binary Trees Intr ...
- 二叉树层次遍历python_根据二叉树层序遍历顺序(数组),将其转换为二叉树(Python)...
1.创建二叉树结点和值 class Node: def __init__(self, value): self.value = value self.left = None self.right = ...
- 二叉树期权定价python代码_欧式和美式期权的二叉树和Greek(1)——PYTHON
欧式和美式期权(Option)是最基础的衍生证卷.如何定价是初学金融工程者的必备知识.二叉树的又是最简单但是容易理解的工具.在网上有期权计算器可以很容易得到.但是练习自己编写还是有收益的. 欧式期权的 ...
- 剑指offe——61序列化二叉树(Python)
[题目]请实现两个函数,分别用来序列化和反序列化二叉树 二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存.序列化可以基于先序.中 ...
最新文章
- wxpython中的所有文本框都是TextCtrl,不同的形式的文本框通过样式来实现,下面创建了一个密码输入框...
- 第三十六讲 ASP.NET中的Web服务(三)
- java中Date与String的相互转化
- Byte和byte[]数组
- 解决html2canvas截取页面部分div黑屏问题
- 她穿着由自己17封拒稿信做成的裙子,通过了博士论文答辩!
- Python rabbitmq的使用(五)
- VALSE 2020 线上大会简明日程发布(7月31日-8月5日)
- “智享未来 知行合一”,开为科技助力企业开启人工智能新时代
- Javascript作用域、闭包原理以及性能问题
- Cygwin ssh
- C语言libxml用法,c语言libxml2库的安装和使用.....
- ussd代码大全_魅族ussd补电代码 | 手游网游页游攻略大全
- ElementUI table 样式修改
- Android app客户端性能测试工具Emmagee 浅析
- 设计模式-工厂方法的应用场景及Java中对工厂方法的应用
- 数据库系统概念笔记——第4章 中级SQL
- 做iOS自动化测试必须知道的一些知识
- Word2007 无法给尾部空格加下划线
- 计算机报名没有有效的安全证书,win10 因为没有使用有效的安全证书进行签名怎么解决...
热门文章
- 【移动安全高级篇】————2、浅谈Android软件安全自动化审计
- 计算机学院品牌活动总结,计算机学院研究生会工作总结
- mysql8 启动报错:Error while setting value ‘STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DI
- oracle新增字段 加注释,Oracle数据库表的字段添加注释和向现有表添加字段 | 学步园...
- SEO优化外包 避坑指南
- SGX软硬件栈(零)——基本介绍
- html中文输入转换成英文,中英文转换,键盘如何中英文切换!
- 解决Mac无法睡眠问题
- 【运维心得】只有百度能打开,其他页面打不开怎么办?
- 手机怎样看WiFi的密码