本文整理汇总了Python中bst.BinarySearchTree类的典型用法代码示例。如果您正苦于以下问题:Python BinarySearchTree类的具体用法?Python BinarySearchTree怎么用?Python BinarySearchTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BinarySearchTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_iterator
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)
示例2: test_print_node
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")
示例3: test_multiple_adds
def test_multiple_adds():
tree = BinarySearchTree()
tree.add(3)
tree.add(1)
tree.add(9)
tree.add(6)
assert(len(tree) == 4)
示例4: test_nonleaf_rightnone_delete
def test_nonleaf_rightnone_delete(self):
""" Try to delete non leaf node with no right child. """
a = BinarySearchTree(7, 'Harry')
a[8] = 'Ron'
a[5] = 'Ginny'
a[4] = 'Hermione'
a.delete(5)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (7, 'Harry'), (8, 'Ron')])
示例5: test_multiple_leaf
def test_multiple_leaf(self):
""" Tree of single item. """
a = BinarySearchTree()
a[7] = 'Harry'
a[8] = 'Ron'
a.put(4, 'Hermione')
a[9] = 'Ginny'
self.assertFalse(a._get(7, a.root).isLeaf())
示例6: test_multiple_element_get
def test_multiple_element_get(self):
""" Put mulitple items into an empty bst and test inorder. """
a = BinarySearchTree()
a.put(7, 'Harry')
a[8] = 'Ron'
a.put(4, 'Hermione')
a[9] = 'Ginny'
self.assertTrue(a[4] == 'Hermione')
示例7: test_has_children
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())
示例8: test_nonleaf_delete
def test_nonleaf_delete(self):
""" Try to delete non leaf node with both children. """
a = BinarySearchTree(7, 'Harry')
a[8] = 'Ron'
a[5] = 'Ginny'
a[4] = 'Hermione'
a[6] = 'Neville'
""" 7 - Harry
/ \
5- Ginny 8 - Ron
/ \
4-Hermione 6-Neville
"""
a.delete(7)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (5, 'Ginny'), (6, 'Neville'), (8, 'Ron')])
self.assertTrue(a.root.value == 'Neville')
a[7] = 'Harry'
self.assertTrue(a.inorder() == [(4, 'Hermione'), (5, 'Ginny'), (6, 'Neville'), (7, 'Harry'), (8, 'Ron')])
a.delete(6)
self.assertTrue(a.inorder() == [(4, 'Hermione'), (5, 'Ginny'), (7, 'Harry'), (8, 'Ron')])
self.assertTrue(a.root.value == 'Ginny')
示例9: main
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)
示例10: test_delete_without_children
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)
示例11: test_parent_pointer_after_delete
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)
示例12: __init__
def __init__(self, data, snp, sample_id):
'''
Construct a genotype set from data arrays:
- snp: SNP metadata record array (contains chromosome, name, morgans, base-pair location)
- data: a 3-D genotype data array: (individual x SNP x allele)
- sample_id: genotyped individuals' ID set
'''
# People's IDs
self.sample_id = sample_id
self.data = data
self._num_snps = self.data.shape[0]
self._num_samples = self.data.shape[1]
self._snp_range = None
# SNP metadata: SNP label, chromosome number, Genetic distance in Morgans, and
# base pair location for each SNP
self.snp = snp
# Base-pair-location to snp-index map, lazily-initialized + cached
base_pair = self.snp['base_pair']
self._base_pair = base_pair # np.array([int(base_pair)]) if base_pair.size == 1 else base_pair
self._bp_to_snp = dict_invert(dict(enumerate(self._base_pair)))
# Construct a BST for fast bp queries
self._snp_tree = BinarySearchTree(values=self._base_pair[optimal_insertion_order(self._num_snps)])
self._snp_index_tree = util.list_index_tree(self._base_pair)
# A genetic map: lists the two allele letters corresponding to 1 and 2 for each SNP, according
# their order in the self.snp array.
self.map = []
# General metadata, for easy handling of CGI data
self.metadata = []
# samples for which the parent-of-origin phase is determined
self.poo_phase = np.zeros((self._num_samples,), dtype=np.byte)
示例13: test_delete_two_children
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)
示例14: test_delete_root
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)
示例15: test_add
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)