Example #1
0
def convert_list_to_binary_search_tree(raw_list: Optional[list[int]]) -> Optional[Node]:
    if not raw_list:
        return None
    root = Node(raw_list[0])
    root.right = convert_list_to_binary_search_tree(raw_list[1:])
    root.left = convert_list_to_binary_search_tree(raw_list[2:])

    return root
Example #2
0
def process_file(f):
    nodes = {}
    for line in f:
        num, neighbors = line.strip().split(' <-> ')
        num = int(num)
        nodes[num] = Node(num)
        nodes[num].neighbors = set(int(n) for n in neighbors.split(', '))
    return nodes
Example #3
0
def test_solution():
    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(20)
    items.head.next = Node(30)
    items.head.next.next = Node(40)

    assert items.search(30) == True
    assert items.search(10) == False
Example #4
0
def test_handles_failing_example():
    candidate_tree = Node(3)
    candidate_tree.left = Node(2)
    candidate_tree.left.left = Node(1)
    candidate_tree.left.right = Node(4)
    candidate_tree.right = Node(6)
    candidate_tree.right.left = Node(5)
    candidate_tree.right.right = Node(7)

    assert (check_binary_search_tree_(candidate_tree) == False)
Example #5
0
def test_solution():
    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(20)
    items.head.next = Node(30)
    items.head.next.next = Node(50)
    items.insert_after_item(30, 0)
    assert items.head.data == 20
    assert items.head.next.data == 30
    assert items.head.next.next.data == 0
Example #6
0
def test_solution():
    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(20)
    items.head.next = Node(30)
    items.head.next.next = Node(40)
    items.reverse()

    assert items.head.data == 40
    assert items.head.next.data == 30
Example #7
0
def test_solution():
    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(20)
    items.head.next = Node(30)
    items.head.next.next = Node(40)
    items.insert_at_index(2, 2)

    assert items.head.data == 20
    assert items.head.next.data == 2
Example #8
0
def test_solution():
    from solution import Node, linkedlist

    items = linkedlist()
    items.head = Node(20)
    items.head.next = Node(30)
    items.head.next.next = Node(50)
    print(items.head, items.head.next, items.head.next.next)
    items.delete_at_end()
    assert items.head.data == 20
    assert items.head.next.data == 30
    assert items.head.next.next == None
Example #9
0
def test_handles_valid_nested_tree():
    candidate_tree = Node(4)
    candidate_tree.left = Node(2)
    candidate_tree.left.left = Node(1)
    candidate_tree.left.right = Node(3)
    candidate_tree.right = Node(6)
    candidate_tree.right.left = Node(5)

    assert (check_binary_search_tree_(candidate_tree) == True)
Example #10
0
def test_solution():
    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(20)
    items.head.next = Node(30)
    items.head.next.next = Node(40)
    items.head.next.next.next = Node(50)
    items.delete_item_by_value(40)

    assert items.head.data == 20
    assert items.head.next.data == 30
    assert items.head.next.next.data == 50
Example #11
0
def process_file(f):
    programs = {}
    parents = {}  # maps child to parent
    for line in f:
        match = pattern.match(line.strip())
        name, weight, children = match.group('name'), match.group(
            'weight'), match.group('children')
        programs[name] = Node(name, int(weight))
        if children:
            programs[name].children = set(children.split(', '))
    for child_name, parent_name in parents.items():
        programs[child_name].parent_name = parent_name
    return programs
Example #12
0
    def test_intersection(self):
        a = Node(1)
        a.next = Node(2)
        a.next.next = Node(3)
        a.next.next.next = Node(4)

        b = Node(6)
        b.next = a.next.next

        self.assertEqual([3, 4], intersection(a, b).toList())
Example #13
0
def test_solution():
    from solution import Node, search

    items = Node(5)
    items.left = Node(6)
    items.right = Node(7)
    items.left.left = Node(8)
    items.left.right = Node(9)
    items.right.left = Node(10)
    items.right.right = Node(11)
    result_value = search(items, 11)
    result_value_2 = search(items, 99)
    assert result_value == True
    assert result_value_2 == False
Example #14
0
def test_solution():
    from solution import Node, NthPreorder

    items = Node(25)
    items.left = Node(20)
    items.right = Node(30)
    items.left.left = Node(18)
    items.left.right = Node(22)
    items.right.left = Node(24)
    items.right.right = Node(32)

    assert NthPreorder(items, 6) == 24
    assert NthPreorder(items, 4) == 22
    assert NthPreorder(items, 8) == "no 8-th element"
Example #15
0
 def test1(self):
     node0 = Node(0)
     node1 = Node(1)
     node2 = Node(2)
     node3 = Node(3)
     node4 = Node(4)
     node5 = Node(5)
     node6 = Node(6)
     node0.friends = [node1, node2, node3, node4]
     node1.friends = [node0, node6]
     node2.friends = [node0, node5, node6]
     node3.friends = [node0, node5]
     node4.friends = [node0, node5]
     res = recommend(node0)
     self.assertEqual(res, node5)
Example #16
0
def test_inorder():
    root = Node(4)
    root.left = Node(2)
    root.right = Node(5)
    root.left.left = Node(1)
    root.left.right = Node(3)

    res = inorder(root)
    assert res == [1, 2, 3, 4, 5]
Example #17
0
    def test_remove_kth_from_linked_list(self):
        self.assertEqual(None, remove_kth_from_linked_list(Node(1), 1))

        self.assertEqual([2, 3, 4, 5], remove_kth_from_linked_list(buildLinkedList(), 5).toList())

        self.assertEqual([1, 2, 4, 5], remove_kth_from_linked_list(buildLinkedList(), 3).toList())

        self.assertEqual([1, 2, 3, 4], remove_kth_from_linked_list(buildLinkedList(), 1).toList())
Example #18
0
    def test_serialize(self):
        #     1
        #    / \
        #   3   4
        #  / \   \
        # 2   5   7
        tree = Node(1)
        tree.left = Node(3)
        tree.left.left = Node(2)
        tree.left.right = Node(5)
        tree.right = Node(4)
        tree.right.right = Node(7)

        self.assertEqual('1 3 2 # # 5 # # 4 # 7 # #', serialize(tree))
Example #19
0
def test_handles_bad_tree_right():
    bad_tree = Node(8)
    bad_tree.left = Node(10)

    tree = Node(7)
    tree.right = bad_tree
    assert (check_binary_search_tree_(tree) == False)
Example #20
0
def test_solution(monkeypatch):
    ret_val = []

    def g(num):
        ret_val.append(num)

    monkeypatch.setattr("builtins.print", g)

    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(1)
    e2 = Node(2)
    items.head.next = e2
    items.traverse()

    assert ret_val[0] == 1
    assert ret_val[1] == 2
Example #21
0
def test_solution():
    from solution import Node, Preorder

    items = Node(5)
    items.left = Node(6)
    items.right = Node(7)
    items.left.left = Node(8)
    items.left.right = Node(9)
    result_value_1 = Preorder(items)
    result = [5, 6, 8, 9, 7]
    assert result_value_1 == result
Example #22
0
def test_solution():
    from solution import Node, linkedList

    items = linkedList()
    items.head = Node(20)
    items.insert_at_start(40)
    items.insert_at_start(50)
    assert items.head.data == 50
    assert items.head.next.data == 40
    assert items.head.next.next.data == 20
Example #23
0
    def test_deepest(self):
        root = Node('a')
        root.left = Node('b')
        root.left.left = Node('d')
        root.right = Node('c')

        n, d = deepest(root)

        self.assertEqual('d', n.val)
        self.assertEqual(3, d)
Example #24
0
def test_solution():
    from solution import Node, NthInorder

    items = Node(5)
    items.left = Node(6)
    items.right = Node(7)
    items.left.left = Node(8)
    items.left.right = Node(9)

    [8, 6, 9, 5, 7]

    assert NthInorder(items, 4) == 5
    assert NthInorder(items, 1) == 8
    assert NthInorder(items, 6) == "no 6-th element"
Example #25
0
def test_solution():
    from solution import Node, height

    left_child = Node(6, None, None)
    right_child = Node(7, None, None)
    items = Node(5, left_child, right_child)
    items.left.left = Node(8)
    items.left.right = Node(9)
    items.right.left = Node(10)
    result = height(items)
    assert result == 3
Example #26
0
    def test_romanToInt(self):
        # Given this tree:
        #     1
        #    / \
        #   2   3
        #  /   / \
        # 0   9   4

        # We want a tree like:
        #     1
        #    / \
        #   0   3
        #      / \
        #     9   4

        tree = Node(1)
        tree.left = Node(2)
        tree.right = Node(3)
        tree.right.right = Node(4)
        tree.right.left = Node(9)
        tree.left.left = Node(0)
        self.assertEqual('1\n03\n94', fullBinaryTree(tree).__str__())
Example #27
0
    def test1(self):
        head = Node()
        head.buff = [1, 2, 3]
        head.count = 3

        n1 = Node()
        n1.buff = [4, 5, 6]
        n1.count = 3

        head.next = n1

        sol = Solution()
        res = sol.get_idx_at(head, 0)
        exp = 1
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 1)
        exp = 2
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 4)
        exp = 5
        self.assertEqual(res, exp)
Example #28
0
    def test1(self):
        node1 = Node(1)
        node2 = Node(2)
        node3 = Node(3)
        node4 = Node(4)
        node5 = Node(5)
        node6 = Node(6)
        node7 = Node(7)
        node8 = Node(8)
        node9 = Node(9)
        node10 = Node(10)
        node11 = Node(11)
        node1.left = node2
        node1.right = node3
        node2.left = node4
        node2.right = node5
        node3.left = node6
        node3.right = node7
        node6.left = node8
        node6.right = node9
        node7.left = node10
        node7.right = node11

        s = Solution()
        res = s.compress(node1)
        print res
        root = s.decompress(res)
        res2 = s.compress(root)
        print res2
        self.assertEqual(res, res2)
Example #29
0
from solution import Node, Solution


def traverse(node, ar):
    ar.append(node.val)
    for child_node in node.children:
        traverse(child_node, ar)
    return ar


s = Solution()

left = Node(val=3,
            children=[Node(val=5, children=[]),
                      Node(val=6, children=[])])
center = Node(val=2, children=[])
right = Node(val=4, children=[])
root = Node(val=1, children=[left, center, right])

assert s.preorder(root) == traverse(root, [])

assert s.preorder(None) == []
Example #30
0
def test_solution():
    from solution import Node, closest_value

    root = Node(8)
    root.left = Node(5)
    root.right = Node(14)
    root.left.left = Node(4)
    root.left.right = Node(6)
    root.left.right.left = Node(8)
    root.left.right.right = Node(7)
    root.right.right = Node(24)
    root.right.right.left = Node(22)
    result = closest_value(root, 9)
    assert result == 8
Example #31
0
    def test_insert_2(self):
        head = Node()
        head.buff = [1, 2, 3, 4, 5]
        head.count = 5

        n1 = Node()
        n1.buff = [4, 0, 0, 0, 0]
        n1.count = 1

        n2 = Node()
        n2.buff = [5, 6, 7, 8, 9]
        n2.count = 5

        head.next = n1
        n1.next = n2

        sol = Solution()
        sol.insert_at(head, 10, 11)
        self.assertEqual(n2.buff, [5, 6, 7, 8, 11])
        self.assertEqual(n2.next.buff, [9, 0, 0, 0, 0])
Example #32
0
    def test_insert_1(self):
        head = Node()
        head.buff = [1, 2, 3, 0, 0]
        head.count = 3

        n1 = Node()
        n1.buff = [4, 0, 0, 0, 0]
        n1.count = 1

        n2 = Node()
        n2.buff = [5, 6, 0, 0, 0]
        n2.count = 2

        head.next = n1
        n1.next = n2

        sol = Solution()
        sol.insert_at(head, 0, 0)
        self.assertEqual(head.buff, [0, 1, 2, 3, 0])
        sol.insert_at(head, 0, 0)
        self.assertEqual(head.buff, [0, 0, 1, 2, 3])
        sol.insert_at(head, 0, 0)
        self.assertEqual(head.buff, [0, 0, 0, 1, 2])
        self.assertEqual(n1.buff, [3, 4, 0, 0, 0])
Example #33
0
    def test2(self):
        head = Node()
        head.buff = [1, 2, 3]
        head.count = 3

        n1 = Node()
        n1.buff = [4]
        n1.count = 1

        n2 = Node()
        n2.buff = [5, 6]
        n2.count = 2

        head.next = n1
        n1.next = n2

        sol = Solution()
        res = sol.get_idx_at(head, 0)
        exp = 1
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 1)
        exp = 2
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 4)
        exp = 5
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 5)
        exp = 6
        self.assertEqual(res, exp)
Example #34
0
 def test_merge(self):
     a = Node(1, Node(3, Node(5)))
     b = Node(2, Node(4, Node(6)))
     self.assertEqual('123456', merge([a, b]).__str__())
Example #35
0
def test_solution():
    from solution import Node

    items = Node(10)
    items.left = Node(11)
    items.left.left = Node(7)
    items.right = Node(9)
    items.right.left = Node(15)
    items.right.right = Node(8)
    val = 12
    items.insert(val)
    assert items.left.right.key == 12
Example #36
0
def test_no_children():
    tree = Node(7)
    assert (check_binary_search_tree_(tree) == True)