本文整理汇总了Python中bst.BinarySearchTree.insert方法的典型用法代码示例。如果您正苦于以下问题:Python BinarySearchTree.insert方法的具体用法?Python BinarySearchTree.insert怎么用?Python BinarySearchTree.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.insert方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_depth
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def test_depth():
tree = BinarySearchTree()
tree.insert(2)
tree.insert(4)
tree.insert(5)
tree.insert(1)
assert tree.depth() == 3
示例2: test_balance
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def test_balance():
tree = BinarySearchTree()
tree.insert(2)
tree.insert(4)
tree.insert(5)
tree.insert(1)
assert tree.balance() == -1
示例3: test_size
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def test_size():
tree = BinarySearchTree()
tree.insert(2)
tree.insert(4)
tree.insert(5)
tree.insert(1)
assert tree.size() == 4
示例4: test_deletion
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def test_deletion(self):
tree = BinarySearchTree()
tree.insert(8)
tree.insert(3)
tree.insert(10)
tree.insert(1)
tree.insert(6)
tree.insert(4)
tree.insert(7)
tree.insert(14)
tree.insert(13)
tree.delete(1)
tree.delete(14)
tree.delete(6)
self.assertEquals(tree.root.value, 8)
self.assertEquals(tree.root.left.value, 3)
self.assertEquals(tree.root.left.left, None)
self.assertEquals(tree.root.left.right.value, 7)
self.assertEquals(tree.root.left.right.left.value, 4)
self.assertEquals(tree.root.left.right.right, None)
self.assertEquals(tree.root.right.value, 10)
self.assertEquals(tree.root.right.right.value, 13)
tree.delete(8)
self.assertEquals(tree.root.value, 10)
self.assertEquals(tree.root.right.value, 13)
示例5: fixed_six_node_bst
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def fixed_six_node_bst():
tree = BinarySearchTree()
tree.insert(15)
tree.insert(7)
tree.insert(20)
tree.insert(3)
tree.insert(9)
tree.insert(2)
return tree
示例6: test_search
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def test_search(self):
# Emptry Tree
self.assertEquals(BinarySearchTree().search(8), None)
tree = BinarySearchTree()
tree.insert(8)
tree.insert(3)
tree.insert(10)
tree.insert(1)
tree.insert(6)
tree.insert(4)
tree.insert(7)
tree.insert(14)
tree.insert(13)
self.assertEquals(tree.search(6).value, 6)
self.assertEquals(tree.search(2), None)
self.assertEquals(tree.search(15), None)
self.assertEquals(tree.search(13).value, 13)
self.assertEquals(tree.search(1).value, 1)
示例7: build_subsequence_tree
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def build_subsequence_tree(input_list):
"""
Builds and returns a binary search tree containing elements of the
sequence using the container class. Also returns the last element
of a longest increasing subsequence. This way the length of a
longest increasing subsequence and that sequence can easily be
calculated.
input: A list of integers. No repeats!
output: An ordered pair (bst,longest)
bst = The binary search tree representing subsequences
longest = Is the last element of a longest increasing subsequence
in its container class.
"""
# The last element of one of the longest increasing sbusequences
longest = Element(input_list[0],1,None)
# The binary search tree we'll use.
bst = BinarySearchTree()
# For each element of the input list, we generate an element
# container class for the element to represent it's behavior as
# the last element of an increasing subsequence. If this
# subsequence is longer than any other calculated, we record this.
for i in input_list:
greatest_element_less_than_i = bst.find_greatest_key_less_than(i)
# If it exists, put i in an element that points back to the
# greatest element less than i. Record that the subsequence is
# longer.
if greatest_element_less_than_i != None:
current = Element(i,
greatest_element_less_than_i.length+1,
greatest_element_less_than_i)
bst.insert(current)
# If this is now the longest increasing subsequence, keep
# track of that.
if current.length > longest.length:
longest = current
else: # This is the smallest element we've encountered so
# far. Put it in the BST
bst.insert(Element(i,1,None))
return (bst,longest)
示例8: TestRange
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
class TestRange(unittest.TestCase):
def setUp(self):
self.bst = BinarySearchTree(5)
# Notice that we're using the interface method
# to add values to the tree, rather than
# manipulating the left and right attributes directly.
self.bst.insert(5)
self.bst.insert(10)
self.bst.insert(15)
self.bst.insert(20)
self.bst.insert(25)
self.bst.insert(30)
self.bst.insert(30)
def test_simple(self):
self.assertEqual(self.bst.list_range(4, 7), [5, 5])
self.assertEqual(self.bst.list_range(12, 50), [15, 20, 25, 30, 30])
def test_inclusive(self):
self.assertEqual(self.bst.list_range(5, 15), [5, 5, 10, 15])
self.assertEqual(self.bst.list_range(25, 30), [25, 30, 30])
def test_simple_empty(self):
self.assertEqual(self.bst.list_range(6, 7), [])
示例9: BinarySearchTree
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
"""
ASTR 598 HW 7
Brett Morris
"""
from __future__ import print_function # Python 2 compatibility
from bst import BinarySearchTree
# Generate some random integers:
import numpy as np
integers = np.random.randint(0, 1000, size=100).tolist()
bst = BinarySearchTree()
# Insert some random integers into the binary search tree:
for integer in integers:
bst.insert(integer)
# Traverse that tree in order (notice they're in order, wahoo!)
print("Traverse:")
bst.traverse()
# Find min, max:
print("Min: {0}, max: {1}".format(bst.min(), bst.max()))
示例10: replace
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
if __name__ == "__main__":
def replace(string):
"""
Search uses regex for searching with wildcards.
The . (dot) is any char.
"""
return string.replace('*', '.', 25)
parser = argparse.ArgumentParser(description="Binary Search Tree. Builds\
a tree from a file containing a list of\
words. Then searches that tree for the\
inputed word. Wildcards(*) are allowed.")
parser.add_argument('file', type=str, help="File location")
parser.add_argument('word', type=str, help="Word to search")
args = parser.parse_args()
search_value = replace(args.word)
with open(args.file, 'r') as reader:
print("Building binary search tree...")
bst = BinarySearchTree()
for line in reader:
bst.insert(line.rstrip())
words = bst.search(search_value)
"""
There's a issue in the bst search function that returns None.
Lazy fix is list comprehension to remove all Nones.
"""
words = [word for word in words if word is not None]
print("Following words were matched: \n" + str(words))
示例11: fixed_unbalanced_bst
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def fixed_unbalanced_bst():
tree = BinarySearchTree()
tree.insert(15)
tree.insert(7)
tree.insert(20)
tree.insert(3)
tree.insert(9)
tree.insert(2)
tree.insert(4)
tree.insert(44)
tree.insert(1)
tree.insert(4)
tree.insert(5)
return tree
示例12: fixed_eight_node_bst
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import insert [as 别名]
def fixed_eight_node_bst():
tree = BinarySearchTree()
tree.insert(15)
tree.insert(7)
tree.insert(20)
tree.insert(3)
tree.insert(9)
tree.insert(2)
tree.insert(4)
tree.insert(44)
return tree