本文整理汇总了Python中bst.BinarySearchTree.delete方法的典型用法代码示例。如果您正苦于以下问题:Python BinarySearchTree.delete方法的具体用法?Python BinarySearchTree.delete怎么用?Python BinarySearchTree.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.delete方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nonleaf_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_nonleaf_delete(self):
""" Try to delete non leaf node with both children. """
a = BinarySearchTree(7, 'Harry')
a[8] = 'Ron'
a[5] = 'Ginny'
a[4] = 'Hermione'
a[6] = 'Neville'
""" 7 - Harry
/ \
5- Ginny 8 - Ron
/ \
4-Hermione 6-Neville
"""
a.delete(7)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (5, 'Ginny'), (6, 'Neville'), (8, 'Ron')])
self.assertTrue(a.root.value == 'Neville')
a[7] = 'Harry'
self.assertTrue(a.inorder() == [(4, 'Hermione'), (5, 'Ginny'), (6, 'Neville'), (7, 'Harry'), (8, 'Ron')])
a.delete(6)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (5, 'Ginny'), (7, 'Harry'), (8, 'Ron')])
self.assertTrue(a.root.value == 'Ginny')
示例2: test_delete_without_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_delete_without_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.delete(6)
assert(tree.get(3).childcount() == 0)
示例3: test_parent_pointer_after_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_parent_pointer_after_delete():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.delete(3)
assert(tree.get(6).parent.value == 1)
示例4: test_nonleaf_rightnone_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_nonleaf_rightnone_delete(self):
""" Try to delete non leaf node with no right child. """
a = BinarySearchTree(7, 'Harry')
a[8] = 'Ron'
a[5] = 'Ginny'
a[4] = 'Hermione'
a.delete(5)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (7, 'Harry'), (8, 'Ron')])
示例5: test_delete_root
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_delete_root():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.add(2)
tree.delete(1)
assert(tree.root.value == 3)
示例6: test_delete_two_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_delete_two_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.add(2)
tree.delete(3)
assert(tree.contains(3) is False)
assert(len(tree) == 3)
示例7: test_delete_successor_with_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_delete_successor_with_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(5)
tree.add(4)
tree.add(10)
tree.add(9)
tree.add(7)
tree.add(8)
tree.delete(5)
assert(tree.get(9).get_only_child().value == 8)
示例8: test_deletion
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_deletion(self):
tree = BinarySearchTree()
tree.insert(8)
tree.insert(3)
tree.insert(10)
tree.insert(1)
tree.insert(6)
tree.insert(4)
tree.insert(7)
tree.insert(14)
tree.insert(13)
tree.delete(1)
tree.delete(14)
tree.delete(6)
self.assertEquals(tree.root.value, 8)
self.assertEquals(tree.root.left.value, 3)
self.assertEquals(tree.root.left.left, None)
self.assertEquals(tree.root.left.right.value, 7)
self.assertEquals(tree.root.left.right.left.value, 4)
self.assertEquals(tree.root.left.right.right, None)
self.assertEquals(tree.root.right.value, 10)
self.assertEquals(tree.root.right.right.value, 13)
tree.delete(8)
self.assertEquals(tree.root.value, 10)
self.assertEquals(tree.root.right.value, 13)
示例9: test_leaf_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_leaf_delete(self):
""" Try to delete leaf nodes. """
a = BinarySearchTree(7, 'Harry')
a[8] = 'Ron'
a[9] = 'Ginny'
a[4] = 'Hermione'
self.assertTrue(a.inorder() == [(4, 'Hermione'), (7, 'Harry'), (8, 'Ron'), (9, 'Ginny')])
a.delete(9)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (7, 'Harry'), (8, 'Ron')])
a.delete(4)
self.assertTrue(a.inorder() == [(7, 'Harry'), (8, 'Ron')])
a.delete(8)
self.assertTrue(a.inorder() == [(7, 'Harry')])
示例10: test_root_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_root_delete(self):
""" Try to delete the root in a single element tree. """
a = BinarySearchTree(7, 'Harry')
a.delete(7)
self.assertTrue(a.inorder() == [])
示例11: test_non_existing_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
def test_non_existing_delete(self):
""" Try to delete a non existant item. """
a = BinarySearchTree(7, 'Harry')
a.delete(8)
self.assertTrue(a.inorder() == [(7, 'Harry')])
示例12: BinarySearchTree
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import delete [as 别名]
from bst import BinarySearchTree
bst = BinarySearchTree()
bst.put('3', 'dog')
bst.put('8', 'pig')
bst.put('4', 'goat')
bst.put('2', 'lion')
bst.put('1', 'cat')
bst.put('5', "horse")
for item in bst:
print(item, bst[item])
print(bst.get('2'))
bst['2'] = 'rabbit'
print(bst['2'])
bst.delete('2')
try:
del bst['2']
except KeyError:
print("Not in the BST!")
#for item in bst:
# print(item)