本文整理匯總了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)