本文整理汇总了Python中Tree.levelorderTraversal方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.levelorderTraversal方法的具体用法?Python Tree.levelorderTraversal怎么用?Python Tree.levelorderTraversal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree.levelorderTraversal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: isBalanced
# 需要导入模块: import Tree [as 别名]
# 或者: from Tree import levelorderTraversal [as 别名]
"""
import Tree
def isBalanced(bt):
if bt is None:
return True
d = Tree.getHeight(bt.left) - Tree.getHeight(bt.right)
if abs(d) > 1:
return False
else:
return isBalanced(bt.left) and isBalanced(bt.right)
# Balanced tree
bt = Tree.createPerfectTree(3)
Tree.levelorderTraversal(bt)
print()
print(Tree.getHeight(bt))
print(isBalanced(bt))
# Unbalanced tree
bt = Tree.Node(0)
bt.left = Tree.Node(1)
bt.right = Tree.Node(2)
bt2 = Tree.Node(3)
bt2.left = bt
Tree.levelorderTraversal(bt2)
print()
print(Tree.getHeight(bt2))
print(isBalanced(bt2))
示例2: sorted
# 需要导入模块: import Tree [as 别名]
# 或者: from Tree import levelorderTraversal [as 别名]
"""
Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
"""
import Tree
def createMBST_a(ary, start, end):
if end < start:
return None
mid = int((start + end) / 2)
n = Tree.Node(ary[mid])
n.left = createMBST_a(ary, start, mid - 1)
n.right = createMBST_a(ary, mid + 1, end)
return n
def createMBST(ary):
return createMBST_a(ary, 0, len(ary) - 1)
ary = [0, 1, 2, 3, 4, 5, 6]
n = createMBST(ary)
Tree.levelorderTraversal(n)