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


Python Tree.insert方法代码示例

本文整理汇总了Python中tree.Tree.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.insert方法的具体用法?Python Tree.insert怎么用?Python Tree.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tree.Tree的用法示例。


在下文中一共展示了Tree.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestTree

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
class TestTree(unittest.TestCase):

    def __init__(self, methodName, humanName, maxPts):
        super(TestTree, self).__init__(methodName)
        self.methodName = methodName
        self.humanName = humanName
        self.maxPts = maxPts
        self.points = 0

    def setUp(self):
        self.tree = Tree()

    def testInsert(self):
        self.assertEquals(self.tree.size(), 0)
        self.tree.insert(Node(7))
        self.assertEquals(self.tree.size(), 1)
        self.tree.bulk_insert(7,3,4,5,3,4,6,6,2,1,10)
        self.assertEquals(self.tree.size(), 12)
        self.assertEquals(self.tree.size(), 3)

    def testNothing(self):
        pass

    def __str__(self):
        return self.humanName
开发者ID:dhellen,项目名称:play,代码行数:27,代码来源:test_tree.py

示例2: testTraverse

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
    def testTraverse(self):
        tree = Tree()
        for word in insert_queue:
            tree.insert(word)

        insert_queue.sort()
        self.assertEqual(tree.inorder_traverse(), " ".join(insert_queue))
开发者ID:theevocater,项目名称:pydatastructures,代码行数:9,代码来源:test.py

示例3: test_exceptions

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
    def test_exceptions(self):
        with self.assertRaises(ValueError):
            t = Tree("t", "T")
            t.insert('t', "T")

        with self.assertRaises(KeyError):
            t = Tree('t', "T")
            t.find('z')        
开发者ID:cbabil,项目名称:python-classes,代码行数:10,代码来源:test_tree.py

示例4: testSearch

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
    def testSearch(self):
        tree = Tree()
        for word in insert_queue:
            tree.insert(word)

        for i in insert_queue:
            self.assertEqual(tree.search(i), i)

        self.assertEqual(tree.search("not found"), None)
开发者ID:theevocater,项目名称:pydatastructures,代码行数:11,代码来源:test.py

示例5: test_tree_find

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
 def test_tree_find(self):
     'Verify find method, with data and without'
     t = Tree('D')
     for c, i in (('F', [6,7,8]),('C', 3), ('G', dict(one=1, two=2)), ('A', 1), ('E', 5), ('B', 2)):
         t.insert(c, i)
     self.assertEqual(t.find('D'), None)
     self.assertEqual(t.find('A'), 1)
     self.assertEqual(t.find('F'), [6,7,8])
     self.assertEqual(t.find('G'), {'one':1, 'two':2})
     self.assertRaises(KeyError, t.find, 'Z')
开发者ID:rwehner,项目名称:rl,代码行数:12,代码来源:test_tree.py

示例6: testDelete

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [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

示例7: Test

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
class Test(unittest.TestCase):


    def setUp(self):
        self.test = Tree("D", ord("D"))
        for c in "BJQKFAC":
            self.test.insert(c, ord(c)) 


    def testTree(self):
        self.assertEqual(self.test.answer(70), 'F')
        self.assertEqual(self.test.answer(81), 'Q')
        
    def testExceptions(self):
        self.assertRaises(KeyError, self.test.find(99))
开发者ID:paulrefalo,项目名称:Python-2---4,代码行数:17,代码来源:test_tree.py

示例8: testInsert

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
 def testInsert(self):
     tree = Tree()
     temp = "b"
     tree.insert(temp)
     self.assertEqual(tree.head.item, temp)
     temp = "a"
     tree.insert(temp)
     self.assertEqual(tree.head.left.item, temp)
     temp = "cat"
     tree.insert(temp)
     self.assertEqual(tree.head.right.item, temp)
开发者ID:theevocater,项目名称:pydatastructures,代码行数:13,代码来源:test.py

示例9: getpath

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
    return None
def getpath(root,node):
    l =[]
    r = []
    if root == node:
        return [root]
    if root.left:
        l = getpath(root.left,node)
    if root.right:
        r = getpath(root.right,node)
    if l != [] or r != []:
        return [root] + l + r
    return []

def left_most(root):
    temp = root
    while temp.left:
        temp = temp.left
    return temp

t = Tree()
for i in range(15):
    t.insert(i)
print t.inorder(t.root)
for i in range(15):
    n = t.getNodeinstance(i)
    if inorder_next(t.root,n) != None:
        print n.val,"->",inorder_next(t.root,n).val
    else:
        print n.val,"->",None
开发者ID:aadiuppal,项目名称:programming,代码行数:32,代码来源:inorder_next.py

示例10: print

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
from tree import Tree

print("Tree test")

t = Tree()
t.insert(1, t)
t.insert(30, t)
t.insert(15, t)
开发者ID:jerrylu8888,项目名称:python-practice,代码行数:10,代码来源:test.py

示例11: show

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
	def show(self, result):
		by_product = {}
		by_id = {}
		class TreeBug(dict):
			def __init__(self, b):
				for i in b:
					self[i] = b[i]
			def __str__(self):
				res = ""
				if 'notmybug' in self:
					res = '\033[33m'
				res += str(self['id']) + " "
				res += str(self['priority']) + " "
				res += str(self['estimated_time']) + " "
				res += str(self['status'][:3]) + " "
				res += str(self['severity'][:3]) + " "
				res += str(self['summary'].encode('utf-8')) + " "
				res += str(self['blocks']) + " "
				creat = datetime.strptime(str(self['creation_time']).split('T')[0], "%Y%m%d").date()
				creat += timedelta(days=severity_importance[self['severity']])
				if self['severity'] != "enhancement":
					res += str((creat - date.today()).days)
				if 'notmybug' in self:
					res += '\033[0m'
				#res += str(self['blocklist']) + " "
				return res

			def equals(self, b):
				return self['id'] == b['id']
			def is_above(self, b):
				return self.is_parent_of(b)
			def is_parent_of(self, b):
				return self['id'] in b['blocklist']
			def update(self, b):
				pass
		class Walker(object):
			def __init__(self):
				self.parents = []
			def _update_parents(self, bug):
				while len(self.parents):
					parent = self.parents[-1]
					self.parents = self.parents[:-1]
					if parent.is_parent_of(bug):
						self.parents.append(parent)
						break
		class TreePrinter(Walker):
			def __call__(self, bug):
				self._update_parents(bug)
				print '{} {}'.format('  ' * len(self.parents), str(bug))
				self.parents.append(bug)
				return True;
		class TreeEstimator(Walker):
			def __call__(self, bug):
				self._update_parents(bug)
				for parent in self.parents:
					parent['estimated_time'] += bug['estimated_time']
				self.parents.append(bug)
				return True;

		mybugs = []
		for bug in result:
			mybugs.append(TreeBug(bug))
		result = None

		for bug in mybugs:
			by_product.setdefault(bug['product'], [])
			by_product[bug['product']].append(bug)
			by_id[bug['id']] = bug

		for bug in mybugs:
			res = []
			self.addblock(by_id, bug, res)
			bug['blocklist'] = res
		for product in by_product:
			print product
			bugtree = Tree()
			#by_product[product] = sorted(by_product[product], cmp=bugcmp)
			for bug in sorted(by_product[product], cmp=bugcmp):
				bugtree.insert(bug)
			bugtree.walk(TreeEstimator())
			bugtree.walk(TreePrinter())
			print
开发者ID:sikroz,项目名称:pybugz,代码行数:84,代码来源:cli.py

示例12: test_tree_no_right

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
 def test_tree_no_right(self):
     t = Tree('D', 4)
     for c, i in (('C', 3), ('A', 1), ('B', 2)):
         t.insert(c, i)
     self.assertEqual(list(t.walk()), ['A', 'B', 'C', 'D'])
开发者ID:rwehner,项目名称:rl,代码行数:7,代码来源:test_tree.py

示例13: test_insert

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
 def test_insert(self):
     t = Tree("t", "T")
     t.insert('u', "U")
     observed = list(t.walk())
     expected = ['t', 'u']
     self.assertEqual(observed, expected)
开发者ID:cbabil,项目名称:python-classes,代码行数:8,代码来源:test_tree.py

示例14: test_find_lowest_common_ancestor

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
 def test_find_lowest_common_ancestor(self):
     tree = Tree()
     tree.insert(20)
     tree.insert(8)
     tree.insert(22)
     tree.insert(4)
     tree.insert(12)
     tree.insert(10)
     tree.insert(14)
     self.assertEqual(find_lowest_common_ancestor(tree.root, 4, 14).data, 8)
开发者ID:platzhirsch,项目名称:algorithms,代码行数:12,代码来源:lowest_common_ancestor.py

示例15: test_tree_both_lr

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import insert [as 别名]
 def test_tree_both_lr(self):
     'Test tree with both L and R nodes. Also no data passed in.'
     t = Tree('D')
     for c in 'FGCAEB':
         t.insert(c)
     self.assertEqual(list(t.walk()), ['A', 'B', 'C', 'D', 'E', 'F', 'G']) 
开发者ID:rwehner,项目名称:rl,代码行数:8,代码来源:test_tree.py


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