Skip to content

Commit 387be02

Browse files
committed
rewrite min_interval_search, rename to interval_search_lowest
1 parent 8537a25 commit 387be02

2 files changed

Lines changed: 15 additions & 18 deletions

File tree

src/chapter14/exercise14_3_3.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
from chapter14.textbook14_3 import interval_search, overlap
1+
from chapter14.textbook14_3 import overlap
22

33

4-
def min_interval_search(T, i):
5-
x = interval_search(T, i)
6-
if x is not T.nil:
7-
y = x.left
8-
while y is not T.nil:
9-
if overlap(i, y.int):
10-
x = y
11-
y = x.left
12-
else:
13-
if y.left is not T.nil and y.left.max >= i.low:
14-
y = y.left
15-
else:
16-
y = y.right
17-
return x
4+
def interval_search_lowest(T, i):
5+
x = T.root
6+
y = T.nil
7+
while x is not T.nil:
8+
if overlap(i, x.int) and (y is T.nil or x.int.low < y.int.low):
9+
y = x
10+
if x.left is not T.nil and x.left.max >= i.low:
11+
x = x.left
12+
else:
13+
x = x.right
14+
return y

test/test_chapter14/test_exercise14_3_3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from hamcrest import *
55

6-
from chapter14.exercise14_3_3 import min_interval_search
6+
from chapter14.exercise14_3_3 import interval_search_lowest
77
from chapter14.textbook14_3 import overlap
88
from datastructures.array import Array
99
from datastructures.essential import Interval
@@ -12,15 +12,15 @@
1212

1313
class TestExercise14_3_3(TestCase):
1414

15-
def test_min_interval_search(self):
15+
def test_interval_search_lowest(self):
1616
tree = get_random_interval_tree()
1717
inorder_nodes = get_binary_search_tree_inorder_nodes(tree)
1818
inorder_intervals = Array(node.int for node in inorder_nodes)
1919
low_endpoint = random.randint(0, 949)
2020
high_endpoint = low_endpoint + random.randint(0, 50)
2121
interval = Interval(low_endpoint, high_endpoint)
2222

23-
actual_found = min_interval_search(tree, interval)
23+
actual_found = interval_search_lowest(tree, interval)
2424

2525
if actual_found is not tree.nil:
2626
assert_that(overlap(actual_found.int, interval))

0 commit comments

Comments
 (0)