京公网安备 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-05大学期间能报的证书,先别急着看“含金量榜单”。更实用的第一步是问:我现在这个身份、这个学历,符合报考条件吗? 很多证书宣 ...
2026-11-05大学期间能考的证书很多,但真正需要先回答的是两个问题:当前有没有报考资格,以及这张证书对应什么岗位任务。 前者决定你能不 ...
2026-11-05先把结论说清楚:真正“只有在校大学生能考、毕业就彻底没资格”的证书数量不多,主要集中在语言类考试的部分专项、面向高校学生 ...
2026-11-05上班族问“考什么证书有用”,真正要解决的不是找一张“最值钱”的证,而是判断哪张证能嵌进你现有的岗位任务。同样一张证书,做 ...
2026-11-05职称证书不是统一的考试项目。同样“考职称”,经济、会计、统计、审计、软考走全国统一考试,工程、教师、卫生等系列多数走评审 ...
2026-11-0533岁选证书,最怕两件事:一是被“适合女生”的清单带着走,考完发现岗位用不上;二是投入大半年,报名时才发现学历、专业或工作 ...
2026-11-05“女生容易考的证书”这个问题,真正难的不是找一份名单,而是判断“容易”到底指什么。报名条件宽松、考试通过率高、拿证后能找 ...
2026-11-05“女生适合考什么证书”这类搜索,背后往往是同一个困惑:网上榜单太多,口径却说不清楚。有人把培训证书和职业资格混在一起排, ...
2026-11-05先把结论说清楚:没有一张证书对所有人都“又好考又实用”。同一个证,对在校生可能报名门槛都过不了,对有三年工作经验的人却只 ...
2026-11-05“考证书有哪些”这个问题,如果只想要一份名单,网上能搜到成百上千个。但真正有用的答案不是“有哪些”,而是“它们分别是什么 ...
2026-11-05“考什么证好一点”这句话,十个人问可能有十个意思。有人想问哪个证容易过,有人想知道哪个证找工作管用,还有人关心哪个证长期 ...
2026-11-05“考哪些证书最吃香”很难用一张榜单回答。同一张证书,在有的岗位是硬门槛,在另一些岗位只是加分项;对在校生和在职者的价值也 ...
2026-11-05家庭主妇能考的证书不少,但真正值得花时间的,不是“哪个证热门”,而是先确认你当前能不能报,再按每周可投入时间和未来出口缩 ...
2026-11-05“国家认证的职业技能证书”并不是某一本证书的正式名称,而是一句被大量使用的宣传口径。它可能指向人社部门的国家职业资格、备 ...
2026-11-05工程领域的证书名目确实多,但乱的不是证书本身,而是很多人把不同制度属性的证书放在一个篮子里比较。有人拿培训结业证去对标注 ...
2026-11-05大专学历不是考证的天花板,但确实存在一批证书对学历、专业或工作年限有硬性要求。与其先问“哪个证最值钱”,不如先做一道筛选 ...
2026-11-05大专学历想找“含金量高”的证书,最容易踩的坑不是选错方向,而是没看清报考条件就投入几个月时间。直接回答:大专可以报考的高 ...
2026-11-05大学期间能考的证书不少,但真正值得先解决的问题不是“有哪些”,而是“哪些我现在就能报”。同样一个证书名称,有的在校即可报 ...
2026-11-05大学要考哪些证,没有一份适合所有人的统一清单。更实用的做法是先按证书性质分类,再按目标方向筛选,最后核对在校身份能否报考 ...
2026-11-05