本文整理汇总了Python中bst.BinarySearchTree.add方法的典型用法代码示例。如果您正苦于以下问题:Python BinarySearchTree.add方法的具体用法?Python BinarySearchTree.add怎么用?Python BinarySearchTree.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parent_pointer_after_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_parent_pointer_after_delete():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.delete(3)
assert(tree.get(6).parent.value == 1)
示例2: test_delete_without_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_delete_without_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.delete(6)
assert(tree.get(3).childcount() == 0)
示例3: test_multiple_adds
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_multiple_adds():
tree = BinarySearchTree()
tree.add(3)
tree.add(1)
tree.add(9)
tree.add(6)
assert(len(tree) == 4)
示例4: test_delete_root
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_delete_root():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.add(2)
tree.delete(1)
assert(tree.root.value == 3)
示例5: test_delete_two_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_delete_two_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.add(2)
tree.delete(3)
assert(tree.contains(3) is False)
assert(len(tree) == 3)
示例6: test_delete_successor_with_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_delete_successor_with_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(5)
tree.add(4)
tree.add(10)
tree.add(9)
tree.add(7)
tree.add(8)
tree.delete(5)
assert(tree.get(9).get_only_child().value == 8)
示例7: test_print_node
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_print_node(self):
bst = BinarySearchTree()
self.assertEqual(bst.min(), None)
node_val1 = 6
bst.add(node_val1)
node_val2 = 4
bst.add(node_val2)
node_val3 = 10
bst.add(node_val3)
node_val4 = 16
bst.add(node_val4)
# 6
# / \
# 4 10
# \
# 16
#self.assertEqual(bst.__str__(), "4 6 10")
self.assertEqual(bst.root_node.__str__(), "4 6 10")
self.assertEqual(bst.root_node.right_child.__str__(), "10 16")
self.assertEqual(bst.root_node.right_child.right_child.__str__(), "16")
示例8: test_iterator
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_iterator(self):
bst = BinarySearchTree()
self.assertEqual(bst.min(), None)
node_val1 = 6
bst.add(node_val1)
node_val2 = 4
bst.add(node_val2)
node_val3 = 10
bst.add(node_val3)
node_val4 = 16
bst.add(node_val4)
# 6
# / \
# 4 10
# \
# 16
ite = BinarySearchIterator(bst)
self.assertEquals(ite.tree, bst)
self.assertEquals(ite.next(), 4)
self.assertEquals(ite.next(), 6)
self.assertEquals(ite.next(), 10)
self.assertEquals(ite.next(), 16)
self.assertRaises(StopIteration, ite.next)
示例9: test_has_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_has_children(self):
# 6
# / \
# 4 10
# \
# 16
bst = BinarySearchTree()
node_val1 = 6
bst.add(node_val1)
node_val2 = 4
bst.add(node_val2)
node_val3 = 10
bst.add(node_val3)
node_val4 = 16
bst.add(node_val4)
self.assertEqual(bst.size, 4)
node = bst.root_node # we have 6
self.assertEqual(node.value, node_val1)
self.assertTrue(node.has_left_child())
self.assertEqual(node.left_child.value, node_val2)
self.assertTrue(node.has_right_child())
self.assertEqual(node.right_child.value, node_val3)
node = node.left_child # we have 4 now
self.assertFalse(node.has_left_child())
self.assertFalse(node.has_right_child())
示例10: test_is_search_tree
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_is_search_tree(self):
# No assertion here, I just want a beautifully designed tree.
bst = BinarySearchTree()
self.assertEqual(bst.min(), None)
node_val1 = 6
bst.add(node_val1)
node_val2 = 4
bst.add(node_val2)
node_val3 = 10
bst.add(node_val3)
node_val4 = 16
bst.add(node_val4)
# 6
# / \
# 4 10
# \
# 16
self.assertTrue(BinarySearchTree.is_search_tree(bst.root_node))
# 6
# / \
# 8 10
# \
# 16
bst.root_node.left_child.value = 8
self.assertEqual(bst.root_node.left_child.value, 8)
self.assertFalse(BinarySearchTree.is_search_tree(bst.root_node))
# 12
# / \
# 4 10
# \
# 16
bst.root_node.left_child.value = 4
self.assertEqual(bst.root_node.left_child.value, 4)
bst.root_node.value = 12
self.assertEqual(bst.root_node.value, 12)
self.assertFalse(BinarySearchTree.is_search_tree(bst.root_node))
# 6
# / \
# 4 20
# \
# 16
bst.root_node.left_child.value = 6
self.assertEqual(bst.root_node.left_child.value, 6)
bst.root_node.right_child.value = 20
self.assertEqual(bst.root_node.right_child.value, 20)
self.assertFalse(BinarySearchTree.is_search_tree(bst.root_node))
示例11: main
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def main():
a = BinarySearchTree()
a.add(2, '')
a.add(1, '')
a.add(4, '')
a.add(5, '')
b = BinarySearchTree()
b.add(5, '')
b.add(2, '')
b.add(3, '')
print diff(a, b)
示例12: main
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def main():
name = sys.argv[1];
for i in xrange(1, 11):
fname = '../data/%d_catalog.txt' % i
#read the catalog and load to a list
phonebook = []
append = phonebook.append
with open(fname, 'r') as f:
for l in f:
if l:
append(Entry.fromline(l))
#print 'phonebook contains %d entries' % len(phonebook)
name_idx = BinarySearchTree()
for i, e in enumerate(phonebook):
name_idx.add(e.name, i)
start = time.time()
idxs = name_idx.get(name)
bstelapsed = (time.time() - start) * 1000
# let's sort the entries by name
phonebook.sort(key=lambda e: e.name)
# let' search for a non existing name using naive search
start = time.time()
idx = search.naive(phonebook, name)
nelapsed = (time.time() - start) * 1000
# the same using binary search
bstart = time.time()
idx = search.binary_search(phonebook, name)
bselapsed = (time.time() - bstart) * 1000
print '%d - naive: %g - binary: %g - bst: %g'\
% (len(phonebook), nelapsed, bselapsed, bstelapsed)
示例13: test_add
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_add(self):
bst = BinarySearchTree()
self.assertEqual(bst.size, 0)
node_val1 = 6
bst.add(node_val1)
self.assertEqual(bst.size, 1)
node_val2 = 4
bst.add(node_val2)
node_val3 = 10
bst.add(node_val3)
node_val4 = 16
bst.add(node_val4)
self.assertEqual(bst.size, 4)
示例14: test_bst
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_bst():
B = BinarySearchTree()
assert B.size() == 0
B.add(25)
assert B.size() == 1
B.add(25)
assert B.size() == 1
B.add(25)
assert B.size() == 1
B.add(15)
assert B.size() == 2
assert B.find(25)
assert B.find(15)
assert not B.find(2)
B.remove(15)
assert B.size() == 1
assert not B.find(15)
assert B.find(25)
assert not B.find(2)
示例15: test_print_tree
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import add [as 别名]
def test_print_tree(self):
bst = BinarySearchTree()
self.assertEqual(bst.min(), None)
node_val1 = 6
bst.add(node_val1)
node_val2 = 4
bst.add(node_val2)
node_val3 = 10
bst.add(node_val3)
node_val4 = 16
bst.add(node_val4)
# 6
# / \
# 4 10
# \
# 16
self.assertEqual(bst.__str__(), "4 6 10 16")