-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tree_intersection.py
More file actions
54 lines (40 loc) · 1.78 KB
/
Copy pathtest_tree_intersection.py
File metadata and controls
54 lines (40 loc) · 1.78 KB
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
import pytest
from code_challenges.tree_intersection import tree_intersection
from data_structures.binary_tree import BinaryTree, Node
from data_structures.queue import Queue
def test_exists():
assert tree_intersection
# @pytest.mark.skip("TODO")
def test_tree_intersection():
tree_a = BinaryTree()
values = [150, 100, 250, 75, 160, 200, 350, 125, 175, 300, 500]
add_values_to_empty_tree(tree_a, values)
tree_b = BinaryTree()
values = [42, 100, 100, 15, 160, 200, 350, 125, 175, 4, 500]
add_values_to_empty_tree(tree_b, values)
actual = tree_intersection(tree_a, tree_b)
expected = [125, 175, 100, 160, 500, 200, 350]
assert sorted(actual) == sorted(expected)
def add_values_to_empty_tree(tree, values):
"""
Helper function to add given values to a BinaryTree level by level.
Assumes values are provided in an array representing the desired tree level by level.
"""
if not values:
return # Return if the values list is empty
root = Node(values.pop(0)) # Create the root node from the first value
tree.root = root # Set the root of the tree
q = Queue() # Initialize a queue to help with level-order insertion
q.enqueue(root) # Enqueue the root
while values and not q.is_empty():
node = q.dequeue() # Dequeue the next node
if values:
val = values.pop(0) # Get the next value for the left child
if val is not None: # Only add a node if the value is not None
node.left = Node(val)
q.enqueue(node.left)
if values:
val = values.pop(0) # Get the next value for the right child
if val is not None: # Only add a node if the value is not None
node.right = Node(val)
q.enqueue(node.right)