京公网安备 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-02-04在数据分析场景中,基准比是衡量指标表现、评估业务成效、对比个体/群体差异的核心工具,广泛应用于绩效评估、业务监控、竞品对 ...
2026-02-04业务数据分析是企业日常运营的核心支撑,其核心价值在于将零散的业务数据转化为可落地的业务洞察,破解运营痛点、优化业务流程、 ...
2026-02-04在信贷业务中,违约率是衡量信贷资产质量、把控信用风险、制定风控策略的核心指标,其统计分布特征直接决定了风险定价的合理性、 ...
2026-02-03在数字化业务迭代中,AB测试已成为验证产品优化、策略调整、运营活动效果的核心工具。但多数业务场景中,单纯的“AB组差异对比” ...
2026-02-03企业战略决策的科学性,决定了其长远发展的格局与竞争力。战略分析方法作为一套系统化、专业化的思维工具,为企业研判行业趋势、 ...
2026-02-03在统计调查与数据分析中,抽样方法分为简单随机抽样与复杂抽样两大类。简单随机抽样因样本均匀、计算简便,是基础的抽样方式,但 ...
2026-02-02在数据驱动企业发展的今天,“数据分析”已成为企业经营决策的核心支撑,但实践中,战略数据分析与业务数据分析两个概念常被混淆 ...
2026-02-02在数据驱动企业发展的今天,“数据分析”已成为企业经营决策的核心支撑,但实践中,战略数据分析与业务数据分析两个概念常被混淆 ...
2026-02-02B+树作为数据库索引的核心数据结构,其高效的查询、插入、删除性能,离不开节点间指针的合理设计。在日常学习和数据库开发中,很 ...
2026-01-30在数据库开发中,UUID(通用唯一识别码)是生成唯一主键、唯一标识的常用方式,其标准格式包含4个短横线(如550e8400-e29b-41d4- ...
2026-01-30商业数据分析的价值落地,离不开标准化、系统化的总体流程作为支撑;而CDA(Certified Data Analyst)数据分析师,作为经过系统 ...
2026-01-30在数据分析、质量控制、科研实验等场景中,数据波动性(离散程度)的精准衡量是判断数据可靠性、稳定性的核心环节。标准差(Stan ...
2026-01-29在数据分析、质量检测、科研实验等领域,判断数据间是否存在本质差异是核心需求,而t检验、F检验是实现这一目标的经典统计方法。 ...
2026-01-29统计制图(数据可视化)是数据分析的核心呈现载体,它将抽象的数据转化为直观的图表、图形,让数据规律、业务差异与潜在问题一目 ...
2026-01-29箱线图(Box Plot)作为数据分布可视化的核心工具,能清晰呈现数据的中位数、四分位数、异常值等关键统计特征,广泛应用于数据分 ...
2026-01-28在回归分析、机器学习建模等数据分析场景中,多重共线性是高频数据问题——当多个自变量间存在较强的线性关联时,会导致模型系数 ...
2026-01-28数据分析的价值落地,离不开科学方法的支撑。六种核心分析方法——描述性分析、诊断性分析、预测性分析、规范性分析、对比分析、 ...
2026-01-28在机器学习与数据分析领域,特征是连接数据与模型的核心载体,而特征重要性分析则是挖掘数据价值、优化模型性能、赋能业务决策的 ...
2026-01-27关联分析是数据挖掘领域中挖掘数据间潜在关联关系的经典方法,广泛应用于零售购物篮分析、电商推荐、用户行为路径挖掘等场景。而 ...
2026-01-27