-
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
Conversation
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... 📒 Files selected for processing (2)
Tip You can disable sequence diagrams in the walkthrough.Disable the WalkthroughA new "Find Closest Value in BST" puzzle is introduced with a function implementation using breadth-first search traversal to locate the node with the smallest absolute difference to a target value. Corresponding documentation, tests, a BST class method, and directory listing updates are included. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Function as find_closest_value_in_bst
participant Queue
participant Node as BinaryTreeNode
Caller->>Function: call with target value
alt Empty tree
Function-->>Caller: return None
else Root matches target
Function-->>Caller: return root.data
else Traverse tree
Function->>Queue: initialize with root
loop Process nodes
Function->>Queue: dequeue node
Function->>Node: get node.data
Function->>Function: calculate diff = abs(target - node.data)
alt diff < current min
Function->>Function: update closest_value
end
alt left child exists
Function->>Queue: enqueue left
end
alt right child exists
Function->>Queue: enqueue right
end
end
Function-->>Caller: return closest_value
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (3)
puzzles/search/binary_search/find_closest_value/test_find_closest_value.py (1)
7-7: Rename test method to be descriptive.The test method name
test_somethingis too generic and doesn't convey what is being tested.Apply this diff to improve test clarity:
- def test_something(self): + def test_find_closest_value_in_bst_returns_closest_value(self):DIRECTORY.md (1)
548-549: Fix list indentation to match project style.The indentation for these list items is inconsistent with the expected formatting.
Apply this diff to fix the indentation:
- * Find Closest Value - * [Test Find Closest Value](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/search/binary_search/find_closest_value/test_find_closest_value.py) + * Find Closest Value + * [Test Find Closest Value](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/search/binary_search/find_closest_value/test_find_closest_value.py)puzzles/search/binary_search/find_closest_value/__init__.py (1)
6-42: Consider refactoring to reduce code duplication.This function has very similar logic to the
find_closest_value_in_bstmethod indatastructures/trees/binary/search_tree/__init__.py(lines 222-266). The main difference is that this returns an integer value while the BST method returns a node. Consider whether one implementation could call the other to reduce duplication.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
DIRECTORY.md(1 hunks)datastructures/trees/binary/search_tree/__init__.py(1 hunks)puzzles/search/binary_search/find_closest_number/README.md(1 hunks)puzzles/search/binary_search/find_closest_value/README.md(1 hunks)puzzles/search/binary_search/find_closest_value/__init__.py(1 hunks)puzzles/search/binary_search/find_closest_value/test_find_closest_value.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
puzzles/search/binary_search/find_closest_value/__init__.py (2)
datastructures/queues/__init__.py (1)
Queue(7-66)datastructures/trees/binary/search_tree/__init__.py (1)
find_closest_value_in_bst(222-266)
datastructures/trees/binary/search_tree/__init__.py (3)
puzzles/search/binary_search/find_closest_value/__init__.py (1)
find_closest_value_in_bst(6-42)datastructures/trees/binary/node.py (1)
BinaryTreeNode(6-184)datastructures/queues/fifo/__init__.py (1)
FifoQueue(6-62)
puzzles/search/binary_search/find_closest_value/test_find_closest_value.py (2)
datastructures/trees/binary/search_tree/__init__.py (1)
find_closest_value_in_bst(222-266)puzzles/search/binary_search/find_closest_value/__init__.py (1)
find_closest_value_in_bst(6-42)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md
548-548: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
549-549: Unordered list indentation
Expected: 6; Actual: 8
(MD007, ul-indent)
🔇 Additional comments (1)
puzzles/search/binary_search/find_closest_value/README.md (1)
1-43: LGTM!The documentation is well-structured and provides clear guidance on the problem, including helpful hints and complexity analysis.
… of a queue in find closed value
Describe your change:
An algorithm to find the closest value in a binary search tree using breadth first search approach iteratively using a first in first out queue to find the closest value to the provided target value.
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
New Features
Documentation
Tests