-
Notifications
You must be signed in to change notification settings - Fork 2
feat(datastructures, puzzles): find the closest value in a binary search tree #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Find Closest Value in BST | ||
|
|
||
| Write a function that takes in a Binary Search Tree (BST) and a target integer | ||
| value and returns the closest value to that target value contained in the BST. | ||
|
|
||
| You can assume that there will only be one closest value. | ||
|
|
||
| Each <span>BST</span> node has an integer <span>value</span>, a | ||
| <span>left</span> child node, and a <span>right</span> child node. A node is | ||
| said to be a valid <span>BST</span> node if and only if it satisfies the BST | ||
| property: its <span>value</span> is strictly greater than the values of every | ||
| node to its left; its <span>value</span> is less than or equal to the values | ||
| of every node to its right; and its children nodes are either valid | ||
| <span>BST</span> nodes themselves or <span>None</span> / <span>null</span>. | ||
|
|
||
| Sample Input: | ||
|
|
||
| ```text | ||
| tree = 10 | ||
| / \ | ||
| 5 15 | ||
| / \ / \ | ||
| 2 5 13 22 | ||
| / \ | ||
| 1 14 | ||
| target = 12 | ||
| ``` | ||
|
|
||
| Sample output: 13 | ||
|
|
||
| ## Hints | ||
|
|
||
| - Try traversing the BST node by node, all the while keeping track of the node with the value closest to the target value. | ||
| Calculating the absolute value of the difference between a node's value and the target value should allow you to | ||
| check if that node is closer than the current closest one. | ||
| - Make use of the BST property to determine what side of any given node has values close to the target value and is | ||
| therefore worth exploring. | ||
| - What are the advantages and disadvantages of solving this problem iteratively as opposed to recursively? | ||
|
|
||
| ## Optimal Space & Time Complexity | ||
|
|
||
| Average: O(log(n)) time | O(1) space where n is the number of nodes in the tree | ||
| BST Worst: O(n) time | O(1) space where n is the number of nodes in the tree |
42 changes: 42 additions & 0 deletions
42
puzzles/search/binary_search/find_closest_value/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from typing import Optional | ||
| from queue import Queue | ||
| from datastructures.trees.binary.search_tree import BinaryTreeNode | ||
|
|
||
|
|
||
| def find_closest_value_in_bst(node: BinaryTreeNode, target: int) -> Optional[int]: | ||
| # edge case for empty nodes, if none is provided, we can't find a value that is close to the target | ||
| if not node: | ||
| return None | ||
|
|
||
| # if the node's data is the target, exit early by returning it | ||
| if node.data == target: | ||
| return node.data | ||
|
|
||
| # this keeps track of the minimum on both the left and the right | ||
| min_diff = min_diff_left = min_diff_right = float("inf") | ||
| closest_value = None | ||
| fifo_queue = Queue() | ||
| fifo_queue.put(node) | ||
|
|
||
| # while the queue is not empty, we pop off nodes from the queue and check for their values | ||
| while not fifo_queue.empty(): | ||
| current_node = fifo_queue.get() | ||
|
|
||
| min_diff_left = abs(target - current_node.data) | ||
| min_diff_right = abs(target - current_node.data) | ||
|
|
||
| if min_diff_left < min_diff: | ||
| min_diff = min_diff_left | ||
| closest_value = current_node.data | ||
|
|
||
| if min_diff_right < min_diff: | ||
| min_diff = min_diff_right | ||
| closest_value = current_node.data | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if current_node.left: | ||
| fifo_queue.put(current_node.left) | ||
|
|
||
| if current_node.right: | ||
| fifo_queue.put(current_node.right) | ||
|
|
||
| return closest_value | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
31 changes: 31 additions & 0 deletions
31
puzzles/search/binary_search/find_closest_value/test_find_closest_value.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import unittest | ||
| from datastructures.trees.binary.search_tree import BinaryTreeNode | ||
| from . import find_closest_value_in_bst | ||
|
|
||
|
|
||
| class FindClosestValueTestCases(unittest.TestCase): | ||
| def test_something(self): | ||
| root = BinaryTreeNode( | ||
| data=10, | ||
| left=BinaryTreeNode(data=5, | ||
| left=BinaryTreeNode( | ||
| data=2, | ||
| left=BinaryTreeNode(data=1), | ||
| right=BinaryTreeNode(data=5)) | ||
| ), | ||
| right=BinaryTreeNode(data=15, | ||
| left=BinaryTreeNode( | ||
| data=13, | ||
| right=BinaryTreeNode( | ||
| data=14, | ||
| right=BinaryTreeNode(data=22) | ||
| ) | ||
| )) | ||
| ) | ||
| expected = 13 | ||
| actual = find_closest_value_in_bst(root, target=12) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.