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


Python Tree.delete方法代码示例

本文整理汇总了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))
开发者ID:theevocater,项目名称:pydatastructures,代码行数:14,代码来源:test.py

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

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


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