tree_set.py

TreeSet implementation using Red-Black Tree for efficient sorted set operations.

This module provides a TreeSet class that implements a sorted set data structure using an underlying Red-Black Tree. It offers efficient insertion, deletion, and lookup operations while maintaining elements in sorted order.

Classes

TreeSet

A sorted set implementation that maintains unique elements in sorted order using a Red-Black Tree as the underlying data structure.

class tree_set.TreeSet(item_type: type, collection: Sequence[Any] | None = None, funct: Callable[[Any], Any] | None = None)[source]

Bases: object

Implements a TreeSet using a Red-Black Tree. .. attribute:: _tree

The underlying Red-Black Tree used to store the elements.

type:

RedBlackTree

_size

The number of elements in the TreeSet.

Type:

int

add(value: object) bool[source]

Adds a value to the TreeSet. :param value: The value to be added to the TreeSet. :type value: object

Returns:

True if the value was added, False if it was already present.

Return type:

bool

Raises:
  • TypeError – If the value is not of the correct type for the TreeSet.

  • ValueError – If the value is None or if it cannot be compared with existing elements.

add_all(collection: Sequence[Any]) bool[source]

Adds all elements from a collection to the TreeSet. :param collection: A sequence of elements to be added to the TreeSet. :type collection: Sequence[Any]

Returns:

True if any elements were added, False if the collection was empty or all elements were already present.

Return type:

bool

ceiling(value: object) object[source]

Returns the least element in the set greater than or equal to the given value. :param value: The value to compare against. Returns :type value: object :param object: The least element greater than or equal to the given value, or None if no such element exists.

clear()[source]

Clears the TreeSet, removing all elements.

clone()[source]

Creates a shallow copy of the TreeSet.

Returns:

A new TreeSet instance containing the same elements.

Return type:

TreeSet

contains(value: object) bool[source]

Checks if the TreeSet contains a specific value. :param value: The value to check for presence in the TreeSet. :type value: object

Returns:

True if the value is present, False otherwise.

Return type:

bool

Raises:
  • TypeError – If the value is not of the correct type for the TreeSet.

  • ValueError – If the value is None or if it cannot be compared with existing elements.

descending_iterator() Iterator[Any][source]

Returns an iterator that traverses the elements of the TreeSet in descending order.

Returns:

An iterator that yields elements in descending order.

Return type:

Iterator[Any]

first() object[source]

Returns the first (smallest) element in the TreeSet.

Returns:

The first element in the TreeSet, or None if the set is empty.

Return type:

object

floor(value: object) object[source]

Returns the greatest element in the set less than or equal to the given value. :param value: The value to compare against. :type value: object

Returns:

The greatest element less than or equal to the given value, or None if no such element exists.

Return type:

object

higher(value: object) object[source]

Returns the least element in the set strictly greater than the given value. :param value: The value to compare against. :type value: object

Returns:

The least element strictly greater than the given value, or None if no such element exists.

Return type:

object

is_empty() bool[source]

Checks if the TreeSet is empty. :returns: True if the TreeSet is empty, False otherwise. :rtype: bool

iterator() Iterator[Any][source]

Returns an iterator that traverses the elements of the TreeSet in ascending order. :returns: An iterator that yields elements in ascending order. :rtype: Iterator[Any]

last() object[source]

Returns the last (largest) element in the TreeSet. :returns: The last element in the TreeSet, or None if the set is empty. :rtype: object

lower(value: object) object[source]

Returns the greatest element in the set strictly less than the given value. :param value: The value to compare against. :type value: object

Returns:

The greatest element strictly less than the given value, or None if no such element exists.

Return type:

object

poll_first() object[source]

Removes and returns the first (smallest) element in the TreeSet. :returns: The first element in the TreeSet, or None if the set is empty. :rtype: object

poll_last() object[source]

Removes and returns the last (largest) element in the TreeSet. :returns: The last element in the TreeSet, or None if the set is empty. :rtype: object

remove(value: object) bool[source]

Removes a specific value from the TreeSet. :param value: The value to be removed from the TreeSet. :type value: object

Returns:

True if the value was removed, False if it was not present in the TreeSet.

Return type:

bool

Raises:
  • TypeError – If the value is not of the correct type for the TreeSet.

  • ValueError – If the value is None or if it cannot be compared with existing elements.

size() int[source]

Returns the number of elements in the TreeSet. :returns: The number of elements in the TreeSet. :rtype: int

visualize_tree()[source]

Visualizes the Red-Black Tree using a PyQt5 application. This method creates a PyQt5 application and displays the tree in a graphical view.