京公网安备 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
箱线图(Box Plot)作为数据分布可视化的核心工具,凭借简洁的结构直观呈现数据的中位数、四分位数、异常值等关键信息,广泛应用 ...
2025-12-25在数据驱动决策的时代,基于历史数据进行精准预测已成为企业核心需求——无论是预测未来销售额、客户流失概率,还是产品需求趋势 ...
2025-12-25在数据驱动业务的实践中,CDA(Certified Data Analyst)数据分析师的核心工作,本质上是通过“指标”这一数据语言,解读业务现 ...
2025-12-25在金融行业的数字化转型进程中,SQL作为数据处理与分析的核心工具,贯穿于零售银行、证券交易、保险理赔、支付结算等全业务链条 ...
2025-12-24在数据分析领域,假设检验是验证“数据差异是否显著”的核心工具,而独立样本t检验与卡方检验则是其中最常用的两种方法。很多初 ...
2025-12-24在企业数字化转型的深水区,数据已成为核心生产要素,而“让数据可用、好用”则是挖掘数据价值的前提。对CDA(Certified Data An ...
2025-12-24数据分析师认证考试全面升级后,除了考试场次和报名时间,小伙伴们最关心的就是报名费了,报 ...
2025-12-23CDA中国官网是全国统一的数据分析师认证报名网站,由认证考试委员会与持证人会员、企业会员以及行业知名第三方机构共同合作,致 ...
2025-12-23在Power BI数据可视化分析中,矩阵是多维度数据汇总的核心工具,而“动态计算平均值”则是矩阵分析的高频需求——无论是按类别计 ...
2025-12-23在SQL数据分析场景中,“日期转期间”是高频核心需求——无论是按日、周、月、季度还是年度统计数据,都需要将原始的日期/时间字 ...
2025-12-23在数据驱动决策的浪潮中,CDA(Certified Data Analyst)数据分析师的核心价值,早已超越“整理数据、输出报表”的基础层面,转 ...
2025-12-23在使用Excel数据透视表进行数据分析时,我们常需要在透视表旁添加备注列,用于标注数据背景、异常说明、业务解读等关键信息。但 ...
2025-12-22在MySQL数据库的性能优化体系中,索引是提升查询效率的“核心武器”——一个合理的索引能将百万级数据的查询耗时从秒级压缩至毫 ...
2025-12-22在数据量爆炸式增长的数字化时代,企业数据呈现“来源杂、格式多、价值不均”的特点,不少CDA(Certified Data Analyst)数据分 ...
2025-12-22在企业数据化运营体系中,同比、环比分析是洞察业务趋势、评估运营效果的核心手段。同比(与上年同期对比)可消除季节性波动影响 ...
2025-12-19在数字化时代,用户已成为企业竞争的核心资产,而“理解用户”则是激活这一资产的关键。用户行为分析系统(User Behavior Analys ...
2025-12-19在数字化转型的深水区,企业对数据价值的挖掘不再局限于零散的分析项目,而是转向“体系化运营”——数据治理体系作为保障数据全 ...
2025-12-19在数据科学的工具箱中,析因分析(Factor Analysis, FA)、聚类分析(Clustering Analysis)与主成分分析(Principal Component ...
2025-12-18自2017年《Attention Is All You Need》一文问世以来,Transformer模型凭借自注意力机制的强大建模能力,在NLP、CV、语音等领域 ...
2025-12-18在CDA(Certified Data Analyst)数据分析师的时间序列分析工作中,常面临这样的困惑:某电商平台月度销售额增长20%,但增长是来 ...
2025-12-18