def test_two_items(): head = Node(1, next=Node(2)) expected = Node(2, next=Node(1)) actual = reverse_linked_list(head) assert actual == expected assert has_cycle(actual) is True
def test_example(): root = Node(2, left=Node(1), right=Node(3)) node1 = Node(1) node2 = Node(3) expected = 2 assert recursive_solution(root, node1, node2) == expected
def clone_graph(node): if not node: return None node_map = dict() queue = [] queue.append(node) node_map[node] = Node(node.data, neighbors=[]) while queue: current_node = queue.pop(0) for neighbor in current_node.neighbors: if neighbor not in node_map: # add it to the map node_map.update({neighbor: Node(neighbor.data, neighbors=[])}) # add the neighbor to queue queue.append(neighbor) # update the cloned graph clone_of_current_node = node_map.get(current_node) clone_of_current_node.neighbors.append(node_map.get(neighbor)) return node_map.get(node)
def test_non_valid_bst(): node3 = Node(3) node6 = Node(6) node4 = Node(4, left=node3, right=node6) root = Node(5, left=Node(1), right=node4) assert is_valid_bst(root) is False
def test_negative_nums(): nodeneg1 = Node(-1) node4 = Node(4) node2 = Node(2, left=nodeneg1, right=node4) node3 = Node(3) root = Node(-5, left=node2, right=node3) assert find_max_sum_binary_tree(root) == 6
def test_basic_case(): node2leaf = Node(2) node3leaf = Node(3) node2 = Node(2, left=node2leaf, right=node3leaf) node4 = Node(4) root = Node(1, left=node2, right=node4) assert find_max_sum_binary_tree(root) == 10
def build_complete_binary_tree(arr, root, index, node_count): if index < node_count: root = Node(arr[index]) root.left = build_complete_binary_tree(arr, root.left, 2 * index + 1, node_count) root.right = build_complete_binary_tree(arr, root.right, 2 * index + 2, node_count) return root
def iterative_solution(head): temp = Node(-11) temp.next = head visited = set() while temp: if temp.next in visited: return True visited.add(temp.next) temp = temp.next return False
def test_balanced_binary_tree(): """ a balanced binary tree is a tree in which the left and right subtrees of every node differ in height by no more than 1 1 / \ 2 5 \ 6 """ node5 = Node(5, right=Node(6)) root = Node(1, left=Node(2), right=node5) assert inorder_traversal(root) == [2, 1, 5, 6]
def test_degenerate_binary_tree(): """ a binary tree in which each parent node only has one child, essentially behaving like a linked list 1 \ 2 / 3 """ node3 = Node(3) node2 = Node(2, left=node3) root = Node(1, right=node2) assert inorder_traversal(root) == [1, 3, 2]
def test_simple_case_should_be_true(): root = Node(3) root_left = Node(9) root_right = Node(20, left=Node(15), right=Node(7)) root.left = root_left root.right = root_right assert is_balanced(root) is True
def iterative_solution(lists): """ Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 """ pre_root = Node(-1) current = pre_root while list1 and list2: if list1.data <= list2.data: current.next = list1 list1 = list1.next else: current.next = list2 list2 = list2.next current = current.next if not list1: current.next = list2 else: current.next = list1 return pre_root.next
def test_example(): head = Node(1) temp = head for i in range(2, 6): temp.next = Node(i) temp = temp.next expected = Node(5) new_temp = expected for i in range(4, 0, -1): new_temp.next = Node(i) new_temp = new_temp.next actual = reverse_linked_list(head) assert actual == expected assert has_cycle(actual) is True
def test_ancestor_on_right(): root = Node(2, left=Node(0), right=Node(4, left=Node(3), right=Node(5))) node1 = Node(0) node2 = Node(2) expected = 2 assert recursive_solution(root, node1, node2) == expected
def two_pointers_solution(head, n): ans = Node(-1, next=head) left = ans right = ans for _ in range(n + 1): right = right.next while right: right = right.next left = left.next left.next = left.next.next return ans.next
def get_length_then_remove_nth_node(head, n): length = 0 ans = Node(-1, next=head) temp = ans while temp: length += 1 temp = temp.next steps_to_nth_plus_one_node = length - n temp = ans while steps_to_nth_plus_one_node > 0: steps_to_nth_plus_one_node -= 1 temp = temp.next temp.next = temp.next.next return ans.next
def test_full_binary_tree(): """ a full binary tree is a tree in which every node has 0 or 2 children 5 / \ 42 10 / \ 6 10 / \ 3 4 """ node10 = Node(10, left=Node(3), right=Node(4)) node42 = Node(42, left=Node(6), right=node10) root = Node(5, left=node42, right=Node(10)) assert inorder_traversal(root) == [6, 42, 3, 10, 4, 5, 10]
def test_left_branch(): assert get_nearest_ancestor(root, 5, 19) == Node(10)
def test_typical_case(): assert get_nearest_ancestor(root, 10, 30) == Node(20)
import pytest from src.algorithms.tree_questions.get_nearest_ancestor import get_nearest_ancestor from src.algorithms.node import Node def test_typical_case(): assert get_nearest_ancestor(root, 10, 30) == Node(20) def test_left_branch(): assert get_nearest_ancestor(root, 5, 19) == Node(10) root = Node(20) root.left = Node(10) root.right = Node(30) root.left.left = Node(5) root.left.right = Node(19) root.right.left = Node(25) root.right.right = Node(41)
def test_simplest_valid_bst(): root = Node(2, left=Node(1), right=Node(3)) assert is_valid_bst(root) is True
def test_same_value_nodes_should_not_be_valid_bst(): root = Node(1, left=Node(1), right=Node(1)) assert is_valid_bst(root) is False
def test_easy_case(): assert get_nearest_ancestor_v2(left_child, right_child) == Node(42)
def test_list1_is_longer(): list2 = Node(1, next=Node(2, next=Node(4))) list1 = Node(1, next=Node(3, next=Node(4, next=Node(5)))) expected = Node( 1, next=Node( 1, next=Node(2, next=Node(2, next=Node(3, next=Node(4, next=Node( 4, next=Node(5)))))), ), ) assert merge_two_lists(list1, list2) == expected
def test_unbalanced_at_right_child(): root = Node(1) root_left = Node(2, left=Node(3), right=Node(4)) root.left = root_left root_right = Node(5) root_right_right = Node(6, left=Node(7)) root_right.right = root_right_right root.right = root_right assert is_balanced(root) is False
def test_single_node_should_be_true(): assert is_balanced(Node(2)) is True
def test_simple_case_should_be_false(): root = Node(1) root_right = Node(2) root_left = Node(2) root_left.right = Node(3) root_lower_left = Node(3, left=Node(4), right=Node(4)) root_left.left = root_lower_left root.left = root_left root.right = root_right assert is_balanced(root) is False
def test_list2_is_null(): list2 = None list1 = Node(1, next=Node(3, next=Node(4, next=Node(5)))) expected = Node(1, next=Node(3, next=Node(4, next=Node(5)))) assert merge_two_lists(list1, list2) == expected
def test_example(): list1 = Node(1, next=Node(2, next=Node(4))) list2 = Node(1, next=Node(3, next=Node(4))) expected = Node( 1, next=Node(1, next=Node(2, next=Node(2, next=Node(3, next=Node(4, next=Node(4)))))), ) assert merge_two_lists(list1, list2) == expected
def test_single_tree(): assert inorder_traversal(Node(2)) == [2]