about summary refs log tree commit diff
path: root/data_structures_and_algorithms/bst-checker.py
blob: 689be97a8503b32c793179e66b9b5ea31aad1af7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import unittest


################################################################################
# Implementation
################################################################################
# is_leaf :: Node(a) -> Boolean
def is_leaf(node):
    return not node.left and not node.right


# is_binary_search_tree :: Node(Integer) -> Set(Int) -> Set(Int) -> Boolean
def is_binary_search_tree_a(node, la=set(), ra=set()):
    """My first solution for this problem."""
    for x in la:
        if not node.value < x:
            return False
    for x in ra:
        if not node.value > x:
            return False
    if is_leaf(node):
        return True
    elif not node.left:
        return is_binary_search_tree(
            node.right,
            la=la,
            ra=ra ^ {node.value},
        )
    elif not node.right:
        return is_binary_search_tree(node.left, la=la ^ {node.value}, ra=ra)
    else:
        return all([
            is_binary_search_tree(node.left, la=la ^ {node.value}, ra=ra),
            is_binary_search_tree(node.right, la=la, ra=ra ^ {node.value})
        ])


# is_binary_search_tree :: Node(Int) -> Maybe(Int) -> Maybe(Int) -> Boolean
def is_binary_search_tree(node, lb=None, ub=None):
    if lb:
        if node.value < lb:
            return False
    if ub:
        if node.value > ub:
            return False
    if is_leaf(node):
        return True
    elif not node.right:
        return is_binary_search_tree(node.left, lb=lb, ub=node.value)
    elif not node.left:
        return is_binary_search_tree(node.right, lb=node.value, ub=ub)
    else:
        return is_binary_search_tree(
            node.left, lb=lb, ub=node.value) and is_binary_search_tree(
                node.right, lb=node.value, ub=ub)


################################################################################
# Tests
################################################################################
class Test(unittest.TestCase):
    class BinaryTreeNode(object):
        def __init__(self, value):
            self.value = value
            self.left = None
            self.right = None

        def insert_left(self, value):
            self.left = Test.BinaryTreeNode(value)
            return self.left

        def insert_right(self, value):
            self.right = Test.BinaryTreeNode(value)
            return self.right

    def test_valid_full_tree(self):
        tree = Test.BinaryTreeNode(50)
        left = tree.insert_left(30)
        right = tree.insert_right(70)
        left.insert_left(10)
        left.insert_right(40)
        right.insert_left(60)
        right.insert_right(80)
        result = is_binary_search_tree(tree)
        self.assertTrue(result)

    def test_both_subtrees_valid(self):
        tree = Test.BinaryTreeNode(50)
        left = tree.insert_left(30)
        right = tree.insert_right(80)
        left.insert_left(20)
        left.insert_right(60)
        right.insert_left(70)
        right.insert_right(90)
        result = is_binary_search_tree(tree)
        self.assertFalse(result)

    def test_descending_linked_list(self):
        tree = Test.BinaryTreeNode(50)
        left = tree.insert_left(40)
        left_left = left.insert_left(30)
        left_left_left = left_left.insert_left(20)
        left_left_left.insert_left(10)
        result = is_binary_search_tree(tree)
        self.assertTrue(result)

    def test_out_of_order_linked_list(self):
        tree = Test.BinaryTreeNode(50)
        right = tree.insert_right(70)
        right_right = right.insert_right(60)
        right_right.insert_right(80)
        result = is_binary_search_tree(tree)
        self.assertFalse(result)

    def test_one_node_tree(self):
        tree = Test.BinaryTreeNode(50)
        result = is_binary_search_tree(tree)
        self.assertTrue(result)


unittest.main(verbosity=2)