本文整理汇总了Python中bst.BST.depth方法的典型用法代码示例。如果您正苦于以下问题:Python BST.depth方法的具体用法?Python BST.depth怎么用?Python BST.depth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BST
的用法示例。
在下文中一共展示了BST.depth方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_depth
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_depth():
aBST = BST()
assert aBST.depth() == 0
for num, depth in zip([4, 2, 6, 3, 7, 8, 9, 10, 11],
[1, 2, 2, 3, 3, 4, 5, 6, 7]):
aBST.insert(num)
assert aBST.depth() == depth
示例2: test_rebalance
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_rebalance():
"""Tests my rebalancing algorithm"""
bintree = BST()
for i in xrange(100):
bintree.insert(random.randint(0, 1e6))
assert bintree.size() == 100
original_depth = bintree.depth()
bintree.rebalance()
assert abs(bintree.balance()) < 2
assert original_depth > bintree.depth()
bintree = BST()
for i in xrange(100):
bintree.insert(i) # Horribly unbalanced
assert bintree.size() == 100
assert bintree.depth() == 100
assert bintree.balance() == -99
bintree.rebalance()
assert abs(bintree.balance()) < 2
assert bintree.depth() < 10 # Much better, anyway
示例3: test_depth
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_depth():
bst = BST()
bst.insert(5)
assert bst.depth(node=bst.root) == 1
bst.insert(3)
bst.insert(4)
bst.insert(6)
assert bst.depth(node=bst.root) == 3
示例4: test_depth
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_depth():
tree = BST()
assert tree.depth() == 0
items = [0, -2, -1, -4, -3]
for item in items:
tree.insert(item)
assert tree.depth() == 4
items = [1, 2, 3, 4]
for item in items:
tree.insert(item)
assert tree.depth() == 5
示例5: test_depth_collatz
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_depth_collatz():
bst = BST()
for i in range(1,11):
if not i % 2:
i /= 2
else:
i = i * 3 + 1
print('i = {}'.format(i))
bst.insert(i)
assert bst.depth(bst.root) == 5
示例6: test_depth
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_depth():
"""depth(self) should return an integer representing the total number of
levels in the tree. If there is one value, the depth should be 1, if two
values it will be 2, if three values it may be 2 or three, depending."""
bintree = BST()
with pytest.raises(TypeError):
bintree.depth(1) # No values allowed
assert bintree.depth() == 0
bintree.insert(5)
# 5
assert bintree.depth() == 1
bintree.insert(3)
# 5
# /
# 3
assert bintree.depth() == 2
bintree.insert(7)
# 5
# / \
# 3 7
assert bintree.depth() == 2
bintree.insert(2)
bintree.insert(4)
# 5
# / \
# 3 7
# /\
# 2 4
assert bintree.depth() == 3
bintree.insert(1)
bintree.insert(6)
# 5
# / \
# 3 7
# /\ /
# 2 4 6
# |
# 1
assert bintree.depth() == 4
示例7: test_depth_all_left
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_depth_all_left():
bst = BST()
for i in range(110,10,-10):
bst.insert(i)
assert bst.depth(bst.root) == 10
示例8: test_size_empty_depth
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import depth [as 别名]
def test_size_empty_depth():
"""Assert an empty tree has a depth of zero."""
from bst import BST
b = BST([])
assert b.depth() == 0