当前位置: 首页>>代码示例>>Python>>正文


Python Tree.insertData方法代码示例

本文整理汇总了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)
开发者ID:chaudharyp,项目名称:immutable-tree,代码行数:44,代码来源:treeTests.py

示例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)
开发者ID:chaudharyp,项目名称:immutable-tree,代码行数:33,代码来源:app.py


注:本文中的tree.Tree.insertData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。