-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Floor and ceil in Binary search tree added #10432
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
76fd929
earliest deadline first scheduling algo added
Arunsiva003 4363669
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bbac298
earliest deadline first scheduling algo added
Arunsiva003 4d156e5
earliest deadline first scheduling algo added 2
Arunsiva003 628b1b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9397c73
ceil and floor and bst
Arunsiva003 31e43db
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 22f40d9
ceil and floor and bst 2
Arunsiva003 1f6eed8
ceil and floor and bst 2
Arunsiva003 6238c4a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f845dd4
ceil and floor and bst 3
Arunsiva003 f31b31e
Merge branch 'create' of https://github.com/Arunsiva003/Python into c…
Arunsiva003 76db986
Update and rename floor_ceil_in_bst.py to floor_and_ceiling.py
cclauss 7432b70
Delete scheduling/shortest_deadline_first.py
cclauss 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
In a binary search tree (BST): | ||
* The floor of key 'k' is the maximum value that is smaller than or equal to 'k'. | ||
* The ceiling of key 'k' is the minimum value that is greater than or equal to 'k'. | ||
|
||
Reference: | ||
https://bit.ly/46uB0a2 | ||
|
||
Author : Arunkumar | ||
Date : 14th October 2023 | ||
""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
key: int | ||
left: Node | None = None | ||
right: Node | None = None | ||
|
||
def __iter__(self) -> Iterator[int]: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self.left: | ||
yield from self.left | ||
yield self.key | ||
if self.right: | ||
yield from self.right | ||
|
||
def __len__(self) -> int: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sum(1 for _ in self) | ||
|
||
|
||
def floor_ceiling(root: Node | None, key: int) -> tuple[int | None, int | None]: | ||
""" | ||
Find the floor and ceiling values for a given key in a Binary Search Tree (BST). | ||
|
||
Args: | ||
root: The root of the binary search tree. | ||
key: The key for which to find the floor and ceiling. | ||
|
||
Returns: | ||
A tuple containing the floor and ceiling values, respectively. | ||
|
||
Examples: | ||
>>> root = Node(10) | ||
>>> root.left = Node(5) | ||
>>> root.right = Node(20) | ||
>>> root.left.left = Node(3) | ||
>>> root.left.right = Node(7) | ||
>>> root.right.left = Node(15) | ||
>>> root.right.right = Node(25) | ||
>>> tuple(root) | ||
(3, 5, 7, 10, 15, 20, 25) | ||
>>> floor_ceiling(root, 8) | ||
(7, 10) | ||
>>> floor_ceiling(root, 14) | ||
(10, 15) | ||
>>> floor_ceiling(root, -1) | ||
(None, 3) | ||
>>> floor_ceiling(root, 30) | ||
(25, None) | ||
""" | ||
floor_val = None | ||
ceiling_val = None | ||
|
||
while root: | ||
if root.key == key: | ||
floor_val = root.key | ||
ceiling_val = root.key | ||
break | ||
|
||
if key < root.key: | ||
ceiling_val = root.key | ||
root = root.left | ||
else: | ||
floor_val = root.key | ||
root = root.right | ||
|
||
return floor_val, ceiling_val | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.