京公网安备 11010802034615号
经营许可证编号:京B2-20210330
python实现的二叉树算法和kmp算法实例
最近重温数据结构,又用python,所以就用python重新写了数据结构的一些东西,以下是二叉树的python写法
要是:前序遍历、中序遍历、后序遍历、层级遍历、非递归前序遍历、非递归中序遍历、非递归后序遍历
class TreeNode(object):
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
class Tree(object):
def __init__(self, root=None):
self.root = None
def makeTree(self, data, left, right):
self.root = TreeNode(data, left, right)
def is_empty(self):
"""是否为空 """
if self.root is None:
return True
return False
def preOrder(self, r):
"""前序遍历 """
if not r.is_empty():
print r.root.data
if r.root.left is not None:
r.preOrder(r.root.left)
if r.root.right is not None:
r.preOrder(r.root.right)
def inOrder(self, r):
"""中序遍历 """
if not r.is_empty():
if r.root.left is not None:
r.preOrder(r.root.left)
print r.root.data
if r.root.right is not None:
r.preOrder(r.root.right)
def postOrder(self, r):
"""后续遍历 """
if not r.is_empty():
if r.root.left is not None:
r.preOrder(r.root.left)
print r.root.data
if r.root.right is not None:
r.preOrder(r.root.right)
def levelOrder(self, r):
"""层级遍历 """
if not r.is_empty():
s = [r]
while len(s) > 0:
temp = s.pop(0) # 先弹出最先append到的点
if temp and temp.root is not None:
print temp.root.data
if temp.root.left is not None:
s.append(temp.root.left)
if self.root.right is not None:
s.append(temp.root.right)
def preOrder1(self, r):
"""非递归 前序遍历 """
stack = []
current = r
while len(stack) > 0 or (current and not current.is_empty()):
while current and not current.is_empty():
print current.root.data
stack.append(current)
current = current.root.left
if len(stack) > 0:
current = stack.pop()
current = current.root.right
def inOrder1(self, r):
"""非递归 中序遍历 """
stack = []
current = r
while len(stack) > 0 or (current and not current.is_empty()):
while current and not current.is_empty():
stack.append(current)
current = current.root.left
if len(stack) > 0:
current = stack.pop()
print current.root.data
current = current.root.right
def postOrder1(self, r):
"""非递归 后续遍历 """
stack = []
current = r
pre = None
while len(stack) > 0 or (current and not current.is_empty()):
if current and not current.is_empty():
stack.append(current)
current = current.root.left
elif stack[-1].root.right != pre:
current = stack[-1].root.right
pre = None
else:
pre = stack.pop()
print pre.root.data
def leaves_count(self, r):
"""求叶子节点个数 """
if r.is_empty():
return 0
elif (not r.root.left) and (not r.root.right):
return 1
else:
return r.root.left.leaves_count(r.root.left) + r.root.right.leaves_count(r.root.right)
if __name__ == '__main__':
"""二叉树"""
ra, rb, rc, rd, re, rf = Tree(), Tree(), Tree(), Tree(), Tree(), Tree()
ra.makeTree("a", None, None)
rb.makeTree("b", None, None)
rc.makeTree("c", None, None)
rd.makeTree("d", None, None)
re.makeTree("e", None, None)
rf.makeTree("f", None, None)
r1, r2, r3, r4, r = Tree(), Tree(), Tree(), Tree(), Tree()
r1.makeTree("-", rc, rd)
r2.makeTree("*", rb, r1)
r3.makeTree("+", ra, r2)
r4.makeTree("/", re, rf)
r.makeTree("-", r3, r4)
r.preOrder(r)
r.inOrder(r)
r.postOrder(r)
r.levelOrder(r)
print r.leaves_count(r)
大学的时候学过kmp算法,最近在看的时候发现竟然忘了,所以去重新看了看书,然后用python写下了这个算法:
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
初中毕业想考证,先确认“我能不能报”,再比较证书价值。学历门槛不够的证书,宣传再多也与你无关;能报但对应不上岗位的证书, ...
2026-11-02市场岗确有一批报考不设专业限制的证书,但“不限专业”只解决能不能报,真正决定值不值的是三件事:对应市场岗哪条子路径、招聘 ...
2026-11-02“大学生必考证书有哪些”这个问题,本身就藏着一个需要先拆掉的假设:似乎存在一份人人适用的必考清单。实际上,除了少数与毕业 ...
2026-11-02“最好考的一级证书”这个问题,最容易踩的坑不是选错证书,而是没先弄清“一级”在目标证书体系里到底指什么。有的体系里,一级 ...
2026-11-02“职业证书哪个含金量高”这个问题,很难用一张榜单回答。原因不复杂:证书的“含金量”取决于你拿它做什么。想进入必须持证的行 ...
2026-11-02“证件含金量排名”是搜索量很高的词,但直接给你一份“十大证书排行榜”的文章,基本可以跳过。原因很简单:证书的含金量不是单 ...
2026-11-02在校大专生能考的证书,大体分三类:通用能力类(英语等级、计算机等级、普通话水平测试)、职业技能等级类,以及部分不要求工作 ...
2026-11-02在家闲着想考个证书,最容易踩的坑是先去搜“含金量排行榜”,然后被一堆培训广告牵着走。更稳妥的顺序是反过来的:先看自己属于 ...
2026-11-02“注册类证书”不是一个可以随手列全的清单,它更像一个制度口径:通常指国家职业资格目录中设有注册管理、与执业准入挂钩的资格 ...
2026-11-02“现在考什么证有用”很难有统一答案。同一张证书,对想进会计事务所的人有用,对想做新媒体运营的人可能帮助有限;对需要评职称 ...
2026-11-02“适合女生考的证书有哪些含金量高”,这个问题里藏着两个容易走偏的前提。一是把“女生”当成职业属性,仿佛有一份统一的女性证 ...
2026-11-02先说结论:工商管理专业女生不必追“女生必考”榜单。证书有没有用,取决于它能不能对上你准备投的岗位。比较稳妥的做法,是先在 ...
2026-11-02“什么证好考又实用”这个问题,最怕两种回答:一种是列一堆“通过率高”的证书,却不告诉你考完能干什么;另一种是反过来只谈含 ...
2026-11-02“女士可以考哪些证书”并没有一份统一答案。真正决定你能考什么的,不是性别,而是三件事:你现在的身份(在校、在职还是准备转 ...
2026-11-02大专学历想考运营岗位证书,先给结论:能报的证书不少,但值得考的集中在三类——平台官方运营/投放认证、职业技能等级评价、数 ...
2026-11-02大学生判断一张证书是否重要,不用先看宣传热度,而要看四件事:它是否匹配你的目标岗位,校招时是否真用得上,在校期间是否具备 ...
2026-11-02先把结论说清楚:在校期间以2—4张核心证书为宜,其中1—2张通用底证、1—2张与目标岗位对应的方向证;高壁垒资格证只在符合报考 ...
2026-11-02大学生问证书推荐,最容易掉进的坑是“别人考什么我也考什么”。证书本身没有统一的含金量排序,同一张证,对目标岗位是硬门槛, ...
2026-11-02经管毕业生没有一张“通吃”的证书。比较有用的选择,是先按财会、金融、市场、人力、综合管理几个方向锁定目标岗位,再把证书分 ...
2026-11-02“程序员考什么证书含金量高”,没有放之四海皆准的答案。同样是“高含金量”,对后端开发、云原生工程师、数据开发和安全工程师 ...
2026-11-02