Esempio n. 1
0
def do_minimal_tree(arr, low, high):
    if low > high:
        return None
    mid = (low + high) // 2

    node = BinaryNode(arr[mid])
    node.left = do_minimal_tree(arr, low, mid - 1)
    node.right = do_minimal_tree(arr, mid + 1, high)

    return node
Esempio n. 2
0
 def test_to_string_leaf(self):
     n = BinaryNode(BinaryNodeRef(), 'k', ValueRef(address=999),
                    BinaryNodeRef(), 1)
     pickled = BinaryNodeRef.referent_to_string(n)
     d = pickle.loads(pickled)
     eq_(d['left'], 0)
     eq_(d['key'], 'k')
     eq_(d['value'], 999)
     eq_(d['right'], 0)
Esempio n. 3
0
 def test_to_string_nonleaf(self):
     left_ref = BinaryNodeRef(address=123)
     right_ref = BinaryNodeRef(address=321)
     n = BinaryNode(left_ref, 'k', ValueRef(address=999), right_ref, 3)
     pickled = BinaryNodeRef.referent_to_string(n)
     d = pickle.loads(pickled)
     eq_(d['left'], 123)
     eq_(d['key'], 'k')
     eq_(d['value'], 999)
     eq_(d['right'], 321)
Esempio n. 4
0
IMBALANCED = -1

def check_balanced(node):
    if not node:
        return 0
    left_height = check_balanced(node.left)
    if left_height < 0:
        return IMBALANCED

    right_height = check_balanced(node.right)
    if right_height < 0:
        return IMBALANCED

    if abs(left_height - right_height) > 1:
        return IMBALANCED

    return 1 + max(left_height, right_height)

if __name__ == '__main__':
    tree = BinaryNode(0)
    tree.left = BinaryNode(1)
    tree.right = BinaryNode(2)
    # tree.right.left = BinaryNode(6)
    tree.left.left = BinaryNode(3)
    tree.left.right = BinaryNode(4)
    tree.left.right.right = BinaryNode(5)

    print(check_balanced(tree))
    print(check_balanced(tree.left))

Esempio n. 5
0
"""
Validate BST: Implement a function to check if a binary tree is a binary search tree.
"""
from math import inf
from binary_tree import BinaryNode


def validate_bst(node, minimum=-inf, maximum=inf):
    if not node:
        return True

    if node.value <= minimum or node.value > maximum:
        return False

    return validate_bst(node.left, minimum, node.value) and validate_bst(
        node.right, node.value, maximum)


if __name__ == '__main__':
    tree = BinaryNode(0)
    tree.left = BinaryNode(1)
    print(validate_bst(tree))
    print(validate_bst(tree.left))
    tree.left.left = BinaryNode(0)
    tree.left.right = BinaryNode(2)
    tree.left.left.left = BinaryNode(-1)
    print(validate_bst(tree.left))
Esempio n. 6
0
            if node.left:
                node_idx += node.left.children_count + 1
            if idx == node_idx:
                return node
            elif idx < node_idx:
                node = node.left
            else:
                node = node.right
                start = node_idx + 1

        return None


if __name__ == '__main__':
    from binary_tree import BinaryNode
    tree = BinaryNode(10)
    tree.children_count = 5
    tree.left = BinaryNode(8)
    tree.left.children_count = 1
    tree.left.right = BinaryNode(9)
    tree.left.right.children_count = 0
    tree.right = BinaryNode(13)
    tree.right.children_count = 2
    tree.right.left = BinaryNode(11)
    tree.right.left.children_count = 0
    tree.right.right = BinaryNode(14)
    tree.right.right.children_count = 0

    bt = BinaryTree()
    bt.root = tree
    randomness_dict = {}
Esempio n. 7
0
from binary_tree import BinarySearchTree, BinaryNode

def validate(node, min=None, max=None):
    if node == None:
        return True
    if (min != None and node.data <= min) or (max != None and node.data > max):
        return False
    if validate(node.left, min, node.data) == False or validate(node.right, node.data, max) == False:
        return False
    return True

if __name__ == '__main__':
    tree = BinarySearchTree([8, 4, 9, 7, 10, 1])
    node = BinaryNode(5)
    node.left = BinaryNode(8)
    node.right = BinaryNode(4)

    print(validate(node))