黑松山资源网 Design By www.paidiu.com
本文实例讲述了Python定义二叉树及4种遍历方法。分享给大家供大家参考,具体如下:
Python & BinaryTree
1. BinaryTree (二叉树)
二叉树是有限个元素的集合,该集合或者为空、或者有一个称为根节点(root)的元素及两个互不相交的、分别被称为左子树和右子树的二叉树组成。
- 二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。
- 二叉树的第i层至多有2^{i-1}个结点
- 深度为k的二叉树至多有2^k-1个结点;
- 对任何一棵二叉树T,如果其终端结点数为N0,度为2的结点数为N2,则N0=N2+1
2. 二叉树
生成二叉树
# init a tree def InitBinaryTree(dataSource, length): root = BTNode(dataSource[0]) for x in xrange(1,length): node = BTNode(dataSource[x]) InsertElementBinaryTree(root, node) return root print 'Done...'
前序遍历
# pre-order def PreorderTraversalBinaryTree(root): if root: print '%d | ' % root.data, PreorderTraversalBinaryTree(root.leftChild) PreorderTraversalBinaryTree(root.rightChild)
中序遍历
# in-order def InorderTraversalBinaryTree(root): if root: InorderTraversalBinaryTree(root.leftChild) print '%d | ' % root.data, InorderTraversalBinaryTree(root.rightChild)
后序遍历
# post-order def PostorderTraversalBinaryTree(root): if root: PostorderTraversalBinaryTree(root.leftChild) PostorderTraversalBinaryTree(root.rightChild) print '%d | ' % root.data,
按层遍历
# layer-order def TraversalByLayer(root, length): stack = [] stack.append(root) for x in xrange(length): node = stack[x] print '%d | ' % node.data, if node.leftChild: stack.append(node.leftChild) if node.rightChild: stack.append(node.rightChild)
Result
二叉树的思想重在“递归”, 并不是非要用递归处理,而是去理解二叉树递归的思想
完整代码段
# -*- coding:utf-8 -*- ################# ### implement Binary Tree using python ### Hongwing ### 2016-9-4 ################# import math class BTNode(object): """docstring for BTNode""" def __init__(self, data): self.data = data self.leftChild = None self.rightChild = None # insert element def InsertElementBinaryTree(root, node): if root: if node.data < root.data: if root.leftChild: InsertElementBinaryTree(root.leftChild, node) else: root.leftChild = node else: if root.rightChild: InsertElementBinaryTree(root.rightChild, node) else: root.rightChild = node else: return 0 # init a tree def InitBinaryTree(dataSource, length): root = BTNode(dataSource[0]) for x in xrange(1,length): node = BTNode(dataSource[x]) InsertElementBinaryTree(root, node) return root print 'Done...' # pre-order def PreorderTraversalBinaryTree(root): if root: print '%d | ' % root.data, PreorderTraversalBinaryTree(root.leftChild) PreorderTraversalBinaryTree(root.rightChild) # in-order def InorderTraversalBinaryTree(root): if root: InorderTraversalBinaryTree(root.leftChild) print '%d | ' % root.data, InorderTraversalBinaryTree(root.rightChild) # post-order def PostorderTraversalBinaryTree(root): if root: PostorderTraversalBinaryTree(root.leftChild) PostorderTraversalBinaryTree(root.rightChild) print '%d | ' % root.data, # layer-order def TraversalByLayer(root, length): stack = [] stack.append(root) for x in xrange(length): node = stack[x] print '%d | ' % node.data, if node.leftChild: stack.append(node.leftChild) if node.rightChild: stack.append(node.rightChild) if __name__ == '__main__': dataSource = [3, 4, 2, 6, 7, 1, 8, 5] length = len(dataSource) BTree = InitBinaryTree(dataSource, length) print '****NLR:' PreorderTraversalBinaryTree(BTree) print '\n****LNR' InorderTraversalBinaryTree(BTree) print '\n****LRN' PostorderTraversalBinaryTree(BTree) print '\n****LayerTraversal' TraversalByLayer(BTree, length)
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月08日
2024年10月08日
- 《丁当 20首重量级歌曲 Fu Good 下一站天后 2CD》[WAV/分轨][650MB]
- 歌莉雅.2012-My.Voice.Story【环星】【FLAC分轨】
- 群星.1993-一曲成名·青春无悔【飞碟】【WAV+CUE】
- 刘德华.2000-爱无知(金装版)【NEWMELODY】【WAV+CUE】
- 许巍《时光漫步》1CD[FLAC/分轨][804.3MB]
- 群星《2024第一季度百度歌曲排行抖音榜top100》1CD[FLAC/分轨][2.3GB]
- 张柏芝《全新经验》[FLAC/分轨][450MB]
- 唐朝.2008-浪漫骑士【音乐家】【WAV+CUE】
- 张芸京.2016-失败的高歌【泡耳音乐】【WAV+CUE】
- 群星.1991-音乐工厂1·皇后大道东【音乐工厂】【WAV+CUE】
- 群星.1992-音乐工厂2·首都【音乐工厂】【WAV+CUE】
- 群星.1994-音乐工厂3·儿童乐园【音乐工厂】【WAV+CUE】
- 胡鸿钧.2021-ex:CHANGE【星梦娱乐】【WAV+CUE】
- 班得瑞原装进口《第一张新世纪专辑:仙境》1CD[APE/CUE分轨][292.3MB]
- 伍佰《摇滚教父 伍佰黄金精选 2CD》[WAV+CUE][990MB]