
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
基于 SPSS 的 ROC 曲线平滑调整方法与实践指南 摘要 受试者工作特征曲线(ROC 曲线)是评估诊断模型或预测指标效能的核心工具, ...
2025-08-25神经网络隐藏层神经元个数的确定方法与实践 摘要 在神经网络模型设计中,隐藏层神经元个数的确定是影响模型性能、训练效率与泛 ...
2025-08-25CDA 数据分析师与数据思维:驱动企业管理升级的核心力量 在数字化浪潮席卷全球的当下,数据已成为企业继人力、物力、财力之后的 ...
2025-08-25CDA数据分析师与数据指标:基础概念与协同逻辑 一、CDA 数据分析师:数据驱动时代的核心角色 1.1 定义与行业价值 CDA(Certified ...
2025-08-22Power Query 移动加权平均计算 Power Query 移动加权平均设置全解析:从原理到实战 一、移动加权平均法的核心逻辑 移动加权平均 ...
2025-08-22描述性统计:CDA数据分析师的基础核心与实践应用 一、描述性统计的定位:CDA 认证的 “入门基石” 在 CDA(Certified Data Analy ...
2025-08-22基于 Python response.text 的科技新闻数据清洗去噪实践 在通过 Python requests 库的 response.text 获取 API 数据后,原始数据 ...
2025-08-21基于 Python response.text 的科技新闻综述 在 Python 网络爬虫与 API 调用场景中,response.text 是 requests 库发起请求后获取 ...
2025-08-21数据治理新浪潮:CDA 数据分析师的战略价值与驱动逻辑 一、数据治理的多维驱动引擎 在数字经济与人工智能深度融合的时代,数据治 ...
2025-08-21Power BI 热力地图制作指南:从数据准备到实战分析 在数据可视化领域,热力地图凭借 “直观呈现数据密度与分布趋势” 的核心优势 ...
2025-08-20PyTorch 矩阵运算加速库:从原理到实践的全面解析 在深度学习领域,矩阵运算堪称 “计算基石”。无论是卷积神经网络(CNN)中的 ...
2025-08-20数据建模:CDA 数据分析师的核心驱动力 在数字经济浪潮中,数据已成为企业决策的核心资产。CDA(Certified Data Analyst)数据分 ...
2025-08-20KS 曲线不光滑:模型评估的隐形陷阱,从原因到破局的全指南 在分类模型(如风控违约预测、电商用户流失预警、医疗疾病诊断)的评 ...
2025-08-20偏态分布:揭开数据背后的非对称真相,赋能精准决策 在数据分析的世界里,“正态分布” 常被视为 “理想模型”—— 数据围绕均值 ...
2025-08-19CDA 数据分析师:数字化时代的价值创造者与决策智囊 在数据洪流席卷全球的今天,“数据驱动” 已从企业战略口号落地为核心 ...
2025-08-19CDA 数据分析师:善用 Power BI 索引列,提升数据处理与分析效率 在 Power BI 数据分析流程中,“数据准备” 是决定后续分析质量 ...
2025-08-18CDA 数据分析师:巧用 SQL 多个聚合函数,解锁数据多维洞察 在企业数据分析场景中,单一维度的统计(如 “总销售额”“用户总数 ...
2025-08-18CDA 数据分析师:驾驭表格结构数据的核心角色与实践应用 在企业日常数据存储与分析场景中,表格结构数据(如 Excel 表格、数据库 ...
2025-08-18PowerBI 累计曲线制作指南:从 DAX 度量到可视化落地 在业务数据分析中,“累计趋势” 是衡量业务进展的核心视角 —— 无论是 “ ...
2025-08-15Python 函数 return 多个数据:用法、实例与实战技巧 在 Python 编程中,函数是代码复用与逻辑封装的核心载体。多数场景下,我们 ...
2025-08-15