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


Python BST.insert方法代码示例

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


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

示例1: test_delete_one_item

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_delete_one_item():
    from bst import BST
    bst = BST()
    bst.insert(42)
    bst.delete(42)
    assert not bst._search(42)
    assert not bst.contains(42)
开发者ID:KyleRichVA,项目名称:data-structures,代码行数:9,代码来源:test_bst.py

示例2: test_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_balance():
    aBST = BST()
    assert aBST.balance() == 0
    for num, bal in zip([4, 2, 6, 3, 7, 8, 9, 10, 11],
                        [0, 1, 0, 1, 0, -1, -2, -3]):
        aBST.insert(num)
        assert aBST.balance() == bal
开发者ID:tpeek,项目名称:data-structures-1,代码行数:9,代码来源:test_bst.py

示例3: test_delete_no_children

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_delete_no_children():
    """delete(self, val) should remove val from the tree if present,
    if not present it should do nothing. Return None in all cases"""
    bintree = BST()
    with pytest.raises(TypeError):
        bintree.delete()  # 1 value required

    bintree.insert(5)
    bintree.insert(3)
    bintree.insert(7)
    bintree.insert(2)
    bintree.insert(4)
    bintree.insert(1)
    bintree.insert(9)
    bintree.insert(10)
    #     5
    #    / \
    #   3   7
    #  /\    \
    # 2  4    9
    # |       |
    # 1       10
    assert bintree.size() == 8
    assert bintree.delete(-1) is None
    assert bintree.size() == 8
    assert bintree.delete(4) is None
    assert bintree.size() == 7
    assert not bintree.leftchild.rightchild  # 4 is gone
    assert bintree.delete(10) is None
    assert bintree.size() == 6
    assert not bintree.rightchild.rightchild.leftchild  # 9 is a leaf
    assert not bintree.rightchild.rightchild.rightchild
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:34,代码来源:test_bst.py

示例4: __init__

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
class PhoneBook:

    def __init__(self):
        self.__phonebook = BST()

    def insert(self, number, name):
        entry = self.__phonebook.lookup(name)
        if entry is None:
            self.__phonebook.insert(name, [number])
        else:
            value = entry.get_value()
            value.append(number)

    def lookup(self, name):
        entry = self.__phonebook.lookup(name)
        if entry is not None:
            print('{}: [{}]'.format(entry.get_key(), entry.get_value()))
        else:
            print("NOT FOUND!")

    def list(self):
        all_entries = self.__phonebook.traverse(1)
        for e in all_entries:
            print('{}: [{}]'.format(e[0], ', '.join(e[1])))

    def remove(self, name):
        self.__phonebook.remove(name)
开发者ID:presian,项目名称:HackBulgaria,代码行数:29,代码来源:phone_book.py

示例5: test_right_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_right_balance():
    bst = BST()
    bst.insert(5)
    bst.insert(6)
    bst.insert(7)
    bst.insert(3)
    assert bst.balance() == -1
开发者ID:EyuelAbebe,项目名称:data-structures1,代码行数:9,代码来源:test_bst.py

示例6: PhoneBook

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
class PhoneBook():
    def __init__(self):
        self.tree = BST(None, compare)

#inserts a new contact
    def insert(self, number, name):
        found = self.tree.lookup(lambda x: x["value"]["name"] is name)
        if found is not None:
            found["value"]["number"] = number
        else:
            self.tree.insert({
                "name": name,
                "number": number
            })

#lookup a name and print its phone number
    def lookup(self, name):
        return self.tree.lookup(lambda x: x["value"]["name"] is name)

#list all records in an alphabetical order
    def list(self):
        result = []
        self.tree.traverse(lambda x: result.append(x["value"]))
        return result

#remove a record for a given name
    def remove(name):
        pass
开发者ID:nshahpazov,项目名称:Algo-1,代码行数:30,代码来源:phone_book.py

示例7: test_contains

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_contains():
    bst = BST()
    for i in range(1,11):
        bst.insert(i)
    assert bst.contains(1)
    assert bst.contains(5)
    assert not bst.contains(15)
开发者ID:EyuelAbebe,项目名称:data-structures1,代码行数:9,代码来源:test_bst.py

示例8: test_rebalance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_rebalance():
    """Tests my rebalancing algorithm"""
    bintree = BST()

    for i in xrange(100):
        bintree.insert(random.randint(0, 1e6))
    assert bintree.size() == 100

    original_depth = bintree.depth()

    bintree.rebalance()
    assert abs(bintree.balance()) < 2
    assert original_depth > bintree.depth()

    bintree = BST()

    for i in xrange(100):
        bintree.insert(i)  # Horribly unbalanced
    assert bintree.size() == 100
    assert bintree.depth() == 100
    assert bintree.balance() == -99

    bintree.rebalance()
    assert abs(bintree.balance()) < 2
    assert bintree.depth() < 10  # Much better, anyway
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:27,代码来源:test_bst.py

示例9: test_depth

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_depth():
    aBST = BST()
    assert aBST.depth() == 0
    for num, depth in zip([4, 2, 6, 3, 7, 8, 9, 10, 11],
                          [1, 2, 2, 3, 3, 4, 5,  6,  7]):
        aBST.insert(num)
        assert aBST.depth() == depth
开发者ID:tpeek,项目名称:data-structures-1,代码行数:9,代码来源:test_bst.py

示例10: test_size

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_size():
    bst = BST()
    assert bst.size() == 0
    bst.insert(5)
    assert bst.size() == 1
    bst.insert(5)
    assert bst.size() == 1
开发者ID:EyuelAbebe,项目名称:data-structures1,代码行数:9,代码来源:test_bst.py

示例11: test_removal

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
    def test_removal(self):
        bst = BST()
        bst.insert(5)
        bst.insert(10)
        bst.insert(1)
        bst.insert(3)
        bst.insert(59)

        # remove leaf node
        bst.remove(3)
        inorder = bst.get_inorder()
        self.assertEqual(inorder, [5, 1, 10, 59])
        self.assertEqual(bst.size, 4)

        # remove node with one child
        bst.remove(10)
        inorder = bst.get_inorder()
        self.assertEqual(inorder, [5, 1, 59])
        self.assertEqual(bst.size, 3)

        bst.insert(3)
        bst.insert(4)

        bst.remove(5)
        inorder = bst.get_inorder()
        self.assertEqual(inorder, [4, 1, 3, 59])
        self.assertEqual(bst.size, 4)
开发者ID:derekargueta,项目名称:Data-Structures,代码行数:29,代码来源:tests.py

示例12: test_insert_same

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_insert_same():
    """Test that a value cannot be added to tree if it already exisits."""
    from bst import BST
    new_bst = BST()
    new_bst.insert(5)
    with pytest.raises(ValueError):
        new_bst.insert(5)
开发者ID:palindromed,项目名称:data-structures2,代码行数:9,代码来源:test_bst.py

示例13: test_left_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_left_balance():
    bst = BST()
    bst.insert(5)
    bst.insert(4)
    bst.insert(3)
    bst.insert(6)
    assert bst.balance() == 1
开发者ID:EyuelAbebe,项目名称:data-structures1,代码行数:9,代码来源:test_bst.py

示例14: test_make_balanced_randomly

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
def test_make_balanced_randomly():
    aBST = BST()
    for y in range(300):
        for x in range(1000):
            aBST.insert(randint(0, 1000))
        aBST.make_balanced()
        b = aBST.balance()
        assert b in [1, 0, -1]
开发者ID:tpeek,项目名称:data-structures-1,代码行数:10,代码来源:test_bst.py

示例15: test_inorder_predecessor

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import insert [as 别名]
 def test_inorder_predecessor(self):
     bst = BST()
     bst.insert(5)
     bst.insert(1)
     bst.insert(2)
     bst.insert(3)
     n = bst._get_next_inorder_predecessor(bst.root)
     self.assertEqual(n.val, 3)
开发者ID:derekargueta,项目名称:Data-Structures,代码行数:10,代码来源:tests.py


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