コード例 #1
0
ファイル: main.py プロジェクト: trevanreeser41/binarytree
def main():
    tree = BinaryTree()
    tree.set('c', 'C')
    tree.set('h', 'H')
    tree.set('a', 'A')
    tree.set('e', 'E')
    tree.set('f', 'F')
    tree.set('d', 'D')
    tree.set('b', 'B')
    tree.set('k', 'K')
    tree.set('j', 'J')
    tree.set('i', 'I')
    tree.set('g', 'G')
    tree.set('l', 'L')

    print('Lookups:')
    print(tree.get('f'))
    print(tree.get('b'))
    print(tree.get('i'))
    print()

    print('DFS preorder:')
    for key, value in tree.walk_dfs_preorder():
        print(key, value)
    print()

    print('DFS inorder:')
    for key, value in tree.walk_dfs_inorder():
        print(key, value)
    print()

    print('DFS postorder:')
    for key, value in tree.walk_dfs_postorder():
        print(key, value)
    print()

    print('BFS:')
    for key, value in tree.walk_bfs():
        print(key, value)
    print()

    print('Initial tree:')
    print(tree)
    print()

    print('Remove b:')
    tree.remove('b')
    print(tree)
    print()

    print('Remove f:')
    tree.remove('f')
    print(tree)
    print()

    print('Remove h:')
    tree.remove('h')
    print(tree)
    print()
コード例 #2
0
ファイル: test.py プロジェクト: WannesVanLeemput/algoritmen
def test_binarytree():
    test_passed = True
    test_database = pickle.load( open( 'testdatabase.p', 'rb' ) )
    test_database = test_database['binarytree']

    keys = test_database['keys']
    result = test_database['result']

    BT = BinaryTree()
    for key in keys:
        BT.insert(key,None)

    nodes = []
    nodes.append(BT._root)
    nodes_checked = 0

    while len(nodes) > 0:
        node = nodes.pop()
        if not node is None:
            nodes_checked += 1
            nodes.append(node._left)
            nodes.append(node._right)
            target = result[node._key]
            if node._left is None:
                if not target['l'] is None:
                    test_passed = False
            else:
                if not node._left._key == target['l']:
                    test_passed = False
            if node._right is None:
                if not target['r'] is None:
                    test_passed = False
            else:
                if not node._right._key == target['r']:
                    test_passed = False

    BT = BinaryTree()
    BT.insert('1','a')
    BT.insert('1','b')
    BT.insert('1','c')

    if len(BT.get('1')) != 3:
        test_passed = False

    return test_passed
コード例 #3
0
ファイル: kvdb.py プロジェクト: leofanky/db
class DB(object):
    def __init__(self, f):
        self.storage = Storage(f)
        self.tree = BinaryTree(self.storage)

    def assert_not_closed(self):
        if self.storage.closed:
            print("db closed")
            raise ValueError("db closed")

    def close(self):
        self.storage.close()

    def update(self):
        self.assert_not_closed()
        self.tree.update()

    def __getitem__(self, key):
        self.assert_not_closed()
        return self.tree.get(key)

    def __setitem__(self, key, value):
        self.assert_not_closed()
        return self.tree.set(key, value)

    def __delitem__(self, key):
        self.assert_not_closed()
        return self.tree.delete(key)

    def __contains__(self, key):
        self.assert_not_closed()
        try:
            self[key]
        except KeyError:
            return False
        else:
            return True

    def __len__(self):
        return len(self.tree)

    def listAll(self):
        return self.tree.listAll()