本文整理汇总了Python中tree.Tree.delete方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.delete方法的具体用法?Python Tree.delete怎么用?Python Tree.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.delete方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testDelete
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import delete [as 别名]
def testDelete(self):
tree = Tree()
copy_q = copy(insert_queue)
for word in copy_q:
tree.insert(word)
tree.delete(copy_q[-1])
self.assertEqual(tree.search(copy_q[-1]), None)
copy_q.pop()
copy_q.sort()
self.assertEqual(tree.inorder_traverse(), " ".join(copy_q))
示例2: TestTree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import delete [as 别名]
class TestTree(unittest.TestCase):
def setUp(self):
# Creating the tree
self.tree = Tree()
self.tree.insertData(3)
self.tree.insertData(1)
self.tree.insertData(7)
self.tree.insertData(5)
def tearDown(self):
self.tree = None
def testInsertData(self):
root = self.tree.root
self.assertEqual(root.data, 3)
self.assertEqual(root.lchild.data, 1)
self.assertEqual(root.rchild.data, 7)
self.assertEqual(root.rchild.lchild.data, 5)
def testDelete(self):
root = self.tree.root
# Testing deletion of node with 2 children.
self.tree.delete(root, 3)
self.assertEqual(root.data, 1)
# Testing deletion of node with 1 child.
self.tree.delete(root, 7)
self.assertEqual(root.rchild.data, 5)
# Testing deletion of leaf node.
self.tree.delete(root, 5)
self.assertEqual(root.rchild, None)
def testIsKeyPresent(self):
root = self.tree.root
isKeyPresent = self.tree.isKeyPresent(root, 3)
self.assertTrue(isKeyPresent)
isKeyPresent = self.tree.isKeyPresent(root, 9)
self.assertFalse(isKeyPresent)
def testLargest(self):
root = self.tree.root
largest = self.tree.largest(root)
self.assertEqual(largest.data, 7)
示例3: print
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import delete [as 别名]
tree.insertData(46)
tree.insertData(49)
tree.insertData(55)
tree.insertData(52)
tree.insertData(51)
tree.insertData(29)
tree.insertData(35)
tree.insertData(32)
tree.insertData(36)
# To test immutability.
# The code below throws an error becuase the object is now immutable
#tree.newProp = "testing immutability"
# Inorder traversal of a tree
print("Inorder traversal:")
tree.inorderPrint(tree.root)
# Check if a particular key is present in a tree or not
key = 3
isKeyPresent = tree.isKeyPresent(tree.root, key)
print("Is Key Present: ", isKeyPresent)
# Deleting a few nodes from the tree
tree.delete(tree.root, 50)
tree.delete(tree.root, 36)
tree.delete(tree.root, 40)
# Inorder traversal after deleting the nodes
print("Inorder traversal:")
tree.inorderPrint(tree.root)