国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python定義二叉樹及4種遍歷方法實例詳解

2020-02-15 22:12:46
字體:
來源:轉載
供稿:網友

本文實例講述了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 treedef 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-orderdef PreorderTraversalBinaryTree(root):  if root:    print '%d | ' % root.data,    PreorderTraversalBinaryTree(root.leftChild)    PreorderTraversalBinaryTree(root.rightChild)

中序遍歷

# in-orderdef InorderTraversalBinaryTree(root):  if root:    InorderTraversalBinaryTree(root.leftChild)    print '%d | ' % root.data,    InorderTraversalBinaryTree(root.rightChild)

后序遍歷

# post-orderdef PostorderTraversalBinaryTree(root):  if root:    PostorderTraversalBinaryTree(root.leftChild)    PostorderTraversalBinaryTree(root.rightChild)    print '%d | ' % root.data,

按層遍歷

# layer-orderdef 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 mathclass BTNode(object):  """docstring for BTNode"""  def __init__(self, data):    self.data = data    self.leftChild = None    self.rightChild = None# insert elementdef 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 treedef 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-orderdef PreorderTraversalBinaryTree(root):  if root:    print '%d | ' % root.data,    PreorderTraversalBinaryTree(root.leftChild)    PreorderTraversalBinaryTree(root.rightChild)# in-orderdef InorderTraversalBinaryTree(root):  if root:    InorderTraversalBinaryTree(root.leftChild)    print '%d | ' % root.data,    InorderTraversalBinaryTree(root.rightChild)# post-orderdef PostorderTraversalBinaryTree(root):  if root:    PostorderTraversalBinaryTree(root.leftChild)    PostorderTraversalBinaryTree(root.rightChild)    print '%d | ' % root.data,# layer-orderdef 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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 翁牛特旗| 柳州市| 嵊州市| 阳东县| 景宁| 浏阳市| 武宣县| 西丰县| 略阳县| 昭平县| 镇坪县| 灵寿县| 双流县| 永昌县| 龙游县| 菏泽市| 南丰县| 塘沽区| 兴海县| 永宁县| 朝阳区| 云南省| 汨罗市| 潢川县| 托克托县| 阿拉善右旗| 墨脱县| 江门市| 连城县| 阿拉尔市| 那坡县| 靖江市| 涟源市| 石渠县| 玉门市| 交口县| 乾安县| 丹江口市| 芮城县| 娄烦县| 自治县|