Esempio n. 1
0
def simple_tree():
    t = BinaryTree()
    t.insert(5)
    t.insert(10)
    t.insert(2)
    t.insert(17)
    return t
Esempio n. 2
0
def balanced_tree():
    t = BinaryTree()
    t.insert(5)
    t.insert(2)
    t.insert(1)
    t.insert(3)
    t.insert(8)
    t.insert(7)
    t.insert(9)
    return t
def simple_tree():
    t = BinaryTree()
    t.insert(5)
    t.insert(10)
    t.insert(2)
    t.insert(17)
    return t
Esempio n. 4
0
def test_binary_search_tree():
    # Create a complete binary search tree of 3, 7, or 15 items in level-order
    # items = [2, 1, 3]
    items = [4, 2, 6, 1, 3, 5, 7]
    # items = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15]
    print('items: {}'.format(items))

    tree = BinarySearchTree()
    print('tree: {}'.format(tree))
    print('root: {}'.format(tree.root))

    print('\nInserting items:')
    for item in items:
        tree.insert(item)
        print('insert({}), size: {}'.format(item, tree.size))
    print('root: {}'.format(tree.root))

    print('\nSearching for items:')
    for item in items:
        result = tree.search(item)
        print('search({}): {}'.format(item, result))
    item = 123
    result = tree.search(item)
    print('search({}): {}'.format(item, result))

    print('\nTraversing items:')
    print('items in-order:    {}'.format(tree.items_in_order()))
    print('items pre-order:   {}'.format(tree.items_pre_order()))
    print('items post-order:  {}'.format(tree.items_post_order()))
    print('items level-order: {}'.format(tree.items_level_order()))
def balanced_tree():
    t = BinaryTree()
    t.insert(5)
    t.insert(2)
    t.insert(1)
    t.insert(3)
    t.insert(8)
    t.insert(7)
    t.insert(9)
    return t
Esempio n. 6
0
def unbalanced_tree():
    t = BinaryTree()
    t.insert(5)
    t.insert(6)
    t.insert(7)
    t.insert(8)
    t.insert(9)
    t.insert(4)
    return t
def unbalanced_tree():
    t = BinaryTree()
    t.insert(5)
    t.insert(6)
    t.insert(7)
    t.insert(8)
    t.insert(9)
    t.insert(4)
    return t
class TreeMap:

    def __init__(self):
        self.tree = BinaryTree()
        self.size = 0

    def __str__(self):
        """Return a formatted string representation of cursed tree map."""
        items = ['{!r}: {!r}'.format(key, val) for key, val in self.items()]
        return '{' + ', '.join(items) + '}'

    def __repr__(self):
        """Return a string representation of this cursed tree map."""
        return 'TreeMap({!r})'.format(self.items())

    def items(self):
        items = self.tree.items_in_order()
        retlist = []
        for item in items:
            retlist.append((item.key, item.value))
        return retlist

    def keys(self):
        items = self.tree.items_in_order()
        for item in items:
            yield item.key

    def set(self, key, value):
        self.tree.set_insert(KeyValuePackage(key, value))

    def get(self, key):
        return self.tree.search(key).value

    def contains(self, key):
        return self.tree.contains(key)

    def delete(self, key):
        self.tree.delete(key)
Esempio n. 9
0
def complex_tree():
    t = BinaryTree()
    t.insert(100)
    t.insert(80)
    t.insert(60)
    t.insert(40)
    t.insert(20)
    t.insert(50)
    t.insert(70)
    t.insert(90)
    t.insert(85)
    t.insert(95)
    t.insert(120)
    t.insert(140)
    t.insert(160)
    t.insert(180)
    t.insert(110)
    t.insert(175)
    t.insert(170)
    t.insert(176)
    return t
Esempio n. 10
0
def large_binary_tree():
    bt = BinaryTree([20, 18, 12, 19, 11, 14, 40, 31, 22, 33])
    return bt
Esempio n. 11
0
def test_binary_tree_initializes():
    assert BinaryTree([1, 2, 3])
Esempio n. 12
0
    if root is None: return []
    stack, res = [(root, False)], []
    while stack:
        cur, visited = stack.pop()
        if cur is None: continue
        if visited:
            res.append(cur.val)
        else:
            stack.append((cur, True))
            stack.append((cur.right, False))
            stack.append((cur.left, False))
    return res


if __name__ == '__main__':
    bst = BinaryTree(serialize=','.join(map(str, range(1, 1 << 5))))

    pre = bst.pre_order()
    assert pre == pre_order1(bst.root)
    assert pre == pre_order2(bst.root)
    assert pre == pre_order3(bst.root)
    assert pre == pre_order4(bst.root)

    in_ = bst.in_order()
    assert in_ == in_order1(bst.root)
    assert in_ == in_order2(bst.root)
    assert in_ == in_order3(bst.root)

    post = bst.post_order()
    assert post == post_order1(bst.root)
    assert post == post_order2(bst.root)
 def __init__(self):
     self.tree = BinaryTree()
     self.size = 0
def complex_tree():
    t = BinaryTree()
    t.insert(100)
    t.insert(80)
    t.insert(60)
    t.insert(40)
    t.insert(20)
    t.insert(50)
    t.insert(70)
    t.insert(90)
    t.insert(85)
    t.insert(95)
    t.insert(120)
    t.insert(140)
    t.insert(160)
    t.insert(180)
    t.insert(110)
    t.insert(175)
    t.insert(170)
    t.insert(176)
    return t
Esempio n. 15
0
def empty_tree():
    t = BinaryTree()
    return t
Esempio n. 16
0
def test_binarytree():
    t = BinaryTree()
    assert t.root is None
Esempio n. 17
0
def small_binary_tree():
    bt = BinaryTree([10, 4, 39])
    return bt
Esempio n. 18
0
def test_balance_one_node_tree_returns_1():
    t = BinaryTree()
    t.insert(1)
    assert t.balance() == 0
def test_balance_one_node_tree_returns_1():
    t = BinaryTree()
    t.insert(1)
    assert t.balance() == 0