Skip to content

Commit fb749bd

Browse files
committed
refactor(datastructures, binary-search-tree, puzzles): remove the use of a queue in find closed value
1 parent 1d8d377 commit fb749bd

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

datastructures/trees/binary/search_tree/__init__.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -237,33 +237,27 @@ def find_closest_value_in_bst(self, target: T) -> Optional[BinaryTreeNode]:
237237
return self.root
238238

239239
# this keeps track of the minimum on both the left and the right
240-
min_diff = min_diff_left = min_diff_right = float("inf")
241-
closest_value = None
242-
fifo_queue = FifoQueue()
243-
fifo_queue.enqueue(self.root)
240+
closest_node = self.root
241+
min_diff = abs(target - self.root.data)
242+
current = self.root
244243

245244
# while the queue is not empty, we pop off nodes from the queue and check for their values
246-
while not fifo_queue.is_empty():
247-
current_node = fifo_queue.dequeue()
248-
249-
min_diff_left = abs(target - current_node.data)
250-
min_diff_right = abs(target - current_node.data)
251-
252-
if min_diff_left < min_diff:
253-
min_diff = min_diff_left
254-
closest_value = current_node
245+
while current:
246+
current_diff = abs(target - self.root.data)
255247

256-
if min_diff_right < min_diff:
257-
min_diff = min_diff_right
258-
closest_value = current_node
248+
if current_diff < min_diff:
249+
min_diff = current_diff
250+
closest_node = current
259251

260-
if current_node.left:
261-
fifo_queue.enqueue(current_node.left)
252+
if current.data == target:
253+
return current
262254

263-
if current_node.right:
264-
fifo_queue.enqueue(current_node.right)
255+
if target < current.data:
256+
current = current.left
257+
else:
258+
current = current.right
265259

266-
return closest_value
260+
return closest_node
267261

268262
def range_sum(self, low: int, high: int):
269263
"""
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Optional
2-
from queue import Queue
32
from datastructures.trees.binary.search_tree import BinaryTreeNode
43

54

@@ -13,30 +12,24 @@ def find_closest_value_in_bst(node: BinaryTreeNode, target: int) -> Optional[int
1312
return node.data
1413

1514
# this keeps track of the minimum on both the left and the right
16-
min_diff = min_diff_left = min_diff_right = float("inf")
17-
closest_value = None
18-
fifo_queue = Queue()
19-
fifo_queue.put(node)
15+
closest_value = node.data
16+
min_diff = abs(target - node.data)
17+
current = node
2018

2119
# while the queue is not empty, we pop off nodes from the queue and check for their values
22-
while not fifo_queue.empty():
23-
current_node = fifo_queue.get()
20+
while current:
21+
current_diff = abs(target - current.data)
2422

25-
min_diff_left = abs(target - current_node.data)
26-
min_diff_right = abs(target - current_node.data)
23+
if current_diff < min_diff:
24+
min_diff = current_diff
25+
closest_value = current.data
2726

28-
if min_diff_left < min_diff:
29-
min_diff = min_diff_left
30-
closest_value = current_node.data
27+
if current.data == target:
28+
return current.data
3129

32-
if min_diff_right < min_diff:
33-
min_diff = min_diff_right
34-
closest_value = current_node.data
35-
36-
if current_node.left:
37-
fifo_queue.put(current_node.left)
38-
39-
if current_node.right:
40-
fifo_queue.put(current_node.right)
30+
if target < current.data:
31+
current = current.left
32+
else:
33+
current = current.right
4134

4235
return closest_value

0 commit comments

Comments
 (0)