-
-
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 9 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,83 @@ | ||
""" | ||
The floor of a key 'k' in a BST is the maximum | ||
value that is smaller than or equal to 'k'. | ||
|
||
The ceiling of a key 'k' in a BST is the minimum | ||
value that is greater than or equal to 'k'. | ||
|
||
Reference: | ||
https://bit.ly/46uB0a2 | ||
|
||
Author : Arunkumar | ||
Date : 14th October 2023 | ||
""" | ||
|
||
|
||
|
||
class TreeNode: | ||
def __init__(self, key: int): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Initialize a TreeNode with the given key. | ||
|
||
Args: | ||
key (int): The key value for the node. | ||
""" | ||
self.key = key | ||
self.left: TreeNode | None = None | ||
self.right: TreeNode | None = None | ||
|
||
|
||
def floor_ceiling(root: TreeNode | 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 (TreeNode): The root of the BST. | ||
key (int): The key for which to find the floor and ceiling. | ||
|
||
Returns: | ||
tuple[int | None, int | None]: | ||
A tuple containing the floor and ceiling values, respectively. | ||
|
||
Examples: | ||
>>> root = TreeNode(10) | ||
>>> root.left = TreeNode(5) | ||
>>> root.right = TreeNode(20) | ||
>>> root.left.left = TreeNode(3) | ||
>>> root.left.right = TreeNode(7) | ||
>>> root.right.left = TreeNode(15) | ||
>>> root.right.right = TreeNode(25) | ||
>>> floor, ceiling = floor_ceiling(root, 8) | ||
>>> floor | ||
7 | ||
>>> ceiling | ||
10 | ||
>>> floor, ceiling = floor_ceiling(root, 14) | ||
>>> floor | ||
10 | ||
>>> ceiling | ||
15 | ||
""" | ||
floor_val = None | ||
ceiling_val = None | ||
|
||
while root is not None: | ||
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() |
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,70 @@ | ||
""" | ||
Earliest Deadline First (EDF) Scheduling Algorithm | ||
|
||
This code implements the Earliest Deadline First (EDF) | ||
scheduling algorithm, which schedules processes based on their deadlines. | ||
If a process cannot meet its deadline, it is marked as "Idle." | ||
|
||
Reference: | ||
https://www.geeksforgeeks.org/ | ||
earliest-deadline-first-edf-cpu-scheduling-algorithm/ | ||
|
||
Author: Arunkumar | ||
Date: 14th October 2023 | ||
""" | ||
|
||
|
||
def earliest_deadline_first_scheduling( | ||
processes: list[tuple[str, int, int, int]] | ||
) -> list[str]: | ||
""" | ||
Perform Earliest Deadline First (EDF) scheduling. | ||
|
||
Args: | ||
processes (List[Tuple[str, int, int, int]]): A list of | ||
processes with their names, | ||
arrival times, deadlines, and execution times. | ||
|
||
Returns: | ||
List[str]: A list of process names in the order they are executed. | ||
|
||
Examples: | ||
>>> processes = [("A", 1, 5, 2), ("B", 2, 8, 3), ("C", 3, 4, 1)] | ||
>>> execution_order = earliest_deadline_first_scheduling(processes) | ||
>>> execution_order | ||
['Idle', 'A', 'C', 'B'] | ||
|
||
""" | ||
result = [] | ||
current_time = 0 | ||
|
||
while processes: | ||
available_processes = [ | ||
process for process in processes if process[1] <= current_time | ||
] | ||
|
||
if not available_processes: | ||
result.append("Idle") | ||
current_time += 1 | ||
else: | ||
next_process = min( | ||
available_processes, key=lambda tuple_values: tuple_values[2] | ||
) | ||
name, _, deadline, execution_time = next_process | ||
|
||
if current_time + execution_time <= deadline: | ||
result.append(name) | ||
current_time += execution_time | ||
processes.remove(next_process) | ||
else: | ||
result.append("Idle") | ||
current_time += 1 | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
processes = [("A", 1, 5, 2), ("B", 2, 8, 3), ("C", 3, 4, 1)] | ||
execution_order = earliest_deadline_first_scheduling(processes) | ||
for i, process in enumerate(execution_order): | ||
print(f"Time {i}: Executing process {process}") |
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.