예제 #1
0
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
예제 #2
0
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
예제 #3
0
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
예제 #4
0
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
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)
예제 #6
0
    assert get_nearest_ancestor_v2(child1, child2) == Node(13)


def test_ancerstor_unbalanced():
    assert get_nearest_ancestor_v2(child1, child2_1_1) == Node(13)


def test_ancestor_unbalanced_v2():
    assert get_nearest_ancestor_v2(right_child, child2_1_1) == Node(42)


def test_ancestor_unbalanced_v3():
    assert get_nearest_ancestor_v2(child2_1, child2_1_1) == Node(7)


root = Node(42)
left_child = Node(13, parent=root)
right_child = Node(15, parent=root)
root.left = left_child
root.right = right_child

child1 = Node(32, parent=left_child)
child2 = Node(7, parent=left_child)
left_child.left = child1
left_child.right = child2

child2_1 = Node(8, parent=child2)
child1.left = child2_1
child2_1_1 = Node(10, parent=child2_1)
child2_1.left = child2_1_1