本文整理汇总了Python中bst.Bst.getsize方法的典型用法代码示例。如果您正苦于以下问题:Python Bst.getsize方法的具体用法?Python Bst.getsize怎么用?Python Bst.getsize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.Bst
的用法示例。
在下文中一共展示了Bst.getsize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_one_child_root
# 需要导入模块: from bst import Bst [as 别名]
# 或者: from bst.Bst import getsize [as 别名]
def remove_one_child_root():
"""Removing root in a tree where the root has only one child"""
tree = Bst()
tree.add(ROOT_DATA)
tree.add(LEFT_DATA)
self.assertFalse(tree.isempty())
self.assertEquals(tree.getsize(), 2)
self.assertEquals(tree.root.getleft().getdata(), LEFT_DATA)
val = tree.remove(ROOT_DATA)
self.assertFalse(tree.isempty())
self.assertEquals(tree.getsize(), 1)
self.assertEquals(val, ROOT_DATA)
self.assertIsNotNone(tree.root)
self.assertNotEquals(tree.root.getdata(), ROOT_DATA)
self.assertEquals(tree.root, Node(LEFT_DATA))
示例2: test_add
# 需要导入模块: from bst import Bst [as 别名]
# 或者: from bst.Bst import getsize [as 别名]
def test_add(self):
tree = Bst()
tree.add(ROOT_DATA)
self.assertEquals(tree.getsize(), 1)
self.assertIsNotNone(tree.root)
self.assertEquals(tree.root.getheight(), 0)
self.assertEquals(tree.root.getdata(), ROOT_DATA)
# Add left child
tree.add(LEFT_DATA)
self.assertEquals(tree.getsize(), 2)
self.assertIsNotNone(tree.root.getleft())
self.assertIsNone(tree.root.getright())
self.assertEquals(tree.root.getleft().getdata(), LEFT_DATA)
self.assertEquals(tree.root.getheight(), 1)
# Add right child
tree.add(RIGHT_DATA)
self.assertEquals(tree.getsize(), 3)
self.assertIsNotNone(tree.root.getleft())
self.assertIsNotNone(tree.root.getright())
self.assertEquals(tree.root.getright().getdata(), RIGHT_DATA)
self.assertEquals(tree.root.getheight(), 1)
# Add granchildren to left child
tree.add(LL_DATA)
self.assertEquals(tree.getsize(), 4)
self.assertIsNotNone(tree.root.getleft().getleft())
self.assertIsNone(tree.root.getleft().getright())
self.assertEquals(tree.root.getleft().getleft().getdata(), LL_DATA)
self.assertEquals(tree.root.getheight(), 2)
self.assertEquals(tree.root.getleft().getheight(), 1)
tree.add(LR_DATA)
self.assertEquals(tree.getsize(), 5)
self.assertIsNotNone(tree.root.getleft().getleft())
self.assertIsNotNone(tree.root.getleft().getright())
self.assertEquals(tree.root.getleft().getright().getdata(), LR_DATA)
self.assertEquals(tree.root.getheight(), 2)
self.assertEquals(tree.root.getleft().getheight(), 1)
# Add grandchildren to right child
tree.add(RL_DATA)
self.assertEquals(tree.getsize(), 6)
self.assertIsNotNone(tree.root.getright().getleft())
self.assertIsNone(tree.root.getright().getright())
self.assertEquals(tree.root.getright().getleft().getdata(), RL_DATA)
self.assertEquals(tree.root.getheight(), 2)
self.assertEquals(tree.root.getright().getheight(), 1)
tree.add(RR_DATA)
self.assertEquals(tree.getsize(), 7)
self.assertIsNotNone(tree.root.getright().getleft())
self.assertIsNotNone(tree.root.getright().getright())
self.assertEquals(tree.root.getright().getright().getdata(), RR_DATA)
self.assertEquals(tree.root.getheight(), 2)
self.assertEquals(tree.root.getright().getheight(), 1)
示例3: test_size
# 需要导入模块: from bst import Bst [as 别名]
# 或者: from bst.Bst import getsize [as 别名]
def test_size(self):
tree = Bst()
self.assertEquals(tree.size, 0)
self.assertEquals(tree.size, tree.getsize())
# Set tree size to arbitrary number
ARBITRARY_SIZE = 5
tree.size = ARBITRARY_SIZE
self.assertEquals(tree.size, ARBITRARY_SIZE)
self.assertEquals(tree.size, tree.getsize())
# Adding should increment size
tree = balanced_tree()
self.assertEquals(tree.getsize(), 7)
FAR_RIGHT_DATA = 100
tree.add(FAR_RIGHT_DATA)
self.assertEquals(tree.getsize(), 8)
# Removing should decrement size
tree = balanced_tree()
self.assertEquals(tree.getsize(), 7)
tree.remove(ROOT_DATA)
self.assertEquals(tree.getsize(), 6)