Skip to content

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 14 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions data_structures/binary_tree/floor_and_ceiling.py
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]:
if self.left:
yield from self.left
yield self.key
if self.right:
yield from self.right

def __len__(self) -> int:
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()
70 changes: 70 additions & 0 deletions scheduling/shortest_deadline_first.py
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}")