本文整理汇总了Python中tree.Tree.insertData方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.insertData方法的具体用法?Python Tree.insertData怎么用?Python Tree.insertData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.insertData方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestTree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insertData [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)
示例2: Tree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insertData [as 别名]
from tree import Tree
# Creating a tree
tree = Tree()
tree.insertData(50)
tree.insertData(40)
tree.insertData(30)
tree.insertData(45)
tree.insertData(48)
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)