Prompt
stringlengths
16
11.5k
Completions
stringlengths
84
25.3k
Convert a octal value to its decimal equivalent octtodecimal Traceback most recent call last: ... ValueError: Empty string was passed to the function octtodecimal Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimale Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimal8 Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimale Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimal8 Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimal1 1 octtodecimal1 1 octtodecimal12 10 octtodecimal 12 10 octtodecimal45 37 octtodecimal Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimal0 0 octtodecimal4055 2093 octtodecimal20Fm Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function octtodecimal Traceback most recent call last: ... ValueError: Empty string was passed to the function octtodecimal19 Traceback most recent call last: ... ValueError: Nonoctal value was passed to the function
def oct_to_decimal(oct_string: str) -> int: """ Convert a octal value to its decimal equivalent >>> oct_to_decimal("") Traceback (most recent call last): ... ValueError: Empty string was passed to the function >>> oct_to_decimal("-") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("e") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("8") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("-e") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("-8") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("1") 1 >>> oct_to_decimal("-1") -1 >>> oct_to_decimal("12") 10 >>> oct_to_decimal(" 12 ") 10 >>> oct_to_decimal("-45") -37 >>> oct_to_decimal("-") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("0") 0 >>> oct_to_decimal("-4055") -2093 >>> oct_to_decimal("2-0Fm") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("") Traceback (most recent call last): ... ValueError: Empty string was passed to the function >>> oct_to_decimal("19") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function """ oct_string = str(oct_string).strip() if not oct_string: raise ValueError("Empty string was passed to the function") is_negative = oct_string[0] == "-" if is_negative: oct_string = oct_string[1:] if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string): raise ValueError("Non-octal value was passed to the function") decimal_number = 0 for char in oct_string: decimal_number = 8 * decimal_number + int(char) if is_negative: decimal_number = -decimal_number return decimal_number if __name__ == "__main__": from doctest import testmod testmod()
Convert an Octal number to Hexadecimal number. For more information: https:en.wikipedia.orgwikiOctal octaltohex100 '0x40' octaltohex235 '0x9D' octaltohex17 Traceback most recent call last: ... TypeError: Expected a string as input octaltohexAv Traceback most recent call last: ... ValueError: Not a Valid Octal Number octaltohex Traceback most recent call last: ... ValueError: Empty string was passed to the function Main Tests
def octal_to_hex(octal: str) -> str: """ Convert an Octal number to Hexadecimal number. For more information: https://en.wikipedia.org/wiki/Octal >>> octal_to_hex("100") '0x40' >>> octal_to_hex("235") '0x9D' >>> octal_to_hex(17) Traceback (most recent call last): ... TypeError: Expected a string as input >>> octal_to_hex("Av") Traceback (most recent call last): ... ValueError: Not a Valid Octal Number >>> octal_to_hex("") Traceback (most recent call last): ... ValueError: Empty string was passed to the function """ if not isinstance(octal, str): raise TypeError("Expected a string as input") if octal.startswith("0o"): octal = octal[2:] if octal == "": raise ValueError("Empty string was passed to the function") if any(char not in "01234567" for char in octal): raise ValueError("Not a Valid Octal Number") decimal = 0 for char in octal: decimal <<= 3 decimal |= int(char) hex_char = "0123456789ABCDEF" revhex = "" while decimal: revhex += hex_char[decimal & 15] decimal >>= 4 return "0x" + revhex[::-1] if __name__ == "__main__": import doctest doctest.testmod() nums = ["030", "100", "247", "235", "007"] ## Main Tests for num in nums: hexadecimal = octal_to_hex(num) expected = "0x" + hex(int(num, 8))[2:].upper() assert hexadecimal == expected print(f"Hex of '0o{num}' is: {hexadecimal}") print(f"Expected was: {expected}") print("---")
Convert International System of Units SI and Binary prefixes Wikipedia reference: https:en.wikipedia.orgwikiBinaryprefix Wikipedia reference: https:en.wikipedia.orgwikiInternationalSystemofUnits convertsiprefix1, SIUnit.giga, SIUnit.mega 1000 convertsiprefix1, SIUnit.mega, SIUnit.giga 0.001 convertsiprefix1, SIUnit.kilo, SIUnit.kilo 1 convertsiprefix1, 'giga', 'mega' 1000 convertsiprefix1, 'gIGa', 'mEGa' 1000 Wikipedia reference: https:en.wikipedia.orgwikiMetricprefix convertbinaryprefix1, BinaryUnit.giga, BinaryUnit.mega 1024 convertbinaryprefix1, BinaryUnit.mega, BinaryUnit.giga 0.0009765625 convertbinaryprefix1, BinaryUnit.kilo, BinaryUnit.kilo 1 convertbinaryprefix1, 'giga', 'mega' 1024 convertbinaryprefix1, 'gIGa', 'mEGa' 1024
from __future__ import annotations from enum import Enum class SIUnit(Enum): yotta = 24 zetta = 21 exa = 18 peta = 15 tera = 12 giga = 9 mega = 6 kilo = 3 hecto = 2 deca = 1 deci = -1 centi = -2 milli = -3 micro = -6 nano = -9 pico = -12 femto = -15 atto = -18 zepto = -21 yocto = -24 class BinaryUnit(Enum): yotta = 8 zetta = 7 exa = 6 peta = 5 tera = 4 giga = 3 mega = 2 kilo = 1 def convert_si_prefix( known_amount: float, known_prefix: str | SIUnit, unknown_prefix: str | SIUnit, ) -> float: """ Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix Wikipedia reference: https://en.wikipedia.org/wiki/International_System_of_Units >>> convert_si_prefix(1, SIUnit.giga, SIUnit.mega) 1000 >>> convert_si_prefix(1, SIUnit.mega, SIUnit.giga) 0.001 >>> convert_si_prefix(1, SIUnit.kilo, SIUnit.kilo) 1 >>> convert_si_prefix(1, 'giga', 'mega') 1000 >>> convert_si_prefix(1, 'gIGa', 'mEGa') 1000 """ if isinstance(known_prefix, str): known_prefix = SIUnit[known_prefix.lower()] if isinstance(unknown_prefix, str): unknown_prefix = SIUnit[unknown_prefix.lower()] unknown_amount: float = known_amount * ( 10 ** (known_prefix.value - unknown_prefix.value) ) return unknown_amount def convert_binary_prefix( known_amount: float, known_prefix: str | BinaryUnit, unknown_prefix: str | BinaryUnit, ) -> float: """ Wikipedia reference: https://en.wikipedia.org/wiki/Metric_prefix >>> convert_binary_prefix(1, BinaryUnit.giga, BinaryUnit.mega) 1024 >>> convert_binary_prefix(1, BinaryUnit.mega, BinaryUnit.giga) 0.0009765625 >>> convert_binary_prefix(1, BinaryUnit.kilo, BinaryUnit.kilo) 1 >>> convert_binary_prefix(1, 'giga', 'mega') 1024 >>> convert_binary_prefix(1, 'gIGa', 'mEGa') 1024 """ if isinstance(known_prefix, str): known_prefix = BinaryUnit[known_prefix.lower()] if isinstance(unknown_prefix, str): unknown_prefix = BinaryUnit[unknown_prefix.lower()] unknown_amount: float = known_amount * ( 2 ** ((known_prefix.value - unknown_prefix.value) * 10) ) return unknown_amount if __name__ == "__main__": import doctest doctest.testmod()
Author: Manuel Di Lullo https:github.commanueldilullo Description: Convert a number to use the correct SI or Binary unit prefix. Inspired by prefixconversion.py file in this repository by lancepyles URL: https:en.wikipedia.orgwikiMetricprefixListofSIprefixes URL: https:en.wikipedia.orgwikiBinaryprefix Create a generic variable that can be 'Enum', or any subclass. Returns a dictionary with only the elements of this enum that has a positive value from itertools import islice positive SIUnit.getpositive inc iterpositive.items dictisliceinc, lenpositive 2 'yotta': 24, 'zetta': 21, 'exa': 18, 'peta': 15, 'tera': 12 dictinc 'giga': 9, 'mega': 6, 'kilo': 3, 'hecto': 2, 'deca': 1 Returns a dictionary with only the elements of this enum that has a negative value example from itertools import islice negative SIUnit.getnegative inc iternegative.items dictisliceinc, lennegative 2 'deci': 1, 'centi': 2, 'milli': 3, 'micro': 6, 'nano': 9 dictinc 'pico': 12, 'femto': 15, 'atto': 18, 'zepto': 21, 'yocto': 24 Function that converts a number to his version with SI prefix input value an integer example: addsiprefix10000 '10.0 kilo' Function that converts a number to his version with Binary prefix input value an integer example: addbinaryprefix65536 '64.0 kilo'
from __future__ import annotations from enum import Enum, unique from typing import TypeVar # Create a generic variable that can be 'Enum', or any subclass. T = TypeVar("T", bound="Enum") @unique class BinaryUnit(Enum): yotta = 80 zetta = 70 exa = 60 peta = 50 tera = 40 giga = 30 mega = 20 kilo = 10 @unique class SIUnit(Enum): yotta = 24 zetta = 21 exa = 18 peta = 15 tera = 12 giga = 9 mega = 6 kilo = 3 hecto = 2 deca = 1 deci = -1 centi = -2 milli = -3 micro = -6 nano = -9 pico = -12 femto = -15 atto = -18 zepto = -21 yocto = -24 @classmethod def get_positive(cls: type[T]) -> dict: """ Returns a dictionary with only the elements of this enum that has a positive value >>> from itertools import islice >>> positive = SIUnit.get_positive() >>> inc = iter(positive.items()) >>> dict(islice(inc, len(positive) // 2)) {'yotta': 24, 'zetta': 21, 'exa': 18, 'peta': 15, 'tera': 12} >>> dict(inc) {'giga': 9, 'mega': 6, 'kilo': 3, 'hecto': 2, 'deca': 1} """ return {unit.name: unit.value for unit in cls if unit.value > 0} @classmethod def get_negative(cls: type[T]) -> dict: """ Returns a dictionary with only the elements of this enum that has a negative value @example >>> from itertools import islice >>> negative = SIUnit.get_negative() >>> inc = iter(negative.items()) >>> dict(islice(inc, len(negative) // 2)) {'deci': -1, 'centi': -2, 'milli': -3, 'micro': -6, 'nano': -9} >>> dict(inc) {'pico': -12, 'femto': -15, 'atto': -18, 'zepto': -21, 'yocto': -24} """ return {unit.name: unit.value for unit in cls if unit.value < 0} def add_si_prefix(value: float) -> str: """ Function that converts a number to his version with SI prefix @input value (an integer) @example: >>> add_si_prefix(10000) '10.0 kilo' """ prefixes = SIUnit.get_positive() if value > 0 else SIUnit.get_negative() for name_prefix, value_prefix in prefixes.items(): numerical_part = value / (10**value_prefix) if numerical_part > 1: return f"{numerical_part!s} {name_prefix}" return str(value) def add_binary_prefix(value: float) -> str: """ Function that converts a number to his version with Binary prefix @input value (an integer) @example: >>> add_binary_prefix(65536) '64.0 kilo' """ for prefix in BinaryUnit: numerical_part = value / (2**prefix.value) if numerical_part > 1: return f"{numerical_part!s} {prefix.name}" return str(value) if __name__ == "__main__": import doctest doctest.testmod()
Conversion of pressure units. Available Units: Pascal,Bar,Kilopascal,Megapascal,psipound per square inch, inHgin mercury column,torr,atm USAGE : Import this file into their respective project. Use the function pressureconversion for conversion of pressure units. Parameters : value : The number of from units you want to convert fromtype : From which type you want to convert totype : To which type you want to convert REFERENCES : Wikipedia reference: https:en.wikipedia.orgwikiPascalunit Wikipedia reference: https:en.wikipedia.orgwikiPoundpersquareinch Wikipedia reference: https:en.wikipedia.orgwikiInchofmercury Wikipedia reference: https:en.wikipedia.orgwikiTorr https:en.wikipedia.orgwikiStandardatmosphereunit https:msestudent.comwhataretheunitsofpressure https:www.unitconverters.netpressureconverter.html Conversion between pressure units. pressureconversion4, atm, pascal 405300 pressureconversion1, pascal, psi 0.00014401981999999998 pressureconversion1, bar, atm 0.986923 pressureconversion3, kilopascal, bar 0.029999991892499998 pressureconversion2, megapascal, psi 290.074434314 pressureconversion4, psi, torr 206.85984 pressureconversion1, inHg, atm 0.0334211 pressureconversion1, torr, psi 0.019336718261000002 pressureconversion4, wrongUnit, atm Traceback most recent call last: ... ValueError: Invalid 'fromtype' value: 'wrongUnit' Supported values are: atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr
from typing import NamedTuple class FromTo(NamedTuple): from_factor: float to_factor: float PRESSURE_CONVERSION = { "atm": FromTo(1, 1), "pascal": FromTo(0.0000098, 101325), "bar": FromTo(0.986923, 1.01325), "kilopascal": FromTo(0.00986923, 101.325), "megapascal": FromTo(9.86923, 0.101325), "psi": FromTo(0.068046, 14.6959), "inHg": FromTo(0.0334211, 29.9213), "torr": FromTo(0.00131579, 760), } def pressure_conversion(value: float, from_type: str, to_type: str) -> float: """ Conversion between pressure units. >>> pressure_conversion(4, "atm", "pascal") 405300 >>> pressure_conversion(1, "pascal", "psi") 0.00014401981999999998 >>> pressure_conversion(1, "bar", "atm") 0.986923 >>> pressure_conversion(3, "kilopascal", "bar") 0.029999991892499998 >>> pressure_conversion(2, "megapascal", "psi") 290.074434314 >>> pressure_conversion(4, "psi", "torr") 206.85984 >>> pressure_conversion(1, "inHg", "atm") 0.0334211 >>> pressure_conversion(1, "torr", "psi") 0.019336718261000002 >>> pressure_conversion(4, "wrongUnit", "atm") Traceback (most recent call last): ... ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr """ if from_type not in PRESSURE_CONVERSION: raise ValueError( f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" + ", ".join(PRESSURE_CONVERSION) ) if to_type not in PRESSURE_CONVERSION: raise ValueError( f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" + ", ".join(PRESSURE_CONVERSION) ) return ( value * PRESSURE_CONVERSION[from_type].from_factor * PRESSURE_CONVERSION[to_type].to_factor ) if __name__ == "__main__": import doctest doctest.testmod()
Simple RGB to CMYK conversion. Returns percentages of CMYK paint. https:www.programmingalgorithms.comalgorithmrgbtocmyk Note: this is a very popular algorithm that converts colors linearly and gives only approximate results. Actual preparation for printing requires advanced color conversion considering the color profiles and parameters of the target device. rgbtocmyk255, 200, a Traceback most recent call last: ... ValueError: Expected int, found class 'int', class 'int', class 'str' rgbtocmyk255, 255, 999 Traceback most recent call last: ... ValueError: Expected int of the range 0..255 rgbtocmyk255, 255, 255 white 0, 0, 0, 0 rgbtocmyk128, 128, 128 gray 0, 0, 0, 50 rgbtocmyk0, 0, 0 black 0, 0, 0, 100 rgbtocmyk255, 0, 0 red 0, 100, 100, 0 rgbtocmyk0, 255, 0 green 100, 0, 100, 0 rgbtocmyk0, 0, 255 blue 100, 100, 0, 0 changing range from 0..255 to 0..1
def rgb_to_cmyk(r_input: int, g_input: int, b_input: int) -> tuple[int, int, int, int]: """ Simple RGB to CMYK conversion. Returns percentages of CMYK paint. https://www.programmingalgorithms.com/algorithm/rgb-to-cmyk/ Note: this is a very popular algorithm that converts colors linearly and gives only approximate results. Actual preparation for printing requires advanced color conversion considering the color profiles and parameters of the target device. >>> rgb_to_cmyk(255, 200, "a") Traceback (most recent call last): ... ValueError: Expected int, found (<class 'int'>, <class 'int'>, <class 'str'>) >>> rgb_to_cmyk(255, 255, 999) Traceback (most recent call last): ... ValueError: Expected int of the range 0..255 >>> rgb_to_cmyk(255, 255, 255) # white (0, 0, 0, 0) >>> rgb_to_cmyk(128, 128, 128) # gray (0, 0, 0, 50) >>> rgb_to_cmyk(0, 0, 0) # black (0, 0, 0, 100) >>> rgb_to_cmyk(255, 0, 0) # red (0, 100, 100, 0) >>> rgb_to_cmyk(0, 255, 0) # green (100, 0, 100, 0) >>> rgb_to_cmyk(0, 0, 255) # blue (100, 100, 0, 0) """ if ( not isinstance(r_input, int) or not isinstance(g_input, int) or not isinstance(b_input, int) ): msg = f"Expected int, found {type(r_input), type(g_input), type(b_input)}" raise ValueError(msg) if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256: raise ValueError("Expected int of the range 0..255") # changing range from 0..255 to 0..1 r = r_input / 255 g = g_input / 255 b = b_input / 255 k = 1 - max(r, g, b) if k == 1: # pure black return 0, 0, 0, 100 c = round(100 * (1 - r - k) / (1 - k)) m = round(100 * (1 - g - k) / (1 - k)) y = round(100 * (1 - b - k) / (1 - k)) k = round(100 * k) return c, m, y, k if __name__ == "__main__": from doctest import testmod testmod()
The RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue. Meanwhile, the HSV representation models how colors appear under light. In it, colors are represented using three components: hue, saturation and brightnessvalue. This file provides functions for converting colors from one representation to the other. description adapted from https:en.wikipedia.orgwikiRGBcolormodel and https:en.wikipedia.orgwikiHSLandHSV. Conversion from the HSVrepresentation to the RGBrepresentation. Expected RGBvalues taken from https:www.rapidtables.comconvertcolorhsvtorgb.html hsvtorgb0, 0, 0 0, 0, 0 hsvtorgb0, 0, 1 255, 255, 255 hsvtorgb0, 1, 1 255, 0, 0 hsvtorgb60, 1, 1 255, 255, 0 hsvtorgb120, 1, 1 0, 255, 0 hsvtorgb240, 1, 1 0, 0, 255 hsvtorgb300, 1, 1 255, 0, 255 hsvtorgb180, 0.5, 0.5 64, 128, 128 hsvtorgb234, 0.14, 0.88 193, 196, 224 hsvtorgb330, 0.75, 0.5 128, 32, 80 Conversion from the RGBrepresentation to the HSVrepresentation. The tested values are the reverse values from the hsvtorgbdoctests. Function approximatelyequalhsv is needed because of small deviations due to rounding for the RGBvalues. approximatelyequalhsvrgbtohsv0, 0, 0, 0, 0, 0 True approximatelyequalhsvrgbtohsv255, 255, 255, 0, 0, 1 True approximatelyequalhsvrgbtohsv255, 0, 0, 0, 1, 1 True approximatelyequalhsvrgbtohsv255, 255, 0, 60, 1, 1 True approximatelyequalhsvrgbtohsv0, 255, 0, 120, 1, 1 True approximatelyequalhsvrgbtohsv0, 0, 255, 240, 1, 1 True approximatelyequalhsvrgbtohsv255, 0, 255, 300, 1, 1 True approximatelyequalhsvrgbtohsv64, 128, 128, 180, 0.5, 0.5 True approximatelyequalhsvrgbtohsv193, 196, 224, 234, 0.14, 0.88 True approximatelyequalhsvrgbtohsv128, 32, 80, 330, 0.75, 0.5 True Utilityfunction to check that two hsvcolors are approximately equal approximatelyequalhsv0, 0, 0, 0, 0, 0 True approximatelyequalhsv180, 0.5, 0.3, 179.9999, 0.500001, 0.30001 True approximatelyequalhsv0, 0, 0, 1, 0, 0 False approximatelyequalhsv180, 0.5, 0.3, 179.9999, 0.6, 0.30001 False
def hsv_to_rgb(hue: float, saturation: float, value: float) -> list[int]: """ Conversion from the HSV-representation to the RGB-representation. Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html >>> hsv_to_rgb(0, 0, 0) [0, 0, 0] >>> hsv_to_rgb(0, 0, 1) [255, 255, 255] >>> hsv_to_rgb(0, 1, 1) [255, 0, 0] >>> hsv_to_rgb(60, 1, 1) [255, 255, 0] >>> hsv_to_rgb(120, 1, 1) [0, 255, 0] >>> hsv_to_rgb(240, 1, 1) [0, 0, 255] >>> hsv_to_rgb(300, 1, 1) [255, 0, 255] >>> hsv_to_rgb(180, 0.5, 0.5) [64, 128, 128] >>> hsv_to_rgb(234, 0.14, 0.88) [193, 196, 224] >>> hsv_to_rgb(330, 0.75, 0.5) [128, 32, 80] """ if hue < 0 or hue > 360: raise Exception("hue should be between 0 and 360") if saturation < 0 or saturation > 1: raise Exception("saturation should be between 0 and 1") if value < 0 or value > 1: raise Exception("value should be between 0 and 1") chroma = value * saturation hue_section = hue / 60 second_largest_component = chroma * (1 - abs(hue_section % 2 - 1)) match_value = value - chroma if hue_section >= 0 and hue_section <= 1: red = round(255 * (chroma + match_value)) green = round(255 * (second_largest_component + match_value)) blue = round(255 * (match_value)) elif hue_section > 1 and hue_section <= 2: red = round(255 * (second_largest_component + match_value)) green = round(255 * (chroma + match_value)) blue = round(255 * (match_value)) elif hue_section > 2 and hue_section <= 3: red = round(255 * (match_value)) green = round(255 * (chroma + match_value)) blue = round(255 * (second_largest_component + match_value)) elif hue_section > 3 and hue_section <= 4: red = round(255 * (match_value)) green = round(255 * (second_largest_component + match_value)) blue = round(255 * (chroma + match_value)) elif hue_section > 4 and hue_section <= 5: red = round(255 * (second_largest_component + match_value)) green = round(255 * (match_value)) blue = round(255 * (chroma + match_value)) else: red = round(255 * (chroma + match_value)) green = round(255 * (match_value)) blue = round(255 * (second_largest_component + match_value)) return [red, green, blue] def rgb_to_hsv(red: int, green: int, blue: int) -> list[float]: """ Conversion from the RGB-representation to the HSV-representation. The tested values are the reverse values from the hsv_to_rgb-doctests. Function "approximately_equal_hsv" is needed because of small deviations due to rounding for the RGB-values. >>> approximately_equal_hsv(rgb_to_hsv(0, 0, 0), [0, 0, 0]) True >>> approximately_equal_hsv(rgb_to_hsv(255, 255, 255), [0, 0, 1]) True >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 0), [0, 1, 1]) True >>> approximately_equal_hsv(rgb_to_hsv(255, 255, 0), [60, 1, 1]) True >>> approximately_equal_hsv(rgb_to_hsv(0, 255, 0), [120, 1, 1]) True >>> approximately_equal_hsv(rgb_to_hsv(0, 0, 255), [240, 1, 1]) True >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 255), [300, 1, 1]) True >>> approximately_equal_hsv(rgb_to_hsv(64, 128, 128), [180, 0.5, 0.5]) True >>> approximately_equal_hsv(rgb_to_hsv(193, 196, 224), [234, 0.14, 0.88]) True >>> approximately_equal_hsv(rgb_to_hsv(128, 32, 80), [330, 0.75, 0.5]) True """ if red < 0 or red > 255: raise Exception("red should be between 0 and 255") if green < 0 or green > 255: raise Exception("green should be between 0 and 255") if blue < 0 or blue > 255: raise Exception("blue should be between 0 and 255") float_red = red / 255 float_green = green / 255 float_blue = blue / 255 value = max(float_red, float_green, float_blue) chroma = value - min(float_red, float_green, float_blue) saturation = 0 if value == 0 else chroma / value if chroma == 0: hue = 0.0 elif value == float_red: hue = 60 * (0 + (float_green - float_blue) / chroma) elif value == float_green: hue = 60 * (2 + (float_blue - float_red) / chroma) else: hue = 60 * (4 + (float_red - float_green) / chroma) hue = (hue + 360) % 360 return [hue, saturation, value] def approximately_equal_hsv(hsv_1: list[float], hsv_2: list[float]) -> bool: """ Utility-function to check that two hsv-colors are approximately equal >>> approximately_equal_hsv([0, 0, 0], [0, 0, 0]) True >>> approximately_equal_hsv([180, 0.5, 0.3], [179.9999, 0.500001, 0.30001]) True >>> approximately_equal_hsv([0, 0, 0], [1, 0, 0]) False >>> approximately_equal_hsv([180, 0.5, 0.3], [179.9999, 0.6, 0.30001]) False """ check_hue = abs(hsv_1[0] - hsv_2[0]) < 0.2 check_saturation = abs(hsv_1[1] - hsv_2[1]) < 0.002 check_value = abs(hsv_1[2] - hsv_2[2]) < 0.002 return check_hue and check_saturation and check_value
LeetCode No. 13 Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. https:en.wikipedia.orgwikiRomannumerals tests III: 3, CLIV: 154, MIX: 1009, MMD: 2500, MMMCMXCIX: 3999 allromantointkey value for key, value in tests.items True Given a integer, convert it to an roman numeral. https:en.wikipedia.orgwikiRomannumerals tests III: 3, CLIV: 154, MIX: 1009, MMD: 2500, MMMCMXCIX: 3999 allinttoromanvalue key for key, value in tests.items True
ROMAN = [ (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I"), ] def roman_to_int(roman: str) -> int: """ LeetCode No. 13 Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. https://en.wikipedia.org/wiki/Roman_numerals >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} >>> all(roman_to_int(key) == value for key, value in tests.items()) True """ vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} total = 0 place = 0 while place < len(roman): if (place + 1 < len(roman)) and (vals[roman[place]] < vals[roman[place + 1]]): total += vals[roman[place + 1]] - vals[roman[place]] place += 2 else: total += vals[roman[place]] place += 1 return total def int_to_roman(number: int) -> str: """ Given a integer, convert it to an roman numeral. https://en.wikipedia.org/wiki/Roman_numerals >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} >>> all(int_to_roman(value) == key for key, value in tests.items()) True """ result = [] for arabic, roman in ROMAN: (factor, number) = divmod(number, arabic) result.append(roman * factor) if number == 0: break return "".join(result) if __name__ == "__main__": import doctest doctest.testmod()
Convert speed units https:en.wikipedia.orgwikiKilometresperhour https:en.wikipedia.orgwikiMilesperhour https:en.wikipedia.orgwikiKnotunit https:en.wikipedia.orgwikiMetrepersecond Convert speed from one unit to another using the speedchart above. kmh: 1.0, ms: 3.6, mph: 1.609344, knot: 1.852, convertspeed100, kmh, ms 27.778 convertspeed100, kmh, mph 62.137 convertspeed100, kmh, knot 53.996 convertspeed100, ms, kmh 360.0 convertspeed100, ms, mph 223.694 convertspeed100, ms, knot 194.384 convertspeed100, mph, kmh 160.934 convertspeed100, mph, ms 44.704 convertspeed100, mph, knot 86.898 convertspeed100, knot, kmh 185.2 convertspeed100, knot, ms 51.444 convertspeed100, knot, mph 115.078
speed_chart: dict[str, float] = { "km/h": 1.0, "m/s": 3.6, "mph": 1.609344, "knot": 1.852, } speed_chart_inverse: dict[str, float] = { "km/h": 1.0, "m/s": 0.277777778, "mph": 0.621371192, "knot": 0.539956803, } def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: """ Convert speed from one unit to another using the speed_chart above. "km/h": 1.0, "m/s": 3.6, "mph": 1.609344, "knot": 1.852, >>> convert_speed(100, "km/h", "m/s") 27.778 >>> convert_speed(100, "km/h", "mph") 62.137 >>> convert_speed(100, "km/h", "knot") 53.996 >>> convert_speed(100, "m/s", "km/h") 360.0 >>> convert_speed(100, "m/s", "mph") 223.694 >>> convert_speed(100, "m/s", "knot") 194.384 >>> convert_speed(100, "mph", "km/h") 160.934 >>> convert_speed(100, "mph", "m/s") 44.704 >>> convert_speed(100, "mph", "knot") 86.898 >>> convert_speed(100, "knot", "km/h") 185.2 >>> convert_speed(100, "knot", "m/s") 51.444 >>> convert_speed(100, "knot", "mph") 115.078 """ if unit_to not in speed_chart or unit_from not in speed_chart_inverse: msg = ( f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" f"Valid values are: {', '.join(speed_chart_inverse)}" ) raise ValueError(msg) return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to], 3) if __name__ == "__main__": import doctest doctest.testmod()
Convert between different units of temperature def celsiustofahrenheitcelsius: float, ndigits: int 2 float: return roundfloatcelsius 9 5 32, ndigits def celsiustokelvincelsius: float, ndigits: int 2 float: return roundfloatcelsius 273.15, ndigits def celsiustorankinecelsius: float, ndigits: int 2 float: return roundfloatcelsius 9 5 491.67, ndigits def fahrenheittocelsiusfahrenheit: float, ndigits: int 2 float: return roundfloatfahrenheit 32 5 9, ndigits def fahrenheittokelvinfahrenheit: float, ndigits: int 2 float: return roundfloatfahrenheit 32 5 9 273.15, ndigits def fahrenheittorankinefahrenheit: float, ndigits: int 2 float: return roundfloatfahrenheit 459.67, ndigits def kelvintocelsiuskelvin: float, ndigits: int 2 float: return roundfloatkelvin 273.15, ndigits def kelvintofahrenheitkelvin: float, ndigits: int 2 float: return roundfloatkelvin 273.15 9 5 32, ndigits def kelvintorankinekelvin: float, ndigits: int 2 float: return roundfloatkelvin 9 5, ndigits def rankinetocelsiusrankine: float, ndigits: int 2 float: return roundfloatrankine 491.67 5 9, ndigits def rankinetofahrenheitrankine: float, ndigits: int 2 float: return roundfloatrankine 459.67, ndigits def rankinetokelvinrankine: float, ndigits: int 2 float: return roundfloatrankine 5 9, ndigits def reaumurtokelvinreaumur: float, ndigits: int 2 float: return roundfloatreaumur 1.25 273.15, ndigits def reaumurtofahrenheitreaumur: float, ndigits: int 2 float: return roundfloatreaumur 2.25 32, ndigits def reaumurtocelsiusreaumur: float, ndigits: int 2 float: return roundfloatreaumur 1.25, ndigits def reaumurtorankinereaumur: float, ndigits: int 2 float: return roundfloatreaumur 2.25 32 459.67, ndigits if name main: import doctest doctest.testmod
def celsius_to_fahrenheit(celsius: float, ndigits: int = 2) -> float: """ Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Celsius Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit >>> celsius_to_fahrenheit(273.354, 3) 524.037 >>> celsius_to_fahrenheit(273.354, 0) 524.0 >>> celsius_to_fahrenheit(-40.0) -40.0 >>> celsius_to_fahrenheit(-20.0) -4.0 >>> celsius_to_fahrenheit(0) 32.0 >>> celsius_to_fahrenheit(20) 68.0 >>> celsius_to_fahrenheit("40") 104.0 >>> celsius_to_fahrenheit("celsius") Traceback (most recent call last): ... ValueError: could not convert string to float: 'celsius' """ return round((float(celsius) * 9 / 5) + 32, ndigits) def celsius_to_kelvin(celsius: float, ndigits: int = 2) -> float: """ Convert a given value from Celsius to Kelvin and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Celsius Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin >>> celsius_to_kelvin(273.354, 3) 546.504 >>> celsius_to_kelvin(273.354, 0) 547.0 >>> celsius_to_kelvin(0) 273.15 >>> celsius_to_kelvin(20.0) 293.15 >>> celsius_to_kelvin("40") 313.15 >>> celsius_to_kelvin("celsius") Traceback (most recent call last): ... ValueError: could not convert string to float: 'celsius' """ return round(float(celsius) + 273.15, ndigits) def celsius_to_rankine(celsius: float, ndigits: int = 2) -> float: """ Convert a given value from Celsius to Rankine and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Celsius Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale >>> celsius_to_rankine(273.354, 3) 983.707 >>> celsius_to_rankine(273.354, 0) 984.0 >>> celsius_to_rankine(0) 491.67 >>> celsius_to_rankine(20.0) 527.67 >>> celsius_to_rankine("40") 563.67 >>> celsius_to_rankine("celsius") Traceback (most recent call last): ... ValueError: could not convert string to float: 'celsius' """ return round((float(celsius) * 9 / 5) + 491.67, ndigits) def fahrenheit_to_celsius(fahrenheit: float, ndigits: int = 2) -> float: """ Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit Wikipedia reference: https://en.wikipedia.org/wiki/Celsius >>> fahrenheit_to_celsius(273.354, 3) 134.086 >>> fahrenheit_to_celsius(273.354, 0) 134.0 >>> fahrenheit_to_celsius(0) -17.78 >>> fahrenheit_to_celsius(20.0) -6.67 >>> fahrenheit_to_celsius(40.0) 4.44 >>> fahrenheit_to_celsius(60) 15.56 >>> fahrenheit_to_celsius(80) 26.67 >>> fahrenheit_to_celsius("100") 37.78 >>> fahrenheit_to_celsius("fahrenheit") Traceback (most recent call last): ... ValueError: could not convert string to float: 'fahrenheit' """ return round((float(fahrenheit) - 32) * 5 / 9, ndigits) def fahrenheit_to_kelvin(fahrenheit: float, ndigits: int = 2) -> float: """ Convert a given value from Fahrenheit to Kelvin and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin >>> fahrenheit_to_kelvin(273.354, 3) 407.236 >>> fahrenheit_to_kelvin(273.354, 0) 407.0 >>> fahrenheit_to_kelvin(0) 255.37 >>> fahrenheit_to_kelvin(20.0) 266.48 >>> fahrenheit_to_kelvin(40.0) 277.59 >>> fahrenheit_to_kelvin(60) 288.71 >>> fahrenheit_to_kelvin(80) 299.82 >>> fahrenheit_to_kelvin("100") 310.93 >>> fahrenheit_to_kelvin("fahrenheit") Traceback (most recent call last): ... ValueError: could not convert string to float: 'fahrenheit' """ return round(((float(fahrenheit) - 32) * 5 / 9) + 273.15, ndigits) def fahrenheit_to_rankine(fahrenheit: float, ndigits: int = 2) -> float: """ Convert a given value from Fahrenheit to Rankine and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale >>> fahrenheit_to_rankine(273.354, 3) 733.024 >>> fahrenheit_to_rankine(273.354, 0) 733.0 >>> fahrenheit_to_rankine(0) 459.67 >>> fahrenheit_to_rankine(20.0) 479.67 >>> fahrenheit_to_rankine(40.0) 499.67 >>> fahrenheit_to_rankine(60) 519.67 >>> fahrenheit_to_rankine(80) 539.67 >>> fahrenheit_to_rankine("100") 559.67 >>> fahrenheit_to_rankine("fahrenheit") Traceback (most recent call last): ... ValueError: could not convert string to float: 'fahrenheit' """ return round(float(fahrenheit) + 459.67, ndigits) def kelvin_to_celsius(kelvin: float, ndigits: int = 2) -> float: """ Convert a given value from Kelvin to Celsius and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin Wikipedia reference: https://en.wikipedia.org/wiki/Celsius >>> kelvin_to_celsius(273.354, 3) 0.204 >>> kelvin_to_celsius(273.354, 0) 0.0 >>> kelvin_to_celsius(273.15) 0.0 >>> kelvin_to_celsius(300) 26.85 >>> kelvin_to_celsius("315.5") 42.35 >>> kelvin_to_celsius("kelvin") Traceback (most recent call last): ... ValueError: could not convert string to float: 'kelvin' """ return round(float(kelvin) - 273.15, ndigits) def kelvin_to_fahrenheit(kelvin: float, ndigits: int = 2) -> float: """ Convert a given value from Kelvin to Fahrenheit and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit >>> kelvin_to_fahrenheit(273.354, 3) 32.367 >>> kelvin_to_fahrenheit(273.354, 0) 32.0 >>> kelvin_to_fahrenheit(273.15) 32.0 >>> kelvin_to_fahrenheit(300) 80.33 >>> kelvin_to_fahrenheit("315.5") 108.23 >>> kelvin_to_fahrenheit("kelvin") Traceback (most recent call last): ... ValueError: could not convert string to float: 'kelvin' """ return round(((float(kelvin) - 273.15) * 9 / 5) + 32, ndigits) def kelvin_to_rankine(kelvin: float, ndigits: int = 2) -> float: """ Convert a given value from Kelvin to Rankine and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale >>> kelvin_to_rankine(273.354, 3) 492.037 >>> kelvin_to_rankine(273.354, 0) 492.0 >>> kelvin_to_rankine(0) 0.0 >>> kelvin_to_rankine(20.0) 36.0 >>> kelvin_to_rankine("40") 72.0 >>> kelvin_to_rankine("kelvin") Traceback (most recent call last): ... ValueError: could not convert string to float: 'kelvin' """ return round((float(kelvin) * 9 / 5), ndigits) def rankine_to_celsius(rankine: float, ndigits: int = 2) -> float: """ Convert a given value from Rankine to Celsius and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale Wikipedia reference: https://en.wikipedia.org/wiki/Celsius >>> rankine_to_celsius(273.354, 3) -121.287 >>> rankine_to_celsius(273.354, 0) -121.0 >>> rankine_to_celsius(273.15) -121.4 >>> rankine_to_celsius(300) -106.48 >>> rankine_to_celsius("315.5") -97.87 >>> rankine_to_celsius("rankine") Traceback (most recent call last): ... ValueError: could not convert string to float: 'rankine' """ return round((float(rankine) - 491.67) * 5 / 9, ndigits) def rankine_to_fahrenheit(rankine: float, ndigits: int = 2) -> float: """ Convert a given value from Rankine to Fahrenheit and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit >>> rankine_to_fahrenheit(273.15) -186.52 >>> rankine_to_fahrenheit(300) -159.67 >>> rankine_to_fahrenheit("315.5") -144.17 >>> rankine_to_fahrenheit("rankine") Traceback (most recent call last): ... ValueError: could not convert string to float: 'rankine' """ return round(float(rankine) - 459.67, ndigits) def rankine_to_kelvin(rankine: float, ndigits: int = 2) -> float: """ Convert a given value from Rankine to Kelvin and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin >>> rankine_to_kelvin(0) 0.0 >>> rankine_to_kelvin(20.0) 11.11 >>> rankine_to_kelvin("40") 22.22 >>> rankine_to_kelvin("rankine") Traceback (most recent call last): ... ValueError: could not convert string to float: 'rankine' """ return round((float(rankine) * 5 / 9), ndigits) def reaumur_to_kelvin(reaumur: float, ndigits: int = 2) -> float: """ Convert a given value from reaumur to Kelvin and round it to 2 decimal places. Reference:- http://www.csgnetwork.com/temp2conv.html >>> reaumur_to_kelvin(0) 273.15 >>> reaumur_to_kelvin(20.0) 298.15 >>> reaumur_to_kelvin(40) 323.15 >>> reaumur_to_kelvin("reaumur") Traceback (most recent call last): ... ValueError: could not convert string to float: 'reaumur' """ return round((float(reaumur) * 1.25 + 273.15), ndigits) def reaumur_to_fahrenheit(reaumur: float, ndigits: int = 2) -> float: """ Convert a given value from reaumur to fahrenheit and round it to 2 decimal places. Reference:- http://www.csgnetwork.com/temp2conv.html >>> reaumur_to_fahrenheit(0) 32.0 >>> reaumur_to_fahrenheit(20.0) 77.0 >>> reaumur_to_fahrenheit(40) 122.0 >>> reaumur_to_fahrenheit("reaumur") Traceback (most recent call last): ... ValueError: could not convert string to float: 'reaumur' """ return round((float(reaumur) * 2.25 + 32), ndigits) def reaumur_to_celsius(reaumur: float, ndigits: int = 2) -> float: """ Convert a given value from reaumur to celsius and round it to 2 decimal places. Reference:- http://www.csgnetwork.com/temp2conv.html >>> reaumur_to_celsius(0) 0.0 >>> reaumur_to_celsius(20.0) 25.0 >>> reaumur_to_celsius(40) 50.0 >>> reaumur_to_celsius("reaumur") Traceback (most recent call last): ... ValueError: could not convert string to float: 'reaumur' """ return round((float(reaumur) * 1.25), ndigits) def reaumur_to_rankine(reaumur: float, ndigits: int = 2) -> float: """ Convert a given value from reaumur to rankine and round it to 2 decimal places. Reference:- http://www.csgnetwork.com/temp2conv.html >>> reaumur_to_rankine(0) 491.67 >>> reaumur_to_rankine(20.0) 536.67 >>> reaumur_to_rankine(40) 581.67 >>> reaumur_to_rankine("reaumur") Traceback (most recent call last): ... ValueError: could not convert string to float: 'reaumur' """ return round((float(reaumur) * 2.25 + 32 + 459.67), ndigits) if __name__ == "__main__": import doctest doctest.testmod()
A unit of time is any particular time interval, used as a standard way of measuring or expressing duration. The base unit of time in the International System of Units SI, and by extension most of the Western world, is the second, defined as about 9 billion oscillations of the caesium atom. https:en.wikipedia.orgwikiUnitoftime Convert time from one unit to another using the timechart above. converttime3600, seconds, hours 1.0 converttime3500, Seconds, Hours 0.972 converttime1, DaYs, hours 24.0 converttime120, minutes, SeCoNdS 7200.0 converttime2, WEEKS, days 14.0 converttime0.5, hours, MINUTES 30.0 converttime3600, seconds, hours Traceback most recent call last: ... ValueError: 'timevalue' must be a nonnegative number. converttimeHello, hours, minutes Traceback most recent call last: ... ValueError: 'timevalue' must be a nonnegative number. converttime0, 1, 2, weeks, days Traceback most recent call last: ... ValueError: 'timevalue' must be a nonnegative number. converttime1, cool, century doctest: ELLIPSIS Traceback most recent call last: ... ValueError: Invalid unit cool is not in seconds, minutes, hours, days, weeks, ... converttime1, seconds, hot doctest: ELLIPSIS Traceback most recent call last: ... ValueError: Invalid unit hot is not in seconds, minutes, hours, days, weeks, ...
time_chart: dict[str, float] = { "seconds": 1.0, "minutes": 60.0, # 1 minute = 60 sec "hours": 3600.0, # 1 hour = 60 minutes = 3600 seconds "days": 86400.0, # 1 day = 24 hours = 1440 min = 86400 sec "weeks": 604800.0, # 1 week=7d=168hr=10080min = 604800 sec "months": 2629800.0, # Approximate value for a month in seconds "years": 31557600.0, # Approximate value for a year in seconds } time_chart_inverse: dict[str, float] = { key: 1 / value for key, value in time_chart.items() } def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: """ Convert time from one unit to another using the time_chart above. >>> convert_time(3600, "seconds", "hours") 1.0 >>> convert_time(3500, "Seconds", "Hours") 0.972 >>> convert_time(1, "DaYs", "hours") 24.0 >>> convert_time(120, "minutes", "SeCoNdS") 7200.0 >>> convert_time(2, "WEEKS", "days") 14.0 >>> convert_time(0.5, "hours", "MINUTES") 30.0 >>> convert_time(-3600, "seconds", "hours") Traceback (most recent call last): ... ValueError: 'time_value' must be a non-negative number. >>> convert_time("Hello", "hours", "minutes") Traceback (most recent call last): ... ValueError: 'time_value' must be a non-negative number. >>> convert_time([0, 1, 2], "weeks", "days") Traceback (most recent call last): ... ValueError: 'time_value' must be a non-negative number. >>> convert_time(1, "cool", "century") # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: Invalid unit cool is not in seconds, minutes, hours, days, weeks, ... >>> convert_time(1, "seconds", "hot") # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: Invalid unit hot is not in seconds, minutes, hours, days, weeks, ... """ if not isinstance(time_value, (int, float)) or time_value < 0: msg = "'time_value' must be a non-negative number." raise ValueError(msg) unit_from = unit_from.lower() unit_to = unit_to.lower() if unit_from not in time_chart or unit_to not in time_chart: invalid_unit = unit_from if unit_from not in time_chart else unit_to msg = f"Invalid unit {invalid_unit} is not in {', '.join(time_chart)}." raise ValueError(msg) return round( time_value * time_chart[unit_from] * time_chart_inverse[unit_to], 3, ) if __name__ == "__main__": import doctest doctest.testmod() print(f"{convert_time(3600,'seconds', 'hours') = :,}") print(f"{convert_time(360, 'days', 'months') = :,}") print(f"{convert_time(360, 'months', 'years') = :,}") print(f"{convert_time(1, 'years', 'seconds') = :,}")
Conversion of volume units. Available Units: Cubic metre,Litre,KiloLitre,Gallon,Cubic yard,Cubic foot,cup USAGE : Import this file into their respective project. Use the function lengthconversion for conversion of volume units. Parameters : value : The number of from units you want to convert fromtype : From which type you want to convert totype : To which type you want to convert REFERENCES : Wikipedia reference: https:en.wikipedia.orgwikiCubicmetre Wikipedia reference: https:en.wikipedia.orgwikiLitre Wikipedia reference: https:en.wiktionary.orgwikikilolitre Wikipedia reference: https:en.wikipedia.orgwikiGallon Wikipedia reference: https:en.wikipedia.orgwikiCubicyard Wikipedia reference: https:en.wikipedia.orgwikiCubicfoot Wikipedia reference: https:en.wikipedia.orgwikiCupunit Conversion between volume units. volumeconversion4, cubic meter, litre 4000 volumeconversion1, litre, gallon 0.264172 volumeconversion1, kilolitre, cubic meter 1 volumeconversion3, gallon, cubic yard 0.017814279 volumeconversion2, cubic yard, litre 1529.1 volumeconversion4, cubic foot, cup 473.396 volumeconversion1, cup, kilolitre 0.000236588 volumeconversion4, wrongUnit, litre Traceback most recent call last: ... ValueError: Invalid 'fromtype' value: 'wrongUnit' Supported values are: cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup
from typing import NamedTuple class FromTo(NamedTuple): from_factor: float to_factor: float METRIC_CONVERSION = { "cubic meter": FromTo(1, 1), "litre": FromTo(0.001, 1000), "kilolitre": FromTo(1, 1), "gallon": FromTo(0.00454, 264.172), "cubic yard": FromTo(0.76455, 1.30795), "cubic foot": FromTo(0.028, 35.3147), "cup": FromTo(0.000236588, 4226.75), } def volume_conversion(value: float, from_type: str, to_type: str) -> float: """ Conversion between volume units. >>> volume_conversion(4, "cubic meter", "litre") 4000 >>> volume_conversion(1, "litre", "gallon") 0.264172 >>> volume_conversion(1, "kilolitre", "cubic meter") 1 >>> volume_conversion(3, "gallon", "cubic yard") 0.017814279 >>> volume_conversion(2, "cubic yard", "litre") 1529.1 >>> volume_conversion(4, "cubic foot", "cup") 473.396 >>> volume_conversion(1, "cup", "kilolitre") 0.000236588 >>> volume_conversion(4, "wrongUnit", "litre") Traceback (most recent call last): ... ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup """ if from_type not in METRIC_CONVERSION: raise ValueError( f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" + ", ".join(METRIC_CONVERSION) ) if to_type not in METRIC_CONVERSION: raise ValueError( f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" + ", ".join(METRIC_CONVERSION) ) return ( value * METRIC_CONVERSION[from_type].from_factor * METRIC_CONVERSION[to_type].to_factor ) if __name__ == "__main__": import doctest doctest.testmod()
Conversion of weight units. author Anubhav Solanki license MIT version 1.1.0 maintainer Anubhav Solanki email anubhavsolanki0gmail.com USAGE : Import this file into their respective project. Use the function weightconversion for conversion of weight units. Parameters : fromtype : From which type you want to convert totype : To which type you want to convert value : the value which you want to convert REFERENCES : Wikipedia reference: https:en.wikipedia.orgwikiKilogram Wikipedia reference: https:en.wikipedia.orgwikiGram Wikipedia reference: https:en.wikipedia.orgwikiMillimetre Wikipedia reference: https:en.wikipedia.orgwikiTonne Wikipedia reference: https:en.wikipedia.orgwikiLongton Wikipedia reference: https:en.wikipedia.orgwikiShortton Wikipedia reference: https:en.wikipedia.orgwikiPound Wikipedia reference: https:en.wikipedia.orgwikiOunce Wikipedia reference: https:en.wikipedia.orgwikiFinenessKarat Wikipedia reference: https:en.wikipedia.orgwikiDaltonunit Wikipedia reference: https:en.wikipedia.orgwikiStoneunit Conversion of weight unit with the help of KILOGRAMCHART kilogram : 1, gram : pow10, 3, milligram : pow10, 6, metricton : pow10, 3, longton : 0.0009842073, shortton : 0.0011023122, pound : 2.2046244202, stone: 0.1574731728, ounce : 35.273990723, carrat : 5000, atomicmassunit : 6.022136652E26 weightconversionkilogram,kilogram,4 4 weightconversionkilogram,gram,1 1000 weightconversionkilogram,milligram,4 4000000 weightconversionkilogram,metricton,4 0.004 weightconversionkilogram,longton,3 0.0029526219 weightconversionkilogram,shortton,1 0.0011023122 weightconversionkilogram,pound,4 8.8184976808 weightconversionkilogram,stone,5 0.7873658640000001 weightconversionkilogram,ounce,4 141.095962892 weightconversionkilogram,carrat,3 15000 weightconversionkilogram,atomicmassunit,1 6.022136652e26 weightconversiongram,kilogram,1 0.001 weightconversiongram,gram,3 3.0 weightconversiongram,milligram,2 2000.0 weightconversiongram,metricton,4 4e06 weightconversiongram,longton,3 2.9526219e06 weightconversiongram,shortton,3 3.3069366000000003e06 weightconversiongram,pound,3 0.0066138732606 weightconversiongram,stone,4 0.0006298926912000001 weightconversiongram,ounce,1 0.035273990723 weightconversiongram,carrat,2 10.0 weightconversiongram,atomicmassunit,1 6.022136652e23 weightconversionmilligram,kilogram,1 1e06 weightconversionmilligram,gram,2 0.002 weightconversionmilligram,milligram,3 3.0 weightconversionmilligram,metricton,3 3e09 weightconversionmilligram,longton,3 2.9526219e09 weightconversionmilligram,shortton,1 1.1023122e09 weightconversionmilligram,pound,3 6.6138732605999995e06 weightconversionmilligram,ounce,2 7.054798144599999e05 weightconversionmilligram,carrat,1 0.005 weightconversionmilligram,atomicmassunit,1 6.022136652e20 weightconversionmetricton,kilogram,2 2000 weightconversionmetricton,gram,2 2000000 weightconversionmetricton,milligram,3 3000000000 weightconversionmetricton,metricton,2 2.0 weightconversionmetricton,longton,3 2.9526219 weightconversionmetricton,shortton,2 2.2046244 weightconversionmetricton,pound,3 6613.8732606 weightconversionmetricton,ounce,4 141095.96289199998 weightconversionmetricton,carrat,4 20000000 weightconversionmetricton,atomicmassunit,1 6.022136652e29 weightconversionlongton,kilogram,4 4064.18432 weightconversionlongton,gram,4 4064184.32 weightconversionlongton,milligram,3 3048138240.0 weightconversionlongton,metricton,4 4.06418432 weightconversionlongton,longton,3 2.999999907217152 weightconversionlongton,shortton,1 1.119999989746176 weightconversionlongton,pound,3 6720.000000049448 weightconversionlongton,ounce,1 35840.000000060514 weightconversionlongton,carrat,4 20320921.599999998 weightconversionlongton,atomicmassunit,4 2.4475073353955697e30 weightconversionshortton,kilogram,3 2721.5519999999997 weightconversionshortton,gram,3 2721552.0 weightconversionshortton,milligram,1 907184000.0 weightconversionshortton,metricton,4 3.628736 weightconversionshortton,longton,3 2.6785713457296 weightconversionshortton,shortton,3 2.9999999725344 weightconversionshortton,pound,2 4000.0000000294335 weightconversionshortton,ounce,4 128000.00000021611 weightconversionshortton,carrat,4 18143680.0 weightconversionshortton,atomicmassunit,1 5.463186016507968e29 weightconversionpound,kilogram,4 1.814368 weightconversionpound,gram,2 907.184 weightconversionpound,milligram,3 1360776.0 weightconversionpound,metricton,3 0.001360776 weightconversionpound,longton,2 0.0008928571152432 weightconversionpound,shortton,1 0.0004999999954224 weightconversionpound,pound,3 3.0000000000220752 weightconversionpound,ounce,1 16.000000000027015 weightconversionpound,carrat,1 2267.96 weightconversionpound,atomicmassunit,4 1.0926372033015936e27 weightconversionstone,kilogram,5 31.751450000000002 weightconversionstone,gram,2 12700.58 weightconversionstone,milligram,3 19050870.0 weightconversionstone,metricton,3 0.01905087 weightconversionstone,longton,3 0.018750005325351003 weightconversionstone,shortton,3 0.021000006421614002 weightconversionstone,pound,2 28.00000881870372 weightconversionstone,ounce,1 224.00007054835967 weightconversionstone,carrat,2 63502.9 weightconversionounce,kilogram,3 0.0850485 weightconversionounce,gram,3 85.0485 weightconversionounce,milligram,4 113398.0 weightconversionounce,metricton,4 0.000113398 weightconversionounce,longton,4 0.0001116071394054 weightconversionounce,shortton,4 0.0001249999988556 weightconversionounce,pound,1 0.0625000000004599 weightconversionounce,ounce,2 2.000000000003377 weightconversionounce,carrat,1 141.7475 weightconversionounce,atomicmassunit,1 1.70724563015874e25 weightconversioncarrat,kilogram,1 0.0002 weightconversioncarrat,gram,4 0.8 weightconversioncarrat,milligram,2 400.0 weightconversioncarrat,metricton,2 4.0000000000000003e07 weightconversioncarrat,longton,3 5.9052438e07 weightconversioncarrat,shortton,4 8.818497600000002e07 weightconversioncarrat,pound,1 0.00044092488404000004 weightconversioncarrat,ounce,2 0.0141095962892 weightconversioncarrat,carrat,4 4.0 weightconversioncarrat,atomicmassunit,4 4.8177093216e23 weightconversionatomicmassunit,kilogram,4 6.642160796e27 weightconversionatomicmassunit,gram,2 3.321080398e24 weightconversionatomicmassunit,milligram,2 3.3210803980000002e21 weightconversionatomicmassunit,metricton,3 4.9816205970000004e30 weightconversionatomicmassunit,longton,3 4.9029473573977584e30 weightconversionatomicmassunit,shortton,1 1.830433719948128e30 weightconversionatomicmassunit,pound,3 1.0982602420317504e26 weightconversionatomicmassunit,ounce,2 1.1714775914938915e25 weightconversionatomicmassunit,carrat,2 1.660540199e23 weightconversionatomicmassunit,atomicmassunit,2 1.999999998903455
KILOGRAM_CHART: dict[str, float] = { "kilogram": 1, "gram": pow(10, 3), "milligram": pow(10, 6), "metric-ton": pow(10, -3), "long-ton": 0.0009842073, "short-ton": 0.0011023122, "pound": 2.2046244202, "stone": 0.1574731728, "ounce": 35.273990723, "carrat": 5000, "atomic-mass-unit": 6.022136652e26, } WEIGHT_TYPE_CHART: dict[str, float] = { "kilogram": 1, "gram": pow(10, -3), "milligram": pow(10, -6), "metric-ton": pow(10, 3), "long-ton": 1016.04608, "short-ton": 907.184, "pound": 0.453592, "stone": 6.35029, "ounce": 0.0283495, "carrat": 0.0002, "atomic-mass-unit": 1.660540199e-27, } def weight_conversion(from_type: str, to_type: str, value: float) -> float: """ Conversion of weight unit with the help of KILOGRAM_CHART "kilogram" : 1, "gram" : pow(10, 3), "milligram" : pow(10, 6), "metric-ton" : pow(10, -3), "long-ton" : 0.0009842073, "short-ton" : 0.0011023122, "pound" : 2.2046244202, "stone": 0.1574731728, "ounce" : 35.273990723, "carrat" : 5000, "atomic-mass-unit" : 6.022136652E+26 >>> weight_conversion("kilogram","kilogram",4) 4 >>> weight_conversion("kilogram","gram",1) 1000 >>> weight_conversion("kilogram","milligram",4) 4000000 >>> weight_conversion("kilogram","metric-ton",4) 0.004 >>> weight_conversion("kilogram","long-ton",3) 0.0029526219 >>> weight_conversion("kilogram","short-ton",1) 0.0011023122 >>> weight_conversion("kilogram","pound",4) 8.8184976808 >>> weight_conversion("kilogram","stone",5) 0.7873658640000001 >>> weight_conversion("kilogram","ounce",4) 141.095962892 >>> weight_conversion("kilogram","carrat",3) 15000 >>> weight_conversion("kilogram","atomic-mass-unit",1) 6.022136652e+26 >>> weight_conversion("gram","kilogram",1) 0.001 >>> weight_conversion("gram","gram",3) 3.0 >>> weight_conversion("gram","milligram",2) 2000.0 >>> weight_conversion("gram","metric-ton",4) 4e-06 >>> weight_conversion("gram","long-ton",3) 2.9526219e-06 >>> weight_conversion("gram","short-ton",3) 3.3069366000000003e-06 >>> weight_conversion("gram","pound",3) 0.0066138732606 >>> weight_conversion("gram","stone",4) 0.0006298926912000001 >>> weight_conversion("gram","ounce",1) 0.035273990723 >>> weight_conversion("gram","carrat",2) 10.0 >>> weight_conversion("gram","atomic-mass-unit",1) 6.022136652e+23 >>> weight_conversion("milligram","kilogram",1) 1e-06 >>> weight_conversion("milligram","gram",2) 0.002 >>> weight_conversion("milligram","milligram",3) 3.0 >>> weight_conversion("milligram","metric-ton",3) 3e-09 >>> weight_conversion("milligram","long-ton",3) 2.9526219e-09 >>> weight_conversion("milligram","short-ton",1) 1.1023122e-09 >>> weight_conversion("milligram","pound",3) 6.6138732605999995e-06 >>> weight_conversion("milligram","ounce",2) 7.054798144599999e-05 >>> weight_conversion("milligram","carrat",1) 0.005 >>> weight_conversion("milligram","atomic-mass-unit",1) 6.022136652e+20 >>> weight_conversion("metric-ton","kilogram",2) 2000 >>> weight_conversion("metric-ton","gram",2) 2000000 >>> weight_conversion("metric-ton","milligram",3) 3000000000 >>> weight_conversion("metric-ton","metric-ton",2) 2.0 >>> weight_conversion("metric-ton","long-ton",3) 2.9526219 >>> weight_conversion("metric-ton","short-ton",2) 2.2046244 >>> weight_conversion("metric-ton","pound",3) 6613.8732606 >>> weight_conversion("metric-ton","ounce",4) 141095.96289199998 >>> weight_conversion("metric-ton","carrat",4) 20000000 >>> weight_conversion("metric-ton","atomic-mass-unit",1) 6.022136652e+29 >>> weight_conversion("long-ton","kilogram",4) 4064.18432 >>> weight_conversion("long-ton","gram",4) 4064184.32 >>> weight_conversion("long-ton","milligram",3) 3048138240.0 >>> weight_conversion("long-ton","metric-ton",4) 4.06418432 >>> weight_conversion("long-ton","long-ton",3) 2.999999907217152 >>> weight_conversion("long-ton","short-ton",1) 1.119999989746176 >>> weight_conversion("long-ton","pound",3) 6720.000000049448 >>> weight_conversion("long-ton","ounce",1) 35840.000000060514 >>> weight_conversion("long-ton","carrat",4) 20320921.599999998 >>> weight_conversion("long-ton","atomic-mass-unit",4) 2.4475073353955697e+30 >>> weight_conversion("short-ton","kilogram",3) 2721.5519999999997 >>> weight_conversion("short-ton","gram",3) 2721552.0 >>> weight_conversion("short-ton","milligram",1) 907184000.0 >>> weight_conversion("short-ton","metric-ton",4) 3.628736 >>> weight_conversion("short-ton","long-ton",3) 2.6785713457296 >>> weight_conversion("short-ton","short-ton",3) 2.9999999725344 >>> weight_conversion("short-ton","pound",2) 4000.0000000294335 >>> weight_conversion("short-ton","ounce",4) 128000.00000021611 >>> weight_conversion("short-ton","carrat",4) 18143680.0 >>> weight_conversion("short-ton","atomic-mass-unit",1) 5.463186016507968e+29 >>> weight_conversion("pound","kilogram",4) 1.814368 >>> weight_conversion("pound","gram",2) 907.184 >>> weight_conversion("pound","milligram",3) 1360776.0 >>> weight_conversion("pound","metric-ton",3) 0.001360776 >>> weight_conversion("pound","long-ton",2) 0.0008928571152432 >>> weight_conversion("pound","short-ton",1) 0.0004999999954224 >>> weight_conversion("pound","pound",3) 3.0000000000220752 >>> weight_conversion("pound","ounce",1) 16.000000000027015 >>> weight_conversion("pound","carrat",1) 2267.96 >>> weight_conversion("pound","atomic-mass-unit",4) 1.0926372033015936e+27 >>> weight_conversion("stone","kilogram",5) 31.751450000000002 >>> weight_conversion("stone","gram",2) 12700.58 >>> weight_conversion("stone","milligram",3) 19050870.0 >>> weight_conversion("stone","metric-ton",3) 0.01905087 >>> weight_conversion("stone","long-ton",3) 0.018750005325351003 >>> weight_conversion("stone","short-ton",3) 0.021000006421614002 >>> weight_conversion("stone","pound",2) 28.00000881870372 >>> weight_conversion("stone","ounce",1) 224.00007054835967 >>> weight_conversion("stone","carrat",2) 63502.9 >>> weight_conversion("ounce","kilogram",3) 0.0850485 >>> weight_conversion("ounce","gram",3) 85.0485 >>> weight_conversion("ounce","milligram",4) 113398.0 >>> weight_conversion("ounce","metric-ton",4) 0.000113398 >>> weight_conversion("ounce","long-ton",4) 0.0001116071394054 >>> weight_conversion("ounce","short-ton",4) 0.0001249999988556 >>> weight_conversion("ounce","pound",1) 0.0625000000004599 >>> weight_conversion("ounce","ounce",2) 2.000000000003377 >>> weight_conversion("ounce","carrat",1) 141.7475 >>> weight_conversion("ounce","atomic-mass-unit",1) 1.70724563015874e+25 >>> weight_conversion("carrat","kilogram",1) 0.0002 >>> weight_conversion("carrat","gram",4) 0.8 >>> weight_conversion("carrat","milligram",2) 400.0 >>> weight_conversion("carrat","metric-ton",2) 4.0000000000000003e-07 >>> weight_conversion("carrat","long-ton",3) 5.9052438e-07 >>> weight_conversion("carrat","short-ton",4) 8.818497600000002e-07 >>> weight_conversion("carrat","pound",1) 0.00044092488404000004 >>> weight_conversion("carrat","ounce",2) 0.0141095962892 >>> weight_conversion("carrat","carrat",4) 4.0 >>> weight_conversion("carrat","atomic-mass-unit",4) 4.8177093216e+23 >>> weight_conversion("atomic-mass-unit","kilogram",4) 6.642160796e-27 >>> weight_conversion("atomic-mass-unit","gram",2) 3.321080398e-24 >>> weight_conversion("atomic-mass-unit","milligram",2) 3.3210803980000002e-21 >>> weight_conversion("atomic-mass-unit","metric-ton",3) 4.9816205970000004e-30 >>> weight_conversion("atomic-mass-unit","long-ton",3) 4.9029473573977584e-30 >>> weight_conversion("atomic-mass-unit","short-ton",1) 1.830433719948128e-30 >>> weight_conversion("atomic-mass-unit","pound",3) 1.0982602420317504e-26 >>> weight_conversion("atomic-mass-unit","ounce",2) 1.1714775914938915e-25 >>> weight_conversion("atomic-mass-unit","carrat",2) 1.660540199e-23 >>> weight_conversion("atomic-mass-unit","atomic-mass-unit",2) 1.999999998903455 """ if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART: msg = ( f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" f"Supported values are: {', '.join(WEIGHT_TYPE_CHART)}" ) raise ValueError(msg) return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type] if __name__ == "__main__": import doctest doctest.testmod()
Find the Equilibrium Index of an Array. Reference: https:www.geeksforgeeks.orgequilibriumindexofanarray Python doctest can be run with the following command: python m doctest v equilibriumindex.py Given a sequence arr of size n, this function returns an equilibrium index if any or 1 if no equilibrium index exists. The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. Example Input: arr 7, 1, 5, 2, 4, 3, 0 Output: 3 Find the equilibrium index of an array. Args: arr listint: The input array of integers. Returns: int: The equilibrium index or 1 if no equilibrium index exists. Examples: equilibriumindex7, 1, 5, 2, 4, 3, 0 3 equilibriumindex1, 2, 3, 4, 5 1 equilibriumindex1, 1, 1, 1, 1 2 equilibriumindex2, 4, 6, 8, 10, 3 1
def equilibrium_index(arr: list[int]) -> int: """ Find the equilibrium index of an array. Args: arr (list[int]): The input array of integers. Returns: int: The equilibrium index or -1 if no equilibrium index exists. Examples: >>> equilibrium_index([-7, 1, 5, 2, -4, 3, 0]) 3 >>> equilibrium_index([1, 2, 3, 4, 5]) -1 >>> equilibrium_index([1, 1, 1, 1, 1]) 2 >>> equilibrium_index([2, 4, 6, 8, 10, 3]) -1 """ total_sum = sum(arr) left_sum = 0 for i, value in enumerate(arr): total_sum -= value if left_sum == total_sum: return i left_sum += value return -1 if __name__ == "__main__": import doctest doctest.testmod()
Given a list of integers, return elements a, b, c such that a b c 0. Args: nums: list of integers Returns: list of lists of integers where sumeachlist 0 Examples: findtripletswith0sum1, 0, 1, 2, 1, 4 1, 1, 2, 1, 0, 1 findtripletswith0sum findtripletswith0sum0, 0, 0 0, 0, 0 findtripletswith0sum1, 2, 3, 0, 1, 2, 3 3, 0, 3, 3, 1, 2, 2, 1, 3, 2, 0, 2, 1, 0, 1 Function for finding the triplets with a given sum in the array using hashing. Given a list of integers, return elements a, b, c such that a b c 0. Args: nums: list of integers Returns: list of lists of integers where sumeachlist 0 Examples: findtripletswith0sumhashing1, 0, 1, 2, 1, 4 1, 0, 1, 1, 1, 2 findtripletswith0sumhashing findtripletswith0sumhashing0, 0, 0 0, 0, 0 findtripletswith0sumhashing1, 2, 3, 0, 1, 2, 3 1, 0, 1, 3, 1, 2, 2, 0, 2, 2, 1, 3, 3, 0, 3 Time complexity: ON2 Auxiliary Space: ON Initialize the final output array with blank. Set the initial element as arri. to store second elements that can complement the final sum. current sum needed for reaching the target sum Traverse the subarray arri1:. required value for the second element Verify if the desired value exists in the set. finding triplet elements combination. Include the current element in the set for subsequent complement verification. Return all the triplet combinations.
from itertools import combinations def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: """ Given a list of integers, return elements a, b, c such that a + b + c = 0. Args: nums: list of integers Returns: list of lists of integers where sum(each_list) == 0 Examples: >>> find_triplets_with_0_sum([-1, 0, 1, 2, -1, -4]) [[-1, -1, 2], [-1, 0, 1]] >>> find_triplets_with_0_sum([]) [] >>> find_triplets_with_0_sum([0, 0, 0]) [[0, 0, 0]] >>> find_triplets_with_0_sum([1, 2, 3, 0, -1, -2, -3]) [[-3, 0, 3], [-3, 1, 2], [-2, -1, 3], [-2, 0, 2], [-1, 0, 1]] """ return [ list(x) for x in sorted({abc for abc in combinations(sorted(nums), 3) if not sum(abc)}) ] def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]: """ Function for finding the triplets with a given sum in the array using hashing. Given a list of integers, return elements a, b, c such that a + b + c = 0. Args: nums: list of integers Returns: list of lists of integers where sum(each_list) == 0 Examples: >>> find_triplets_with_0_sum_hashing([-1, 0, 1, 2, -1, -4]) [[-1, 0, 1], [-1, -1, 2]] >>> find_triplets_with_0_sum_hashing([]) [] >>> find_triplets_with_0_sum_hashing([0, 0, 0]) [[0, 0, 0]] >>> find_triplets_with_0_sum_hashing([1, 2, 3, 0, -1, -2, -3]) [[-1, 0, 1], [-3, 1, 2], [-2, 0, 2], [-2, -1, 3], [-3, 0, 3]] Time complexity: O(N^2) Auxiliary Space: O(N) """ target_sum = 0 # Initialize the final output array with blank. output_arr = [] # Set the initial element as arr[i]. for index, item in enumerate(arr[:-2]): # to store second elements that can complement the final sum. set_initialize = set() # current sum needed for reaching the target sum current_sum = target_sum - item # Traverse the subarray arr[i+1:]. for other_item in arr[index + 1 :]: # required value for the second element required_value = current_sum - other_item # Verify if the desired value exists in the set. if required_value in set_initialize: # finding triplet elements combination. combination_array = sorted([item, other_item, required_value]) if combination_array not in output_arr: output_arr.append(combination_array) # Include the current element in the set # for subsequent complement verification. set_initialize.add(other_item) # Return all the triplet combinations. return output_arr if __name__ == "__main__": from doctest import testmod testmod()
Retrieves the value of an 0indexed 1D index from a 2D array. There are two ways to retrieve values: 1. Index2DArrayIteratormatrix Iteratorint This iterator allows you to iterate through a 2D array by passing in the matrix and calling nextyouriterator. You can also use the iterator in a loop. Examples: listIndex2DArrayIteratormatrix setIndex2DArrayIteratormatrix tupleIndex2DArrayIteratormatrix sumIndex2DArrayIteratormatrix 5 in Index2DArrayIteratormatrix 2. index2darrayin1darray: listint, index: int int This function allows you to provide a 2D array and a 0indexed 1D integer index, and retrieves the integer value at that index. Python doctests can be run using this command: python3 m doctest v index2darrayin1d.py tupleIndex2DArrayIterator5, 523, 1, 34, 0 5, 523, 1, 34, 0 tupleIndex2DArrayIterator5, 523, 1, 34, 0 5, 523, 1, 34, 0 tupleIndex2DArrayIterator5, 523, 1, 34, 0 5, 523, 1, 34, 0 t Index2DArrayIterator5, 2, 25, 23, 14, 5, 324, 1, 0 tuplet 5, 2, 25, 23, 14, 5, 324, 1, 0 listt 5, 2, 25, 23, 14, 5, 324, 1, 0 sortedt 1, 0, 2, 5, 5, 14, 23, 25, 324 tuplet3 23 sumt 397 1 in t True t iterIndex2DArrayIterator5, 523, 1, 34, 0 nextt 5 nextt 523 Retrieves the value of the onedimensional index from a twodimensional array. Args: array: A 2D array of integers where all rows are the same size and all columns are the same size. index: A 1D index. Returns: int: The 0indexed value of the 1D index in the array. Examples: index2darrayin1d0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5 5 index2darrayin1d0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1 Traceback most recent call last: ... ValueError: index out of range index2darrayin1d0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 Traceback most recent call last: ... ValueError: index out of range index2darrayin1d, 0 Traceback most recent call last: ... ValueError: no items in array
from collections.abc import Iterator from dataclasses import dataclass @dataclass class Index2DArrayIterator: matrix: list[list[int]] def __iter__(self) -> Iterator[int]: """ >>> tuple(Index2DArrayIterator([[5], [-523], [-1], [34], [0]])) (5, -523, -1, 34, 0) >>> tuple(Index2DArrayIterator([[5, -523, -1], [34, 0]])) (5, -523, -1, 34, 0) >>> tuple(Index2DArrayIterator([[5, -523, -1, 34, 0]])) (5, -523, -1, 34, 0) >>> t = Index2DArrayIterator([[5, 2, 25], [23, 14, 5], [324, -1, 0]]) >>> tuple(t) (5, 2, 25, 23, 14, 5, 324, -1, 0) >>> list(t) [5, 2, 25, 23, 14, 5, 324, -1, 0] >>> sorted(t) [-1, 0, 2, 5, 5, 14, 23, 25, 324] >>> tuple(t)[3] 23 >>> sum(t) 397 >>> -1 in t True >>> t = iter(Index2DArrayIterator([[5], [-523], [-1], [34], [0]])) >>> next(t) 5 >>> next(t) -523 """ for row in self.matrix: yield from row def index_2d_array_in_1d(array: list[list[int]], index: int) -> int: """ Retrieves the value of the one-dimensional index from a two-dimensional array. Args: array: A 2D array of integers where all rows are the same size and all columns are the same size. index: A 1D index. Returns: int: The 0-indexed value of the 1D index in the array. Examples: >>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], 5) 5 >>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], -1) Traceback (most recent call last): ... ValueError: index out of range >>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], 12) Traceback (most recent call last): ... ValueError: index out of range >>> index_2d_array_in_1d([[]], 0) Traceback (most recent call last): ... ValueError: no items in array """ rows = len(array) cols = len(array[0]) if rows == 0 or cols == 0: raise ValueError("no items in array") if index < 0 or index >= rows * cols: raise ValueError("index out of range") return array[index // cols][index % cols] if __name__ == "__main__": import doctest doctest.testmod()
Given an array of integers and an integer k, find the kth largest element in the array. https:stackoverflow.comquestions251781 Partitions list based on the pivot element. This function rearranges the elements in the input list 'elements' such that all elements greater than or equal to the chosen pivot are on the right side of the pivot, and all elements smaller than the pivot are on the left side. Args: arr: The list to be partitioned low: The lower index of the list high: The higher index of the list Returns: int: The index of pivot element after partitioning Examples: partition3, 1, 4, 5, 9, 2, 6, 5, 3, 5, 0, 9 4 partition7, 1, 4, 5, 9, 2, 6, 5, 8, 0, 8 1 partition'apple', 'cherry', 'date', 'banana', 0, 3 2 partition3.1, 1.2, 5.6, 4.7, 0, 3 1 Finds the kth largest element in a list. Should deliver similar results to: python def kthlargestelementarr, position: return sortedarrposition Args: nums: The list of numbers. k: The position of the desired kth largest element. Returns: int: The kth largest element. Examples: kthlargestelement3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 3 5 kthlargestelement2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5, 1 9 kthlargestelement2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5, 2 Traceback most recent call last: ... ValueError: Invalid value of 'position' kthlargestelement9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9, 110 Traceback most recent call last: ... ValueError: Invalid value of 'position' kthlargestelement1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3, 0 Traceback most recent call last: ... ValueError: Invalid value of 'position' kthlargestelement'apple', 'cherry', 'date', 'banana', 2 'cherry' kthlargestelement3.1, 1.2, 5.6, 4.7,7.9,5,0, 2 5.6 kthlargestelement2, 5, 4, 1, 1 1 kthlargestelement, 1 1 kthlargestelement3.1, 1.2, 5.6, 4.7, 7.9, 5, 0, 1.5 Traceback most recent call last: ... ValueError: The position should be an integer kthlargestelement4, 6, 1, 2, 4 Traceback most recent call last: ... TypeError: 'tuple' object does not support item assignment
def partition(arr: list[int], low: int, high: int) -> int: """ Partitions list based on the pivot element. This function rearranges the elements in the input list 'elements' such that all elements greater than or equal to the chosen pivot are on the right side of the pivot, and all elements smaller than the pivot are on the left side. Args: arr: The list to be partitioned low: The lower index of the list high: The higher index of the list Returns: int: The index of pivot element after partitioning Examples: >>> partition([3, 1, 4, 5, 9, 2, 6, 5, 3, 5], 0, 9) 4 >>> partition([7, 1, 4, 5, 9, 2, 6, 5, 8], 0, 8) 1 >>> partition(['apple', 'cherry', 'date', 'banana'], 0, 3) 2 >>> partition([3.1, 1.2, 5.6, 4.7], 0, 3) 1 """ pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] >= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i + 1], arr[high] = arr[high], arr[i + 1] return i + 1 def kth_largest_element(arr: list[int], position: int) -> int: """ Finds the kth largest element in a list. Should deliver similar results to: ```python def kth_largest_element(arr, position): return sorted(arr)[-position] ``` Args: nums: The list of numbers. k: The position of the desired kth largest element. Returns: int: The kth largest element. Examples: >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 3) 5 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) 9 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], -2) Traceback (most recent call last): ... ValueError: Invalid value of 'position' >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 110) Traceback (most recent call last): ... ValueError: Invalid value of 'position' >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) Traceback (most recent call last): ... ValueError: Invalid value of 'position' >>> kth_largest_element(['apple', 'cherry', 'date', 'banana'], 2) 'cherry' >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2) 5.6 >>> kth_largest_element([-2, -5, -4, -1], 1) -1 >>> kth_largest_element([], 1) -1 >>> kth_largest_element([3.1, 1.2, 5.6, 4.7, 7.9, 5, 0], 1.5) Traceback (most recent call last): ... ValueError: The position should be an integer >>> kth_largest_element((4, 6, 1, 2), 4) Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment """ if not arr: return -1 if not isinstance(position, int): raise ValueError("The position should be an integer") if not 1 <= position <= len(arr): raise ValueError("Invalid value of 'position'") low, high = 0, len(arr) - 1 while low <= high: if low > len(arr) - 1 or high < 0: return -1 pivot_index = partition(arr, low, high) if pivot_index == position - 1: return arr[pivot_index] elif pivot_index > position - 1: high = pivot_index - 1 else: low = pivot_index + 1 return -1 if __name__ == "__main__": import doctest doctest.testmod()
https:www.enjoyalgorithms.comblogmedianoftwosortedarrays Find the median of two arrays. Args: nums1: The first array. nums2: The second array. Returns: The median of the two arrays. Examples: findmediansortedarrays1, 3, 2 2.0 findmediansortedarrays1, 2, 3, 4 2.5 findmediansortedarrays0, 0, 0, 0 0.0 findmediansortedarrays, Traceback most recent call last: ... ValueError: Both input arrays are empty. findmediansortedarrays, 1 1.0 findmediansortedarrays1000, 1000 0.0 findmediansortedarrays1.1, 2.2, 3.3, 4.4 2.75 Merge the arrays into a single sorted array. If the total number of elements is even, calculate the average of the two middle elements as the median.
def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays. Args: nums1: The first array. nums2: The second array. Returns: The median of the two arrays. Examples: >>> find_median_sorted_arrays([1, 3], [2]) 2.0 >>> find_median_sorted_arrays([1, 2], [3, 4]) 2.5 >>> find_median_sorted_arrays([0, 0], [0, 0]) 0.0 >>> find_median_sorted_arrays([], []) Traceback (most recent call last): ... ValueError: Both input arrays are empty. >>> find_median_sorted_arrays([], [1]) 1.0 >>> find_median_sorted_arrays([-1000], [1000]) 0.0 >>> find_median_sorted_arrays([-1.1, -2.2], [-3.3, -4.4]) -2.75 """ if not nums1 and not nums2: raise ValueError("Both input arrays are empty.") # Merge the arrays into a single sorted array. merged = sorted(nums1 + nums2) total = len(merged) if total % 2 == 1: # If the total number of elements is odd return float(merged[total // 2]) # then return the middle element # If the total number of elements is even, calculate # the average of the two middle elements as the median. middle1 = merged[total // 2 - 1] middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 if __name__ == "__main__": import doctest doctest.testmod()
https:leetcode.comproblemsmonotonicarray Check if a list is monotonic. ismonotonic1, 2, 2, 3 True ismonotonic6, 5, 4, 4 True ismonotonic1, 3, 2 False Test the function with your examples Test the function with your examples
# https://leetcode.com/problems/monotonic-array/ def is_monotonic(nums: list[int]) -> bool: """ Check if a list is monotonic. >>> is_monotonic([1, 2, 2, 3]) True >>> is_monotonic([6, 5, 4, 4]) True >>> is_monotonic([1, 3, 2]) False """ return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)) or all( nums[i] >= nums[i + 1] for i in range(len(nums) - 1) ) # Test the function with your examples if __name__ == "__main__": # Test the function with your examples print(is_monotonic([1, 2, 2, 3])) # Output: True print(is_monotonic([6, 5, 4, 4])) # Output: True print(is_monotonic([1, 3, 2])) # Output: False
!usrbinenv python3 Given an array of integers and an integer reqsum, find the number of pairs of array elements whose sum is equal to reqsum. https:practice.geeksforgeeks.orgproblemscountpairswithgivensum50220 Return the no. of pairs with sum sum pairswithsum1, 5, 7, 1, 6 2 pairswithsum1, 1, 1, 1, 1, 1, 1, 1, 2 28 pairswithsum1, 7, 6, 2, 5, 4, 3, 1, 9, 8, 7 4
#!/usr/bin/env python3 """ Given an array of integers and an integer req_sum, find the number of pairs of array elements whose sum is equal to req_sum. https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 """ from itertools import combinations def pairs_with_sum(arr: list, req_sum: int) -> int: """ Return the no. of pairs with sum "sum" >>> pairs_with_sum([1, 5, 7, 1], 6) 2 >>> pairs_with_sum([1, 1, 1, 1, 1, 1, 1, 1], 2) 28 >>> pairs_with_sum([1, 7, 6, 2, 5, 4, 3, 1, 9, 8], 7) 4 """ return len([1 for a, b in combinations(arr, 2) if a + b == req_sum]) if __name__ == "__main__": from doctest import testmod testmod()
Return all permutations. permuterecursive1, 2, 3 3, 2, 1, 2, 3, 1, 1, 3, 2, 3, 1, 2, 2, 1, 3, 1, 2, 3 Return all permutations of the given list. permutebacktrack1, 2, 3 1, 2, 3, 1, 3, 2, 2, 1, 3, 2, 3, 1, 3, 2, 1, 3, 1, 2
def permute_recursive(nums: list[int]) -> list[list[int]]: """ Return all permutations. >>> permute_recursive([1, 2, 3]) [[3, 2, 1], [2, 3, 1], [1, 3, 2], [3, 1, 2], [2, 1, 3], [1, 2, 3]] """ result: list[list[int]] = [] if len(nums) == 0: return [[]] for _ in range(len(nums)): n = nums.pop(0) permutations = permute_recursive(nums.copy()) for perm in permutations: perm.append(n) result.extend(permutations) nums.append(n) return result def permute_backtrack(nums: list[int]) -> list[list[int]]: """ Return all permutations of the given list. >>> permute_backtrack([1, 2, 3]) [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]] """ def backtrack(start: int) -> None: if start == len(nums) - 1: output.append(nums[:]) else: for i in range(start, len(nums)): nums[start], nums[i] = nums[i], nums[start] backtrack(start + 1) nums[start], nums[i] = nums[i], nums[start] # backtrack output: list[list[int]] = [] backtrack(0) return output if __name__ == "__main__": import doctest result = permute_backtrack([1, 2, 3]) print(result) doctest.testmod()
Author : Alexander Pantyukhin Date : November 3, 2022 Implement the class of prefix sum with useful functions based on it. The function returns the sum of array from the start to the end indexes. Runtime : O1 Space: O1 PrefixSum1,2,3.getsum0, 2 6 PrefixSum1,2,3.getsum1, 2 5 PrefixSum1,2,3.getsum2, 2 3 PrefixSum1,2,3.getsum2, 3 Traceback most recent call last: ... IndexError: list index out of range The function returns True if array contains the targetsum, False otherwise. Runtime : On Space: On PrefixSum1,2,3.containssum6 True PrefixSum1,2,3.containssum5 True PrefixSum1,2,3.containssum3 True PrefixSum1,2,3.containssum4 False PrefixSum1,2,3.containssum7 False PrefixSum1,2,3.containssum2 True
class PrefixSum: def __init__(self, array: list[int]) -> None: len_array = len(array) self.prefix_sum = [0] * len_array if len_array > 0: self.prefix_sum[0] = array[0] for i in range(1, len_array): self.prefix_sum[i] = self.prefix_sum[i - 1] + array[i] def get_sum(self, start: int, end: int) -> int: """ The function returns the sum of array from the start to the end indexes. Runtime : O(1) Space: O(1) >>> PrefixSum([1,2,3]).get_sum(0, 2) 6 >>> PrefixSum([1,2,3]).get_sum(1, 2) 5 >>> PrefixSum([1,2,3]).get_sum(2, 2) 3 >>> PrefixSum([1,2,3]).get_sum(2, 3) Traceback (most recent call last): ... IndexError: list index out of range """ if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def contains_sum(self, target_sum: int) -> bool: """ The function returns True if array contains the target_sum, False otherwise. Runtime : O(n) Space: O(n) >>> PrefixSum([1,2,3]).contains_sum(6) True >>> PrefixSum([1,2,3]).contains_sum(5) True >>> PrefixSum([1,2,3]).contains_sum(3) True >>> PrefixSum([1,2,3]).contains_sum(4) False >>> PrefixSum([1,2,3]).contains_sum(7) False >>> PrefixSum([1,-2,3]).contains_sum(2) True """ sums = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(sum_item) return False if __name__ == "__main__": import doctest doctest.testmod()
Calculate the Product Sum from a Special Array. reference: https:dev.tosfrasicaalgorithmsproductsumfromanarraydc6 Python doctests can be run with the following command: python m doctest v productsum.py Calculate the product sum of a special array which can contain integers or nested arrays. The product sum is obtained by adding all elements and multiplying by their respective depths. For example, in the array x, y, the product sum is x y. In the array x, y, z, the product sum is x 2 y z. In the array x, y, z, the product sum is x 2 y 3z. Example Input: 5, 2, 7, 1, 3, 6, 13, 8, 4 Output: 12 Recursively calculates the product sum of an array. The product sum of an array is defined as the sum of its elements multiplied by their respective depths. If an element is a list, its product sum is calculated recursively by multiplying the sum of its elements with its depth plus one. Args: arr: The array of integers and nested lists. depth: The current depth level. Returns: int: The product sum of the array. Examples: productsum1, 2, 3, 1 6 productsum1, 2, 3, 4, 2 8 productsum1, 2, 3, 1 6 productsum1, 2, 3, 0 0 productsum1, 2, 3, 7 42 productsum1, 2, 3, 7 42 productsum1, 2, 3, 7 42 productsum1, 1, 1 0 productsum1, 2, 1 1 productsum3.5, 1, 0.5, 1 1.5 Calculates the product sum of an array. Args: array ListUnionint, List: The array of integers and nested lists. Returns: int: The product sum of the array. Examples: productsumarray1, 2, 3 6 productsumarray1, 2, 3 11 productsumarray1, 2, 3, 4 47 productsumarray0 0 productsumarray3.5, 1, 0.5 1.5 productsumarray1, 2 1
def product_sum(arr: list[int | list], depth: int) -> int: """ Recursively calculates the product sum of an array. The product sum of an array is defined as the sum of its elements multiplied by their respective depths. If an element is a list, its product sum is calculated recursively by multiplying the sum of its elements with its depth plus one. Args: arr: The array of integers and nested lists. depth: The current depth level. Returns: int: The product sum of the array. Examples: >>> product_sum([1, 2, 3], 1) 6 >>> product_sum([-1, 2, [-3, 4]], 2) 8 >>> product_sum([1, 2, 3], -1) -6 >>> product_sum([1, 2, 3], 0) 0 >>> product_sum([1, 2, 3], 7) 42 >>> product_sum((1, 2, 3), 7) 42 >>> product_sum({1, 2, 3}, 7) 42 >>> product_sum([1, -1], 1) 0 >>> product_sum([1, -2], 1) -1 >>> product_sum([-3.5, [1, [0.5]]], 1) 1.5 """ total_sum = 0 for ele in arr: total_sum += product_sum(ele, depth + 1) if isinstance(ele, list) else ele return total_sum * depth def product_sum_array(array: list[int | list]) -> int: """ Calculates the product sum of an array. Args: array (List[Union[int, List]]): The array of integers and nested lists. Returns: int: The product sum of the array. Examples: >>> product_sum_array([1, 2, 3]) 6 >>> product_sum_array([1, [2, 3]]) 11 >>> product_sum_array([1, [2, [3, 4]]]) 47 >>> product_sum_array([0]) 0 >>> product_sum_array([-3.5, [1, [0.5]]]) 1.5 >>> product_sum_array([1, -2]) -1 """ return product_sum(array, 1) if __name__ == "__main__": import doctest doctest.testmod()
Sparse table is a data structure that allows answering range queries on a static number list, i.e. the elements do not change throughout all the queries. The implementation below will solve the problem of Range Minimum Query: Finding the minimum value of a subset L..R of a static number list. Overall time complexity: Onlogn Overall space complexity: Onlogn Wikipedia link: https:en.wikipedia.orgwikiRangeminimumquery Precompute range minimum queries with power of two length and store the precomputed values in a table. buildsparsetable8, 1, 0, 3, 4, 9, 3 8, 1, 0, 3, 4, 9, 3, 1, 0, 0, 3, 4, 3, 0, 0, 0, 0, 3, 0, 0, 0 buildsparsetable3, 1, 9 3, 1, 9, 1, 1, 0 buildsparsetable Traceback most recent call last: ... ValueError: empty number list not allowed Initialise sparsetable sparsetableji represents the minimum value of the subset of length 2 j of numberlist, starting from index i. smallest power of 2 subset length that fully covers numberlist minimum of subset of length 1 is that value itself compute the minimum value for all intervals with size 2 j while subset starting from i still have at least 2 j elements split range i, i 2 j and find minimum of 2 halves querybuildsparsetable8, 1, 0, 3, 4, 9, 3, 0, 4 0 querybuildsparsetable8, 1, 0, 3, 4, 9, 3, 4, 6 3 querybuildsparsetable3, 1, 9, 2, 2 9 querybuildsparsetable3, 1, 9, 0, 1 1 querybuildsparsetable8, 1, 0, 3, 4, 9, 3, 0, 11 Traceback most recent call last: ... IndexError: list index out of range querybuildsparsetable, 0, 0 Traceback most recent call last: ... ValueError: empty number list not allowed highest subset length of power of 2 that is within range leftbound, rightbound minimum of 2 overlapping smaller subsets: leftbound, leftbound 2 j 1 and rightbound 2 j 1, rightbound
from math import log2 def build_sparse_table(number_list: list[int]) -> list[list[int]]: """ Precompute range minimum queries with power of two length and store the precomputed values in a table. >>> build_sparse_table([8, 1, 0, 3, 4, 9, 3]) [[8, 1, 0, 3, 4, 9, 3], [1, 0, 0, 3, 4, 3, 0], [0, 0, 0, 3, 0, 0, 0]] >>> build_sparse_table([3, 1, 9]) [[3, 1, 9], [1, 1, 0]] >>> build_sparse_table([]) Traceback (most recent call last): ... ValueError: empty number list not allowed """ if not number_list: raise ValueError("empty number list not allowed") length = len(number_list) # Initialise sparse_table -- sparse_table[j][i] represents the minimum value of the # subset of length (2 ** j) of number_list, starting from index i. # smallest power of 2 subset length that fully covers number_list row = int(log2(length)) + 1 sparse_table = [[0 for i in range(length)] for j in range(row)] # minimum of subset of length 1 is that value itself for i, value in enumerate(number_list): sparse_table[0][i] = value j = 1 # compute the minimum value for all intervals with size (2 ** j) while (1 << j) <= length: i = 0 # while subset starting from i still have at least (2 ** j) elements while (i + (1 << j) - 1) < length: # split range [i, i + 2 ** j] and find minimum of 2 halves sparse_table[j][i] = min( sparse_table[j - 1][i + (1 << (j - 1))], sparse_table[j - 1][i] ) i += 1 j += 1 return sparse_table def query(sparse_table: list[list[int]], left_bound: int, right_bound: int) -> int: """ >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 0, 4) 0 >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 4, 6) 3 >>> query(build_sparse_table([3, 1, 9]), 2, 2) 9 >>> query(build_sparse_table([3, 1, 9]), 0, 1) 1 >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 0, 11) Traceback (most recent call last): ... IndexError: list index out of range >>> query(build_sparse_table([]), 0, 0) Traceback (most recent call last): ... ValueError: empty number list not allowed """ if left_bound < 0 or right_bound >= len(sparse_table[0]): raise IndexError("list index out of range") # highest subset length of power of 2 that is within range [left_bound, right_bound] j = int(log2(right_bound - left_bound + 1)) # minimum of 2 overlapping smaller subsets: # [left_bound, left_bound + 2 ** j - 1] and [right_bound - 2 ** j + 1, right_bound] return min(sparse_table[j][right_bound - (1 << j) + 1], sparse_table[j][left_bound]) if __name__ == "__main__": from doctest import testmod testmod() print(f"{query(build_sparse_table([3, 1, 9]), 2, 2) = }")
Please do not modify this file! It is published at https:norvig.comsudoku.html with only minimal changes to work with modern versions of Python. If you have improvements, please make them in a separate file. fmt: off fmt: on Convert grid to a dict of possible values, square: digits, or return False if a contradiction is detected. To start, every square can be any digit; then assign values from the grid. values s: digits for s in squares for s, d in gridvaluesgrid.items: if d in digits and not assignvalues, s, d: return False Fail if we can't assign d to square s. return values def gridvaluesgrid: Convert grid into a dict of square: char with '0' or '.' for empties. chars c for c in grid if c in digits or c in 0. assert lenchars 81 return dictzipsquares, chars def assignvalues, s, d: Eliminate d from valuess; propagate when values or places 2. Return values, except return False if a contradiction is detected. if d not in valuess: return values Already eliminated valuess valuess.replaced, 1 If a square s is reduced to one value d2, then eliminate d2 from the peers. if lenvaluess 0: return False Contradiction: removed last value elif lenvaluess 1: d2 valuess if not alleliminatevalues, s2, d2 for s2 in peerss: return False 2 If a unit u is reduced to only one place for a value d, then put it there. for u in unitss: dplaces s for s in u if d in valuess if lendplaces 0: return False Contradiction: no place for this value elif lendplaces 1: d can only be in one place in unit; assign it there if not assignvalues, dplaces0, d: return False return values def displayvalues: Display these values as a 2D grid. width 1 maxlenvaluess for s in squares line .join width 3 3 for r in rows: print .join valuesr c.centerwidth if c in 36 else for c in cols if r in CF: printline print def solvegrid: return searchparsegridgrid def someseq: Return some element of seq that is true. for e in seq: if e: return e return False def searchvalues: Using depthfirst search and propagation, try all possible values. if values is False: return False Failed earlier if alllenvaluess 1 for s in squares: return values Solved! Chose the unfilled square s with the fewest possibilities n, s minlenvaluess, s for s in squares if lenvaluess 1 return somesearchassignvalues.copy, s, d for d in valuess def solveallgrids, name, showif0.0: Display puzzles that take long enough Make a random puzzle with N or more assignments. Restart on contradictions. Note the resulting puzzle is not guaranteed to be solvable, but empirically about 99.8 of them are solvable. Some have multiple solutions. values s: digits for s in squares for s in shuffledsquares: if not assignvalues, s, random.choicevaluess: break ds valuess for s in squares if lenvaluess 1 if lends assignments and lensetds 8: return .joinvaluess if lenvaluess 1 else . for s in squares return randompuzzleassignments Give up and make a new puzzle def shuffledseq: Return a randomly shuffled copy of the input sequence. seq listseq random.shuffleseq return seq grid1 003020600900305001001806400008102900700000008006708200002609500800203009005010300 grid2 4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4...... hard1 .....6....59.....82....8....45........3........6..3.54...325..6.................. if name main: test solveallfromfileeasy50.txt, '', easy, None solveallfromfiletop95.txt, hard, None solveallfromfilehardest.txt, hardest, None solveallrandompuzzle for in range99, random, 100.0 for puzzle in grid1, grid2: , hard1: Takes 22 sec to solve on my M1 Mac. displayparsegridpuzzle start time.monotonic solvepuzzle t time.monotonic start printSolved: .5f sec t
import random import time def cross(items_a, items_b): "Cross product of elements in A and elements in B." return [a + b for a in items_a for b in items_b] digits = "123456789" rows = "ABCDEFGHI" cols = digits squares = cross(rows, cols) unitlist = ( [cross(rows, c) for c in cols] + [cross(r, cols) for r in rows] + [cross(rs, cs) for rs in ("ABC", "DEF", "GHI") for cs in ("123", "456", "789")] ) units = {s: [u for u in unitlist if s in u] for s in squares} peers = {s: set(sum(units[s], [])) - {s} for s in squares} def test(): "A set of unit tests." assert len(squares) == 81 assert len(unitlist) == 27 assert all(len(units[s]) == 3 for s in squares) assert all(len(peers[s]) == 20 for s in squares) assert units["C2"] == [ ["A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2"], ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"], ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"], ] # fmt: off assert peers["C2"] == { "A2", "B2", "D2", "E2", "F2", "G2", "H2", "I2", "C1", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "A1", "A3", "B1", "B3" } # fmt: on print("All tests pass.") def parse_grid(grid): """Convert grid to a dict of possible values, {square: digits}, or return False if a contradiction is detected.""" ## To start, every square can be any digit; then assign values from the grid. values = {s: digits for s in squares} for s, d in grid_values(grid).items(): if d in digits and not assign(values, s, d): return False ## (Fail if we can't assign d to square s.) return values def grid_values(grid): "Convert grid into a dict of {square: char} with '0' or '.' for empties." chars = [c for c in grid if c in digits or c in "0."] assert len(chars) == 81 return dict(zip(squares, chars)) def assign(values, s, d): """Eliminate all the other values (except d) from values[s] and propagate. Return values, except return False if a contradiction is detected.""" other_values = values[s].replace(d, "") if all(eliminate(values, s, d2) for d2 in other_values): return values else: return False def eliminate(values, s, d): """Eliminate d from values[s]; propagate when values or places <= 2. Return values, except return False if a contradiction is detected.""" if d not in values[s]: return values ## Already eliminated values[s] = values[s].replace(d, "") ## (1) If a square s is reduced to one value d2, then eliminate d2 from the peers. if len(values[s]) == 0: return False ## Contradiction: removed last value elif len(values[s]) == 1: d2 = values[s] if not all(eliminate(values, s2, d2) for s2 in peers[s]): return False ## (2) If a unit u is reduced to only one place for a value d, then put it there. for u in units[s]: dplaces = [s for s in u if d in values[s]] if len(dplaces) == 0: return False ## Contradiction: no place for this value elif len(dplaces) == 1: # d can only be in one place in unit; assign it there if not assign(values, dplaces[0], d): return False return values def display(values): "Display these values as a 2-D grid." width = 1 + max(len(values[s]) for s in squares) line = "+".join(["-" * (width * 3)] * 3) for r in rows: print( "".join( values[r + c].center(width) + ("|" if c in "36" else "") for c in cols ) ) if r in "CF": print(line) print() def solve(grid): return search(parse_grid(grid)) def some(seq): "Return some element of seq that is true." for e in seq: if e: return e return False def search(values): "Using depth-first search and propagation, try all possible values." if values is False: return False ## Failed earlier if all(len(values[s]) == 1 for s in squares): return values ## Solved! ## Chose the unfilled square s with the fewest possibilities n, s = min((len(values[s]), s) for s in squares if len(values[s]) > 1) return some(search(assign(values.copy(), s, d)) for d in values[s]) def solve_all(grids, name="", showif=0.0): """Attempt to solve a sequence of grids. Report results. When showif is a number of seconds, display puzzles that take longer. When showif is None, don't display any puzzles.""" def time_solve(grid): start = time.monotonic() values = solve(grid) t = time.monotonic() - start ## Display puzzles that take long enough if showif is not None and t > showif: display(grid_values(grid)) if values: display(values) print("(%.5f seconds)\n" % t) return (t, solved(values)) times, results = zip(*[time_solve(grid) for grid in grids]) if (n := len(grids)) > 1: print( "Solved %d of %d %s puzzles (avg %.2f secs (%d Hz), max %.2f secs)." % (sum(results), n, name, sum(times) / n, n / sum(times), max(times)) ) def solved(values): "A puzzle is solved if each unit is a permutation of the digits 1 to 9." def unitsolved(unit): return {values[s] for s in unit} == set(digits) return values is not False and all(unitsolved(unit) for unit in unitlist) def from_file(filename, sep="\n"): "Parse a file into a list of strings, separated by sep." return open(filename).read().strip().split(sep) # noqa: SIM115 def random_puzzle(assignments=17): """Make a random puzzle with N or more assignments. Restart on contradictions. Note the resulting puzzle is not guaranteed to be solvable, but empirically about 99.8% of them are solvable. Some have multiple solutions.""" values = {s: digits for s in squares} for s in shuffled(squares): if not assign(values, s, random.choice(values[s])): break ds = [values[s] for s in squares if len(values[s]) == 1] if len(ds) >= assignments and len(set(ds)) >= 8: return "".join(values[s] if len(values[s]) == 1 else "." for s in squares) return random_puzzle(assignments) ## Give up and make a new puzzle def shuffled(seq): "Return a randomly shuffled copy of the input sequence." seq = list(seq) random.shuffle(seq) return seq grid1 = ( "003020600900305001001806400008102900700000008006708200002609500800203009005010300" ) grid2 = ( "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......" ) hard1 = ( ".....6....59.....82....8....45........3........6..3.54...325..6.................." ) if __name__ == "__main__": test() # solve_all(from_file("easy50.txt", '========'), "easy", None) # solve_all(from_file("top95.txt"), "hard", None) # solve_all(from_file("hardest.txt"), "hardest", None) solve_all([random_puzzle() for _ in range(99)], "random", 100.0) for puzzle in (grid1, grid2): # , hard1): # Takes 22 sec to solve on my M1 Mac. display(parse_grid(puzzle)) start = time.monotonic() solve(puzzle) t = time.monotonic() - start print("Solved: %.5f sec" % t)
Implementation of an autobalanced binary tree! For doctests run following command: python3 m doctest v avltree.py For testing run: python avltree.py printleft rotation node:, node.getdata ret node.getleft assert ret is not None node.setleftret.getright ret.setrightnode h1 mymaxgetheightnode.getright, getheightnode.getleft 1 node.setheighth1 h2 mymaxgetheightret.getright, getheightret.getleft 1 ret.setheighth2 return ret def leftrotationnode: MyNode MyNode: printright rotation node:, node.getdata ret node.getright assert ret is not None node.setrightret.getleft ret.setleftnode h1 mymaxgetheightnode.getright, getheightnode.getleft 1 node.setheighth1 h2 mymaxgetheightret.getright, getheightret.getleft 1 ret.setheighth2 return ret def lrrotationnode: MyNode MyNode: r A A Br B C LR Br C RR B A Bl Br B UB Bl UB C UB Bl RR rightrotation LR leftrotation An AVL tree doctest Examples: t AVLtree t.insert4 insert:4 printstrt.replace n,n 4 t.insert2 insert:2 printstrt.replace n,n.replace n,n 4 2 t.insert3 insert:3 right rotation node: 2 left rotation node: 4 printstrt.replace n,n.replace n,n 3 2 4 t.getheight 2 t.delnode3 delete:3 printstrt.replace n,n.replace n,n 4 2
from __future__ import annotations import math import random from typing import Any class MyQueue: def __init__(self) -> None: self.data: list[Any] = [] self.head: int = 0 self.tail: int = 0 def is_empty(self) -> bool: return self.head == self.tail def push(self, data: Any) -> None: self.data.append(data) self.tail = self.tail + 1 def pop(self) -> Any: ret = self.data[self.head] self.head = self.head + 1 return ret def count(self) -> int: return self.tail - self.head def print_queue(self) -> None: print(self.data) print("**************") print(self.data[self.head : self.tail]) class MyNode: def __init__(self, data: Any) -> None: self.data = data self.left: MyNode | None = None self.right: MyNode | None = None self.height: int = 1 def get_data(self) -> Any: return self.data def get_left(self) -> MyNode | None: return self.left def get_right(self) -> MyNode | None: return self.right def get_height(self) -> int: return self.height def set_data(self, data: Any) -> None: self.data = data def set_left(self, node: MyNode | None) -> None: self.left = node def set_right(self, node: MyNode | None) -> None: self.right = node def set_height(self, height: int) -> None: self.height = height def get_height(node: MyNode | None) -> int: if node is None: return 0 return node.get_height() def my_max(a: int, b: int) -> int: if a > b: return a return b def right_rotation(node: MyNode) -> MyNode: r""" A B / \ / \ B C Bl A / \ --> / / \ Bl Br UB Br C / UB UB = unbalanced node """ print("left rotation node:", node.get_data()) ret = node.get_left() assert ret is not None node.set_left(ret.get_right()) ret.set_right(node) h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 node.set_height(h1) h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1 ret.set_height(h2) return ret def left_rotation(node: MyNode) -> MyNode: """ a mirror symmetry rotation of the left_rotation """ print("right rotation node:", node.get_data()) ret = node.get_right() assert ret is not None node.set_right(ret.get_left()) ret.set_left(node) h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 node.set_height(h1) h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1 ret.set_height(h2) return ret def lr_rotation(node: MyNode) -> MyNode: r""" A A Br / \ / \ / \ B C LR Br C RR B A / \ --> / \ --> / / \ Bl Br B UB Bl UB C \ / UB Bl RR = right_rotation LR = left_rotation """ left_child = node.get_left() assert left_child is not None node.set_left(left_rotation(left_child)) return right_rotation(node) def rl_rotation(node: MyNode) -> MyNode: right_child = node.get_right() assert right_child is not None node.set_right(right_rotation(right_child)) return left_rotation(node) def insert_node(node: MyNode | None, data: Any) -> MyNode | None: if node is None: return MyNode(data) if data < node.get_data(): node.set_left(insert_node(node.get_left(), data)) if ( get_height(node.get_left()) - get_height(node.get_right()) == 2 ): # an unbalance detected left_child = node.get_left() assert left_child is not None if ( data < left_child.get_data() ): # new node is the left child of the left child node = right_rotation(node) else: node = lr_rotation(node) else: node.set_right(insert_node(node.get_right(), data)) if get_height(node.get_right()) - get_height(node.get_left()) == 2: right_child = node.get_right() assert right_child is not None if data < right_child.get_data(): node = rl_rotation(node) else: node = left_rotation(node) h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 node.set_height(h1) return node def get_right_most(root: MyNode) -> Any: while True: right_child = root.get_right() if right_child is None: break root = right_child return root.get_data() def get_left_most(root: MyNode) -> Any: while True: left_child = root.get_left() if left_child is None: break root = left_child return root.get_data() def del_node(root: MyNode, data: Any) -> MyNode | None: left_child = root.get_left() right_child = root.get_right() if root.get_data() == data: if left_child is not None and right_child is not None: temp_data = get_left_most(right_child) root.set_data(temp_data) root.set_right(del_node(right_child, temp_data)) elif left_child is not None: root = left_child elif right_child is not None: root = right_child else: return None elif root.get_data() > data: if left_child is None: print("No such data") return root else: root.set_left(del_node(left_child, data)) else: # root.get_data() < data if right_child is None: return root else: root.set_right(del_node(right_child, data)) if get_height(right_child) - get_height(left_child) == 2: assert right_child is not None if get_height(right_child.get_right()) > get_height(right_child.get_left()): root = left_rotation(root) else: root = rl_rotation(root) elif get_height(right_child) - get_height(left_child) == -2: assert left_child is not None if get_height(left_child.get_left()) > get_height(left_child.get_right()): root = right_rotation(root) else: root = lr_rotation(root) height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1 root.set_height(height) return root class AVLtree: """ An AVL tree doctest Examples: >>> t = AVLtree() >>> t.insert(4) insert:4 >>> print(str(t).replace(" \\n","\\n")) 4 ************************************* >>> t.insert(2) insert:2 >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) 4 2 * ************************************* >>> t.insert(3) insert:3 right rotation node: 2 left rotation node: 4 >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) 3 2 4 ************************************* >>> t.get_height() 2 >>> t.del_node(3) delete:3 >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) 4 2 * ************************************* """ def __init__(self) -> None: self.root: MyNode | None = None def get_height(self) -> int: return get_height(self.root) def insert(self, data: Any) -> None: print("insert:" + str(data)) self.root = insert_node(self.root, data) def del_node(self, data: Any) -> None: print("delete:" + str(data)) if self.root is None: print("Tree is empty!") return self.root = del_node(self.root, data) def __str__( self, ) -> str: # a level traversale, gives a more intuitive look on the tree output = "" q = MyQueue() q.push(self.root) layer = self.get_height() if layer == 0: return output cnt = 0 while not q.is_empty(): node = q.pop() space = " " * int(math.pow(2, layer - 1)) output += space if node is None: output += "*" q.push(None) q.push(None) else: output += str(node.get_data()) q.push(node.get_left()) q.push(node.get_right()) output += space cnt = cnt + 1 for i in range(100): if cnt == math.pow(2, i) - 1: layer = layer - 1 if layer == 0: output += "\n*************************************" return output output += "\n" break output += "\n*************************************" return output def _test() -> None: import doctest doctest.testmod() if __name__ == "__main__": _test() t = AVLtree() lst = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) print(str(t))
Return a small binary tree with 3 nodes. binarytree BinaryTree.smalltree lenbinarytree 3 listbinarytree 1, 2, 3 Return a medium binary tree with 3 nodes. binarytree BinaryTree.mediumtree lenbinarytree 7 listbinarytree 1, 2, 3, 4, 5, 6, 7 Returns the depth of the tree BinaryTreeNode1.depth 1 BinaryTree.smalltree.depth 2 BinaryTree.mediumtree.depth 4 Returns True if the tree is full BinaryTreeNode1.isfull True BinaryTree.smalltree.isfull True BinaryTree.mediumtree.isfull False
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass @dataclass class Node: data: int left: Node | None = None right: Node | None = None def __iter__(self) -> Iterator[int]: if self.left: yield from self.left yield self.data if self.right: yield from self.right def __len__(self) -> int: return sum(1 for _ in self) def is_full(self) -> bool: if not self or (not self.left and not self.right): return True if self.left and self.right: return self.left.is_full() and self.right.is_full() return False @dataclass class BinaryTree: root: Node def __iter__(self) -> Iterator[int]: return iter(self.root) def __len__(self) -> int: return len(self.root) @classmethod def small_tree(cls) -> BinaryTree: """ Return a small binary tree with 3 nodes. >>> binary_tree = BinaryTree.small_tree() >>> len(binary_tree) 3 >>> list(binary_tree) [1, 2, 3] """ binary_tree = BinaryTree(Node(2)) binary_tree.root.left = Node(1) binary_tree.root.right = Node(3) return binary_tree @classmethod def medium_tree(cls) -> BinaryTree: """ Return a medium binary tree with 3 nodes. >>> binary_tree = BinaryTree.medium_tree() >>> len(binary_tree) 7 >>> list(binary_tree) [1, 2, 3, 4, 5, 6, 7] """ binary_tree = BinaryTree(Node(4)) binary_tree.root.left = two = Node(2) two.left = Node(1) two.right = Node(3) binary_tree.root.right = five = Node(5) five.right = six = Node(6) six.right = Node(7) return binary_tree def depth(self) -> int: """ Returns the depth of the tree >>> BinaryTree(Node(1)).depth() 1 >>> BinaryTree.small_tree().depth() 2 >>> BinaryTree.medium_tree().depth() 4 """ return self._depth(self.root) def _depth(self, node: Node | None) -> int: # noqa: UP007 if not node: return 0 return 1 + max(self._depth(node.left), self._depth(node.right)) def is_full(self) -> bool: """ Returns True if the tree is full >>> BinaryTree(Node(1)).is_full() True >>> BinaryTree.small_tree().is_full() True >>> BinaryTree.medium_tree().is_full() False """ return self.root.is_full() if __name__ == "__main__": import doctest doctest.testmod()
from future import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass from typing import Any, Self dataclass class Node: value: int left: Node None None right: Node None None parent: Node None None Added in order to delete a node easier def iterself Iteratorint: yield from self.left or yield self.value yield from self.right or def reprself str: from pprint import pformat if self.left is None and self.right is None: return strself.value return pformatfself.value: self.left, self.right, indent1 property def isrightself bool: return boolself.parent and self is self.parent.right dataclass class BinarySearchTree: root: Node None None def boolself bool: return boolself.root def iterself Iteratorint: yield from self.root or def strself str: return strself.root def reassignnodesself, node: Node, newchildren: Node None None: if newchildren is not None: reset its kids newchildren.parent node.parent if node.parent is not None: reset its parent if node.isright: If it is the right child node.parent.right newchildren else: node.parent.left newchildren else: self.root newchildren def emptyself bool: return not self.root def insertself, value None: newnode Nodevalue create a new Node if self.empty: if Tree is empty self.root newnode set its root else: Tree is not empty parentnode self.root from root if parentnode is None: return while True: While we don't get to a leaf if value parentnode.value: We go left if parentnode.left is None: parentnode.left newnode We insert the new node in a leaf break else: parentnode parentnode.left else: if parentnode.right is None: parentnode.right newnode break else: parentnode parentnode.right newnode.parent parentnode def insertself, values Self: for value in values: self.insertvalue return self def searchself, value Node None: if self.empty: raise IndexErrorWarning: Tree is empty! please use another. else: node self.root use lazy evaluation here to avoid NoneType Attribute error while node is not None and node.value is not value: node node.left if value node.value else node.right return node def getmaxself, node: Node None None Node None: if node is None: if self.root is None: return None node self.root if not self.empty: while node.right is not None: node node.right return node def getminself, node: Node None None Node None: if node is None: node self.root if self.root is None: return None if not self.empty: node self.root while node.left is not None: node node.left return node def removeself, value: int None: Look for the node with that label node self.searchvalue if node is None: msg fValue value not found raise ValueErrormsg if node.left is None and node.right is None: If it has no children self.reassignnodesnode, None elif node.left is None: Has only right children self.reassignnodesnode, node.right elif node.right is None: Has only left children self.reassignnodesnode, node.left else: predecessor self.getmax node.left Gets the max value of the left branch self.removepredecessor.value type: ignore node.value predecessor.value type: ignore Assigns the value to the node to delete and keep tree structure def preordertraverseself, node: Node None Iterable: if node is not None: yield node Preorder Traversal yield from self.preordertraversenode.left yield from self.preordertraversenode.right def traversaltreeself, traversalfunctionNone Any: if traversalfunction is None: return self.preordertraverseself.root else: return traversalfunctionself.root def inorderself, arr: list, node: Node None None: Return the kth smallest element in a binary search tree arr: listint self.inorderarr, node append all values to list using inorder traversal return arrk 1 def inordercurrnode: Node None listNode: nodelist if currnode is not None: nodelist inordercurrnode.left currnode inordercurrnode.right return nodelist def postordercurrnode: Node None listNode: nodelist if currnode is not None: nodelist postordercurrnode.left postordercurrnode.right currnode return nodelist if name main: import doctest doctest.testmodverboseTrue
r""" A binary search Tree Example 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 >>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7) >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) 8 3 1 6 4 7 10 14 13 >>> tuple(i.value for i in t.traversal_tree(inorder)) (1, 3, 4, 6, 7, 8, 10, 13, 14) >>> tuple(t) (1, 3, 4, 6, 7, 8, 10, 13, 14) >>> t.find_kth_smallest(3, t.root) 4 >>> tuple(t)[3-1] 4 >>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder))) 1 4 7 6 3 13 14 10 8 >>> t.remove(20) Traceback (most recent call last): ... ValueError: Value 20 not found >>> BinarySearchTree().search(6) Traceback (most recent call last): ... IndexError: Warning: Tree is empty! please use another. Other example: >>> testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7) >>> t = BinarySearchTree() >>> for i in testlist: ... t.insert(i) # doctest: +ELLIPSIS BinarySearchTree(root=8) BinarySearchTree(root={'8': (3, None)}) BinarySearchTree(root={'8': ({'3': (None, 6)}, None)}) BinarySearchTree(root={'8': ({'3': (1, 6)}, None)}) BinarySearchTree(root={'8': ({'3': (1, 6)}, 10)}) BinarySearchTree(root={'8': ({'3': (1, 6)}, {'10': (None, 14)})}) BinarySearchTree(root={'8': ({'3': (1, 6)}, {'10': (None, {'14': (13, None)})})}) BinarySearchTree(root={'8': ({'3': (1, {'6': (4, None)})}, {'10': (None, {'14': ... BinarySearchTree(root={'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, ... Prints all the elements of the list in order traversal >>> print(t) {'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, None)})})} Test existence >>> t.search(6) is not None True >>> 6 in t True >>> t.search(-1) is not None False >>> -1 in t False >>> t.search(6).is_right True >>> t.search(1).is_right False >>> t.get_max().value 14 >>> max(t) 14 >>> t.get_min().value 1 >>> min(t) 1 >>> t.empty() False >>> not t False >>> for i in testlist: ... t.remove(i) >>> t.empty() True >>> not t True """ from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass from typing import Any, Self @dataclass class Node: value: int left: Node | None = None right: Node | None = None parent: Node | None = None # Added in order to delete a node easier def __iter__(self) -> Iterator[int]: """ >>> list(Node(0)) [0] >>> list(Node(0, Node(-1), Node(1), None)) [-1, 0, 1] """ yield from self.left or [] yield self.value yield from self.right or [] def __repr__(self) -> str: from pprint import pformat if self.left is None and self.right is None: return str(self.value) return pformat({f"{self.value}": (self.left, self.right)}, indent=1) @property def is_right(self) -> bool: return bool(self.parent and self is self.parent.right) @dataclass class BinarySearchTree: root: Node | None = None def __bool__(self) -> bool: return bool(self.root) def __iter__(self) -> Iterator[int]: yield from self.root or [] def __str__(self) -> str: """ Return a string of all the Nodes using in order traversal """ return str(self.root) def __reassign_nodes(self, node: Node, new_children: Node | None) -> None: if new_children is not None: # reset its kids new_children.parent = node.parent if node.parent is not None: # reset its parent if node.is_right: # If it is the right child node.parent.right = new_children else: node.parent.left = new_children else: self.root = new_children def empty(self) -> bool: """ Returns True if the tree does not have any element(s). False if the tree has element(s). >>> BinarySearchTree().empty() True >>> BinarySearchTree().insert(1).empty() False >>> BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7).empty() False """ return not self.root def __insert(self, value) -> None: """ Insert a new node in Binary Search Tree with value label """ new_node = Node(value) # create a new Node if self.empty(): # if Tree is empty self.root = new_node # set its root else: # Tree is not empty parent_node = self.root # from root if parent_node is None: return while True: # While we don't get to a leaf if value < parent_node.value: # We go left if parent_node.left is None: parent_node.left = new_node # We insert the new node in a leaf break else: parent_node = parent_node.left else: if parent_node.right is None: parent_node.right = new_node break else: parent_node = parent_node.right new_node.parent = parent_node def insert(self, *values) -> Self: for value in values: self.__insert(value) return self def search(self, value) -> Node | None: """ >>> tree = BinarySearchTree().insert(10, 20, 30, 40, 50) >>> tree.search(10) {'10': (None, {'20': (None, {'30': (None, {'40': (None, 50)})})})} >>> tree.search(20) {'20': (None, {'30': (None, {'40': (None, 50)})})} >>> tree.search(30) {'30': (None, {'40': (None, 50)})} >>> tree.search(40) {'40': (None, 50)} >>> tree.search(50) 50 >>> tree.search(5) is None # element not present True >>> tree.search(0) is None # element not present True >>> tree.search(-5) is None # element not present True >>> BinarySearchTree().search(10) Traceback (most recent call last): ... IndexError: Warning: Tree is empty! please use another. """ if self.empty(): raise IndexError("Warning: Tree is empty! please use another.") else: node = self.root # use lazy evaluation here to avoid NoneType Attribute error while node is not None and node.value is not value: node = node.left if value < node.value else node.right return node def get_max(self, node: Node | None = None) -> Node | None: """ We go deep on the right branch >>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_max() 50 >>> BinarySearchTree().insert(-5, -1, 0.1, -0.3, -4.5).get_max() {'0.1': (-0.3, None)} >>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_max() {'78.3': ({'30': (1, 74.0)}, None)} >>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_max() {'783': ({'30': (1, 740)}, None)} """ if node is None: if self.root is None: return None node = self.root if not self.empty(): while node.right is not None: node = node.right return node def get_min(self, node: Node | None = None) -> Node | None: """ We go deep on the left branch >>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_min() {'10': (None, {'20': (None, {'30': (None, {'40': (None, 50)})})})} >>> BinarySearchTree().insert(-5, -1, 0, -0.3, -4.5).get_min() {'-5': (None, {'-1': (-4.5, {'0': (-0.3, None)})})} >>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_min() {'1': (None, {'78.3': ({'30': (1, 74.0)}, None)})} >>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_min() {'1': (None, {'783': ({'30': (1, 740)}, None)})} """ if node is None: node = self.root if self.root is None: return None if not self.empty(): node = self.root while node.left is not None: node = node.left return node def remove(self, value: int) -> None: # Look for the node with that label node = self.search(value) if node is None: msg = f"Value {value} not found" raise ValueError(msg) if node.left is None and node.right is None: # If it has no children self.__reassign_nodes(node, None) elif node.left is None: # Has only right children self.__reassign_nodes(node, node.right) elif node.right is None: # Has only left children self.__reassign_nodes(node, node.left) else: predecessor = self.get_max( node.left ) # Gets the max value of the left branch self.remove(predecessor.value) # type: ignore node.value = ( predecessor.value # type: ignore ) # Assigns the value to the node to delete and keep tree structure def preorder_traverse(self, node: Node | None) -> Iterable: if node is not None: yield node # Preorder Traversal yield from self.preorder_traverse(node.left) yield from self.preorder_traverse(node.right) def traversal_tree(self, traversal_function=None) -> Any: """ This function traversal the tree. You can pass a function to traversal the tree as needed by client code """ if traversal_function is None: return self.preorder_traverse(self.root) else: return traversal_function(self.root) def inorder(self, arr: list, node: Node | None) -> None: """Perform an inorder traversal and append values of the nodes to a list named arr""" if node: self.inorder(arr, node.left) arr.append(node.value) self.inorder(arr, node.right) def find_kth_smallest(self, k: int, node: Node) -> int: """Return the kth smallest element in a binary search tree""" arr: list[int] = [] self.inorder(arr, node) # append all values to list using inorder traversal return arr[k - 1] def inorder(curr_node: Node | None) -> list[Node]: """ inorder (left, self, right) """ node_list = [] if curr_node is not None: node_list = inorder(curr_node.left) + [curr_node] + inorder(curr_node.right) return node_list def postorder(curr_node: Node | None) -> list[Node]: """ postOrder (left, right, self) """ node_list = [] if curr_node is not None: node_list = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node] return node_list if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
This is a python3 implementation of binary search tree using recursion To run tests: python m unittest binarysearchtreerecursive.py To run an example: python binarysearchtreerecursive.py Empties the tree t BinarySearchTree assert t.root is None t.put8 assert t.root is not None Checks if the tree is empty t BinarySearchTree t.isempty True t.put8 t.isempty False Put a new node in the tree t BinarySearchTree t.put8 assert t.root.parent is None assert t.root.label 8 t.put10 assert t.root.right.parent t.root assert t.root.right.label 10 t.put3 assert t.root.left.parent t.root assert t.root.left.label 3 Searches a node in the tree t BinarySearchTree t.put8 t.put10 node t.search8 assert node.label 8 node t.search3 Traceback most recent call last: ... ValueError: Node with label 3 does not exist Removes a node in the tree t BinarySearchTree t.put8 t.put10 t.remove8 assert t.root.label 10 t.remove3 Traceback most recent call last: ... ValueError: Node with label 3 does not exist Checks if a node exists in the tree t BinarySearchTree t.put8 t.put10 t.exists8 True t.exists3 False Gets the max label inserted in the tree t BinarySearchTree t.getmaxlabel Traceback most recent call last: ... ValueError: Binary search tree is empty t.put8 t.put10 t.getmaxlabel 10 Gets the min label inserted in the tree t BinarySearchTree t.getminlabel Traceback most recent call last: ... ValueError: Binary search tree is empty t.put8 t.put10 t.getminlabel 8 Return the inorder traversal of the tree t BinarySearchTree i.label for i in t.inordertraversal t.put8 t.put10 t.put9 i.label for i in t.inordertraversal 8, 9, 10 Return the preorder traversal of the tree t BinarySearchTree i.label for i in t.preordertraversal t.put8 t.put10 t.put9 i.label for i in t.preordertraversal 8, 10, 9 t BinarySearchTree t.put8 t.put3 t.put6 t.put1 t.put10 t.put14 t.put13 t.put4 t.put7 t.put5 return t def testputself None: t BinarySearchTree assert t.isempty t.put8 r 8 assert t.root.right is not None assert t.root.right.parent t.root assert t.root.right.label 10 t.put3 r 8 3 10 assert t.root.left.right is not None assert t.root.left.right.parent t.root.left assert t.root.left.right.label 6 t.put1 r 8 3 10 1 6 assert t.root is not None assert t.root.right is not None assert t.root.right.right is not None assert t.root.right.right.right is None assert t.root.right.right.left is None t.remove7 r 8 3 10 1 6 14 4 5 assert t.root.left.left is not None assert t.root.left.right.right is not None assert t.root.left.left.label 1 assert t.root.left.right.label 4 assert t.root.left.right.right.label 5 assert t.root.left.right.left is None assert t.root.left.left.parent t.root.left assert t.root.left.right.parent t.root.left t.remove3 r 8 4 10 1 5 14 assert t.root.left is not None assert t.root.left.left is not None assert t.root.left.label 5 assert t.root.left.right is None assert t.root.left.left.label 1 assert t.root.left.parent t.root assert t.root.left.left.parent t.root.left def testremove2self None: t self.getbinarysearchtree t.remove3 r 8 4 10 1 6 14 5 7 13 t BinarySearchTree t.put8 t.put3 t.put6 t.put1 t.put10 t.put14 t.put13 t.put4 t.put7 t.put5 print printLabel 6 exists:, t.exists6 printLabel 13 exists:, t.exists13 printLabel 1 exists:, t.exists1 printLabel 12 exists:, t.exists12 Prints all the elements of the list in inorder traversal inordertraversalnodes i.label for i in t.inordertraversal printInorder traversal:, inordertraversalnodes Prints all the elements of the list in preorder traversal preordertraversalnodes i.label for i in t.preordertraversal printPreorder traversal:, preordertraversalnodes printMax. label:, t.getmaxlabel printMin. label:, t.getminlabel Delete elements printnDeleting elements 13, 10, 8, 3, 6, 14 print t.remove13 t.remove10 t.remove8 t.remove3 t.remove6 t.remove14 Prints all the elements of the list in inorder traversal after delete inordertraversalnodes i.label for i in t.inordertraversal printInorder traversal after delete:, inordertraversalnodes Prints all the elements of the list in preorder traversal after delete preordertraversalnodes i.label for i in t.preordertraversal printPreorder traversal after delete:, preordertraversalnodes printMax. label:, t.getmaxlabel printMin. label:, t.getminlabel if name main: binarysearchtreeexample
from __future__ import annotations import unittest from collections.abc import Iterator import pytest class Node: def __init__(self, label: int, parent: Node | None) -> None: self.label = label self.parent = parent self.left: Node | None = None self.right: Node | None = None class BinarySearchTree: def __init__(self) -> None: self.root: Node | None = None def empty(self) -> None: """ Empties the tree >>> t = BinarySearchTree() >>> assert t.root is None >>> t.put(8) >>> assert t.root is not None """ self.root = None def is_empty(self) -> bool: """ Checks if the tree is empty >>> t = BinarySearchTree() >>> t.is_empty() True >>> t.put(8) >>> t.is_empty() False """ return self.root is None def put(self, label: int) -> None: """ Put a new node in the tree >>> t = BinarySearchTree() >>> t.put(8) >>> assert t.root.parent is None >>> assert t.root.label == 8 >>> t.put(10) >>> assert t.root.right.parent == t.root >>> assert t.root.right.label == 10 >>> t.put(3) >>> assert t.root.left.parent == t.root >>> assert t.root.left.label == 3 """ self.root = self._put(self.root, label) def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node: if node is None: node = Node(label, parent) else: if label < node.label: node.left = self._put(node.left, label, node) elif label > node.label: node.right = self._put(node.right, label, node) else: msg = f"Node with label {label} already exists" raise ValueError(msg) return node def search(self, label: int) -> Node: """ Searches a node in the tree >>> t = BinarySearchTree() >>> t.put(8) >>> t.put(10) >>> node = t.search(8) >>> assert node.label == 8 >>> node = t.search(3) Traceback (most recent call last): ... ValueError: Node with label 3 does not exist """ return self._search(self.root, label) def _search(self, node: Node | None, label: int) -> Node: if node is None: msg = f"Node with label {label} does not exist" raise ValueError(msg) else: if label < node.label: node = self._search(node.left, label) elif label > node.label: node = self._search(node.right, label) return node def remove(self, label: int) -> None: """ Removes a node in the tree >>> t = BinarySearchTree() >>> t.put(8) >>> t.put(10) >>> t.remove(8) >>> assert t.root.label == 10 >>> t.remove(3) Traceback (most recent call last): ... ValueError: Node with label 3 does not exist """ node = self.search(label) if node.right and node.left: lowest_node = self._get_lowest_node(node.right) lowest_node.left = node.left lowest_node.right = node.right node.left.parent = lowest_node if node.right: node.right.parent = lowest_node self._reassign_nodes(node, lowest_node) elif not node.right and node.left: self._reassign_nodes(node, node.left) elif node.right and not node.left: self._reassign_nodes(node, node.right) else: self._reassign_nodes(node, None) def _reassign_nodes(self, node: Node, new_children: Node | None) -> None: if new_children: new_children.parent = node.parent if node.parent: if node.parent.right == node: node.parent.right = new_children else: node.parent.left = new_children else: self.root = new_children def _get_lowest_node(self, node: Node) -> Node: if node.left: lowest_node = self._get_lowest_node(node.left) else: lowest_node = node self._reassign_nodes(node, node.right) return lowest_node def exists(self, label: int) -> bool: """ Checks if a node exists in the tree >>> t = BinarySearchTree() >>> t.put(8) >>> t.put(10) >>> t.exists(8) True >>> t.exists(3) False """ try: self.search(label) return True except ValueError: return False def get_max_label(self) -> int: """ Gets the max label inserted in the tree >>> t = BinarySearchTree() >>> t.get_max_label() Traceback (most recent call last): ... ValueError: Binary search tree is empty >>> t.put(8) >>> t.put(10) >>> t.get_max_label() 10 """ if self.root is None: raise ValueError("Binary search tree is empty") node = self.root while node.right is not None: node = node.right return node.label def get_min_label(self) -> int: """ Gets the min label inserted in the tree >>> t = BinarySearchTree() >>> t.get_min_label() Traceback (most recent call last): ... ValueError: Binary search tree is empty >>> t.put(8) >>> t.put(10) >>> t.get_min_label() 8 """ if self.root is None: raise ValueError("Binary search tree is empty") node = self.root while node.left is not None: node = node.left return node.label def inorder_traversal(self) -> Iterator[Node]: """ Return the inorder traversal of the tree >>> t = BinarySearchTree() >>> [i.label for i in t.inorder_traversal()] [] >>> t.put(8) >>> t.put(10) >>> t.put(9) >>> [i.label for i in t.inorder_traversal()] [8, 9, 10] """ return self._inorder_traversal(self.root) def _inorder_traversal(self, node: Node | None) -> Iterator[Node]: if node is not None: yield from self._inorder_traversal(node.left) yield node yield from self._inorder_traversal(node.right) def preorder_traversal(self) -> Iterator[Node]: """ Return the preorder traversal of the tree >>> t = BinarySearchTree() >>> [i.label for i in t.preorder_traversal()] [] >>> t.put(8) >>> t.put(10) >>> t.put(9) >>> [i.label for i in t.preorder_traversal()] [8, 10, 9] """ return self._preorder_traversal(self.root) def _preorder_traversal(self, node: Node | None) -> Iterator[Node]: if node is not None: yield node yield from self._preorder_traversal(node.left) yield from self._preorder_traversal(node.right) class BinarySearchTreeTest(unittest.TestCase): @staticmethod def _get_binary_search_tree() -> BinarySearchTree: r""" 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 \ 5 """ t = BinarySearchTree() t.put(8) t.put(3) t.put(6) t.put(1) t.put(10) t.put(14) t.put(13) t.put(4) t.put(7) t.put(5) return t def test_put(self) -> None: t = BinarySearchTree() assert t.is_empty() t.put(8) r""" 8 """ assert t.root is not None assert t.root.parent is None assert t.root.label == 8 t.put(10) r""" 8 \ 10 """ assert t.root.right is not None assert t.root.right.parent == t.root assert t.root.right.label == 10 t.put(3) r""" 8 / \ 3 10 """ assert t.root.left is not None assert t.root.left.parent == t.root assert t.root.left.label == 3 t.put(6) r""" 8 / \ 3 10 \ 6 """ assert t.root.left.right is not None assert t.root.left.right.parent == t.root.left assert t.root.left.right.label == 6 t.put(1) r""" 8 / \ 3 10 / \ 1 6 """ assert t.root.left.left is not None assert t.root.left.left.parent == t.root.left assert t.root.left.left.label == 1 with pytest.raises(ValueError): t.put(1) def test_search(self) -> None: t = self._get_binary_search_tree() node = t.search(6) assert node.label == 6 node = t.search(13) assert node.label == 13 with pytest.raises(ValueError): t.search(2) def test_remove(self) -> None: t = self._get_binary_search_tree() t.remove(13) r""" 8 / \ 3 10 / \ \ 1 6 14 / \ 4 7 \ 5 """ assert t.root is not None assert t.root.right is not None assert t.root.right.right is not None assert t.root.right.right.right is None assert t.root.right.right.left is None t.remove(7) r""" 8 / \ 3 10 / \ \ 1 6 14 / 4 \ 5 """ assert t.root.left is not None assert t.root.left.right is not None assert t.root.left.right.left is not None assert t.root.left.right.right is None assert t.root.left.right.left.label == 4 t.remove(6) r""" 8 / \ 3 10 / \ \ 1 4 14 \ 5 """ assert t.root.left.left is not None assert t.root.left.right.right is not None assert t.root.left.left.label == 1 assert t.root.left.right.label == 4 assert t.root.left.right.right.label == 5 assert t.root.left.right.left is None assert t.root.left.left.parent == t.root.left assert t.root.left.right.parent == t.root.left t.remove(3) r""" 8 / \ 4 10 / \ \ 1 5 14 """ assert t.root is not None assert t.root.left.label == 4 assert t.root.left.right.label == 5 assert t.root.left.left.label == 1 assert t.root.left.parent == t.root assert t.root.left.left.parent == t.root.left assert t.root.left.right.parent == t.root.left t.remove(4) r""" 8 / \ 5 10 / \ 1 14 """ assert t.root.left is not None assert t.root.left.left is not None assert t.root.left.label == 5 assert t.root.left.right is None assert t.root.left.left.label == 1 assert t.root.left.parent == t.root assert t.root.left.left.parent == t.root.left def test_remove_2(self) -> None: t = self._get_binary_search_tree() t.remove(3) r""" 8 / \ 4 10 / \ \ 1 6 14 / \ / 5 7 13 """ assert t.root is not None assert t.root.left is not None assert t.root.left.left is not None assert t.root.left.right is not None assert t.root.left.right.left is not None assert t.root.left.right.right is not None assert t.root.left.label == 4 assert t.root.left.right.label == 6 assert t.root.left.left.label == 1 assert t.root.left.right.right.label == 7 assert t.root.left.right.left.label == 5 assert t.root.left.parent == t.root assert t.root.left.right.parent == t.root.left assert t.root.left.left.parent == t.root.left assert t.root.left.right.left.parent == t.root.left.right def test_empty(self) -> None: t = self._get_binary_search_tree() t.empty() assert t.root is None def test_is_empty(self) -> None: t = self._get_binary_search_tree() assert not t.is_empty() t.empty() assert t.is_empty() def test_exists(self) -> None: t = self._get_binary_search_tree() assert t.exists(6) assert not t.exists(-1) def test_get_max_label(self) -> None: t = self._get_binary_search_tree() assert t.get_max_label() == 14 t.empty() with pytest.raises(ValueError): t.get_max_label() def test_get_min_label(self) -> None: t = self._get_binary_search_tree() assert t.get_min_label() == 1 t.empty() with pytest.raises(ValueError): t.get_min_label() def test_inorder_traversal(self) -> None: t = self._get_binary_search_tree() inorder_traversal_nodes = [i.label for i in t.inorder_traversal()] assert inorder_traversal_nodes == [1, 3, 4, 5, 6, 7, 8, 10, 13, 14] def test_preorder_traversal(self) -> None: t = self._get_binary_search_tree() preorder_traversal_nodes = [i.label for i in t.preorder_traversal()] assert preorder_traversal_nodes == [8, 3, 1, 6, 4, 5, 7, 10, 14, 13] def binary_search_tree_example() -> None: r""" Example 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 \ 5 Example After Deletion 4 / \ 1 7 \ 5 """ t = BinarySearchTree() t.put(8) t.put(3) t.put(6) t.put(1) t.put(10) t.put(14) t.put(13) t.put(4) t.put(7) t.put(5) print( """ 8 / \\ 3 10 / \\ \\ 1 6 14 / \\ / 4 7 13 \\ 5 """ ) print("Label 6 exists:", t.exists(6)) print("Label 13 exists:", t.exists(13)) print("Label -1 exists:", t.exists(-1)) print("Label 12 exists:", t.exists(12)) # Prints all the elements of the list in inorder traversal inorder_traversal_nodes = [i.label for i in t.inorder_traversal()] print("Inorder traversal:", inorder_traversal_nodes) # Prints all the elements of the list in preorder traversal preorder_traversal_nodes = [i.label for i in t.preorder_traversal()] print("Preorder traversal:", preorder_traversal_nodes) print("Max. label:", t.get_max_label()) print("Min. label:", t.get_min_label()) # Delete elements print("\nDeleting elements 13, 10, 8, 3, 6, 14") print( """ 4 / \\ 1 7 \\ 5 """ ) t.remove(13) t.remove(10) t.remove(8) t.remove(3) t.remove(6) t.remove(14) # Prints all the elements of the list in inorder traversal after delete inorder_traversal_nodes = [i.label for i in t.inorder_traversal()] print("Inorder traversal after delete:", inorder_traversal_nodes) # Prints all the elements of the list in preorder traversal after delete preorder_traversal_nodes = [i.label for i in t.preorder_traversal()] print("Preorder traversal after delete:", preorder_traversal_nodes) print("Max. label:", t.get_max_label()) print("Min. label:", t.get_min_label()) if __name__ == "__main__": binary_search_tree_example()
Problem Description: Given a binary tree, return its mirror. binarytreemirror 1: 2,3, 2: 4,5, 3: 6,7, 7: 8,9, 1 1: 3, 2, 2: 5, 4, 3: 7, 6, 7: 9, 8 binarytreemirror 1: 2,3, 2: 4,5, 3: 6,7, 4: 10,11, 1 1: 3, 2, 2: 5, 4, 3: 7, 6, 4: 11, 10 binarytreemirror 1: 2,3, 2: 4,5, 3: 6,7, 4: 10,11, 5 Traceback most recent call last: ... ValueError: root 5 is not present in the binarytree binarytreemirror, 5 Traceback most recent call last: ... ValueError: binary tree cannot be empty
def binary_tree_mirror_dict(binary_tree_mirror_dictionary: dict, root: int): if not root or root not in binary_tree_mirror_dictionary: return left_child, right_child = binary_tree_mirror_dictionary[root][:2] binary_tree_mirror_dictionary[root] = [right_child, left_child] binary_tree_mirror_dict(binary_tree_mirror_dictionary, left_child) binary_tree_mirror_dict(binary_tree_mirror_dictionary, right_child) def binary_tree_mirror(binary_tree: dict, root: int = 1) -> dict: """ >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1) {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]} >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 1) {1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]} >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 5) Traceback (most recent call last): ... ValueError: root 5 is not present in the binary_tree >>> binary_tree_mirror({}, 5) Traceback (most recent call last): ... ValueError: binary tree cannot be empty """ if not binary_tree: raise ValueError("binary tree cannot be empty") if root not in binary_tree: msg = f"root {root} is not present in the binary_tree" raise ValueError(msg) binary_tree_mirror_dictionary = dict(binary_tree) binary_tree_mirror_dict(binary_tree_mirror_dictionary, root) return binary_tree_mirror_dictionary if __name__ == "__main__": binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]} print(f"Binary tree: {binary_tree}") binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 5) print(f"Binary tree mirror: {binary_tree_mirror_dictionary}")
Sum of all nodes in a binary tree. Python implementation: On time complexity Recurses through :meth:depthfirstsearch with each element. On space complexity At any point in time maximum number of stack frames that could be in memory is n A Node has a value variable and pointers to Nodes to its left and right. def initself, tree: Node None: self.tree tree def depthfirstsearchself, node: Node None int: if node is None: return 0 return node.value self.depthfirstsearchnode.left self.depthfirstsearchnode.right def iterself Iteratorint: yield self.depthfirstsearchself.tree if name main: import doctest doctest.testmod
from __future__ import annotations from collections.abc import Iterator class Node: """ A Node has a value variable and pointers to Nodes to its left and right. """ def __init__(self, value: int) -> None: self.value = value self.left: Node | None = None self.right: Node | None = None class BinaryTreeNodeSum: r""" The below tree looks like this 10 / \ 5 -3 / / \ 12 8 0 >>> tree = Node(10) >>> sum(BinaryTreeNodeSum(tree)) 10 >>> tree.left = Node(5) >>> sum(BinaryTreeNodeSum(tree)) 15 >>> tree.right = Node(-3) >>> sum(BinaryTreeNodeSum(tree)) 12 >>> tree.left.left = Node(12) >>> sum(BinaryTreeNodeSum(tree)) 24 >>> tree.right.left = Node(8) >>> tree.right.right = Node(0) >>> sum(BinaryTreeNodeSum(tree)) 32 """ def __init__(self, tree: Node) -> None: self.tree = tree def depth_first_search(self, node: Node | None) -> int: if node is None: return 0 return node.value + ( self.depth_first_search(node.left) + self.depth_first_search(node.right) ) def __iter__(self) -> Iterator[int]: yield self.depth_first_search(self.tree) if __name__ == "__main__": import doctest doctest.testmod()
Given the root of a binary tree and an integer target, find the number of paths where the sum of the values along the path equals target. Leetcode reference: https:leetcode.comproblemspathsumiii A Node has value variable and pointers to Nodes to its left and right. target: int def initself None: self.paths 0 def depthfirstsearchself, node: Node None, pathsum: int None: if node is None: return if pathsum self.target: self.paths 1 if node.left: self.depthfirstsearchnode.left, pathsum node.left.value if node.right: self.depthfirstsearchnode.right, pathsum node.right.value def pathsumself, node: Node None, target: int None None int: if node is None: return 0 if target is not None: self.target target self.depthfirstsearchnode, node.value self.pathsumnode.left self.pathsumnode.right return self.paths if name main: import doctest doctest.testmod
from __future__ import annotations class Node: """ A Node has value variable and pointers to Nodes to its left and right. """ def __init__(self, value: int) -> None: self.value = value self.left: Node | None = None self.right: Node | None = None class BinaryTreePathSum: r""" The below tree looks like this 10 / \ 5 -3 / \ \ 3 2 11 / \ \ 3 -2 1 >>> tree = Node(10) >>> tree.left = Node(5) >>> tree.right = Node(-3) >>> tree.left.left = Node(3) >>> tree.left.right = Node(2) >>> tree.right.right = Node(11) >>> tree.left.left.left = Node(3) >>> tree.left.left.right = Node(-2) >>> tree.left.right.right = Node(1) >>> BinaryTreePathSum().path_sum(tree, 8) 3 >>> BinaryTreePathSum().path_sum(tree, 7) 2 >>> tree.right.right = Node(10) >>> BinaryTreePathSum().path_sum(tree, 8) 2 """ target: int def __init__(self) -> None: self.paths = 0 def depth_first_search(self, node: Node | None, path_sum: int) -> None: if node is None: return if path_sum == self.target: self.paths += 1 if node.left: self.depth_first_search(node.left, path_sum + node.left.value) if node.right: self.depth_first_search(node.right, path_sum + node.right.value) def path_sum(self, node: Node | None, target: int | None = None) -> int: if node is None: return 0 if target is not None: self.target = target self.depth_first_search(node, node.value) self.path_sum(node.left) self.path_sum(node.right) return self.paths if __name__ == "__main__": import doctest doctest.testmod()
https:en.wikipedia.orgwikiTreetraversal tree Node1 tree.left Node2 tree.right Node3 tree.left.left Node4 tree.left.right Node5 return tree def preorderroot: Node None Generatorint, None, None: if not root: return yield root.data yield from preorderroot.left yield from preorderroot.right def postorderroot: Node None Generatorint, None, None: if not root: return yield from postorderroot.left yield from postorderroot.right yield root.data def inorderroot: Node None Generatorint, None, None: if not root: return yield from inorderroot.left yield root.data yield from inorderroot.right def reverseinorderroot: Node None Generatorint, None, None: if not root: return yield from reverseinorderroot.right yield root.data yield from reverseinorderroot.left def heightroot: Node None int: return maxheightroot.left, heightroot.right 1 if root else 0 def levelorderroot: Node None Generatorint, None, None: if root is None: return processqueue dequeroot while processqueue: node processqueue.popleft yield node.data if node.left: processqueue.appendnode.left if node.right: processqueue.appendnode.right def getnodesfromlefttoright root: Node None, level: int Generatorint, None, None: def populateoutputroot: Node None, level: int Generatorint, None, None: if not root: return if level 1: yield root.data elif level 1: yield from populateoutputroot.left, level 1 yield from populateoutputroot.right, level 1 yield from populateoutputroot, level def getnodesfromrighttoleft root: Node None, level: int Generatorint, None, None: def populateoutputroot: Node None, level: int Generatorint, None, None: if root is None: return if level 1: yield root.data elif level 1: yield from populateoutputroot.right, level 1 yield from populateoutputroot.left, level 1 yield from populateoutputroot, level def zigzagroot: Node None Generatorint, None, None: if root is None: return flag 0 heighttree heightroot for h in range1, heighttree 1: if not flag: yield from getnodesfromlefttorightroot, h flag 1 else: yield from getnodesfromrighttoleftroot, h flag 0 def main None: Main function for testing. Create binary tree. root maketree All Traversals of the binary are as follows: printfInorder Traversal: listinorderroot printfReverse Inorder Traversal: listreverseinorderroot printfPreorder Traversal: listpreorderroot printfPostorder Traversal: listpostorderroot, n printfHeight of Tree: heightroot, n printComplete Level Order Traversal: printflistlevelorderroot n printLevelwise order Traversal: for level in range1, heightroot 1: printfLevel level:, listgetnodesfromlefttorightroot, levellevel printnZigZag order Traversal: printflistzigzagroot if name main: import doctest doctest.testmod main
from __future__ import annotations from collections import deque from collections.abc import Generator from dataclasses import dataclass # https://en.wikipedia.org/wiki/Tree_traversal @dataclass class Node: data: int left: Node | None = None right: Node | None = None def make_tree() -> Node | None: r""" The below tree 1 / \ 2 3 / \ 4 5 """ tree = Node(1) tree.left = Node(2) tree.right = Node(3) tree.left.left = Node(4) tree.left.right = Node(5) return tree def preorder(root: Node | None) -> Generator[int, None, None]: """ Pre-order traversal visits root node, left subtree, right subtree. >>> list(preorder(make_tree())) [1, 2, 4, 5, 3] """ if not root: return yield root.data yield from preorder(root.left) yield from preorder(root.right) def postorder(root: Node | None) -> Generator[int, None, None]: """ Post-order traversal visits left subtree, right subtree, root node. >>> list(postorder(make_tree())) [4, 5, 2, 3, 1] """ if not root: return yield from postorder(root.left) yield from postorder(root.right) yield root.data def inorder(root: Node | None) -> Generator[int, None, None]: """ In-order traversal visits left subtree, root node, right subtree. >>> list(inorder(make_tree())) [4, 2, 5, 1, 3] """ if not root: return yield from inorder(root.left) yield root.data yield from inorder(root.right) def reverse_inorder(root: Node | None) -> Generator[int, None, None]: """ Reverse in-order traversal visits right subtree, root node, left subtree. >>> list(reverse_inorder(make_tree())) [3, 1, 5, 2, 4] """ if not root: return yield from reverse_inorder(root.right) yield root.data yield from reverse_inorder(root.left) def height(root: Node | None) -> int: """ Recursive function for calculating the height of the binary tree. >>> height(None) 0 >>> height(make_tree()) 3 """ return (max(height(root.left), height(root.right)) + 1) if root else 0 def level_order(root: Node | None) -> Generator[int, None, None]: """ Returns a list of nodes value from a whole binary tree in Level Order Traverse. Level Order traverse: Visit nodes of the tree level-by-level. """ if root is None: return process_queue = deque([root]) while process_queue: node = process_queue.popleft() yield node.data if node.left: process_queue.append(node.left) if node.right: process_queue.append(node.right) def get_nodes_from_left_to_right( root: Node | None, level: int ) -> Generator[int, None, None]: """ Returns a list of nodes value from a particular level: Left to right direction of the binary tree. """ def populate_output(root: Node | None, level: int) -> Generator[int, None, None]: if not root: return if level == 1: yield root.data elif level > 1: yield from populate_output(root.left, level - 1) yield from populate_output(root.right, level - 1) yield from populate_output(root, level) def get_nodes_from_right_to_left( root: Node | None, level: int ) -> Generator[int, None, None]: """ Returns a list of nodes value from a particular level: Right to left direction of the binary tree. """ def populate_output(root: Node | None, level: int) -> Generator[int, None, None]: if root is None: return if level == 1: yield root.data elif level > 1: yield from populate_output(root.right, level - 1) yield from populate_output(root.left, level - 1) yield from populate_output(root, level) def zigzag(root: Node | None) -> Generator[int, None, None]: """ ZigZag traverse: Returns a list of nodes value from left to right and right to left, alternatively. """ if root is None: return flag = 0 height_tree = height(root) for h in range(1, height_tree + 1): if not flag: yield from get_nodes_from_left_to_right(root, h) flag = 1 else: yield from get_nodes_from_right_to_left(root, h) flag = 0 def main() -> None: # Main function for testing. # Create binary tree. root = make_tree() # All Traversals of the binary are as follows: print(f"In-order Traversal: {list(inorder(root))}") print(f"Reverse In-order Traversal: {list(reverse_inorder(root))}") print(f"Pre-order Traversal: {list(preorder(root))}") print(f"Post-order Traversal: {list(postorder(root))}", "\n") print(f"Height of Tree: {height(root)}", "\n") print("Complete Level Order Traversal: ") print(f"{list(level_order(root))} \n") print("Level-wise order Traversal: ") for level in range(1, height(root) + 1): print(f"Level {level}:", list(get_nodes_from_left_to_right(root, level=level))) print("\nZigZag order Traversal: ") print(f"{list(zigzag(root))}") if __name__ == "__main__": import doctest doctest.testmod() main()
The diameterwidth of a tree is defined as the number of nodes on the longest path between two end nodes. root Node1 root.depth 1 root.left Node2 root.depth 2 root.left.depth 1 root.right Node3 root.depth 2 root Node1 root.diameter 1 root.left Node2 root.diameter 2 root.left.diameter 1 root.right Node3 root.diameter 3 printfroot.diameter 4 printfroot.left.diameter 3 printfroot.right.diameter 1
from __future__ import annotations from dataclasses import dataclass @dataclass class Node: data: int left: Node | None = None right: Node | None = None def depth(self) -> int: """ >>> root = Node(1) >>> root.depth() 1 >>> root.left = Node(2) >>> root.depth() 2 >>> root.left.depth() 1 >>> root.right = Node(3) >>> root.depth() 2 """ left_depth = self.left.depth() if self.left else 0 right_depth = self.right.depth() if self.right else 0 return max(left_depth, right_depth) + 1 def diameter(self) -> int: """ >>> root = Node(1) >>> root.diameter() 1 >>> root.left = Node(2) >>> root.diameter() 2 >>> root.left.diameter() 1 >>> root.right = Node(3) >>> root.diameter() 3 """ left_depth = self.left.depth() if self.left else 0 right_depth = self.right.depth() if self.right else 0 return left_depth + right_depth + 1 if __name__ == "__main__": from doctest import testmod testmod() root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) r""" Constructed binary tree is 1 / \ 2 3 / \ 4 5 """ print(f"{root.diameter() = }") # 4 print(f"{root.left.diameter() = }") # 3 print(f"{root.right.diameter() = }") # 1
from future import annotations from collections import defaultdict from dataclasses import dataclass dataclass class TreeNode: val: int left: TreeNode None None right: TreeNode None None def maketree TreeNode: return TreeNode3, TreeNode9, TreeNode20, TreeNode15, TreeNode7 def binarytreerightsideviewroot: TreeNode listint: r Function returns the right side view of binary tree. 3 3 9 20 20 15 7 7 binarytreerightsideviewmaketree 3, 20, 7 binarytreerightsideviewNone A depth first search preorder traversal to append the values at right side of tree. def depthfirstsearch root: TreeNode None, depth: int, leftview: listint None: if not root: return if depth lenleftview: leftview.appendroot.val depthfirstsearchroot.left, depth 1, leftview depthfirstsearchroot.right, depth 1, leftview leftview: list if not root: return leftview depthfirstsearchroot, 0, leftview return leftview def binarytreetopsideviewroot: TreeNode listint: r Function returns the top side view of binary tree. 9 3 20 7 3 9 20 15 7 binarytreetopsideviewmaketree 9, 3, 20, 7 binarytreetopsideviewNone A breadth first search traversal with defaultdict ds to append the values of tree from top view from collections import defaultdict def breadthfirstsearchroot: TreeNode, bottomview: listint None: queue root, 0 lookup defaultdictlist while queue: first queue.pop0 node, hd first lookuphd.appendnode.val if node.left: queue.appendnode.left, hd 1 if node.right: queue.appendnode.right, hd 1 for pair in sortedlookup.items, keylambda each: each0: bottomview.appendpair11 bottomview: list if not root: return bottomview breadthfirstsearchroot, bottomview return bottomview if name main: import doctest doctest.testmod
r""" Problem: Given root of a binary tree, return the: 1. binary-tree-right-side-view 2. binary-tree-left-side-view 3. binary-tree-top-side-view 4. binary-tree-bottom-side-view """ from __future__ import annotations from collections import defaultdict from dataclasses import dataclass @dataclass class TreeNode: val: int left: TreeNode | None = None right: TreeNode | None = None def make_tree() -> TreeNode: """ >>> make_tree().val 3 """ return TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7))) def binary_tree_right_side_view(root: TreeNode) -> list[int]: r""" Function returns the right side view of binary tree. 3 <- 3 / \ 9 20 <- 20 / \ 15 7 <- 7 >>> binary_tree_right_side_view(make_tree()) [3, 20, 7] >>> binary_tree_right_side_view(None) [] """ def depth_first_search( root: TreeNode | None, depth: int, right_view: list[int] ) -> None: """ A depth first search preorder traversal to append the values at right side of tree. """ if not root: return if depth == len(right_view): right_view.append(root.val) depth_first_search(root.right, depth + 1, right_view) depth_first_search(root.left, depth + 1, right_view) right_view: list = [] if not root: return right_view depth_first_search(root, 0, right_view) return right_view def binary_tree_left_side_view(root: TreeNode) -> list[int]: r""" Function returns the left side view of binary tree. 3 -> 3 / \ 9 -> 9 20 / \ 15 -> 15 7 >>> binary_tree_left_side_view(make_tree()) [3, 9, 15] >>> binary_tree_left_side_view(None) [] """ def depth_first_search( root: TreeNode | None, depth: int, left_view: list[int] ) -> None: """ A depth first search preorder traversal to append the values at left side of tree. """ if not root: return if depth == len(left_view): left_view.append(root.val) depth_first_search(root.left, depth + 1, left_view) depth_first_search(root.right, depth + 1, left_view) left_view: list = [] if not root: return left_view depth_first_search(root, 0, left_view) return left_view def binary_tree_top_side_view(root: TreeNode) -> list[int]: r""" Function returns the top side view of binary tree. 9 3 20 7 ⬇ ⬇ ⬇ ⬇ 3 / \ 9 20 / \ 15 7 >>> binary_tree_top_side_view(make_tree()) [9, 3, 20, 7] >>> binary_tree_top_side_view(None) [] """ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: """ A breadth first search traversal with defaultdict ds to append the values of tree from top view """ queue = [(root, 0)] lookup = defaultdict(list) while queue: first = queue.pop(0) node, hd = first lookup[hd].append(node.val) if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) for pair in sorted(lookup.items(), key=lambda each: each[0]): top_view.append(pair[1][0]) top_view: list = [] if not root: return top_view breadth_first_search(root, top_view) return top_view def binary_tree_bottom_side_view(root: TreeNode) -> list[int]: r""" Function returns the bottom side view of binary tree 3 / \ 9 20 / \ 15 7 ↑ ↑ ↑ ↑ 9 15 20 7 >>> binary_tree_bottom_side_view(make_tree()) [9, 15, 20, 7] >>> binary_tree_bottom_side_view(None) [] """ from collections import defaultdict def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: """ A breadth first search traversal with defaultdict ds to append the values of tree from bottom view """ queue = [(root, 0)] lookup = defaultdict(list) while queue: first = queue.pop(0) node, hd = first lookup[hd].append(node.val) if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) for pair in sorted(lookup.items(), key=lambda each: each[0]): bottom_view.append(pair[1][-1]) bottom_view: list = [] if not root: return bottom_view breadth_first_search(root, bottom_view) return bottom_view if __name__ == "__main__": import doctest doctest.testmod()
Author : Alexander Pantyukhin Date : November 7, 2022 Task: You are given a tree root of a binary tree with n nodes, where each node has node.data coins. There are exactly n coins in whole tree. In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent. Return the minimum number of moves required to make every node have exactly one coin. Example 1: 3 0 0 Result: 2 Example 2: 0 3 0 Result 3 leetcode: https:leetcode.comproblemsdistributecoinsinbinarytree Implementation notes: User depthfirst search approach. Let n is the number of nodes in tree Runtime: On Space: O1 distributecoinsTreeNode3, TreeNode0, TreeNode0 2 distributecoinsTreeNode0, TreeNode3, TreeNode0 3 distributecoinsTreeNode0, TreeNode0, TreeNode3 3 distributecoinsNone 0 distributecoinsTreeNode0, TreeNode0, TreeNode0 Traceback most recent call last: ... ValueError: The nodes number should be same as the number of coins distributecoinsTreeNode0, TreeNode1, TreeNode1 Traceback most recent call last: ... ValueError: The nodes number should be same as the number of coins Validation countnodesNone 0 countcoinsNone 0 Main calculation getdistribNone namedtupleCoinsDistribResult, 0 2
from __future__ import annotations from dataclasses import dataclass from typing import NamedTuple @dataclass class TreeNode: data: int left: TreeNode | None = None right: TreeNode | None = None class CoinsDistribResult(NamedTuple): moves: int excess: int def distribute_coins(root: TreeNode | None) -> int: """ >>> distribute_coins(TreeNode(3, TreeNode(0), TreeNode(0))) 2 >>> distribute_coins(TreeNode(0, TreeNode(3), TreeNode(0))) 3 >>> distribute_coins(TreeNode(0, TreeNode(0), TreeNode(3))) 3 >>> distribute_coins(None) 0 >>> distribute_coins(TreeNode(0, TreeNode(0), TreeNode(0))) Traceback (most recent call last): ... ValueError: The nodes number should be same as the number of coins >>> distribute_coins(TreeNode(0, TreeNode(1), TreeNode(1))) Traceback (most recent call last): ... ValueError: The nodes number should be same as the number of coins """ if root is None: return 0 # Validation def count_nodes(node: TreeNode | None) -> int: """ >>> count_nodes(None) 0 """ if node is None: return 0 return count_nodes(node.left) + count_nodes(node.right) + 1 def count_coins(node: TreeNode | None) -> int: """ >>> count_coins(None) 0 """ if node is None: return 0 return count_coins(node.left) + count_coins(node.right) + node.data if count_nodes(root) != count_coins(root): raise ValueError("The nodes number should be same as the number of coins") # Main calculation def get_distrib(node: TreeNode | None) -> CoinsDistribResult: """ >>> get_distrib(None) namedtuple("CoinsDistribResult", "0 2") """ if node is None: return CoinsDistribResult(0, 1) left_distrib_moves, left_distrib_excess = get_distrib(node.left) right_distrib_moves, right_distrib_excess = get_distrib(node.right) coins_to_left = 1 - left_distrib_excess coins_to_right = 1 - right_distrib_excess result_moves = ( left_distrib_moves + right_distrib_moves + abs(coins_to_left) + abs(coins_to_right) ) result_excess = node.data - coins_to_left - coins_to_right return CoinsDistribResult(result_moves, result_excess) return get_distrib(root)[0] if __name__ == "__main__": import doctest doctest.testmod()
Fenwick Tree More info: https:en.wikipedia.orgwikiFenwicktree Constructor for the Fenwick tree Parameters: arr list: list of elements to initialize the tree with optional size int: size of the Fenwick tree if arr is None Initialize the Fenwick tree with arr in ON Parameters: arr list: list of elements to initialize the tree with Returns: None a 1, 2, 3, 4, 5 f1 FenwickTreea f2 FenwickTreesizelena for index, value in enumeratea: ... f2.addindex, value f1.tree f2.tree True Get the Normal Array of the Fenwick tree in ON Returns: list: Normal Array of the Fenwick tree a i for i in range128 f FenwickTreea f.getarray a True Add a value to index in Olg N Parameters: index int: index to add value to value int: value to add to index Returns: None f FenwickTree1, 2, 3, 4, 5 f.add0, 1 f.add1, 2 f.add2, 3 f.add3, 4 f.add4, 5 f.getarray 2, 4, 6, 8, 10 Set the value of index in Olg N Parameters: index int: index to set value to value int: value to set in index Returns: None f FenwickTree5, 4, 3, 2, 1 f.update0, 1 f.update1, 2 f.update2, 3 f.update3, 4 f.update4, 5 f.getarray 1, 2, 3, 4, 5 Prefix sum of all elements in 0, right in Olg N Parameters: right int: right bound of the query exclusive Returns: int: sum of all elements in 0, right a i for i in range128 f FenwickTreea res True for i in rangelena: ... res res and f.prefixi suma:i res True Query the sum of all elements in left, right in Olg N Parameters: left int: left bound of the query inclusive right int: right bound of the query exclusive Returns: int: sum of all elements in left, right a i for i in range128 f FenwickTreea res True for i in rangelena: ... for j in rangei 1, lena: ... res res and f.queryi, j sumai:j res True Get value at index in Olg N Parameters: index int: index to get the value Returns: int: Value of element at index a i for i in range128 f FenwickTreea res True for i in rangelena: ... res res and f.geti ai res True Find the largest index with prefixi value in Olg N NOTE: Requires that all values are nonnegative! Parameters: value int: value to find the largest index of Returns: 1: if value is smaller than all elements in prefix sum int: largest index with prefixi value f FenwickTree1, 2, 0, 3, 0, 5 f.rankquery0 1 f.rankquery2 0 f.rankquery1 0 f.rankquery3 2 f.rankquery5 2 f.rankquery6 4 f.rankquery11 5
from copy import deepcopy class FenwickTree: """ Fenwick Tree More info: https://en.wikipedia.org/wiki/Fenwick_tree """ def __init__(self, arr: list[int] | None = None, size: int | None = None) -> None: """ Constructor for the Fenwick tree Parameters: arr (list): list of elements to initialize the tree with (optional) size (int): size of the Fenwick tree (if arr is None) """ if arr is None and size is not None: self.size = size self.tree = [0] * size elif arr is not None: self.init(arr) else: raise ValueError("Either arr or size must be specified") def init(self, arr: list[int]) -> None: """ Initialize the Fenwick tree with arr in O(N) Parameters: arr (list): list of elements to initialize the tree with Returns: None >>> a = [1, 2, 3, 4, 5] >>> f1 = FenwickTree(a) >>> f2 = FenwickTree(size=len(a)) >>> for index, value in enumerate(a): ... f2.add(index, value) >>> f1.tree == f2.tree True """ self.size = len(arr) self.tree = deepcopy(arr) for i in range(1, self.size): j = self.next_(i) if j < self.size: self.tree[j] += self.tree[i] def get_array(self) -> list[int]: """ Get the Normal Array of the Fenwick tree in O(N) Returns: list: Normal Array of the Fenwick tree >>> a = [i for i in range(128)] >>> f = FenwickTree(a) >>> f.get_array() == a True """ arr = self.tree[:] for i in range(self.size - 1, 0, -1): j = self.next_(i) if j < self.size: arr[j] -= arr[i] return arr @staticmethod def next_(index: int) -> int: return index + (index & (-index)) @staticmethod def prev(index: int) -> int: return index - (index & (-index)) def add(self, index: int, value: int) -> None: """ Add a value to index in O(lg N) Parameters: index (int): index to add value to value (int): value to add to index Returns: None >>> f = FenwickTree([1, 2, 3, 4, 5]) >>> f.add(0, 1) >>> f.add(1, 2) >>> f.add(2, 3) >>> f.add(3, 4) >>> f.add(4, 5) >>> f.get_array() [2, 4, 6, 8, 10] """ if index == 0: self.tree[0] += value return while index < self.size: self.tree[index] += value index = self.next_(index) def update(self, index: int, value: int) -> None: """ Set the value of index in O(lg N) Parameters: index (int): index to set value to value (int): value to set in index Returns: None >>> f = FenwickTree([5, 4, 3, 2, 1]) >>> f.update(0, 1) >>> f.update(1, 2) >>> f.update(2, 3) >>> f.update(3, 4) >>> f.update(4, 5) >>> f.get_array() [1, 2, 3, 4, 5] """ self.add(index, value - self.get(index)) def prefix(self, right: int) -> int: """ Prefix sum of all elements in [0, right) in O(lg N) Parameters: right (int): right bound of the query (exclusive) Returns: int: sum of all elements in [0, right) >>> a = [i for i in range(128)] >>> f = FenwickTree(a) >>> res = True >>> for i in range(len(a)): ... res = res and f.prefix(i) == sum(a[:i]) >>> res True """ if right == 0: return 0 result = self.tree[0] right -= 1 # make right inclusive while right > 0: result += self.tree[right] right = self.prev(right) return result def query(self, left: int, right: int) -> int: """ Query the sum of all elements in [left, right) in O(lg N) Parameters: left (int): left bound of the query (inclusive) right (int): right bound of the query (exclusive) Returns: int: sum of all elements in [left, right) >>> a = [i for i in range(128)] >>> f = FenwickTree(a) >>> res = True >>> for i in range(len(a)): ... for j in range(i + 1, len(a)): ... res = res and f.query(i, j) == sum(a[i:j]) >>> res True """ return self.prefix(right) - self.prefix(left) def get(self, index: int) -> int: """ Get value at index in O(lg N) Parameters: index (int): index to get the value Returns: int: Value of element at index >>> a = [i for i in range(128)] >>> f = FenwickTree(a) >>> res = True >>> for i in range(len(a)): ... res = res and f.get(i) == a[i] >>> res True """ return self.query(index, index + 1) def rank_query(self, value: int) -> int: """ Find the largest index with prefix(i) <= value in O(lg N) NOTE: Requires that all values are non-negative! Parameters: value (int): value to find the largest index of Returns: -1: if value is smaller than all elements in prefix sum int: largest index with prefix(i) <= value >>> f = FenwickTree([1, 2, 0, 3, 0, 5]) >>> f.rank_query(0) -1 >>> f.rank_query(2) 0 >>> f.rank_query(1) 0 >>> f.rank_query(3) 2 >>> f.rank_query(5) 2 >>> f.rank_query(6) 4 >>> f.rank_query(11) 5 """ value -= self.tree[0] if value < 0: return -1 j = 1 # Largest power of 2 <= size while j * 2 < self.size: j *= 2 i = 0 while j > 0: if i + j < self.size and self.tree[i + j] <= value: value -= self.tree[i + j] i += j j //= 2 return i if __name__ == "__main__": import doctest doctest.testmod()
Binary Tree Flattening Algorithm This code defines an algorithm to flatten a binary tree into a linked list represented using the right pointers of the tree nodes. It uses inplace flattening and demonstrates the flattening process along with a display function to visualize the flattened linked list. https:www.geeksforgeeks.orgflattenabinarytreeintolinkedlist Author: Arunkumar A Date: 04092023 A TreeNode has data variable and pointers to TreeNode objects for its left and right children. Build and return a sample binary tree. Returns: TreeNode: The root of the binary tree. Examples: root buildtree root.data 1 root.left.data 2 root.right.data 5 root.left.left.data 3 root.left.right.data 4 root.right.right.data 6 Flatten a binary tree into a linked list inplace, where the linked list is represented using the right pointers of the tree nodes. Args: root TreeNode: The root of the binary tree to be flattened. Examples: root TreeNode1 root.left TreeNode2 root.right TreeNode5 root.left.left TreeNode3 root.left.right TreeNode4 root.right.right TreeNode6 flattenroot root.data 1 root.right.right is None False root.right.right TreeNode3 root.right.right.right is None True Flatten the left subtree Save the right subtree Make the left subtree the new right subtree Find the end of the new right subtree Append the original right subtree to the end Flatten the updated right subtree Display the flattened linked list. Args: root TreeNode None: The root of the flattened linked list. Examples: root TreeNode1 root.right TreeNode2 root.right.right TreeNode3 displaylinkedlistroot 1 2 3 root None displaylinkedlistroot
from __future__ import annotations class TreeNode: """ A TreeNode has data variable and pointers to TreeNode objects for its left and right children. """ def __init__(self, data: int) -> None: self.data = data self.left: TreeNode | None = None self.right: TreeNode | None = None def build_tree() -> TreeNode: """ Build and return a sample binary tree. Returns: TreeNode: The root of the binary tree. Examples: >>> root = build_tree() >>> root.data 1 >>> root.left.data 2 >>> root.right.data 5 >>> root.left.left.data 3 >>> root.left.right.data 4 >>> root.right.right.data 6 """ root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(5) root.left.left = TreeNode(3) root.left.right = TreeNode(4) root.right.right = TreeNode(6) return root def flatten(root: TreeNode | None) -> None: """ Flatten a binary tree into a linked list in-place, where the linked list is represented using the right pointers of the tree nodes. Args: root (TreeNode): The root of the binary tree to be flattened. Examples: >>> root = TreeNode(1) >>> root.left = TreeNode(2) >>> root.right = TreeNode(5) >>> root.left.left = TreeNode(3) >>> root.left.right = TreeNode(4) >>> root.right.right = TreeNode(6) >>> flatten(root) >>> root.data 1 >>> root.right.right is None False >>> root.right.right = TreeNode(3) >>> root.right.right.right is None True """ if not root: return # Flatten the left subtree flatten(root.left) # Save the right subtree right_subtree = root.right # Make the left subtree the new right subtree root.right = root.left root.left = None # Find the end of the new right subtree current = root while current.right: current = current.right # Append the original right subtree to the end current.right = right_subtree # Flatten the updated right subtree flatten(right_subtree) def display_linked_list(root: TreeNode | None) -> None: """ Display the flattened linked list. Args: root (TreeNode | None): The root of the flattened linked list. Examples: >>> root = TreeNode(1) >>> root.right = TreeNode(2) >>> root.right.right = TreeNode(3) >>> display_linked_list(root) 1 2 3 >>> root = None >>> display_linked_list(root) """ current = root while current: if current.right is None: print(current.data, end="") break print(current.data, end=" ") current = current.right if __name__ == "__main__": print("Flattened Linked List:") root = build_tree() flatten(root) display_linked_list(root)
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.ly46uB0a2 Author : Arunkumar Date : 14th October 2023 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 Node10 root.left Node5 root.right Node20 root.left.left Node3 root.left.right Node7 root.right.left Node15 root.right.right Node25 tupleroot 3, 5, 7, 10, 15, 20, 25 floorceilingroot, 8 7, 10 floorceilingroot, 14 10, 15 floorceilingroot, 1 None, 3 floorceilingroot, 30 25, None
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()
Illustrate how to implement inorder traversal in binary search tree. Author: Gurneet Singh https:www.geeksforgeeks.orgtreetraversalsinorderpreorderandpostorder Defining the structure of BinaryTreeNode def initself, data: int None: self.data data self.leftchild: BinaryTreeNode None None self.rightchild: BinaryTreeNode None None def insertnode: BinaryTreeNode None, newvalue: int BinaryTreeNode None: if node is None: node BinaryTreeNodenewvalue return node binary search tree is not empty, so we will insert it into the tree if newvalue is less than value of data in node, add it to left subtree and proceed recursively if newvalue node.data: node.leftchild insertnode.leftchild, newvalue else: if newvalue is greater than value of data in node, add it to right subtree and proceed recursively node.rightchild insertnode.rightchild, newvalue return node def inordernode: None BinaryTreeNode listint: if node is None,return if node: inorderarray inordernode.leftchild inorderarray inorderarray, node.data inorderarray inorderarray inordernode.rightchild else: inorderarray return inorderarray def maketree BinaryTreeNode None: root insertNone, 15 insertroot, 10 insertroot, 25 insertroot, 6 insertroot, 14 insertroot, 20 insertroot, 60 return root def main None: main function root maketree printPrinting values of binary search tree in Inorder Traversal. inorderroot if name main: import doctest doctest.testmod main
class BinaryTreeNode: """Defining the structure of BinaryTreeNode""" def __init__(self, data: int) -> None: self.data = data self.left_child: BinaryTreeNode | None = None self.right_child: BinaryTreeNode | None = None def insert(node: BinaryTreeNode | None, new_value: int) -> BinaryTreeNode | None: """ If the binary search tree is empty, make a new node and declare it as root. >>> node_a = BinaryTreeNode(12345) >>> node_b = insert(node_a, 67890) >>> node_a.left_child == node_b.left_child True >>> node_a.right_child == node_b.right_child True >>> node_a.data == node_b.data True """ if node is None: node = BinaryTreeNode(new_value) return node # binary search tree is not empty, # so we will insert it into the tree # if new_value is less than value of data in node, # add it to left subtree and proceed recursively if new_value < node.data: node.left_child = insert(node.left_child, new_value) else: # if new_value is greater than value of data in node, # add it to right subtree and proceed recursively node.right_child = insert(node.right_child, new_value) return node def inorder(node: None | BinaryTreeNode) -> list[int]: # if node is None,return """ >>> inorder(make_tree()) [6, 10, 14, 15, 20, 25, 60] """ if node: inorder_array = inorder(node.left_child) inorder_array = [*inorder_array, node.data] inorder_array = inorder_array + inorder(node.right_child) else: inorder_array = [] return inorder_array def make_tree() -> BinaryTreeNode | None: root = insert(None, 15) insert(root, 10) insert(root, 25) insert(root, 6) insert(root, 14) insert(root, 20) insert(root, 60) return root def main() -> None: # main function root = make_tree() print("Printing values of binary search tree in Inorder Traversal.") inorder(root) if __name__ == "__main__": import doctest doctest.testmod() main()
Given the root of a binary tree, determine if it is a valid binary search tree BST. A valid binary search tree is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. In effect, a binary tree is a valid BST if its nodes are sorted in ascending order. leetcode: https:leetcode.comproblemsvalidatebinarysearchtree If n is the number of nodes in the tree then: Runtime: On Space: O1 root Nodedata2.1 listroot 2.1 root.leftNodedata2.0 listroot 2.0, 2.1 root.rightNodedata2.2 listroot 2.0, 2.1, 2.2 Nodedata'abc'.issorted True Nodedata2, ... leftNodedata1.999, ... rightNodedata3.issorted True Nodedata0, ... leftNodedata0, ... rightNodedata0.issorted True Nodedata0, ... leftNodedata11, ... rightNodedata3.issorted True Nodedata5, ... leftNodedata1, ... rightNodedata4, leftNodedata3.issorted False Nodedata'a', ... leftNodedata1, ... rightNodedata4, leftNodedata3.issorted Traceback most recent call last: ... TypeError: '' not supported between instances of 'str' and 'int' Nodedata2, ... leftNode, ... rightNodedata4, leftNodedata3.issorted Traceback most recent call last: ... TypeError: '' not supported between instances of 'int' and 'list'
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass @dataclass class Node: data: float left: Node | None = None right: Node | None = None def __iter__(self) -> Iterator[float]: """ >>> root = Node(data=2.1) >>> list(root) [2.1] >>> root.left=Node(data=2.0) >>> list(root) [2.0, 2.1] >>> root.right=Node(data=2.2) >>> list(root) [2.0, 2.1, 2.2] """ if self.left: yield from self.left yield self.data if self.right: yield from self.right @property def is_sorted(self) -> bool: """ >>> Node(data='abc').is_sorted True >>> Node(data=2, ... left=Node(data=1.999), ... right=Node(data=3)).is_sorted True >>> Node(data=0, ... left=Node(data=0), ... right=Node(data=0)).is_sorted True >>> Node(data=0, ... left=Node(data=-11), ... right=Node(data=3)).is_sorted True >>> Node(data=5, ... left=Node(data=1), ... right=Node(data=4, left=Node(data=3))).is_sorted False >>> Node(data='a', ... left=Node(data=1), ... right=Node(data=4, left=Node(data=3))).is_sorted Traceback (most recent call last): ... TypeError: '<' not supported between instances of 'str' and 'int' >>> Node(data=2, ... left=Node([]), ... right=Node(data=4, left=Node(data=3))).is_sorted Traceback (most recent call last): ... TypeError: '<' not supported between instances of 'int' and 'list' """ if self.left and (self.data < self.left.data or not self.left.is_sorted): return False if self.right and (self.data > self.right.data or not self.right.is_sorted): return False return True if __name__ == "__main__": import doctest doctest.testmod() tree = Node(data=2.1, left=Node(data=2.0), right=Node(data=2.2)) print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") assert tree.right tree.right.data = 2.0 print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") tree.right.data = 2.1 print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.")
Is a binary tree a sum tree where the value of every nonleaf node is equal to the sum of the values of its left and right subtrees? https:www.geeksforgeeks.orgcheckifagivenbinarytreeissumtree root Node2 listroot 2 root.left Node1 tupleroot 1, 2 root Node2 lenroot 1 root.left Node1 lenroot 2 root Node3 root.issumnode True root.left Node1 root.issumnode False root.right Node2 root.issumnode True listBinaryTree.buildatree 1, 2, 7, 11, 15, 29, 35, 40 lenBinaryTree.buildatree 8 Returns a string representation of the inorder traversal of the binary tree. strlistBinaryTree.buildatree '1, 2, 7, 11, 15, 29, 35, 40' BinaryTree.buildatree.issumtree False BinaryTree.buildasumtree.issumtree True tree BinaryTreeNode11 root tree.root root.left Node2 root.right Node29 root.left.left Node1 root.left.right Node7 root.right.left Node15 root.right.right Node40 root.right.right.left Node35 return tree classmethod def buildasumtreecls BinaryTree: r Create a binary tree with the specified structure: 26 10 3 4 6 3 listBinaryTree.buildasumtree 4, 10, 6, 26, 3, 3
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass @dataclass class Node: data: int left: Node | None = None right: Node | None = None def __iter__(self) -> Iterator[int]: """ >>> root = Node(2) >>> list(root) [2] >>> root.left = Node(1) >>> tuple(root) (1, 2) """ if self.left: yield from self.left yield self.data if self.right: yield from self.right def __len__(self) -> int: """ >>> root = Node(2) >>> len(root) 1 >>> root.left = Node(1) >>> len(root) 2 """ return sum(1 for _ in self) @property def is_sum_node(self) -> bool: """ >>> root = Node(3) >>> root.is_sum_node True >>> root.left = Node(1) >>> root.is_sum_node False >>> root.right = Node(2) >>> root.is_sum_node True """ if not self.left and not self.right: return True # leaf nodes are considered sum nodes left_sum = sum(self.left) if self.left else 0 right_sum = sum(self.right) if self.right else 0 return all( ( self.data == left_sum + right_sum, self.left.is_sum_node if self.left else True, self.right.is_sum_node if self.right else True, ) ) @dataclass class BinaryTree: root: Node def __iter__(self) -> Iterator[int]: """ >>> list(BinaryTree.build_a_tree()) [1, 2, 7, 11, 15, 29, 35, 40] """ return iter(self.root) def __len__(self) -> int: """ >>> len(BinaryTree.build_a_tree()) 8 """ return len(self.root) def __str__(self) -> str: """ Returns a string representation of the inorder traversal of the binary tree. >>> str(list(BinaryTree.build_a_tree())) '[1, 2, 7, 11, 15, 29, 35, 40]' """ return str(list(self)) @property def is_sum_tree(self) -> bool: """ >>> BinaryTree.build_a_tree().is_sum_tree False >>> BinaryTree.build_a_sum_tree().is_sum_tree True """ return self.root.is_sum_node @classmethod def build_a_tree(cls) -> BinaryTree: r""" Create a binary tree with the specified structure: 11 / \ 2 29 / \ / \ 1 7 15 40 \ 35 >>> list(BinaryTree.build_a_tree()) [1, 2, 7, 11, 15, 29, 35, 40] """ tree = BinaryTree(Node(11)) root = tree.root root.left = Node(2) root.right = Node(29) root.left.left = Node(1) root.left.right = Node(7) root.right.left = Node(15) root.right.right = Node(40) root.right.right.left = Node(35) return tree @classmethod def build_a_sum_tree(cls) -> BinaryTree: r""" Create a binary tree with the specified structure: 26 / \ 10 3 / \ \ 4 6 3 >>> list(BinaryTree.build_a_sum_tree()) [4, 10, 6, 26, 3, 3] """ tree = BinaryTree(Node(26)) root = tree.root root.left = Node(10) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(6) root.right.right = Node(3) return tree if __name__ == "__main__": from doctest import testmod testmod() tree = BinaryTree.build_a_tree() print(f"{tree} has {len(tree)} nodes and {tree.is_sum_tree = }.") tree = BinaryTree.build_a_sum_tree() print(f"{tree} has {len(tree)} nodes and {tree.is_sum_tree = }.")
approximate the overall size of segment tree with given value create array to store lazy update segmenttree SegmentTree15 segmenttree.left1 2 segmenttree.left2 4 segmenttree.left12 24 segmenttree SegmentTree15 segmenttree.right1 3 segmenttree.right2 5 segmenttree.right12 25 update with Olg n Normal segment tree without lazy update will take Onlg n for each update update1, 1, size, a, b, v for update val v to a,b query with Olg n query1, 1, size, a, b for query max of a,b A 1, 2, 4, 7, 3, 5, 6, 11, 20, 9, 14, 15, 5, 2, 8 segmenttree SegmentTree15 segmenttree.build1, 1, 15, A segmenttree.query1, 1, 15, 4, 6 7 segmenttree.query1, 1, 15, 7, 11 14 segmenttree.query1, 1, 15, 7, 12 15
from __future__ import annotations import math class SegmentTree: def __init__(self, size: int) -> None: self.size = size # approximate the overall size of segment tree with given value self.segment_tree = [0 for i in range(4 * size)] # create array to store lazy update self.lazy = [0 for i in range(4 * size)] self.flag = [0 for i in range(4 * size)] # flag for lazy update def left(self, idx: int) -> int: """ >>> segment_tree = SegmentTree(15) >>> segment_tree.left(1) 2 >>> segment_tree.left(2) 4 >>> segment_tree.left(12) 24 """ return idx * 2 def right(self, idx: int) -> int: """ >>> segment_tree = SegmentTree(15) >>> segment_tree.right(1) 3 >>> segment_tree.right(2) 5 >>> segment_tree.right(12) 25 """ return idx * 2 + 1 def build( self, idx: int, left_element: int, right_element: int, a: list[int] ) -> None: if left_element == right_element: self.segment_tree[idx] = a[left_element - 1] else: mid = (left_element + right_element) // 2 self.build(self.left(idx), left_element, mid, a) self.build(self.right(idx), mid + 1, right_element, a) self.segment_tree[idx] = max( self.segment_tree[self.left(idx)], self.segment_tree[self.right(idx)] ) def update( self, idx: int, left_element: int, right_element: int, a: int, b: int, val: int ) -> bool: """ update with O(lg n) (Normal segment tree without lazy update will take O(nlg n) for each update) update(1, 1, size, a, b, v) for update val v to [a,b] """ if self.flag[idx] is True: self.segment_tree[idx] = self.lazy[idx] self.flag[idx] = False if left_element != right_element: self.lazy[self.left(idx)] = self.lazy[idx] self.lazy[self.right(idx)] = self.lazy[idx] self.flag[self.left(idx)] = True self.flag[self.right(idx)] = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: self.segment_tree[idx] = val if left_element != right_element: self.lazy[self.left(idx)] = val self.lazy[self.right(idx)] = val self.flag[self.left(idx)] = True self.flag[self.right(idx)] = True return True mid = (left_element + right_element) // 2 self.update(self.left(idx), left_element, mid, a, b, val) self.update(self.right(idx), mid + 1, right_element, a, b, val) self.segment_tree[idx] = max( self.segment_tree[self.left(idx)], self.segment_tree[self.right(idx)] ) return True # query with O(lg n) def query( self, idx: int, left_element: int, right_element: int, a: int, b: int ) -> int | float: """ query(1, 1, size, a, b) for query max of [a,b] >>> A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] >>> segment_tree = SegmentTree(15) >>> segment_tree.build(1, 1, 15, A) >>> segment_tree.query(1, 1, 15, 4, 6) 7 >>> segment_tree.query(1, 1, 15, 7, 11) 14 >>> segment_tree.query(1, 1, 15, 7, 12) 15 """ if self.flag[idx] is True: self.segment_tree[idx] = self.lazy[idx] self.flag[idx] = False if left_element != right_element: self.lazy[self.left(idx)] = self.lazy[idx] self.lazy[self.right(idx)] = self.lazy[idx] self.flag[self.left(idx)] = True self.flag[self.right(idx)] = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] mid = (left_element + right_element) // 2 q1 = self.query(self.left(idx), left_element, mid, a, b) q2 = self.query(self.right(idx), mid + 1, right_element, a, b) return max(q1, q2) def __str__(self) -> str: return str([self.query(1, 1, self.size, i, i) for i in range(1, self.size + 1)]) if __name__ == "__main__": A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] size = 15 segt = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 111) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 235) print(segt)
https:en.wikipedia.orgwikiLowestcommonancestor https:en.wikipedia.orgwikiBreadthfirstsearch Return a tuple b, a when given two integers a and b swap2,3 3, 2 swap3,4 4, 3 swap67, 12 12, 67 creating sparse table which saves each nodes 2ith parent returns lca of node u,v u must be deeper in the tree than v making depth of u same as depth of v at the same depth if uv that mean lca is found moving both nodes upwards till lca in found returning longest common ancestor of u,v runs a breadth first search from root node of the tree sets every nodes direct parent parent of root node is set to 0 calculates depth of each node from root node initializing with 0 initializing with 1 which means every node is unvisited
# https://en.wikipedia.org/wiki/Lowest_common_ancestor # https://en.wikipedia.org/wiki/Breadth-first_search from __future__ import annotations from queue import Queue def swap(a: int, b: int) -> tuple[int, int]: """ Return a tuple (b, a) when given two integers a and b >>> swap(2,3) (3, 2) >>> swap(3,4) (4, 3) >>> swap(67, 12) (12, 67) """ a ^= b b ^= a a ^= b return a, b def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: """ creating sparse table which saves each nodes 2^i-th parent """ j = 1 while (1 << j) < max_node: for i in range(1, max_node + 1): parent[j][i] = parent[j - 1][parent[j - 1][i]] j += 1 return parent # returns lca of node u,v def lowest_common_ancestor( u: int, v: int, level: list[int], parent: list[list[int]] ) -> int: # u must be deeper in the tree than v if level[u] < level[v]: u, v = swap(u, v) # making depth of u same as depth of v for i in range(18, -1, -1): if level[u] - (1 << i) >= level[v]: u = parent[i][u] # at the same depth if u==v that mean lca is found if u == v: return u # moving both nodes upwards till lca in found for i in range(18, -1, -1): if parent[i][u] not in [0, parent[i][v]]: u, v = parent[i][u], parent[i][v] # returning longest common ancestor of u,v return parent[0][u] # runs a breadth first search from root node of the tree def breadth_first_search( level: list[int], parent: list[list[int]], max_node: int, graph: dict[int, list[int]], root: int = 1, ) -> tuple[list[int], list[list[int]]]: """ sets every nodes direct parent parent of root node is set to 0 calculates depth of each node from root node """ level[root] = 0 q: Queue[int] = Queue(maxsize=max_node) q.put(root) while q.qsize() != 0: u = q.get() for v in graph[u]: if level[v] == -1: level[v] = level[u] + 1 q.put(v) parent[0][v] = u return level, parent def main() -> None: max_node = 13 # initializing with 0 parent = [[0 for _ in range(max_node + 10)] for _ in range(20)] # initializing with -1 which means every node is unvisited level = [-1 for _ in range(max_node + 10)] graph: dict[int, list[int]] = { 1: [2, 3, 4], 2: [5], 3: [6, 7], 4: [8], 5: [9, 10], 6: [11], 7: [], 8: [12, 13], 9: [], 10: [], 11: [], 12: [], 13: [], } level, parent = breadth_first_search(level, parent, max_node, graph, 1) parent = create_sparse(max_node, parent) print("LCA of node 1 and 3 is: ", lowest_common_ancestor(1, 3, level, parent)) print("LCA of node 5 and 6 is: ", lowest_common_ancestor(5, 6, level, parent)) print("LCA of node 7 and 11 is: ", lowest_common_ancestor(7, 11, level, parent)) print("LCA of node 6 and 7 is: ", lowest_common_ancestor(6, 7, level, parent)) print("LCA of node 4 and 12 is: ", lowest_common_ancestor(4, 12, level, parent)) print("LCA of node 8 and 8 is: ", lowest_common_ancestor(8, 8, level, parent)) if __name__ == "__main__": main()
Maximum Fenwick Tree More info: https:cpalgorithms.comdatastructuresfenwick.html ft MaxFenwickTree5 ft.query0, 5 0 ft.update4, 100 ft.query0, 5 100 ft.update4, 0 ft.update2, 20 ft.query0, 5 20 ft.update4, 10 ft.query2, 5 20 ft.query1, 5 20 ft.update2, 0 ft.query0, 5 10 ft MaxFenwickTree10000 ft.update255, 30 ft.query0, 10000 30 ft MaxFenwickTree6 ft.update5, 1 ft.query5, 6 1 ft MaxFenwickTree6 ft.update0, 1000 ft.query0, 1 1000 Create empty Maximum Fenwick Tree with specified size Parameters: size: size of Array Returns: None Get next index in O1 Get previous index in O1 Set index to value in Olg2 N Parameters: index: index to update value: value to set Returns: None Answer the query of maximum range l, r in Olg2 N Parameters: left: left index of query range inclusive right: right index of query range exclusive Returns: Maximum value of range left, right
class MaxFenwickTree: """ Maximum Fenwick Tree More info: https://cp-algorithms.com/data_structures/fenwick.html --------- >>> ft = MaxFenwickTree(5) >>> ft.query(0, 5) 0 >>> ft.update(4, 100) >>> ft.query(0, 5) 100 >>> ft.update(4, 0) >>> ft.update(2, 20) >>> ft.query(0, 5) 20 >>> ft.update(4, 10) >>> ft.query(2, 5) 20 >>> ft.query(1, 5) 20 >>> ft.update(2, 0) >>> ft.query(0, 5) 10 >>> ft = MaxFenwickTree(10000) >>> ft.update(255, 30) >>> ft.query(0, 10000) 30 >>> ft = MaxFenwickTree(6) >>> ft.update(5, 1) >>> ft.query(5, 6) 1 >>> ft = MaxFenwickTree(6) >>> ft.update(0, 1000) >>> ft.query(0, 1) 1000 """ def __init__(self, size: int) -> None: """ Create empty Maximum Fenwick Tree with specified size Parameters: size: size of Array Returns: None """ self.size = size self.arr = [0] * size self.tree = [0] * size @staticmethod def get_next(index: int) -> int: """ Get next index in O(1) """ return index | (index + 1) @staticmethod def get_prev(index: int) -> int: """ Get previous index in O(1) """ return (index & (index + 1)) - 1 def update(self, index: int, value: int) -> None: """ Set index to value in O(lg^2 N) Parameters: index: index to update value: value to set Returns: None """ self.arr[index] = value while index < self.size: current_left_border = self.get_prev(index) + 1 if current_left_border == index: self.tree[index] = value else: self.tree[index] = max(value, current_left_border, index) index = self.get_next(index) def query(self, left: int, right: int) -> int: """ Answer the query of maximum range [l, r) in O(lg^2 N) Parameters: left: left index of query range (inclusive) right: right index of query range (exclusive) Returns: Maximum value of range [left, right) """ right -= 1 # Because of right is exclusive result = 0 while left <= right: current_left = self.get_prev(right) if left <= current_left: result = max(result, self.tree[right]) right = current_left else: result = max(result, self.arr[right]) right -= 1 return result if __name__ == "__main__": import doctest doctest.testmod()
!usrlocalbinpython3 Problem Description: Given two binary tree, return the merged tree. The rule for merging is that if two nodes overlap, then put the value sum of both nodes to the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. A binary node has value variable and pointers to its left and right node. Returns root node of the merged tree. tree1 Node5 tree1.left Node6 tree1.right Node7 tree1.left.left Node2 tree2 Node4 tree2.left Node5 tree2.right Node8 tree2.left.right Node1 tree2.right.right Node4 mergedtree mergetwobinarytreestree1, tree2 printpreordermergedtree 9 11 2 1 15 4 Print preorder traversal of the tree. root Node1 root.left Node2 root.right Node3 printpreorderroot 1 2 3 printpreorderroot.right 3
#!/usr/local/bin/python3 """ Problem Description: Given two binary tree, return the merged tree. The rule for merging is that if two nodes overlap, then put the value sum of both nodes to the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. """ from __future__ import annotations class Node: """ A binary node has value variable and pointers to its left and right node. """ def __init__(self, value: int = 0) -> None: self.value = value self.left: Node | None = None self.right: Node | None = None def merge_two_binary_trees(tree1: Node | None, tree2: Node | None) -> Node | None: """ Returns root node of the merged tree. >>> tree1 = Node(5) >>> tree1.left = Node(6) >>> tree1.right = Node(7) >>> tree1.left.left = Node(2) >>> tree2 = Node(4) >>> tree2.left = Node(5) >>> tree2.right = Node(8) >>> tree2.left.right = Node(1) >>> tree2.right.right = Node(4) >>> merged_tree = merge_two_binary_trees(tree1, tree2) >>> print_preorder(merged_tree) 9 11 2 1 15 4 """ if tree1 is None: return tree2 if tree2 is None: return tree1 tree1.value = tree1.value + tree2.value tree1.left = merge_two_binary_trees(tree1.left, tree2.left) tree1.right = merge_two_binary_trees(tree1.right, tree2.right) return tree1 def print_preorder(root: Node | None) -> None: """ Print pre-order traversal of the tree. >>> root = Node(1) >>> root.left = Node(2) >>> root.right = Node(3) >>> print_preorder(root) 1 2 3 >>> print_preorder(root.right) 3 """ if root: print(root.value) print_preorder(root.left) print_preorder(root.right) if __name__ == "__main__": tree1 = Node(1) tree1.left = Node(2) tree1.right = Node(3) tree1.left.left = Node(4) tree2 = Node(2) tree2.left = Node(4) tree2.right = Node(6) tree2.left.right = Node(9) tree2.right.right = Node(5) print("Tree1 is: ") print_preorder(tree1) print("Tree2 is: ") print_preorder(tree2) merged_tree = merge_two_binary_trees(tree1, tree2) print("Merged Tree is: ") print_preorder(merged_tree)
Given the root of a binary tree, mirror the tree, and return its root. Leetcode problem reference: https:leetcode.comproblemsmirrorbinarytree A Node has value variable and pointers to Nodes to its left and right. Mirror the binary tree rooted at this node by swapping left and right children. tree Node0 listtree 0 listtree.mirror 0 tree Node1, Node0, Node3, Node2, Node4, None, Node5 tupletree 0, 1, 2, 3, 4, 5 tupletree.mirror 5, 4, 3, 2, 1, 0 tree Node1 tree.left Node2 tree.right Node3 tree.left.left Node4 tree.left.right Node5 tree.right.left Node6 tree.right.right Node7 return tree def maketreenine Node: r Return a binary tree with 9 nodes that looks like this: 1 2 3 4 5 6 7 8 9 treenine maketreenine lentreenine 9 listtreenine 7, 4, 8, 2, 5, 9, 1, 3, 6 trees zero: Node0, seven: maketreeseven, nine: maketreenine for name, tree in trees.items: printf The name tree: tupletree 0, 4, 2, 5, 1, 6, 3, 7 7, 4, 8, 2, 5, 9, 1, 3, 6 printfMirror of name tree: tupletree.mirror 0, 7, 3, 6, 1, 5, 2, 4 6, 3, 1, 9, 5, 2, 8, 4, 7 if name main: import doctest doctest.testmod main
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass @dataclass class Node: """ A Node has value variable and pointers to Nodes to its left and right. """ value: int left: Node | None = None right: Node | None = None def __iter__(self) -> Iterator[int]: if self.left: yield from self.left yield self.value if self.right: yield from self.right def __len__(self) -> int: return sum(1 for _ in self) def mirror(self) -> Node: """ Mirror the binary tree rooted at this node by swapping left and right children. >>> tree = Node(0) >>> list(tree) [0] >>> list(tree.mirror()) [0] >>> tree = Node(1, Node(0), Node(3, Node(2), Node(4, None, Node(5)))) >>> tuple(tree) (0, 1, 2, 3, 4, 5) >>> tuple(tree.mirror()) (5, 4, 3, 2, 1, 0) """ self.left, self.right = self.right, self.left if self.left: self.left.mirror() if self.right: self.right.mirror() return self def make_tree_seven() -> Node: r""" Return a binary tree with 7 nodes that looks like this: 1 / \ 2 3 / \ / \ 4 5 6 7 >>> tree_seven = make_tree_seven() >>> len(tree_seven) 7 >>> list(tree_seven) [4, 2, 5, 1, 6, 3, 7] """ tree = Node(1) tree.left = Node(2) tree.right = Node(3) tree.left.left = Node(4) tree.left.right = Node(5) tree.right.left = Node(6) tree.right.right = Node(7) return tree def make_tree_nine() -> Node: r""" Return a binary tree with 9 nodes that looks like this: 1 / \ 2 3 / \ \ 4 5 6 / \ \ 7 8 9 >>> tree_nine = make_tree_nine() >>> len(tree_nine) 9 >>> list(tree_nine) [7, 4, 8, 2, 5, 9, 1, 3, 6] """ tree = Node(1) tree.left = Node(2) tree.right = Node(3) tree.left.left = Node(4) tree.left.right = Node(5) tree.right.right = Node(6) tree.left.left.left = Node(7) tree.left.left.right = Node(8) tree.left.right.right = Node(9) return tree def main() -> None: r""" Mirror binary trees with the given root and returns the root >>> tree = make_tree_nine() >>> tuple(tree) (7, 4, 8, 2, 5, 9, 1, 3, 6) >>> tuple(tree.mirror()) (6, 3, 1, 9, 5, 2, 8, 4, 7) nine_tree: 1 / \ 2 3 / \ \ 4 5 6 / \ \ 7 8 9 The mirrored tree looks like this: 1 / \ 3 2 / / \ 6 5 4 / / \ 9 8 7 """ trees = {"zero": Node(0), "seven": make_tree_seven(), "nine": make_tree_nine()} for name, tree in trees.items(): print(f" The {name} tree: {tuple(tree)}") # (0,) # (4, 2, 5, 1, 6, 3, 7) # (7, 4, 8, 2, 5, 9, 1, 3, 6) print(f"Mirror of {name} tree: {tuple(tree.mirror())}") # (0,) # (7, 3, 6, 1, 5, 2, 4) # (6, 3, 1, 9, 5, 2, 8, 4, 7) if __name__ == "__main__": import doctest doctest.testmod() main()
A nonrecursive Segment Tree implementation with range query and single element update, works virtually with any list of the same type of elements with a commutative combiner. Explanation: https:www.geeksforgeeks.orgiterativesegmenttreerangeminimumquery https:www.geeksforgeeks.orgsegmenttreeefficientimplementation SegmentTree1, 2, 3, lambda a, b: a b.query0, 2 6 SegmentTree3, 1, 2, min.query0, 2 1 SegmentTree2, 3, 1, max.query0, 2 3 st SegmentTree1, 5, 7, 1, 6, lambda a, b: a b st.update1, 1 st.update2, 3 st.query1, 2 2 st.query1, 1 1 st.update4, 1 st.query3, 4 0 st SegmentTree1, 2, 3, 3, 2, 1, 1, 1, 1, lambda a, b: ai bi for i ... in rangelena st.query0, 1 4, 4, 4 st.query1, 2 4, 3, 2 st.update1, 1, 1, 1 st.query1, 2 0, 0, 0 st.query0, 2 1, 2, 3 Segment Tree constructor, it works just with commutative combiner. :param arr: list of elements for the segment tree :param fnc: commutative function for combine two elements SegmentTree'a', 'b', 'c', lambda a, b: f'ab'.query0, 2 'abc' SegmentTree1, 2, 2, 3, 3, 4, ... lambda a, b: a0 b0, a1 b1.query0, 2 6, 9 Update an element in logN time :param p: position to be update :param v: new value st SegmentTree3, 1, 2, 4, min st.query0, 3 1 st.update2, 1 st.query0, 3 1 Get range query value in logN time :param l: left element index :param r: right element index :return: element combined in the range l, r st SegmentTree1, 2, 3, 4, lambda a, b: a b st.query0, 2 6 st.query1, 2 5 st.query0, 3 10 st.query2, 3 7 Test all possible segments
from __future__ import annotations from collections.abc import Callable from typing import Any, Generic, TypeVar T = TypeVar("T") class SegmentTree(Generic[T]): def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None: """ Segment Tree constructor, it works just with commutative combiner. :param arr: list of elements for the segment tree :param fnc: commutative function for combine two elements >>> SegmentTree(['a', 'b', 'c'], lambda a, b: f'{a}{b}').query(0, 2) 'abc' >>> SegmentTree([(1, 2), (2, 3), (3, 4)], ... lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2) (6, 9) """ any_type: Any | T = None self.N: int = len(arr) self.st: list[T] = [any_type for _ in range(self.N)] + arr self.fn = fnc self.build() def build(self) -> None: for p in range(self.N - 1, 0, -1): self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1]) def update(self, p: int, v: T) -> None: """ Update an element in log(N) time :param p: position to be update :param v: new value >>> st = SegmentTree([3, 1, 2, 4], min) >>> st.query(0, 3) 1 >>> st.update(2, -1) >>> st.query(0, 3) -1 """ p += self.N self.st[p] = v while p > 1: p = p // 2 self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1]) def query(self, l: int, r: int) -> T | None: # noqa: E741 """ Get range query value in log(N) time :param l: left element index :param r: right element index :return: element combined in the range [l, r] >>> st = SegmentTree([1, 2, 3, 4], lambda a, b: a + b) >>> st.query(0, 2) 6 >>> st.query(1, 2) 5 >>> st.query(0, 3) 10 >>> st.query(2, 3) 7 """ l, r = l + self.N, r + self.N res: T | None = None while l <= r: if l % 2 == 1: res = self.st[l] if res is None else self.fn(res, self.st[l]) if r % 2 == 0: res = self.st[r] if res is None else self.fn(res, self.st[r]) l, r = (l + 1) // 2, (r - 1) // 2 return res if __name__ == "__main__": from functools import reduce test_array = [1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12] test_updates = { 0: 7, 1: 2, 2: 6, 3: -14, 4: 5, 5: 4, 6: 7, 7: -10, 8: 9, 9: 10, 10: 12, 11: 1, } min_segment_tree = SegmentTree(test_array, min) max_segment_tree = SegmentTree(test_array, max) sum_segment_tree = SegmentTree(test_array, lambda a, b: a + b) def test_all_segments() -> None: """ Test all possible segments """ for i in range(len(test_array)): for j in range(i, len(test_array)): min_range = reduce(min, test_array[i : j + 1]) max_range = reduce(max, test_array[i : j + 1]) sum_range = reduce(lambda a, b: a + b, test_array[i : j + 1]) assert min_range == min_segment_tree.query(i, j) assert max_range == max_segment_tree.query(i, j) assert sum_range == sum_segment_tree.query(i, j) test_all_segments() for index, value in test_updates.items(): test_array[index] = value min_segment_tree.update(index, value) max_segment_tree.update(index, value) sum_segment_tree.update(index, value) test_all_segments()
Hey, we are going to find an exciting number called Catalan number which is use to find the number of possible binary search trees from tree of a given number of nodes. We will use the formula: tn SUMMATIONi 1 to nti1tni Further details at Wikipedia: https:en.wikipedia.orgwikiCatalannumber Our Contribution: Basically we Create the 2 function: 1. catalannumbernodecount: int int Returns the number of possible binary search trees for n nodes. 2. binarytreecountnodecount: int int Returns the number of possible binary trees for n nodes. Since Here we Find the Binomial Coefficient: https:en.wikipedia.orgwikiBinomialcoefficient Cn,k n! k!nk! :param n: 2 times of Number of nodes :param k: Number of nodes :return: Integer Value binomialcoefficient4, 2 6 Since Cn, k Cn, nk Calculate Cn,k We can find Catalan number many ways but here we use Binomial Coefficient because it does the job in On return the Catalan number of n using 2nCnn1. :param n: number of nodes :return: Catalan number of n nodes catalannumber5 42 catalannumber6 132 Return the factorial of a number. :param n: Number to find the Factorial of. :return: Factorial of n. import math allfactoriali math.factoriali for i in range10 True factorial5 doctest: ELLIPSIS Traceback most recent call last: ... ValueError: factorial not defined for negative values Return the number of possible of binary trees. :param n: number of nodes :return: Number of possible binary trees binarytreecount5 5040 binarytreecount6 95040
""" Our Contribution: Basically we Create the 2 function: 1. catalan_number(node_count: int) -> int Returns the number of possible binary search trees for n nodes. 2. binary_tree_count(node_count: int) -> int Returns the number of possible binary trees for n nodes. """ def binomial_coefficient(n: int, k: int) -> int: """ Since Here we Find the Binomial Coefficient: https://en.wikipedia.org/wiki/Binomial_coefficient C(n,k) = n! / k!(n-k)! :param n: 2 times of Number of nodes :param k: Number of nodes :return: Integer Value >>> binomial_coefficient(4, 2) 6 """ result = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): k = n - k # Calculate C(n,k) for i in range(k): result *= n - i result //= i + 1 return result def catalan_number(node_count: int) -> int: """ We can find Catalan number many ways but here we use Binomial Coefficient because it does the job in O(n) return the Catalan number of n using 2nCn/(n+1). :param n: number of nodes :return: Catalan number of n nodes >>> catalan_number(5) 42 >>> catalan_number(6) 132 """ return binomial_coefficient(2 * node_count, node_count) // (node_count + 1) def factorial(n: int) -> int: """ Return the factorial of a number. :param n: Number to find the Factorial of. :return: Factorial of n. >>> import math >>> all(factorial(i) == math.factorial(i) for i in range(10)) True >>> factorial(-5) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: factorial() not defined for negative values """ if n < 0: raise ValueError("factorial() not defined for negative values") result = 1 for i in range(1, n + 1): result *= i return result def binary_tree_count(node_count: int) -> int: """ Return the number of possible of binary trees. :param n: number of nodes :return: Number of possible binary trees >>> binary_tree_count(5) 5040 >>> binary_tree_count(6) 95040 """ return catalan_number(node_count) * factorial(node_count) if __name__ == "__main__": node_count = int(input("Enter the number of nodes: ").strip() or 0) if node_count <= 0: raise ValueError("We need some nodes to work with.") print( f"Given {node_count} nodes, there are {binary_tree_count(node_count)} " f"binary trees and {catalan_number(node_count)} binary search trees." )
psfblack : true ruff : passed A RedBlack tree, which is a selfbalancing BST binary search tree. This tree has similar performance to AVL trees, but the balancing is less strict, so it will perform faster for writingdeleting nodes and slower for reading in the average case, though, because they're both balanced binary search trees, both will get the same asymptotic performance. To read more about them, https:en.wikipedia.orgwikiRedblacktree Unless otherwise specified, all asymptotic runtimes are specified in terms of the size of the tree. Initialize a new RedBlack Tree node with the given values: label: The value associated with this node color: 0 if black, 1 if red parent: The parent to this node left: This node's left child right: This node's right child Here are functions which are specific to redblack trees Rotate the subtree rooted at this node to the left and returns the new root to this subtree. Performing one rotation can be done in O1. Rotate the subtree rooted at this node to the right and returns the new root to this subtree. Performing one rotation can be done in O1. Inserts label into the subtree rooted at self, performs any rotations necessary to maintain balance, and then returns the new root to this subtree likely self. This is guaranteed to run in Ologn time. Only possible with an empty tree Repair the coloring from inserting into a tree. if self.parent is None: This node is the root, so it just needs to be black self.color 0 elif colorself.parent 0: If the parent is black, then it just needs to be red self.color 1 else: uncle self.parent.sibling if coloruncle 0: if self.isleft and self.parent.isright: self.parent.rotateright if self.right: self.right.insertrepair elif self.isright and self.parent.isleft: self.parent.rotateleft if self.left: self.left.insertrepair elif self.isleft: if self.grandparent: self.grandparent.rotateright self.parent.color 0 if self.parent.right: self.parent.right.color 1 else: if self.grandparent: self.grandparent.rotateleft self.parent.color 0 if self.parent.left: self.parent.left.color 1 else: self.parent.color 0 if uncle and self.grandparent: uncle.color 0 self.grandparent.color 1 self.grandparent.insertrepair def removeself, label: int RedBlackTree: noqa: PLR0912 It's easier to balance a node with at most one child, so we replace this node with the greatest one less than it and remove that. This node has at most one nonNone child, so we don't need to replace This node is red, and its child is black The only way this happens to a node with one child is if both children are None leaves. We can just remove this node and call it a day. The node is black This node and its child are black The tree is now empty This node is black and its child is red Move the child node here and make it black Repair the coloring of the tree that may have been messed up. if self.parent is None or self.sibling is None or self.parent.sibling is None or self.grandparent is None : return if colorself.sibling 1: self.sibling.color 0 self.parent.color 1 if self.isleft: self.parent.rotateleft else: self.parent.rotateright if colorself.parent 0 and colorself.sibling 0 and colorself.sibling.left 0 and colorself.sibling.right 0 : self.sibling.color 1 self.parent.removerepair return if colorself.parent 1 and colorself.sibling 0 and colorself.sibling.left 0 and colorself.sibling.right 0 : self.sibling.color 1 self.parent.color 0 return if self.isleft and colorself.sibling 0 and colorself.sibling.right 0 and colorself.sibling.left 1 : self.sibling.rotateright self.sibling.color 0 if self.sibling.right: self.sibling.right.color 1 if self.isright and colorself.sibling 0 and colorself.sibling.right 1 and colorself.sibling.left 0 : self.sibling.rotateleft self.sibling.color 0 if self.sibling.left: self.sibling.left.color 1 if self.isleft and colorself.sibling 0 and colorself.sibling.right 1 : self.parent.rotateleft self.grandparent.color self.parent.color self.parent.color 0 self.parent.sibling.color 0 if self.isright and colorself.sibling 0 and colorself.sibling.left 1 : self.parent.rotateright self.grandparent.color self.parent.color self.parent.color 0 self.parent.sibling.color 0 def checkcolorpropertiesself bool: I assume property 1 to hold because there is nothing that can make the color be anything other than 0 or 1. Property 2 if self.color: The root was red printProperty 2 return False Property 3 does not need to be checked, because None is assumed to be black and is all the leaves. Property 4 if not self.checkcoloring: printProperty 4 return False Property 5 if self.blackheight is None: printProperty 5 return False All properties were met return True def checkcoloringself bool: if self.color 1 and 1 in colorself.left, colorself.right: return False if self.left and not self.left.checkcoloring: return False if self.right and not self.right.checkcoloring: return False return True def blackheightself int None: if self is None or self.left is None or self.right is None: If we're already at a leaf, there is no path return 1 left RedBlackTree.blackheightself.left right RedBlackTree.blackheightself.right if left is None or right is None: There are issues with coloring below children nodes return None if left ! right: The two children have unequal depths return None Return the black depth of children, plus one if this node is black return left 1 self.color Here are functions which are general to all binary search trees def containsself, label: int bool: return self.searchlabel is not None def searchself, label: int RedBlackTree None: if self.label label: return self elif self.label is not None and label self.label: if self.right is None: return None else: return self.right.searchlabel else: if self.left is None: return None else: return self.left.searchlabel def floorself, label: int int None: Returns the smallest element in this tree which is at least label. This method is guaranteed to run in Ologn time. Returns the largest element in this tree. This method is guaranteed to run in Ologn time. Go as far right as possible Returns the smallest element in this tree. This method is guaranteed to run in Ologn time. Go as far left as possible Get the current node's grandparent, or None if it doesn't exist. if self.parent is None: return None else: return self.parent.parent property def siblingself RedBlackTree None: Returns true iff this node is the left child of its parent. if self.parent is None: return False return self.parent.left is self.parent.left is self def isrightself bool: Return the number of nodes in this tree. Test if two trees are equal. if not isinstanceother, RedBlackTree: return NotImplemented if self.label other.label: return self.left other.left and self.right other.right else: return False def colornode: RedBlackTree None int: Code for testing the various functions of the redblack tree. Test that the rotateleft and rotateright functions work. Make a tree to test on tree RedBlackTree0 tree.left RedBlackTree10, parenttree tree.right RedBlackTree10, parenttree tree.left.left RedBlackTree20, parenttree.left tree.left.right RedBlackTree5, parenttree.left tree.right.left RedBlackTree5, parenttree.right tree.right.right RedBlackTree20, parenttree.right Make the right rotation leftrot RedBlackTree10 leftrot.left RedBlackTree0, parentleftrot leftrot.left.left RedBlackTree10, parentleftrot.left leftrot.left.right RedBlackTree5, parentleftrot.left leftrot.left.left.left RedBlackTree20, parentleftrot.left.left leftrot.left.left.right RedBlackTree5, parentleftrot.left.left leftrot.right RedBlackTree20, parentleftrot tree tree.rotateleft if tree ! leftrot: return False tree tree.rotateright tree tree.rotateright Make the left rotation rightrot RedBlackTree10 rightrot.left RedBlackTree20, parentrightrot rightrot.right RedBlackTree0, parentrightrot rightrot.right.left RedBlackTree5, parentrightrot.right rightrot.right.right RedBlackTree10, parentrightrot.right rightrot.right.right.left RedBlackTree5, parentrightrot.right.right rightrot.right.right.right RedBlackTree20, parentrightrot.right.right if tree ! rightrot: return False return True def testinsertionspeed bool: tree RedBlackTree1 for i in range300000: tree tree.inserti return True def testinsert bool: tree RedBlackTree0 tree.insert8 tree.insert8 tree.insert4 tree.insert12 tree.insert10 tree.insert11 ans RedBlackTree0, 0 ans.left RedBlackTree8, 0, ans ans.right RedBlackTree8, 1, ans ans.right.left RedBlackTree4, 0, ans.right ans.right.right RedBlackTree11, 0, ans.right ans.right.right.left RedBlackTree10, 1, ans.right.right ans.right.right.right RedBlackTree12, 1, ans.right.right return tree ans def testinsertandsearch bool: Found something not in there Didn't find something in there Test the insert and delete method of the tree, verifying the insertion and removal of elements, and the balancing of the tree. Tests the floor and ceiling functions in the tree. tree RedBlackTree0 tree.insert16 tree.insert16 tree.insert8 tree.insert24 tree.insert20 tree.insert22 tuples 20, None, 16, 10, 16, 0, 8, 8, 8, 50, 24, None for val, floor, ceil in tuples: if tree.floorval ! floor or tree.ceilval ! ceil: return False return True def testminmax bool: Tests the three different tree traversal functions. tree RedBlackTree0 tree tree.insert16 tree.insert16 tree.insert8 tree.insert24 tree.insert20 tree.insert22 if listtree.inordertraverse ! 16, 0, 8, 16, 20, 22, 24: return False if listtree.preordertraverse ! 0, 16, 16, 8, 22, 20, 24: return False if listtree.postordertraverse ! 16, 8, 20, 24, 22, 16, 0: return False return True def testtreechaining bool: pytests
from __future__ import annotations from collections.abc import Iterator class RedBlackTree: """ A Red-Black tree, which is a self-balancing BST (binary search tree). This tree has similar performance to AVL trees, but the balancing is less strict, so it will perform faster for writing/deleting nodes and slower for reading in the average case, though, because they're both balanced binary search trees, both will get the same asymptotic performance. To read more about them, https://en.wikipedia.org/wiki/Red–black_tree Unless otherwise specified, all asymptotic runtimes are specified in terms of the size of the tree. """ def __init__( self, label: int | None = None, color: int = 0, parent: RedBlackTree | None = None, left: RedBlackTree | None = None, right: RedBlackTree | None = None, ) -> None: """Initialize a new Red-Black Tree node with the given values: label: The value associated with this node color: 0 if black, 1 if red parent: The parent to this node left: This node's left child right: This node's right child """ self.label = label self.parent = parent self.left = left self.right = right self.color = color # Here are functions which are specific to red-black trees def rotate_left(self) -> RedBlackTree: """Rotate the subtree rooted at this node to the left and returns the new root to this subtree. Performing one rotation can be done in O(1). """ parent = self.parent right = self.right if right is None: return self self.right = right.left if self.right: self.right.parent = self self.parent = right right.left = self if parent is not None: if parent.left == self: parent.left = right else: parent.right = right right.parent = parent return right def rotate_right(self) -> RedBlackTree: """Rotate the subtree rooted at this node to the right and returns the new root to this subtree. Performing one rotation can be done in O(1). """ if self.left is None: return self parent = self.parent left = self.left self.left = left.right if self.left: self.left.parent = self self.parent = left left.right = self if parent is not None: if parent.right is self: parent.right = left else: parent.left = left left.parent = parent return left def insert(self, label: int) -> RedBlackTree: """Inserts label into the subtree rooted at self, performs any rotations necessary to maintain balance, and then returns the new root to this subtree (likely self). This is guaranteed to run in O(log(n)) time. """ if self.label is None: # Only possible with an empty tree self.label = label return self if self.label == label: return self elif self.label > label: if self.left: self.left.insert(label) else: self.left = RedBlackTree(label, 1, self) self.left._insert_repair() else: if self.right: self.right.insert(label) else: self.right = RedBlackTree(label, 1, self) self.right._insert_repair() return self.parent or self def _insert_repair(self) -> None: """Repair the coloring from inserting into a tree.""" if self.parent is None: # This node is the root, so it just needs to be black self.color = 0 elif color(self.parent) == 0: # If the parent is black, then it just needs to be red self.color = 1 else: uncle = self.parent.sibling if color(uncle) == 0: if self.is_left() and self.parent.is_right(): self.parent.rotate_right() if self.right: self.right._insert_repair() elif self.is_right() and self.parent.is_left(): self.parent.rotate_left() if self.left: self.left._insert_repair() elif self.is_left(): if self.grandparent: self.grandparent.rotate_right() self.parent.color = 0 if self.parent.right: self.parent.right.color = 1 else: if self.grandparent: self.grandparent.rotate_left() self.parent.color = 0 if self.parent.left: self.parent.left.color = 1 else: self.parent.color = 0 if uncle and self.grandparent: uncle.color = 0 self.grandparent.color = 1 self.grandparent._insert_repair() def remove(self, label: int) -> RedBlackTree: # noqa: PLR0912 """Remove label from this tree.""" if self.label == label: if self.left and self.right: # It's easier to balance a node with at most one child, # so we replace this node with the greatest one less than # it and remove that. value = self.left.get_max() if value is not None: self.label = value self.left.remove(value) else: # This node has at most one non-None child, so we don't # need to replace child = self.left or self.right if self.color == 1: # This node is red, and its child is black # The only way this happens to a node with one child # is if both children are None leaves. # We can just remove this node and call it a day. if self.parent: if self.is_left(): self.parent.left = None else: self.parent.right = None else: # The node is black if child is None: # This node and its child are black if self.parent is None: # The tree is now empty return RedBlackTree(None) else: self._remove_repair() if self.is_left(): self.parent.left = None else: self.parent.right = None self.parent = None else: # This node is black and its child is red # Move the child node here and make it black self.label = child.label self.left = child.left self.right = child.right if self.left: self.left.parent = self if self.right: self.right.parent = self elif self.label is not None and self.label > label: if self.left: self.left.remove(label) else: if self.right: self.right.remove(label) return self.parent or self def _remove_repair(self) -> None: """Repair the coloring of the tree that may have been messed up.""" if ( self.parent is None or self.sibling is None or self.parent.sibling is None or self.grandparent is None ): return if color(self.sibling) == 1: self.sibling.color = 0 self.parent.color = 1 if self.is_left(): self.parent.rotate_left() else: self.parent.rotate_right() if ( color(self.parent) == 0 and color(self.sibling) == 0 and color(self.sibling.left) == 0 and color(self.sibling.right) == 0 ): self.sibling.color = 1 self.parent._remove_repair() return if ( color(self.parent) == 1 and color(self.sibling) == 0 and color(self.sibling.left) == 0 and color(self.sibling.right) == 0 ): self.sibling.color = 1 self.parent.color = 0 return if ( self.is_left() and color(self.sibling) == 0 and color(self.sibling.right) == 0 and color(self.sibling.left) == 1 ): self.sibling.rotate_right() self.sibling.color = 0 if self.sibling.right: self.sibling.right.color = 1 if ( self.is_right() and color(self.sibling) == 0 and color(self.sibling.right) == 1 and color(self.sibling.left) == 0 ): self.sibling.rotate_left() self.sibling.color = 0 if self.sibling.left: self.sibling.left.color = 1 if ( self.is_left() and color(self.sibling) == 0 and color(self.sibling.right) == 1 ): self.parent.rotate_left() self.grandparent.color = self.parent.color self.parent.color = 0 self.parent.sibling.color = 0 if ( self.is_right() and color(self.sibling) == 0 and color(self.sibling.left) == 1 ): self.parent.rotate_right() self.grandparent.color = self.parent.color self.parent.color = 0 self.parent.sibling.color = 0 def check_color_properties(self) -> bool: """Check the coloring of the tree, and return True iff the tree is colored in a way which matches these five properties: (wording stolen from wikipedia article) 1. Each node is either red or black. 2. The root node is black. 3. All leaves are black. 4. If a node is red, then both its children are black. 5. Every path from any node to all of its descendent NIL nodes has the same number of black nodes. This function runs in O(n) time, because properties 4 and 5 take that long to check. """ # I assume property 1 to hold because there is nothing that can # make the color be anything other than 0 or 1. # Property 2 if self.color: # The root was red print("Property 2") return False # Property 3 does not need to be checked, because None is assumed # to be black and is all the leaves. # Property 4 if not self.check_coloring(): print("Property 4") return False # Property 5 if self.black_height() is None: print("Property 5") return False # All properties were met return True def check_coloring(self) -> bool: """A helper function to recursively check Property 4 of a Red-Black Tree. See check_color_properties for more info. """ if self.color == 1 and 1 in (color(self.left), color(self.right)): return False if self.left and not self.left.check_coloring(): return False if self.right and not self.right.check_coloring(): return False return True def black_height(self) -> int | None: """Returns the number of black nodes from this node to the leaves of the tree, or None if there isn't one such value (the tree is color incorrectly). """ if self is None or self.left is None or self.right is None: # If we're already at a leaf, there is no path return 1 left = RedBlackTree.black_height(self.left) right = RedBlackTree.black_height(self.right) if left is None or right is None: # There are issues with coloring below children nodes return None if left != right: # The two children have unequal depths return None # Return the black depth of children, plus one if this node is # black return left + (1 - self.color) # Here are functions which are general to all binary search trees def __contains__(self, label: int) -> bool: """Search through the tree for label, returning True iff it is found somewhere in the tree. Guaranteed to run in O(log(n)) time. """ return self.search(label) is not None def search(self, label: int) -> RedBlackTree | None: """Search through the tree for label, returning its node if it's found, and None otherwise. This method is guaranteed to run in O(log(n)) time. """ if self.label == label: return self elif self.label is not None and label > self.label: if self.right is None: return None else: return self.right.search(label) else: if self.left is None: return None else: return self.left.search(label) def floor(self, label: int) -> int | None: """Returns the largest element in this tree which is at most label. This method is guaranteed to run in O(log(n)) time.""" if self.label == label: return self.label elif self.label is not None and self.label > label: if self.left: return self.left.floor(label) else: return None else: if self.right: attempt = self.right.floor(label) if attempt is not None: return attempt return self.label def ceil(self, label: int) -> int | None: """Returns the smallest element in this tree which is at least label. This method is guaranteed to run in O(log(n)) time. """ if self.label == label: return self.label elif self.label is not None and self.label < label: if self.right: return self.right.ceil(label) else: return None else: if self.left: attempt = self.left.ceil(label) if attempt is not None: return attempt return self.label def get_max(self) -> int | None: """Returns the largest element in this tree. This method is guaranteed to run in O(log(n)) time. """ if self.right: # Go as far right as possible return self.right.get_max() else: return self.label def get_min(self) -> int | None: """Returns the smallest element in this tree. This method is guaranteed to run in O(log(n)) time. """ if self.left: # Go as far left as possible return self.left.get_min() else: return self.label @property def grandparent(self) -> RedBlackTree | None: """Get the current node's grandparent, or None if it doesn't exist.""" if self.parent is None: return None else: return self.parent.parent @property def sibling(self) -> RedBlackTree | None: """Get the current node's sibling, or None if it doesn't exist.""" if self.parent is None: return None elif self.parent.left is self: return self.parent.right else: return self.parent.left def is_left(self) -> bool: """Returns true iff this node is the left child of its parent.""" if self.parent is None: return False return self.parent.left is self.parent.left is self def is_right(self) -> bool: """Returns true iff this node is the right child of its parent.""" if self.parent is None: return False return self.parent.right is self def __bool__(self) -> bool: return True def __len__(self) -> int: """ Return the number of nodes in this tree. """ ln = 1 if self.left: ln += len(self.left) if self.right: ln += len(self.right) return ln def preorder_traverse(self) -> Iterator[int | None]: yield self.label if self.left: yield from self.left.preorder_traverse() if self.right: yield from self.right.preorder_traverse() def inorder_traverse(self) -> Iterator[int | None]: if self.left: yield from self.left.inorder_traverse() yield self.label if self.right: yield from self.right.inorder_traverse() def postorder_traverse(self) -> Iterator[int | None]: if self.left: yield from self.left.postorder_traverse() if self.right: yield from self.right.postorder_traverse() yield self.label def __repr__(self) -> str: from pprint import pformat if self.left is None and self.right is None: return f"'{self.label} {(self.color and 'red') or 'blk'}'" return pformat( { f"{self.label} {(self.color and 'red') or 'blk'}": ( self.left, self.right, ) }, indent=1, ) def __eq__(self, other: object) -> bool: """Test if two trees are equal.""" if not isinstance(other, RedBlackTree): return NotImplemented if self.label == other.label: return self.left == other.left and self.right == other.right else: return False def color(node: RedBlackTree | None) -> int: """Returns the color of a node, allowing for None leaves.""" if node is None: return 0 else: return node.color """ Code for testing the various functions of the red-black tree. """ def test_rotations() -> bool: """Test that the rotate_left and rotate_right functions work.""" # Make a tree to test on tree = RedBlackTree(0) tree.left = RedBlackTree(-10, parent=tree) tree.right = RedBlackTree(10, parent=tree) tree.left.left = RedBlackTree(-20, parent=tree.left) tree.left.right = RedBlackTree(-5, parent=tree.left) tree.right.left = RedBlackTree(5, parent=tree.right) tree.right.right = RedBlackTree(20, parent=tree.right) # Make the right rotation left_rot = RedBlackTree(10) left_rot.left = RedBlackTree(0, parent=left_rot) left_rot.left.left = RedBlackTree(-10, parent=left_rot.left) left_rot.left.right = RedBlackTree(5, parent=left_rot.left) left_rot.left.left.left = RedBlackTree(-20, parent=left_rot.left.left) left_rot.left.left.right = RedBlackTree(-5, parent=left_rot.left.left) left_rot.right = RedBlackTree(20, parent=left_rot) tree = tree.rotate_left() if tree != left_rot: return False tree = tree.rotate_right() tree = tree.rotate_right() # Make the left rotation right_rot = RedBlackTree(-10) right_rot.left = RedBlackTree(-20, parent=right_rot) right_rot.right = RedBlackTree(0, parent=right_rot) right_rot.right.left = RedBlackTree(-5, parent=right_rot.right) right_rot.right.right = RedBlackTree(10, parent=right_rot.right) right_rot.right.right.left = RedBlackTree(5, parent=right_rot.right.right) right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right) if tree != right_rot: return False return True def test_insertion_speed() -> bool: """Test that the tree balances inserts to O(log(n)) by doing a lot of them. """ tree = RedBlackTree(-1) for i in range(300000): tree = tree.insert(i) return True def test_insert() -> bool: """Test the insert() method of the tree correctly balances, colors, and inserts. """ tree = RedBlackTree(0) tree.insert(8) tree.insert(-8) tree.insert(4) tree.insert(12) tree.insert(10) tree.insert(11) ans = RedBlackTree(0, 0) ans.left = RedBlackTree(-8, 0, ans) ans.right = RedBlackTree(8, 1, ans) ans.right.left = RedBlackTree(4, 0, ans.right) ans.right.right = RedBlackTree(11, 0, ans.right) ans.right.right.left = RedBlackTree(10, 1, ans.right.right) ans.right.right.right = RedBlackTree(12, 1, ans.right.right) return tree == ans def test_insert_and_search() -> bool: """Tests searching through the tree for values.""" tree = RedBlackTree(0) tree.insert(8) tree.insert(-8) tree.insert(4) tree.insert(12) tree.insert(10) tree.insert(11) if 5 in tree or -6 in tree or -10 in tree or 13 in tree: # Found something not in there return False if not (11 in tree and 12 in tree and -8 in tree and 0 in tree): # Didn't find something in there return False return True def test_insert_delete() -> bool: """Test the insert() and delete() method of the tree, verifying the insertion and removal of elements, and the balancing of the tree. """ tree = RedBlackTree(0) tree = tree.insert(-12) tree = tree.insert(8) tree = tree.insert(-8) tree = tree.insert(15) tree = tree.insert(4) tree = tree.insert(12) tree = tree.insert(10) tree = tree.insert(9) tree = tree.insert(11) tree = tree.remove(15) tree = tree.remove(-12) tree = tree.remove(9) if not tree.check_color_properties(): return False if list(tree.inorder_traverse()) != [-8, 0, 4, 8, 10, 11, 12]: return False return True def test_floor_ceil() -> bool: """Tests the floor and ceiling functions in the tree.""" tree = RedBlackTree(0) tree.insert(-16) tree.insert(16) tree.insert(8) tree.insert(24) tree.insert(20) tree.insert(22) tuples = [(-20, None, -16), (-10, -16, 0), (8, 8, 8), (50, 24, None)] for val, floor, ceil in tuples: if tree.floor(val) != floor or tree.ceil(val) != ceil: return False return True def test_min_max() -> bool: """Tests the min and max functions in the tree.""" tree = RedBlackTree(0) tree.insert(-16) tree.insert(16) tree.insert(8) tree.insert(24) tree.insert(20) tree.insert(22) if tree.get_max() != 22 or tree.get_min() != -16: return False return True def test_tree_traversal() -> bool: """Tests the three different tree traversal functions.""" tree = RedBlackTree(0) tree = tree.insert(-16) tree.insert(16) tree.insert(8) tree.insert(24) tree.insert(20) tree.insert(22) if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]: return False if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]: return False if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]: return False return True def test_tree_chaining() -> bool: """Tests the three different tree chaining functions.""" tree = RedBlackTree(0) tree = tree.insert(-16).insert(16).insert(8).insert(24).insert(20).insert(22) if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]: return False if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]: return False if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]: return False return True def print_results(msg: str, passes: bool) -> None: print(str(msg), "works!" if passes else "doesn't work :(") def pytests() -> None: assert test_rotations() assert test_insert() assert test_insert_and_search() assert test_insert_delete() assert test_floor_ceil() assert test_tree_traversal() assert test_tree_chaining() def main() -> None: """ >>> pytests() """ print_results("Rotating right and left", test_rotations()) print_results("Inserting", test_insert()) print_results("Searching", test_insert_and_search()) print_results("Deleting", test_insert_delete()) print_results("Floor and ceil", test_floor_ceil()) print_results("Tree traversal", test_tree_traversal()) print_results("Tree traversal", test_tree_chaining()) print("Testing tree balancing...") print("This should only be a few seconds.") test_insertion_speed() print("Done!") if __name__ == "__main__": main()
Returns the left child index for a given index in a binary tree. s SegmentTree1, 2, 3 s.left1 2 s.left2 4 Returns the right child index for a given index in a binary tree. s SegmentTree1, 2, 3 s.right1 3 s.right2 5 Update the values in the segment tree in the range a,b with the given value. s SegmentTree1, 2, 3, 4, 5 s.update2, 4, 10 True s.query1, 5 10 update1, 1, N, a, b, v for update val v to a,b Query the maximum value in the range a,b. s SegmentTree1, 2, 3, 4, 5 s.query1, 3 3 s.query1, 5 5 query1, 1, N, a, b for query max of a,b
import math class SegmentTree: def __init__(self, a): self.A = a self.N = len(self.A) self.st = [0] * ( 4 * self.N ) # approximate the overall size of segment tree with array N if self.N: self.build(1, 0, self.N - 1) def left(self, idx): """ Returns the left child index for a given index in a binary tree. >>> s = SegmentTree([1, 2, 3]) >>> s.left(1) 2 >>> s.left(2) 4 """ return idx * 2 def right(self, idx): """ Returns the right child index for a given index in a binary tree. >>> s = SegmentTree([1, 2, 3]) >>> s.right(1) 3 >>> s.right(2) 5 """ return idx * 2 + 1 def build(self, idx, l, r): # noqa: E741 if l == r: self.st[idx] = self.A[l] else: mid = (l + r) // 2 self.build(self.left(idx), l, mid) self.build(self.right(idx), mid + 1, r) self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) def update(self, a, b, val): """ Update the values in the segment tree in the range [a,b] with the given value. >>> s = SegmentTree([1, 2, 3, 4, 5]) >>> s.update(2, 4, 10) True >>> s.query(1, 5) 10 """ return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val) def update_recursive(self, idx, l, r, a, b, val): # noqa: E741 """ update(1, 1, N, a, b, v) for update val v to [a,b] """ if r < a or l > b: return True if l == r: self.st[idx] = val return True mid = (l + r) // 2 self.update_recursive(self.left(idx), l, mid, a, b, val) self.update_recursive(self.right(idx), mid + 1, r, a, b, val) self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) return True def query(self, a, b): """ Query the maximum value in the range [a,b]. >>> s = SegmentTree([1, 2, 3, 4, 5]) >>> s.query(1, 3) 3 >>> s.query(1, 5) 5 """ return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1) def query_recursive(self, idx, l, r, a, b): # noqa: E741 """ query(1, 1, N, a, b) for query max of [a,b] """ if r < a or l > b: return -math.inf if l >= a and r <= b: return self.st[idx] mid = (l + r) // 2 q1 = self.query_recursive(self.left(idx), l, mid, a, b) q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b) return max(q1, q2) def show_data(self): show_list = [] for i in range(1, N + 1): show_list += [self.query(i, i)] print(show_list) if __name__ == "__main__": A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] N = 15 segt = SegmentTree(A) print(segt.query(4, 6)) print(segt.query(7, 11)) print(segt.query(7, 12)) segt.update(1, 3, 111) print(segt.query(1, 15)) segt.update(7, 8, 235) segt.show_data()
Segmenttree creates a segment tree with a given array and function, allowing queries to be done later in logN time function takes 2 values and returns a same type value import operator numarr SegmentTree2, 1, 5, 3, 4, operator.add tuplenumarr.traverse doctest: NORMALIZEWHITESPACE SegmentTreeNodestart0, end4, val15, SegmentTreeNodestart0, end2, val8, SegmentTreeNodestart3, end4, val7, SegmentTreeNodestart0, end1, val3, SegmentTreeNodestart2, end2, val5, SegmentTreeNodestart3, end3, val3, SegmentTreeNodestart4, end4, val4, SegmentTreeNodestart0, end0, val2, SegmentTreeNodestart1, end1, val1 numarr.update1, 5 tuplenumarr.traverse doctest: NORMALIZEWHITESPACE SegmentTreeNodestart0, end4, val19, SegmentTreeNodestart0, end2, val12, SegmentTreeNodestart3, end4, val7, SegmentTreeNodestart0, end1, val7, SegmentTreeNodestart2, end2, val5, SegmentTreeNodestart3, end3, val3, SegmentTreeNodestart4, end4, val4, SegmentTreeNodestart0, end0, val2, SegmentTreeNodestart1, end1, val5 numarr.queryrange3, 4 7 numarr.queryrange2, 2 5 numarr.queryrange1, 3 13 maxarr SegmentTree2, 1, 5, 3, 4, max for node in maxarr.traverse: ... printnode ... SegmentTreeNodestart0, end4, val5 SegmentTreeNodestart0, end2, val5 SegmentTreeNodestart3, end4, val4 SegmentTreeNodestart0, end1, val2 SegmentTreeNodestart2, end2, val5 SegmentTreeNodestart3, end3, val3 SegmentTreeNodestart4, end4, val4 SegmentTreeNodestart0, end0, val2 SegmentTreeNodestart1, end1, val1 maxarr.update1, 5 for node in maxarr.traverse: ... printnode ... SegmentTreeNodestart0, end4, val5 SegmentTreeNodestart0, end2, val5 SegmentTreeNodestart3, end4, val4 SegmentTreeNodestart0, end1, val5 SegmentTreeNodestart2, end2, val5 SegmentTreeNodestart3, end3, val3 SegmentTreeNodestart4, end4, val4 SegmentTreeNodestart0, end0, val2 SegmentTreeNodestart1, end1, val5 maxarr.queryrange3, 4 4 maxarr.queryrange2, 2 5 maxarr.queryrange1, 3 5 minarr SegmentTree2, 1, 5, 3, 4, min for node in minarr.traverse: ... printnode ... SegmentTreeNodestart0, end4, val1 SegmentTreeNodestart0, end2, val1 SegmentTreeNodestart3, end4, val3 SegmentTreeNodestart0, end1, val1 SegmentTreeNodestart2, end2, val5 SegmentTreeNodestart3, end3, val3 SegmentTreeNodestart4, end4, val4 SegmentTreeNodestart0, end0, val2 SegmentTreeNodestart1, end1, val1 minarr.update1, 5 for node in minarr.traverse: ... printnode ... SegmentTreeNodestart0, end4, val2 SegmentTreeNodestart0, end2, val2 SegmentTreeNodestart3, end4, val3 SegmentTreeNodestart0, end1, val2 SegmentTreeNodestart2, end2, val5 SegmentTreeNodestart3, end3, val3 SegmentTreeNodestart4, end4, val4 SegmentTreeNodestart0, end0, val2 SegmentTreeNodestart1, end1, val5 minarr.queryrange3, 4 3 minarr.queryrange2, 2 5 minarr.queryrange1, 3 3 Update an element in logN time :param i: position to be update :param val: new value import operator numarr SegmentTree2, 1, 5, 3, 4, operator.add numarr.update1, 5 numarr.queryrange1, 3 13 Get range query value in logN time :param i: left element index :param j: right element index :return: element combined in the range i, j import operator numarr SegmentTree2, 1, 5, 3, 4, operator.add numarr.update1, 5 numarr.queryrange3, 4 7 numarr.queryrange2, 2 5 numarr.queryrange1, 3 13 range in left child tree range in left child tree and right child tree range in right child tree
from collections.abc import Sequence from queue import Queue class SegmentTreeNode: def __init__(self, start, end, val, left=None, right=None): self.start = start self.end = end self.val = val self.mid = (start + end) // 2 self.left = left self.right = right def __repr__(self): return f"SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})" class SegmentTree: """ >>> import operator >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) >>> tuple(num_arr.traverse()) # doctest: +NORMALIZE_WHITESPACE (SegmentTreeNode(start=0, end=4, val=15), SegmentTreeNode(start=0, end=2, val=8), SegmentTreeNode(start=3, end=4, val=7), SegmentTreeNode(start=0, end=1, val=3), SegmentTreeNode(start=2, end=2, val=5), SegmentTreeNode(start=3, end=3, val=3), SegmentTreeNode(start=4, end=4, val=4), SegmentTreeNode(start=0, end=0, val=2), SegmentTreeNode(start=1, end=1, val=1)) >>> >>> num_arr.update(1, 5) >>> tuple(num_arr.traverse()) # doctest: +NORMALIZE_WHITESPACE (SegmentTreeNode(start=0, end=4, val=19), SegmentTreeNode(start=0, end=2, val=12), SegmentTreeNode(start=3, end=4, val=7), SegmentTreeNode(start=0, end=1, val=7), SegmentTreeNode(start=2, end=2, val=5), SegmentTreeNode(start=3, end=3, val=3), SegmentTreeNode(start=4, end=4, val=4), SegmentTreeNode(start=0, end=0, val=2), SegmentTreeNode(start=1, end=1, val=5)) >>> >>> num_arr.query_range(3, 4) 7 >>> num_arr.query_range(2, 2) 5 >>> num_arr.query_range(1, 3) 13 >>> >>> max_arr = SegmentTree([2, 1, 5, 3, 4], max) >>> for node in max_arr.traverse(): ... print(node) ... SegmentTreeNode(start=0, end=4, val=5) SegmentTreeNode(start=0, end=2, val=5) SegmentTreeNode(start=3, end=4, val=4) SegmentTreeNode(start=0, end=1, val=2) SegmentTreeNode(start=2, end=2, val=5) SegmentTreeNode(start=3, end=3, val=3) SegmentTreeNode(start=4, end=4, val=4) SegmentTreeNode(start=0, end=0, val=2) SegmentTreeNode(start=1, end=1, val=1) >>> >>> max_arr.update(1, 5) >>> for node in max_arr.traverse(): ... print(node) ... SegmentTreeNode(start=0, end=4, val=5) SegmentTreeNode(start=0, end=2, val=5) SegmentTreeNode(start=3, end=4, val=4) SegmentTreeNode(start=0, end=1, val=5) SegmentTreeNode(start=2, end=2, val=5) SegmentTreeNode(start=3, end=3, val=3) SegmentTreeNode(start=4, end=4, val=4) SegmentTreeNode(start=0, end=0, val=2) SegmentTreeNode(start=1, end=1, val=5) >>> >>> max_arr.query_range(3, 4) 4 >>> max_arr.query_range(2, 2) 5 >>> max_arr.query_range(1, 3) 5 >>> >>> min_arr = SegmentTree([2, 1, 5, 3, 4], min) >>> for node in min_arr.traverse(): ... print(node) ... SegmentTreeNode(start=0, end=4, val=1) SegmentTreeNode(start=0, end=2, val=1) SegmentTreeNode(start=3, end=4, val=3) SegmentTreeNode(start=0, end=1, val=1) SegmentTreeNode(start=2, end=2, val=5) SegmentTreeNode(start=3, end=3, val=3) SegmentTreeNode(start=4, end=4, val=4) SegmentTreeNode(start=0, end=0, val=2) SegmentTreeNode(start=1, end=1, val=1) >>> >>> min_arr.update(1, 5) >>> for node in min_arr.traverse(): ... print(node) ... SegmentTreeNode(start=0, end=4, val=2) SegmentTreeNode(start=0, end=2, val=2) SegmentTreeNode(start=3, end=4, val=3) SegmentTreeNode(start=0, end=1, val=2) SegmentTreeNode(start=2, end=2, val=5) SegmentTreeNode(start=3, end=3, val=3) SegmentTreeNode(start=4, end=4, val=4) SegmentTreeNode(start=0, end=0, val=2) SegmentTreeNode(start=1, end=1, val=5) >>> >>> min_arr.query_range(3, 4) 3 >>> min_arr.query_range(2, 2) 5 >>> min_arr.query_range(1, 3) 3 >>> """ def __init__(self, collection: Sequence, function): self.collection = collection self.fn = function if self.collection: self.root = self._build_tree(0, len(collection) - 1) def update(self, i, val): """ Update an element in log(N) time :param i: position to be update :param val: new value >>> import operator >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) >>> num_arr.update(1, 5) >>> num_arr.query_range(1, 3) 13 """ self._update_tree(self.root, i, val) def query_range(self, i, j): """ Get range query value in log(N) time :param i: left element index :param j: right element index :return: element combined in the range [i, j] >>> import operator >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) >>> num_arr.update(1, 5) >>> num_arr.query_range(3, 4) 7 >>> num_arr.query_range(2, 2) 5 >>> num_arr.query_range(1, 3) 13 >>> """ return self._query_range(self.root, i, j) def _build_tree(self, start, end): if start == end: return SegmentTreeNode(start, end, self.collection[start]) mid = (start + end) // 2 left = self._build_tree(start, mid) right = self._build_tree(mid + 1, end) return SegmentTreeNode(start, end, self.fn(left.val, right.val), left, right) def _update_tree(self, node, i, val): if node.start == i and node.end == i: node.val = val return if i <= node.mid: self._update_tree(node.left, i, val) else: self._update_tree(node.right, i, val) node.val = self.fn(node.left.val, node.right.val) def _query_range(self, node, i, j): if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left, i, j) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left, i, node.mid), self._query_range(node.right, node.mid + 1, j), ) else: # range in right child tree return self._query_range(node.right, i, j) def traverse(self): if self.root is not None: queue = Queue() queue.put(self.root) while not queue.empty(): node = queue.get() yield node if node.left is not None: queue.put(node.left) if node.right is not None: queue.put(node.right) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print("*" * 50) arr = SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
A binary tree node has a value, left child, and right child. Props: value: The value of the node. left: The left child of the node. right: The right child of the node. Iterate through the tree in preorder. Returns: An iterator of the tree nodes. listTreeNode1 1,null,null tupleTreeNode1, TreeNode2, TreeNode3 1,2,null,null,3,null,null, 2,null,null, 3,null,null Count the number of nodes in the tree. Returns: The number of nodes in the tree. lenTreeNode1 1 lenTreeNode1, TreeNode2, TreeNode3 3 Represent the tree as a string. Returns: A string representation of the tree. reprTreeNode1 '1,null,null' reprTreeNode1, TreeNode2, TreeNode3 '1,2,null,null,3,null,null' reprTreeNode1, TreeNode2, TreeNode3, TreeNode4, TreeNode5 '1,2,null,null,3,4,null,null,5,null,null' reprTreeNode.fivetree '1,2,null,null,3,4,null,null,5,null,null' Deserialize a string to a binary tree. Args: datastr: The serialized string. Returns: The root of the binary tree. root TreeNode.fivetree serialzeddata reprroot deserialized deserializeserialzeddata root deserialized True root is deserialized two separate trees False root.right.right.value 6 root deserialized False serialzeddata reprroot deserialized deserializeserialzeddata root deserialized True deserialize Traceback most recent call last: ... ValueError: Data cannot be empty. Split the serialized string by a comma to get node values Get the next value from the list
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass @dataclass class TreeNode: """ A binary tree node has a value, left child, and right child. Props: value: The value of the node. left: The left child of the node. right: The right child of the node. """ value: int = 0 left: TreeNode | None = None right: TreeNode | None = None def __post_init__(self): if not isinstance(self.value, int): raise TypeError("Value must be an integer.") def __iter__(self) -> Iterator[TreeNode]: """ Iterate through the tree in preorder. Returns: An iterator of the tree nodes. >>> list(TreeNode(1)) [1,null,null] >>> tuple(TreeNode(1, TreeNode(2), TreeNode(3))) (1,2,null,null,3,null,null, 2,null,null, 3,null,null) """ yield self yield from self.left or () yield from self.right or () def __len__(self) -> int: """ Count the number of nodes in the tree. Returns: The number of nodes in the tree. >>> len(TreeNode(1)) 1 >>> len(TreeNode(1, TreeNode(2), TreeNode(3))) 3 """ return sum(1 for _ in self) def __repr__(self) -> str: """ Represent the tree as a string. Returns: A string representation of the tree. >>> repr(TreeNode(1)) '1,null,null' >>> repr(TreeNode(1, TreeNode(2), TreeNode(3))) '1,2,null,null,3,null,null' >>> repr(TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5)))) '1,2,null,null,3,4,null,null,5,null,null' """ return f"{self.value},{self.left!r},{self.right!r}".replace("None", "null") @classmethod def five_tree(cls) -> TreeNode: """ >>> repr(TreeNode.five_tree()) '1,2,null,null,3,4,null,null,5,null,null' """ root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.right.left = TreeNode(4) root.right.right = TreeNode(5) return root def deserialize(data: str) -> TreeNode | None: """ Deserialize a string to a binary tree. Args: data(str): The serialized string. Returns: The root of the binary tree. >>> root = TreeNode.five_tree() >>> serialzed_data = repr(root) >>> deserialized = deserialize(serialzed_data) >>> root == deserialized True >>> root is deserialized # two separate trees False >>> root.right.right.value = 6 >>> root == deserialized False >>> serialzed_data = repr(root) >>> deserialized = deserialize(serialzed_data) >>> root == deserialized True >>> deserialize("") Traceback (most recent call last): ... ValueError: Data cannot be empty. """ if not data: raise ValueError("Data cannot be empty.") # Split the serialized string by a comma to get node values nodes = data.split(",") def build_tree() -> TreeNode | None: # Get the next value from the list value = nodes.pop(0) if value == "null": return None node = TreeNode(int(value)) node.left = build_tree() # Recursively build left subtree node.right = build_tree() # Recursively build right subtree return node return build_tree() if __name__ == "__main__": import doctest doctest.testmod()
Given the root of a binary tree, check whether it is a mirror of itself i.e., symmetric around its center. Leetcode reference: https:leetcode.comproblemssymmetrictree A Node has data variable and pointers to Nodes to its left and right. root Node1 root.left Node2 root.right Node2 root.left.left Node3 root.left.right Node4 root.right.left Node4 root.right.right Node3 return root def makeasymmetrictree Node: r Create a asymmetric tree for testing. The tree looks like this: 1 2 2 3 4 3 4 Test cases for issymmetrictree function issymmetrictreemakesymmetrictree True issymmetrictreemakeasymmetrictree False tree1 makesymmetrictree tree1.right.right Node3 ismirrortree1.left, tree1.right True tree2 makeasymmetrictree ismirrortree2.left, tree2.right False Both sides are empty, which is symmetric. One side is empty while the other is not, which is not symmetric. The values match, so check the subtree
from __future__ import annotations from dataclasses import dataclass @dataclass class Node: """ A Node has data variable and pointers to Nodes to its left and right. """ data: int left: Node | None = None right: Node | None = None def make_symmetric_tree() -> Node: r""" Create a symmetric tree for testing. The tree looks like this: 1 / \ 2 2 / \ / \ 3 4 4 3 """ root = Node(1) root.left = Node(2) root.right = Node(2) root.left.left = Node(3) root.left.right = Node(4) root.right.left = Node(4) root.right.right = Node(3) return root def make_asymmetric_tree() -> Node: r""" Create a asymmetric tree for testing. The tree looks like this: 1 / \ 2 2 / \ / \ 3 4 3 4 """ root = Node(1) root.left = Node(2) root.right = Node(2) root.left.left = Node(3) root.left.right = Node(4) root.right.left = Node(3) root.right.right = Node(4) return root def is_symmetric_tree(tree: Node) -> bool: """ Test cases for is_symmetric_tree function >>> is_symmetric_tree(make_symmetric_tree()) True >>> is_symmetric_tree(make_asymmetric_tree()) False """ if tree: return is_mirror(tree.left, tree.right) return True # An empty tree is considered symmetric. def is_mirror(left: Node | None, right: Node | None) -> bool: """ >>> tree1 = make_symmetric_tree() >>> tree1.right.right = Node(3) >>> is_mirror(tree1.left, tree1.right) True >>> tree2 = make_asymmetric_tree() >>> is_mirror(tree2.left, tree2.right) False """ if left is None and right is None: # Both sides are empty, which is symmetric. return True if left is None or right is None: # One side is empty while the other is not, which is not symmetric. return False if left.data == right.data: # The values match, so check the subtree return is_mirror(left.left, right.right) and is_mirror(left.right, right.left) return False if __name__ == "__main__": from doctest import testmod testmod()
Treap's node Treap is a binary tree by value and heap by priority We split current tree into 2 trees with value: Left tree contains all values less than split value. Right tree contains all values greater or equal, than split value Right tree's root will be current node. Now we splitwith the same value current node's left son Left tree: left part of that split Right tree's left son: right part of that split Just symmetric to previous case We merge 2 trees into one. Note: all left tree's values must be less than all right tree's Left will be root because it has more priority Now we need to merge left's right son and right tree Symmetric as well Insert element Split current tree with a value into left, right, Insert new node into the middle Merge left, node, right into root Erase element Split all nodes with values less into left, Split all nodes with values greater into right. Merge left, right Just recursive print of a tree Commands: value to add value into treap value to erase all nodes with value root interacttreapNone, 1 inorderroot 1, root interacttreaproot, 3 5 17 19 2 16 4 0 inorderroot 0,1,2,3,4,5,16,17,19, root interacttreaproot, 4 4 4 inorderroot 0,1,2,3,4,4,4,4,5,16,17,19, root interacttreaproot, 0 inorderroot 1,2,3,4,4,4,4,5,16,17,19, root interacttreaproot, 4 inorderroot 1,2,3,5,16,17,19, root interacttreaproot, 0 Unknown command After each command, program prints treap root None print enter numbers to create a tree, value to add value into treap, value to erase all nodes with value. 'q' to quit. args input while args ! q: root interacttreaproot, args printroot args input printgood by! if name main: import doctest doctest.testmod main
from __future__ import annotations from random import random class Node: """ Treap's node Treap is a binary tree by value and heap by priority """ def __init__(self, value: int | None = None): self.value = value self.prior = random() self.left: Node | None = None self.right: Node | None = None def __repr__(self) -> str: from pprint import pformat if self.left is None and self.right is None: return f"'{self.value}: {self.prior:.5}'" else: return pformat( {f"{self.value}: {self.prior:.5}": (self.left, self.right)}, indent=1 ) def __str__(self) -> str: value = str(self.value) + " " left = str(self.left or "") right = str(self.right or "") return value + left + right def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]: """ We split current tree into 2 trees with value: Left tree contains all values less than split value. Right tree contains all values greater or equal, than split value """ if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: """ Right tree's root will be current node. Now we split(with the same value) current node's left son Left tree: left part of that split Right tree's left son: right part of that split """ left, root.left = split(root.left, value) return left, root else: """ Just symmetric to previous case """ root.right, right = split(root.right, value) return root, right def merge(left: Node | None, right: Node | None) -> Node | None: """ We merge 2 trees into one. Note: all left tree's values must be less than all right tree's """ if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: """ Left will be root because it has more priority Now we need to merge left's right son and right tree """ left.right = merge(left.right, right) return left else: """ Symmetric as well """ right.left = merge(left, right.left) return right def insert(root: Node | None, value: int) -> Node | None: """ Insert element Split current tree with a value into left, right, Insert new node into the middle Merge left, node, right into root """ node = Node(value) left, right = split(root, value) return merge(merge(left, node), right) def erase(root: Node | None, value: int) -> Node | None: """ Erase element Split all nodes with values less into left, Split all nodes with values greater into right. Merge left, right """ left, right = split(root, value - 1) _, right = split(right, value) return merge(left, right) def inorder(root: Node | None) -> None: """ Just recursive print of a tree """ if not root: # None return else: inorder(root.left) print(root.value, end=",") inorder(root.right) def interact_treap(root: Node | None, args: str) -> Node | None: """ Commands: + value to add value into treap - value to erase all nodes with value >>> root = interact_treap(None, "+1") >>> inorder(root) 1, >>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0") >>> inorder(root) 0,1,2,3,4,5,16,17,19, >>> root = interact_treap(root, "+4 +4 +4") >>> inorder(root) 0,1,2,3,4,4,4,4,5,16,17,19, >>> root = interact_treap(root, "-0") >>> inorder(root) 1,2,3,4,4,4,4,5,16,17,19, >>> root = interact_treap(root, "-4") >>> inorder(root) 1,2,3,5,16,17,19, >>> root = interact_treap(root, "=0") Unknown command """ for arg in args.split(): if arg[0] == "+": root = insert(root, int(arg[1:])) elif arg[0] == "-": root = erase(root, int(arg[1:])) else: print("Unknown command") return root def main() -> None: """After each command, program prints treap""" root = None print( "enter numbers to create a tree, + value to add value into treap, " "- value to erase all nodes with value. 'q' to quit. " ) args = input() while args != "q": root = interact_treap(root, args) print(root) args = input() print("good by!") if __name__ == "__main__": import doctest doctest.testmod() main()
Wavelet tree is a datastructure designed to efficiently answer various range queries for arrays. Wavelets trees are different from other binary trees in the sense that the nodes are split based on the actual values of the elements and not on indices, such as the with segment trees or fenwick trees. You can read more about them here: 1. https:users.dcc.uchile.cljperezpapersioiconf16.pdf 2. https:www.youtube.comwatch?v4aSv9PcecDwt811s 3. https:www.youtube.comwatch?vCybAgVFMMct1178s node Nodelength27 reprnode 'Nodeminvalue1 maxvalue1' reprnode strnode True Builds the tree for arr and returns the root of the constructed tree buildtreetestarray Nodeminvalue0 maxvalue9 Leaf node case where the node contains only one unique value Take the mean of min and max element of arr as the pivot and partition arr into leftarr and rightarr with all elements pivot in the leftarr and the rest in rightarr, maintaining the order of the elements, then recursively build trees for leftarr and rightarr Returns the number of occurrences of num in interval 0, index in the list root buildtreetestarray ranktillindexroot, 6, 6 1 ranktillindexroot, 2, 0 1 ranktillindexroot, 1, 10 2 ranktillindexroot, 17, 7 0 ranktillindexroot, 0, 9 1 Leaf node cases go the left subtree and map index to the left subtree go to the right subtree and map index to the right subtree Returns the number of occurrences of num in interval start, end in the list root buildtreetestarray rankroot, 6, 3, 13 2 rankroot, 2, 0, 19 4 rankroot, 9, 2 ,2 0 rankroot, 0, 5, 10 2 Returns the index'th smallest element in interval start, end in the list index is 0indexed root buildtreetestarray quantileroot, 2, 2, 5 5 quantileroot, 5, 2, 13 4 quantileroot, 0, 6, 6 8 quantileroot, 4, 2, 5 1 Leaf node case Number of elements in the left subtree in interval start, end Returns the number of elements in range startnum, endnum in interval start, end in the list root buildtreetestarray rangecountingroot, 1, 10, 3, 7 3 rangecountingroot, 2, 2, 1, 4 1 rangecountingroot, 0, 19, 0, 100 20 rangecountingroot, 1, 0, 1, 100 0 rangecountingroot, 0, 17, 100, 1 0
from __future__ import annotations test_array = [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7] class Node: def __init__(self, length: int) -> None: self.minn: int = -1 self.maxx: int = -1 self.map_left: list[int] = [-1] * length self.left: Node | None = None self.right: Node | None = None def __repr__(self) -> str: """ >>> node = Node(length=27) >>> repr(node) 'Node(min_value=-1 max_value=-1)' >>> repr(node) == str(node) True """ return f"Node(min_value={self.minn} max_value={self.maxx})" def build_tree(arr: list[int]) -> Node | None: """ Builds the tree for arr and returns the root of the constructed tree >>> build_tree(test_array) Node(min_value=0 max_value=9) """ root = Node(len(arr)) root.minn, root.maxx = min(arr), max(arr) # Leaf node case where the node contains only one unique value if root.minn == root.maxx: return root """ Take the mean of min and max element of arr as the pivot and partition arr into left_arr and right_arr with all elements <= pivot in the left_arr and the rest in right_arr, maintaining the order of the elements, then recursively build trees for left_arr and right_arr """ pivot = (root.minn + root.maxx) // 2 left_arr: list[int] = [] right_arr: list[int] = [] for index, num in enumerate(arr): if num <= pivot: left_arr.append(num) else: right_arr.append(num) root.map_left[index] = len(left_arr) root.left = build_tree(left_arr) root.right = build_tree(right_arr) return root def rank_till_index(node: Node | None, num: int, index: int) -> int: """ Returns the number of occurrences of num in interval [0, index] in the list >>> root = build_tree(test_array) >>> rank_till_index(root, 6, 6) 1 >>> rank_till_index(root, 2, 0) 1 >>> rank_till_index(root, 1, 10) 2 >>> rank_till_index(root, 17, 7) 0 >>> rank_till_index(root, 0, 9) 1 """ if index < 0 or node is None: return 0 # Leaf node cases if node.minn == node.maxx: return index + 1 if node.minn == num else 0 pivot = (node.minn + node.maxx) // 2 if num <= pivot: # go the left subtree and map index to the left subtree return rank_till_index(node.left, num, node.map_left[index] - 1) else: # go to the right subtree and map index to the right subtree return rank_till_index(node.right, num, index - node.map_left[index]) def rank(node: Node | None, num: int, start: int, end: int) -> int: """ Returns the number of occurrences of num in interval [start, end] in the list >>> root = build_tree(test_array) >>> rank(root, 6, 3, 13) 2 >>> rank(root, 2, 0, 19) 4 >>> rank(root, 9, 2 ,2) 0 >>> rank(root, 0, 5, 10) 2 """ if start > end: return 0 rank_till_end = rank_till_index(node, num, end) rank_before_start = rank_till_index(node, num, start - 1) return rank_till_end - rank_before_start def quantile(node: Node | None, index: int, start: int, end: int) -> int: """ Returns the index'th smallest element in interval [start, end] in the list index is 0-indexed >>> root = build_tree(test_array) >>> quantile(root, 2, 2, 5) 5 >>> quantile(root, 5, 2, 13) 4 >>> quantile(root, 0, 6, 6) 8 >>> quantile(root, 4, 2, 5) -1 """ if index > (end - start) or start > end or node is None: return -1 # Leaf node case if node.minn == node.maxx: return node.minn # Number of elements in the left subtree in interval [start, end] num_elements_in_left_tree = node.map_left[end] - ( node.map_left[start - 1] if start else 0 ) if num_elements_in_left_tree > index: return quantile( node.left, index, (node.map_left[start - 1] if start else 0), node.map_left[end] - 1, ) else: return quantile( node.right, index - num_elements_in_left_tree, start - (node.map_left[start - 1] if start else 0), end - node.map_left[end], ) def range_counting( node: Node | None, start: int, end: int, start_num: int, end_num: int ) -> int: """ Returns the number of elements in range [start_num, end_num] in interval [start, end] in the list >>> root = build_tree(test_array) >>> range_counting(root, 1, 10, 3, 7) 3 >>> range_counting(root, 2, 2, 1, 4) 1 >>> range_counting(root, 0, 19, 0, 100) 20 >>> range_counting(root, 1, 0, 1, 100) 0 >>> range_counting(root, 0, 17, 100, 1) 0 """ if ( start > end or node is None or start_num > end_num or node.minn > end_num or node.maxx < start_num ): return 0 if start_num <= node.minn and node.maxx <= end_num: return end - start + 1 left = range_counting( node.left, (node.map_left[start - 1] if start else 0), node.map_left[end] - 1, start_num, end_num, ) right = range_counting( node.right, start - (node.map_left[start - 1] if start else 0), end - node.map_left[end], start_num, end_num, ) return left + right if __name__ == "__main__": import doctest doctest.testmod()
Implements a disjoint set using Lists and some added heuristics for efficiency Union by Rank Heuristic and Path Compression Initialize with a list of the number of items in each set and with rank 1 for each set Merge two sets together using Union by rank heuristic Return True if successful Merge two disjoint sets A DisjointSet1, 1, 1 A.merge1, 2 True A.merge0, 2 True A.merge0, 1 False Find the Parent of a given set A DisjointSet1, 1, 1 A.merge1, 2 True A.getparent0 0 A.getparent1 2
class DisjointSet: def __init__(self, set_counts: list) -> None: """ Initialize with a list of the number of items in each set and with rank = 1 for each set """ self.set_counts = set_counts self.max_set = max(set_counts) num_sets = len(set_counts) self.ranks = [1] * num_sets self.parents = list(range(num_sets)) def merge(self, src: int, dst: int) -> bool: """ Merge two sets together using Union by rank heuristic Return True if successful Merge two disjoint sets >>> A = DisjointSet([1, 1, 1]) >>> A.merge(1, 2) True >>> A.merge(0, 2) True >>> A.merge(0, 1) False """ src_parent = self.get_parent(src) dst_parent = self.get_parent(dst) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] self.set_counts[src_parent] = 0 self.parents[src_parent] = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 joined_set_size = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] self.set_counts[dst_parent] = 0 self.parents[dst_parent] = src_parent joined_set_size = self.set_counts[src_parent] self.max_set = max(self.max_set, joined_set_size) return True def get_parent(self, disj_set: int) -> int: """ Find the Parent of a given set >>> A = DisjointSet([1, 1, 1]) >>> A.merge(1, 2) True >>> A.get_parent(0) 0 >>> A.get_parent(1) 2 """ if self.parents[disj_set] == disj_set: return disj_set self.parents[disj_set] = self.get_parent(self.parents[disj_set]) return self.parents[disj_set]
Disjoint set. Reference: https:en.wikipedia.orgwikiDisjointsetdatastructure Make x as a set. rank is the distance from x to its' parent root's rank is 0 Union of two sets. set with bigger rank should be parent, so that the disjoint set tree will be more flat. Return the parent of x Return a Python Standard Library set that contains i. testdisjointset
class Node: def __init__(self, data: int) -> None: self.data = data self.rank: int self.parent: Node def make_set(x: Node) -> None: """ Make x as a set. """ # rank is the distance from x to its' parent # root's rank is 0 x.rank = 0 x.parent = x def union_set(x: Node, y: Node) -> None: """ Union of two sets. set with bigger rank should be parent, so that the disjoint set tree will be more flat. """ x, y = find_set(x), find_set(y) if x == y: return elif x.rank > y.rank: y.parent = x else: x.parent = y if x.rank == y.rank: y.rank += 1 def find_set(x: Node) -> Node: """ Return the parent of x """ if x != x.parent: x.parent = find_set(x.parent) return x.parent def find_python_set(node: Node) -> set: """ Return a Python Standard Library set that contains i. """ sets = ({0, 1, 2}, {3, 4, 5}) for s in sets: if node.data in s: return s msg = f"{node.data} is not in {sets}" raise ValueError(msg) def test_disjoint_set() -> None: """ >>> test_disjoint_set() """ vertex = [Node(i) for i in range(6)] for v in vertex: make_set(v) union_set(vertex[0], vertex[1]) union_set(vertex[1], vertex[2]) union_set(vertex[3], vertex[4]) union_set(vertex[3], vertex[5]) for node0 in vertex: for node1 in vertex: if find_python_set(node0).isdisjoint(find_python_set(node1)): assert find_set(node0) != find_set(node1) else: assert find_set(node0) == find_set(node1) if __name__ == "__main__": test_disjoint_set()
See https:en.wikipedia.orgwikiBloomfilter The use of this data structure is to test membership in a set. Compared to Python's builtin set it is more spaceefficient. In the following example, only 8 bits of memory will be used: bloom Bloomsize8 Initially, the filter contains all zeros: bloom.bitstring '00000000' When an element is added, two bits are set to 1 since there are 2 hash functions in this implementation: Titanic in bloom False bloom.addTitanic bloom.bitstring '01100000' Titanic in bloom True However, sometimes only one bit is added because both hash functions return the same value bloom.addAvatar Avatar in bloom True bloom.formathashAvatar '00000100' bloom.bitstring '01100100' Not added elements should return False ... notpresentfilms The Godfather, Interstellar, Parasite, Pulp Fiction ... film: bloom.formathashfilm for film in notpresentfilms ... doctest: NORMALIZEWHITESPACE 'The Godfather': '00000101', 'Interstellar': '00000011', 'Parasite': '00010010', 'Pulp Fiction': '10000100' anyfilm in bloom for film in notpresentfilms False but sometimes there are false positives: Ratatouille in bloom True bloom.formathashRatatouille '01100000' The probability increases with the number of elements added. The probability decreases with the number of bits in the bitarray. bloom.estimatederrorrate 0.140625 bloom.addThe Godfather bloom.estimatederrorrate 0.25 bloom.bitstring '01100101'
from hashlib import md5, sha256 HASH_FUNCTIONS = (sha256, md5) class Bloom: def __init__(self, size: int = 8) -> None: self.bitarray = 0b0 self.size = size def add(self, value: str) -> None: h = self.hash_(value) self.bitarray |= h def exists(self, value: str) -> bool: h = self.hash_(value) return (h & self.bitarray) == h def __contains__(self, other: str) -> bool: return self.exists(other) def format_bin(self, bitarray: int) -> str: res = bin(bitarray)[2:] return res.zfill(self.size) @property def bitstring(self) -> str: return self.format_bin(self.bitarray) def hash_(self, value: str) -> int: res = 0b0 for func in HASH_FUNCTIONS: position = ( int.from_bytes(func(value.encode()).digest(), "little") % self.size ) res |= 2**position return res def format_hash(self, value: str) -> str: return self.format_bin(self.hash_(value)) @property def estimated_error_rate(self) -> float: n_ones = bin(self.bitarray).count("1") return (n_ones / self.size) ** len(HASH_FUNCTIONS)
!usrbinenv python3 Double hashing is a collision resolving technique in Open Addressed Hash tables. Double hashing uses the idea of applying a second hash function to key when a collision occurs. The advantage of Double hashing is that it is one of the best form of probing, producing a uniform distribution of records throughout a hash table. This technique does not yield any clusters. It is one of effective method for resolving collisions. Double hashing can be done using: hash1key i hash2key TABLESIZE Where hash1 and hash2 are hash functions and TABLESIZE is size of hash table. Reference: https:en.wikipedia.orgwikiDoublehashing Hash Table example with open addressing and Double Hash Examples: 1. Try to add three data elements when the size is three dh DoubleHash3 dh.insertdata10 dh.insertdata20 dh.insertdata30 dh.keys 1: 10, 2: 20, 0: 30 2. Try to add three data elements when the size is two dh DoubleHash2 dh.insertdata10 dh.insertdata20 dh.insertdata30 dh.keys 10: 10, 9: 20, 8: 30 3. Try to add three data elements when the size is four dh DoubleHash4 dh.insertdata10 dh.insertdata20 dh.insertdata30 dh.keys 9: 20, 10: 10, 8: 30
#!/usr/bin/env python3 """ Double hashing is a collision resolving technique in Open Addressed Hash tables. Double hashing uses the idea of applying a second hash function to key when a collision occurs. The advantage of Double hashing is that it is one of the best form of probing, producing a uniform distribution of records throughout a hash table. This technique does not yield any clusters. It is one of effective method for resolving collisions. Double hashing can be done using: (hash1(key) + i * hash2(key)) % TABLE_SIZE Where hash1() and hash2() are hash functions and TABLE_SIZE is size of hash table. Reference: https://en.wikipedia.org/wiki/Double_hashing """ from .hash_table import HashTable from .number_theory.prime_numbers import is_prime, next_prime class DoubleHash(HashTable): """ Hash Table example with open addressing and Double Hash """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def __hash_function_2(self, value, data): next_prime_gt = ( next_prime(value % self.size_table) if not is_prime(value % self.size_table) else value % self.size_table ) # gt = bigger than return next_prime_gt - (data % next_prime_gt) def __hash_double_function(self, key, data, increment): return (increment * self.__hash_function_2(key, data)) % self.size_table def _collision_resolution(self, key, data=None): """ Examples: 1. Try to add three data elements when the size is three >>> dh = DoubleHash(3) >>> dh.insert_data(10) >>> dh.insert_data(20) >>> dh.insert_data(30) >>> dh.keys() {1: 10, 2: 20, 0: 30} 2. Try to add three data elements when the size is two >>> dh = DoubleHash(2) >>> dh.insert_data(10) >>> dh.insert_data(20) >>> dh.insert_data(30) >>> dh.keys() {10: 10, 9: 20, 8: 30} 3. Try to add three data elements when the size is four >>> dh = DoubleHash(4) >>> dh.insert_data(10) >>> dh.insert_data(20) >>> dh.insert_data(30) >>> dh.keys() {9: 20, 10: 10, 8: 30} """ i = 1 new_key = self.hash_function(data) while self.values[new_key] is not None and self.values[new_key] != key: new_key = ( self.__hash_double_function(key, data, i) if self.balanced_factor() >= self.lim_charge else None ) if new_key is None: break else: i += 1 return new_key if __name__ == "__main__": import doctest doctest.testmod()
Hash map with open addressing. https:en.wikipedia.orgwikiHashtable Another hash map implementation, with a good explanation. Modern Dictionaries by Raymond Hettinger https:www.youtube.comwatch?vp33CVV29OG8 Hash map with open addressing. Get next index. Implements linear open addressing. HashMap5.getnextind3 4 HashMap5.getnextind5 1 HashMap5.getnextind6 2 HashMap5.getnextind9 0 Try to add value to the bucket. If bucket is empty or key is the same, does insert and return True. If bucket has another key or deleted placeholder, that means that we need to check next bucket. Return true if we have reached safe capacity. So we need to increase the number of buckets to avoid collisions. hm HashMap2 hm.additem1, 10 hm.additem2, 20 hm.isfull True HashMap2.isfull False Return true if we need twice fewer buckets when we have now. if lenself.buckets self.initialblocksize: return False limit lenself.buckets self.capacityfactor 2 return lenself limit def resizeself, newsize: int None: oldbuckets self.buckets self.buckets None newsize self.len 0 for item in oldbuckets: if item: self.additemitem.key, item.val def sizeupself None: self.resizelenself.buckets 2 def sizedownself None: self.resizelenself.buckets 2 def iteratebucketsself, key: KEY Iteratorint: ind self.getbucketindexkey for in rangelenself.buckets: yield ind ind self.getnextindind def additemself, key: KEY, val: VAL None: for ind in self.iteratebucketskey: if self.trysetind, key, val: break def setitemself, key: KEY, val: VAL None: if self.isfull: self.sizeup self.additemkey, val def delitemself, key: KEY None: Trying to remove a nonexisting item for ind in self.iteratebucketskey: item self.bucketsind if item is None: raise KeyErrorkey if item is deleted: continue if item.key key: self.bucketsind deleted self.len 1 break if self.issparse: self.sizedown def getitemself, key: KEY VAL: for ind in self.iteratebucketskey: item self.bucketsind if item is None: break if item is deleted: continue if item.key key: return item.val raise KeyErrorkey def lenself int: return self.len def iterself IteratorKEY: yield from item.key for item in self.buckets if item def reprself str: valstring , .join fitem.key: item.val for item in self.buckets if item return fHashMapvalstring if name main: import doctest doctest.testmod
from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar KEY = TypeVar("KEY") VAL = TypeVar("VAL") @dataclass(frozen=True, slots=True) class _Item(Generic[KEY, VAL]): key: KEY val: VAL class _DeletedItem(_Item): def __init__(self) -> None: super().__init__(None, None) def __bool__(self) -> bool: return False _deleted = _DeletedItem() class HashMap(MutableMapping[KEY, VAL]): """ Hash map with open addressing. """ def __init__( self, initial_block_size: int = 8, capacity_factor: float = 0.75 ) -> None: self._initial_block_size = initial_block_size self._buckets: list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 self._capacity_factor = capacity_factor self._len = 0 def _get_bucket_index(self, key: KEY) -> int: return hash(key) % len(self._buckets) def _get_next_ind(self, ind: int) -> int: """ Get next index. Implements linear open addressing. >>> HashMap(5)._get_next_ind(3) 4 >>> HashMap(5)._get_next_ind(5) 1 >>> HashMap(5)._get_next_ind(6) 2 >>> HashMap(5)._get_next_ind(9) 0 """ return (ind + 1) % len(self._buckets) def _try_set(self, ind: int, key: KEY, val: VAL) -> bool: """ Try to add value to the bucket. If bucket is empty or key is the same, does insert and return True. If bucket has another key or deleted placeholder, that means that we need to check next bucket. """ stored = self._buckets[ind] if not stored: self._buckets[ind] = _Item(key, val) self._len += 1 return True elif stored.key == key: self._buckets[ind] = _Item(key, val) return True else: return False def _is_full(self) -> bool: """ Return true if we have reached safe capacity. So we need to increase the number of buckets to avoid collisions. >>> hm = HashMap(2) >>> hm._add_item(1, 10) >>> hm._add_item(2, 20) >>> hm._is_full() True >>> HashMap(2)._is_full() False """ limit = len(self._buckets) * self._capacity_factor return len(self) >= int(limit) def _is_sparse(self) -> bool: """Return true if we need twice fewer buckets when we have now.""" if len(self._buckets) <= self._initial_block_size: return False limit = len(self._buckets) * self._capacity_factor / 2 return len(self) < limit def _resize(self, new_size: int) -> None: old_buckets = self._buckets self._buckets = [None] * new_size self._len = 0 for item in old_buckets: if item: self._add_item(item.key, item.val) def _size_up(self) -> None: self._resize(len(self._buckets) * 2) def _size_down(self) -> None: self._resize(len(self._buckets) // 2) def _iterate_buckets(self, key: KEY) -> Iterator[int]: ind = self._get_bucket_index(key) for _ in range(len(self._buckets)): yield ind ind = self._get_next_ind(ind) def _add_item(self, key: KEY, val: VAL) -> None: """ Try to add 3 elements when the size is 5 >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm._add_item(2, 20) >>> hm._add_item(3, 30) >>> hm HashMap(1: 10, 2: 20, 3: 30) Try to add 3 elements when the size is 5 >>> hm = HashMap(5) >>> hm._add_item(-5, 10) >>> hm._add_item(6, 30) >>> hm._add_item(-7, 20) >>> hm HashMap(-5: 10, 6: 30, -7: 20) Try to add 3 elements when size is 1 >>> hm = HashMap(1) >>> hm._add_item(10, 13.2) >>> hm._add_item(6, 5.26) >>> hm._add_item(7, 5.155) >>> hm HashMap(10: 13.2) Trying to add an element with a key that is a floating point value >>> hm = HashMap(5) >>> hm._add_item(1.5, 10) >>> hm HashMap(1.5: 10) 5. Trying to add an item with the same key >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm._add_item(1, 20) >>> hm HashMap(1: 20) """ for ind in self._iterate_buckets(key): if self._try_set(ind, key, val): break def __setitem__(self, key: KEY, val: VAL) -> None: """ 1. Changing value of item whose key is present >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm.__setitem__(1, 20) >>> hm HashMap(1: 20) 2. Changing value of item whose key is not present >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm.__setitem__(0, 20) >>> hm HashMap(0: 20, 1: 10) 3. Changing the value of the same item multiple times >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm.__setitem__(1, 20) >>> hm.__setitem__(1, 30) >>> hm HashMap(1: 30) """ if self._is_full(): self._size_up() self._add_item(key, val) def __delitem__(self, key: KEY) -> None: """ >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm._add_item(2, 20) >>> hm._add_item(3, 30) >>> hm.__delitem__(3) >>> hm HashMap(1: 10, 2: 20) >>> hm = HashMap(5) >>> hm._add_item(-5, 10) >>> hm._add_item(6, 30) >>> hm._add_item(-7, 20) >>> hm.__delitem__(-5) >>> hm HashMap(6: 30, -7: 20) # Trying to remove a non-existing item >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm._add_item(2, 20) >>> hm._add_item(3, 30) >>> hm.__delitem__(4) Traceback (most recent call last): ... KeyError: 4 """ for ind in self._iterate_buckets(key): item = self._buckets[ind] if item is None: raise KeyError(key) if item is _deleted: continue if item.key == key: self._buckets[ind] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__(self, key: KEY) -> VAL: """ Returns the item at the given key >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm.__getitem__(1) 10 >>> hm = HashMap(5) >>> hm._add_item(10, -10) >>> hm._add_item(20, -20) >>> hm.__getitem__(20) -20 >>> hm = HashMap(5) >>> hm._add_item(-1, 10) >>> hm.__getitem__(-1) 10 """ for ind in self._iterate_buckets(key): item = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(key) def __len__(self) -> int: """ Returns the number of items present in hashmap >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm._add_item(2, 20) >>> hm._add_item(3, 30) >>> hm.__len__() 3 >>> hm = HashMap(5) >>> hm.__len__() 0 """ return self._len def __iter__(self) -> Iterator[KEY]: yield from (item.key for item in self._buckets if item) def __repr__(self) -> str: val_string = ", ".join( f"{item.key}: {item.val}" for item in self._buckets if item ) return f"HashMap({val_string})" if __name__ == "__main__": import doctest doctest.testmod()
!usrbinenv python3 Basic Hash Table example with open addressing and linear probing The keys function returns a dictionary containing the key value pairs. key being the index number in hash table and value being the data value. Examples: 1. creating HashTable with size 10 and inserting 3 elements ht HashTable10 ht.insertdata10 ht.insertdata20 ht.insertdata30 ht.keys 0: 10, 1: 20, 2: 30 2. creating HashTable with size 5 and inserting 5 elements ht HashTable5 ht.insertdata5 ht.insertdata4 ht.insertdata3 ht.insertdata2 ht.insertdata1 ht.keys 0: 5, 4: 4, 3: 3, 2: 2, 1: 1 Generates hash for the given key value Examples: Creating HashTable with size 5 ht HashTable5 ht.hashfunction10 0 ht.hashfunction20 0 ht.hashfunction4 4 ht.hashfunction18 3 ht.hashfunction18 2 ht.hashfunction18.5 3.5 ht.hashfunction0 0 ht.hashfunction0 0 bulkinsert is used for entering more than one element at a time in the HashTable. Examples: 1. ht HashTable5 ht.bulkinsert10,20,30 step 1 0, 1, 2, 3, 4 10, None, None, None, None step 2 0, 1, 2, 3, 4 10, 20, None, None, None step 3 0, 1, 2, 3, 4 10, 20, 30, None, None 2. ht HashTable5 ht.bulkinsert5,4,3,2,1 step 1 0, 1, 2, 3, 4 5, None, None, None, None step 2 0, 1, 2, 3, 4 5, None, None, None, 4 step 3 0, 1, 2, 3, 4 5, None, None, 3, 4 step 4 0, 1, 2, 3, 4 5, None, 2, 3, 4 step 5 0, 1, 2, 3, 4 5, 1, 2, 3, 4 setvalue functions allows to update value at a particular hash Examples: 1. setvalue in HashTable of size 5 ht HashTable5 ht.insertdata10 ht.insertdata20 ht.insertdata30 ht.setvalue0,15 ht.keys 0: 15, 1: 20, 2: 30 2. setvalue in HashTable of size 2 ht HashTable2 ht.insertdata17 ht.insertdata18 ht.insertdata99 ht.setvalue3,15 ht.keys 3: 15, 2: 17, 4: 99 3. setvalue in HashTable when hash is not present ht HashTable2 ht.insertdata17 ht.insertdata18 ht.insertdata99 ht.setvalue0,15 ht.keys 3: 18, 2: 17, 4: 99, 0: 15 4. setvalue in HashTable when multiple hash are not present ht HashTable2 ht.insertdata17 ht.insertdata18 ht.insertdata99 ht.setvalue0,15 ht.setvalue1,20 ht.keys 3: 18, 2: 17, 4: 99, 0: 15, 1: 20 This method is a type of open addressing which is used for handling collision. In this implementation the concept of linear probing has been used. The hash table is searched sequentially from the original location of the hash, if the new hashlocation we get is already occupied we check for the next hashlocation. references: https:en.wikipedia.orgwikiLinearprobing Examples: 1. The collision will be with keys 18 99, so new hash will be created for 99 ht HashTable3 ht.insertdata17 ht.insertdata18 ht.insertdata99 ht.keys 2: 17, 0: 18, 1: 99 2. The collision will be with keys 17 101, so new hash will be created for 101 ht HashTable4 ht.insertdata17 ht.insertdata18 ht.insertdata99 ht.insertdata101 ht.keys 1: 17, 2: 18, 3: 99, 0: 101 2. The collision will be with all keys, so new hash will be created for all ht HashTable1 ht.insertdata17 ht.insertdata18 ht.insertdata99 ht.keys 2: 17, 3: 18, 4: 99 3. Trying to insert float key in hash ht HashTable1 ht.insertdata17 ht.insertdata18 ht.insertdata99.99 Traceback most recent call last: ... TypeError: list indices must be integers or slices, not float insertdata is used for inserting a single element at a time in the HashTable. Examples: ht HashTable3 ht.insertdata5 ht.keys 2: 5 ht HashTable5 ht.insertdata30 ht.insertdata50 ht.keys 0: 30, 1: 50
#!/usr/bin/env python3 from .number_theory.prime_numbers import next_prime class HashTable: """ Basic Hash Table example with open addressing and linear probing """ def __init__( self, size_table: int, charge_factor: int | None = None, lim_charge: float | None = None, ) -> None: self.size_table = size_table self.values = [None] * self.size_table self.lim_charge = 0.75 if lim_charge is None else lim_charge self.charge_factor = 1 if charge_factor is None else charge_factor self.__aux_list: list = [] self._keys: dict = {} def keys(self): """ The keys function returns a dictionary containing the key value pairs. key being the index number in hash table and value being the data value. Examples: 1. creating HashTable with size 10 and inserting 3 elements >>> ht = HashTable(10) >>> ht.insert_data(10) >>> ht.insert_data(20) >>> ht.insert_data(30) >>> ht.keys() {0: 10, 1: 20, 2: 30} 2. creating HashTable with size 5 and inserting 5 elements >>> ht = HashTable(5) >>> ht.insert_data(5) >>> ht.insert_data(4) >>> ht.insert_data(3) >>> ht.insert_data(2) >>> ht.insert_data(1) >>> ht.keys() {0: 5, 4: 4, 3: 3, 2: 2, 1: 1} """ return self._keys def balanced_factor(self): return sum(1 for slot in self.values if slot is not None) / ( self.size_table * self.charge_factor ) def hash_function(self, key): """ Generates hash for the given key value Examples: Creating HashTable with size 5 >>> ht = HashTable(5) >>> ht.hash_function(10) 0 >>> ht.hash_function(20) 0 >>> ht.hash_function(4) 4 >>> ht.hash_function(18) 3 >>> ht.hash_function(-18) 2 >>> ht.hash_function(18.5) 3.5 >>> ht.hash_function(0) 0 >>> ht.hash_function(-0) 0 """ return key % self.size_table def _step_by_step(self, step_ord): print(f"step {step_ord}") print(list(range(len(self.values)))) print(self.values) def bulk_insert(self, values): """ bulk_insert is used for entering more than one element at a time in the HashTable. Examples: 1. >>> ht = HashTable(5) >>> ht.bulk_insert((10,20,30)) step 1 [0, 1, 2, 3, 4] [10, None, None, None, None] step 2 [0, 1, 2, 3, 4] [10, 20, None, None, None] step 3 [0, 1, 2, 3, 4] [10, 20, 30, None, None] 2. >>> ht = HashTable(5) >>> ht.bulk_insert([5,4,3,2,1]) step 1 [0, 1, 2, 3, 4] [5, None, None, None, None] step 2 [0, 1, 2, 3, 4] [5, None, None, None, 4] step 3 [0, 1, 2, 3, 4] [5, None, None, 3, 4] step 4 [0, 1, 2, 3, 4] [5, None, 2, 3, 4] step 5 [0, 1, 2, 3, 4] [5, 1, 2, 3, 4] """ i = 1 self.__aux_list = values for value in values: self.insert_data(value) self._step_by_step(i) i += 1 def _set_value(self, key, data): """ _set_value functions allows to update value at a particular hash Examples: 1. _set_value in HashTable of size 5 >>> ht = HashTable(5) >>> ht.insert_data(10) >>> ht.insert_data(20) >>> ht.insert_data(30) >>> ht._set_value(0,15) >>> ht.keys() {0: 15, 1: 20, 2: 30} 2. _set_value in HashTable of size 2 >>> ht = HashTable(2) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht._set_value(3,15) >>> ht.keys() {3: 15, 2: 17, 4: 99} 3. _set_value in HashTable when hash is not present >>> ht = HashTable(2) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht._set_value(0,15) >>> ht.keys() {3: 18, 2: 17, 4: 99, 0: 15} 4. _set_value in HashTable when multiple hash are not present >>> ht = HashTable(2) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht._set_value(0,15) >>> ht._set_value(1,20) >>> ht.keys() {3: 18, 2: 17, 4: 99, 0: 15, 1: 20} """ self.values[key] = data self._keys[key] = data def _collision_resolution(self, key, data=None): """ This method is a type of open addressing which is used for handling collision. In this implementation the concept of linear probing has been used. The hash table is searched sequentially from the original location of the hash, if the new hash/location we get is already occupied we check for the next hash/location. references: - https://en.wikipedia.org/wiki/Linear_probing Examples: 1. The collision will be with keys 18 & 99, so new hash will be created for 99 >>> ht = HashTable(3) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.keys() {2: 17, 0: 18, 1: 99} 2. The collision will be with keys 17 & 101, so new hash will be created for 101 >>> ht = HashTable(4) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.insert_data(101) >>> ht.keys() {1: 17, 2: 18, 3: 99, 0: 101} 2. The collision will be with all keys, so new hash will be created for all >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.keys() {2: 17, 3: 18, 4: 99} 3. Trying to insert float key in hash >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99.99) Traceback (most recent call last): ... TypeError: list indices must be integers or slices, not float """ new_key = self.hash_function(key + 1) while self.values[new_key] is not None and self.values[new_key] != key: if self.values.count(None) > 0: new_key = self.hash_function(new_key + 1) else: new_key = None break return new_key def rehashing(self): survivor_values = [value for value in self.values if value is not None] self.size_table = next_prime(self.size_table, factor=2) self._keys.clear() self.values = [None] * self.size_table # hell's pointers D: don't DRY ;/ for value in survivor_values: self.insert_data(value) def insert_data(self, data): """ insert_data is used for inserting a single element at a time in the HashTable. Examples: >>> ht = HashTable(3) >>> ht.insert_data(5) >>> ht.keys() {2: 5} >>> ht = HashTable(5) >>> ht.insert_data(30) >>> ht.insert_data(50) >>> ht.keys() {0: 30, 1: 50} """ key = self.hash_function(data) if self.values[key] is None: self._set_value(key, data) elif self.values[key] == data: pass else: collision_resolution = self._collision_resolution(key, data) if collision_resolution is not None: self._set_value(collision_resolution, data) else: self.rehashing() self.insert_data(data) if __name__ == "__main__": import doctest doctest.testmod()
!usrbinenv python3 module to operations with prime numbers Checks to see if a number is a prime in Osqrtn. A number is prime if it has exactly two factors: 1 and itself. isprime0 False isprime1 False isprime2 True isprime3 True isprime27 False isprime87 False isprime563 True isprime2999 True isprime67483 False precondition 2 and 3 are primes Negatives, 0, 1 and all even numbers are not primes
#!/usr/bin/env python3 """ module to operations with prime numbers """ import math def is_prime(number: int) -> bool: """Checks to see if a number is a prime in O(sqrt(n)). A number is prime if it has exactly two factors: 1 and itself. >>> is_prime(0) False >>> is_prime(1) False >>> is_prime(2) True >>> is_prime(3) True >>> is_prime(27) False >>> is_prime(87) False >>> is_prime(563) True >>> is_prime(2999) True >>> is_prime(67483) False """ # precondition assert isinstance(number, int) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or not number % 2: # Negatives, 0, 1 and all even numbers are not primes return False odd_numbers = range(3, int(math.sqrt(number) + 1), 2) return not any(not number % i for i in odd_numbers) def next_prime(value, factor=1, **kwargs): value = factor * value first_value_val = value while not is_prime(value): value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1 if value == first_value_val: return next_prime(value + 1, **kwargs) return value
!usrbinenv python3 Basic Hash Table example with open addressing using Quadratic Probing Quadratic probing is an open addressing scheme used for resolving collisions in hash table. It works by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until open slot is found. Hash 1, Hash 2, Hash 3 .... Hash n reference: https:en.wikipedia.orgwikiQuadraticprobing e.g: 1. Create hash table with size 7 qp QuadraticProbing7 qp.insertdata90 qp.insertdata340 qp.insertdata24 qp.insertdata45 qp.insertdata99 qp.insertdata73 qp.insertdata7 qp.keys 11: 45, 14: 99, 7: 24, 0: 340, 5: 73, 6: 90, 8: 7 2. Create hash table with size 8 qp QuadraticProbing8 qp.insertdata0 qp.insertdata999 qp.insertdata111 qp.keys 0: 0, 7: 999, 3: 111 3. Try to add three data elements when the size is two qp QuadraticProbing2 qp.insertdata0 qp.insertdata999 qp.insertdata111 qp.keys 0: 0, 4: 999, 1: 111 4. Try to add three data elements when the size is one qp QuadraticProbing1 qp.insertdata0 qp.insertdata999 qp.insertdata111 qp.keys 4: 999, 1: 111
#!/usr/bin/env python3 from .hash_table import HashTable class QuadraticProbing(HashTable): """ Basic Hash Table example with open addressing using Quadratic Probing """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _collision_resolution(self, key, data=None): """ Quadratic probing is an open addressing scheme used for resolving collisions in hash table. It works by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until open slot is found. Hash + 1², Hash + 2², Hash + 3² .... Hash + n² reference: - https://en.wikipedia.org/wiki/Quadratic_probing e.g: 1. Create hash table with size 7 >>> qp = QuadraticProbing(7) >>> qp.insert_data(90) >>> qp.insert_data(340) >>> qp.insert_data(24) >>> qp.insert_data(45) >>> qp.insert_data(99) >>> qp.insert_data(73) >>> qp.insert_data(7) >>> qp.keys() {11: 45, 14: 99, 7: 24, 0: 340, 5: 73, 6: 90, 8: 7} 2. Create hash table with size 8 >>> qp = QuadraticProbing(8) >>> qp.insert_data(0) >>> qp.insert_data(999) >>> qp.insert_data(111) >>> qp.keys() {0: 0, 7: 999, 3: 111} 3. Try to add three data elements when the size is two >>> qp = QuadraticProbing(2) >>> qp.insert_data(0) >>> qp.insert_data(999) >>> qp.insert_data(111) >>> qp.keys() {0: 0, 4: 999, 1: 111} 4. Try to add three data elements when the size is one >>> qp = QuadraticProbing(1) >>> qp.insert_data(0) >>> qp.insert_data(999) >>> qp.insert_data(111) >>> qp.keys() {4: 999, 1: 111} """ i = 1 new_key = self.hash_function(key + i * i) while self.values[new_key] is not None and self.values[new_key] != key: i += 1 new_key = ( self.hash_function(key + i * i) if not self.balanced_factor() >= self.lim_charge else None ) if new_key is None: break return new_key if __name__ == "__main__": import doctest doctest.testmod()
Binomial Heap Reference: Advanced Data Structures, Peter Brass Node in a doublylinked binomial tree, containing: value size of left subtree link to left, right and parent nodes Number of nodes in left subtree Inplace merge of two binomial trees of equal size. Returns the root of the resulting tree 31 def initself, bottomrootNone, minnodeNone, heapsize0: self.size heapsize self.bottomroot bottomroot self.minnode minnode def mergeheapsself, other: Empty heaps corner cases if other.size 0: return None if self.size 0: self.size other.size self.bottomroot other.bottomroot self.minnode other.minnode return None Update size self.size self.size other.size Update min.node if self.minnode.val other.minnode.val: self.minnode other.minnode Merge Order roots by leftsubtreesize combinedrootslist i, j self.bottomroot, other.bottomroot while i or j: if i and not j or i.lefttreesize j.lefttreesize: combinedrootslist.appendi, True i i.parent else: combinedrootslist.appendj, False j j.parent Insert links between them for i in rangelencombinedrootslist 1: if combinedrootslisti1 ! combinedrootslisti 11: combinedrootslisti0.parent combinedrootslisti 10 combinedrootslisti 10.left combinedrootslisti0 Consecutively merge roots with same lefttreesize i combinedrootslist00 while i.parent: if i.lefttreesize i.parent.lefttreesize and not i.parent.parent or i.lefttreesize i.parent.lefttreesize and i.lefttreesize ! i.parent.parent.lefttreesize : Neighbouring Nodes previousnode i.left nextnode i.parent.parent Merging trees i i.mergetreesi.parent Updating links i.left previousnode i.parent nextnode if previousnode: previousnode.parent i if nextnode: nextnode.left i else: i i.parent Updating self.bottomroot while i.left: i i.left self.bottomroot i Update other other.size self.size other.bottomroot self.bottomroot other.minnode self.minnode Return the merged heap return self def insertself, val: if self.size 0: self.bottomroot Nodeval self.size 1 self.minnode self.bottomroot else: Create new node newnode Nodeval Update size self.size 1 update minnode if val self.minnode.val: self.minnode newnode Put newnode as a bottomroot in heap self.bottomroot.left newnode newnode.parent self.bottomroot self.bottomroot newnode Consecutively merge roots with same lefttreesize while self.bottomroot.parent and self.bottomroot.lefttreesize self.bottomroot.parent.lefttreesize : Next node nextnode self.bottomroot.parent.parent Merge self.bottomroot self.bottomroot.mergetreesself.bottomroot.parent Update Links self.bottomroot.parent nextnode self.bottomroot.left None if nextnode: nextnode.left self.bottomroot def peekself: return self.minnode.val def isemptyself: return self.size 0 def deleteminself: assert not self.isEmpty, Empty Heap Save minimal value minvalue self.minnode.val Last element in heap corner case if self.size 1: Update size self.size 0 Update bottom root self.bottomroot None Update minnode self.minnode None return minvalue No right subtree corner case The structure of the tree implies that this should be the bottom root and there is at least one other root if self.minnode.right is None: Update size self.size 1 Update bottom root self.bottomroot self.bottomroot.parent self.bottomroot.left None Update minnode self.minnode self.bottomroot i self.bottomroot.parent while i: if i.val self.minnode.val: self.minnode i i i.parent return minvalue General case Find the BinomialHeap of the right subtree of minnode bottomofnew self.minnode.right bottomofnew.parent None minofnew bottomofnew sizeofnew 1 Size, minnode and bottomroot while bottomofnew.left: sizeofnew sizeofnew 2 1 bottomofnew bottomofnew.left if bottomofnew.val minofnew.val: minofnew bottomofnew Corner case of single root on top left path if not self.minnode.left and not self.minnode.parent: self.size sizeofnew self.bottomroot bottomofnew self.minnode minofnew printSingle root, multiple nodes case return minvalue Remaining cases Construct heap of right subtree newheap BinomialHeap bottomrootbottomofnew, minnodeminofnew, heapsizesizeofnew Update size self.size self.size 1 sizeofnew Neighbour nodes previousnode self.minnode.left nextnode self.minnode.parent Initialize new bottomroot and minnode self.minnode previousnode or nextnode self.bottomroot nextnode Update links of previousnode and search below for new minnode and bottomroot if previousnode: previousnode.parent nextnode Update bottomroot and search for minnode below self.bottomroot previousnode self.minnode previousnode while self.bottomroot.left: self.bottomroot self.bottomroot.left if self.bottomroot.val self.minnode.val: self.minnode self.bottomroot if nextnode: nextnode.left previousnode Search for new minnode above minnode i nextnode while i: if i.val self.minnode.val: self.minnode i i i.parent Merge heaps self.mergeheapsnewheap return minvalue def preorderself: Find top root toproot self.bottomroot while toproot.parent: toproot toproot.parent preorder heappreorder self.traversaltoproot, heappreorder return heappreorder def traversalself, currnode, preorder, level0: if currnode: preorder.appendcurrnode.val, level self.traversalcurrnode.left, preorder, level 1 self.traversalcurrnode.right, preorder, level 1 else: preorder.append, level def strself: if self.isempty: return preorderheap self.preorder return n.join level strvalue for value, level in preorderheap Unit Tests if name main: import doctest doctest.testmod
class Node: """ Node in a doubly-linked binomial tree, containing: - value - size of left subtree - link to left, right and parent nodes """ def __init__(self, val): self.val = val # Number of nodes in left subtree self.left_tree_size = 0 self.left = None self.right = None self.parent = None def merge_trees(self, other): """ In-place merge of two binomial trees of equal size. Returns the root of the resulting tree """ assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks" if self.val < other.val: other.left = self.right other.parent = None if self.right: self.right.parent = other self.right = other self.left_tree_size = self.left_tree_size * 2 + 1 return self else: self.left = other.right self.parent = None if other.right: other.right.parent = self other.right = self other.left_tree_size = other.left_tree_size * 2 + 1 return other class BinomialHeap: r""" Min-oriented priority queue implemented with the Binomial Heap data structure implemented with the BinomialHeap class. It supports: - Insert element in a heap with n elements: Guaranteed logn, amoratized 1 - Merge (meld) heaps of size m and n: O(logn + logm) - Delete Min: O(logn) - Peek (return min without deleting it): O(1) Example: Create a random permutation of 30 integers to be inserted and 19 of them deleted >>> import numpy as np >>> permutation = np.random.permutation(list(range(30))) Create a Heap and insert the 30 integers __init__() test >>> first_heap = BinomialHeap() 30 inserts - insert() test >>> for number in permutation: ... first_heap.insert(number) Size test >>> first_heap.size 30 Deleting - delete() test >>> [first_heap.delete_min() for _ in range(20)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Create a new Heap >>> second_heap = BinomialHeap() >>> vals = [17, 20, 31, 34] >>> for value in vals: ... second_heap.insert(value) The heap should have the following structure: 17 / \ # 31 / \ 20 34 / \ / \ # # # # preOrder() test >>> " ".join(str(x) for x in second_heap.pre_order()) "(17, 0) ('#', 1) (31, 1) (20, 2) ('#', 3) ('#', 3) (34, 2) ('#', 3) ('#', 3)" printing Heap - __str__() test >>> print(second_heap) 17 -# -31 --20 ---# ---# --34 ---# ---# mergeHeaps() test >>> >>> merged = second_heap.merge_heaps(first_heap) >>> merged.peek() 17 values in merged heap; (merge is inplace) >>> results = [] >>> while not first_heap.is_empty(): ... results.append(first_heap.delete_min()) >>> results [17, 20, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 34] """ def __init__(self, bottom_root=None, min_node=None, heap_size=0): self.size = heap_size self.bottom_root = bottom_root self.min_node = min_node def merge_heaps(self, other): """ In-place merge of two binomial heaps. Both of them become the resulting merged heap """ # Empty heaps corner cases if other.size == 0: return None if self.size == 0: self.size = other.size self.bottom_root = other.bottom_root self.min_node = other.min_node return None # Update size self.size = self.size + other.size # Update min.node if self.min_node.val > other.min_node.val: self.min_node = other.min_node # Merge # Order roots by left_subtree_size combined_roots_list = [] i, j = self.bottom_root, other.bottom_root while i or j: if i and ((not j) or i.left_tree_size < j.left_tree_size): combined_roots_list.append((i, True)) i = i.parent else: combined_roots_list.append((j, False)) j = j.parent # Insert links between them for i in range(len(combined_roots_list) - 1): if combined_roots_list[i][1] != combined_roots_list[i + 1][1]: combined_roots_list[i][0].parent = combined_roots_list[i + 1][0] combined_roots_list[i + 1][0].left = combined_roots_list[i][0] # Consecutively merge roots with same left_tree_size i = combined_roots_list[0][0] while i.parent: if ( (i.left_tree_size == i.parent.left_tree_size) and (not i.parent.parent) ) or ( i.left_tree_size == i.parent.left_tree_size and i.left_tree_size != i.parent.parent.left_tree_size ): # Neighbouring Nodes previous_node = i.left next_node = i.parent.parent # Merging trees i = i.merge_trees(i.parent) # Updating links i.left = previous_node i.parent = next_node if previous_node: previous_node.parent = i if next_node: next_node.left = i else: i = i.parent # Updating self.bottom_root while i.left: i = i.left self.bottom_root = i # Update other other.size = self.size other.bottom_root = self.bottom_root other.min_node = self.min_node # Return the merged heap return self def insert(self, val): """ insert a value in the heap """ if self.size == 0: self.bottom_root = Node(val) self.size = 1 self.min_node = self.bottom_root else: # Create new node new_node = Node(val) # Update size self.size += 1 # update min_node if val < self.min_node.val: self.min_node = new_node # Put new_node as a bottom_root in heap self.bottom_root.left = new_node new_node.parent = self.bottom_root self.bottom_root = new_node # Consecutively merge roots with same left_tree_size while ( self.bottom_root.parent and self.bottom_root.left_tree_size == self.bottom_root.parent.left_tree_size ): # Next node next_node = self.bottom_root.parent.parent # Merge self.bottom_root = self.bottom_root.merge_trees(self.bottom_root.parent) # Update Links self.bottom_root.parent = next_node self.bottom_root.left = None if next_node: next_node.left = self.bottom_root def peek(self): """ return min element without deleting it """ return self.min_node.val def is_empty(self): return self.size == 0 def delete_min(self): """ delete min element and return it """ # assert not self.isEmpty(), "Empty Heap" # Save minimal value min_value = self.min_node.val # Last element in heap corner case if self.size == 1: # Update size self.size = 0 # Update bottom root self.bottom_root = None # Update min_node self.min_node = None return min_value # No right subtree corner case # The structure of the tree implies that this should be the bottom root # and there is at least one other root if self.min_node.right is None: # Update size self.size -= 1 # Update bottom root self.bottom_root = self.bottom_root.parent self.bottom_root.left = None # Update min_node self.min_node = self.bottom_root i = self.bottom_root.parent while i: if i.val < self.min_node.val: self.min_node = i i = i.parent return min_value # General case # Find the BinomialHeap of the right subtree of min_node bottom_of_new = self.min_node.right bottom_of_new.parent = None min_of_new = bottom_of_new size_of_new = 1 # Size, min_node and bottom_root while bottom_of_new.left: size_of_new = size_of_new * 2 + 1 bottom_of_new = bottom_of_new.left if bottom_of_new.val < min_of_new.val: min_of_new = bottom_of_new # Corner case of single root on top left path if (not self.min_node.left) and (not self.min_node.parent): self.size = size_of_new self.bottom_root = bottom_of_new self.min_node = min_of_new # print("Single root, multiple nodes case") return min_value # Remaining cases # Construct heap of right subtree new_heap = BinomialHeap( bottom_root=bottom_of_new, min_node=min_of_new, heap_size=size_of_new ) # Update size self.size = self.size - 1 - size_of_new # Neighbour nodes previous_node = self.min_node.left next_node = self.min_node.parent # Initialize new bottom_root and min_node self.min_node = previous_node or next_node self.bottom_root = next_node # Update links of previous_node and search below for new min_node and # bottom_root if previous_node: previous_node.parent = next_node # Update bottom_root and search for min_node below self.bottom_root = previous_node self.min_node = previous_node while self.bottom_root.left: self.bottom_root = self.bottom_root.left if self.bottom_root.val < self.min_node.val: self.min_node = self.bottom_root if next_node: next_node.left = previous_node # Search for new min_node above min_node i = next_node while i: if i.val < self.min_node.val: self.min_node = i i = i.parent # Merge heaps self.merge_heaps(new_heap) return min_value def pre_order(self): """ Returns the Pre-order representation of the heap including values of nodes plus their level distance from the root; Empty nodes appear as # """ # Find top root top_root = self.bottom_root while top_root.parent: top_root = top_root.parent # preorder heap_pre_order = [] self.__traversal(top_root, heap_pre_order) return heap_pre_order def __traversal(self, curr_node, preorder, level=0): """ Pre-order traversal of nodes """ if curr_node: preorder.append((curr_node.val, level)) self.__traversal(curr_node.left, preorder, level + 1) self.__traversal(curr_node.right, preorder, level + 1) else: preorder.append(("#", level)) def __str__(self): """ Overwriting str for a pre-order print of nodes in heap; Performance is poor, so use only for small examples """ if self.is_empty(): return "" preorder_heap = self.pre_order() return "\n".join(("-" * level + str(value)) for value, level in preorder_heap) # Unit Tests if __name__ == "__main__": import doctest doctest.testmod()
A Max Heap Implementation unsorted 103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5 h Heap h.buildmaxheapunsorted h 209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5 h.extractmax 209 h 201, 107, 25, 103, 11, 15, 1, 9, 7, 5 h.insert100 h 201, 107, 25, 103, 100, 15, 1, 9, 7, 5, 11 h.heapsort h 1, 5, 7, 9, 11, 15, 25, 100, 103, 107, 201 returns the parent index based on the given child index h Heap h.buildmaxheap103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5 h 209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5 h.parentindex1 returns none if index is 0 h.parentindex0 returns none if index is 0 h.parentindex1 0 h.parentindex2 0 h.parentindex3 1 h.parentindex4 1 h.parentindex5 2 h.parentindex10.5 4.0 h.parentindex209.0 104.0 h.parentindexTest Traceback most recent call last: ... TypeError: '' not supported between instances of 'str' and 'int' return the left child index if the left child exists. if not, return None. return the right child index if the right child exists. if not, return None. correct a single violation of the heap property in a subtree's root. It is the function that is responsible for restoring the property of Max heap i.e the maximum element is always at top. check which child is larger than its parent if violation indeed exists swap to fix the violation fix the subsequent violation recursively if any build max heap from an unsorted array h Heap h.buildmaxheap20,40,50,20,10 h 50, 40, 20, 20, 10 h Heap h.buildmaxheap1,2,3,4,5,6,7,8,9,0 h 9, 8, 7, 4, 5, 6, 3, 2, 1, 0 h Heap h.buildmaxheap514,5,61,57,8,99,105 h 514, 57, 105, 5, 8, 99, 61 h Heap h.buildmaxheap514,5,61.6,57,8,9.9,105 h 514, 57, 105, 5, 8, 9.9, 61.6 maxheapify from right to left but exclude leaves last level get and remove max from heap h Heap h.buildmaxheap20,40,50,20,10 h.extractmax 50 h Heap h.buildmaxheap514,5,61,57,8,99,105 h.extractmax 514 h Heap h.buildmaxheap1,2,3,4,5,6,7,8,9,0 h.extractmax 9 insert a new value into the max heap h Heap h.insert10 h 10 h Heap h.insert10 h.insert10 h 10, 10 h Heap h.insert10 h.insert10.1 h 10.1, 10 h Heap h.insert0.1 h.insert0 h.insert9 h.insert5 h 9, 5, 0.1, 0 run doc test demo
from __future__ import annotations from abc import abstractmethod from collections.abc import Iterable from typing import Generic, Protocol, TypeVar class Comparable(Protocol): @abstractmethod def __lt__(self: T, other: T) -> bool: pass @abstractmethod def __gt__(self: T, other: T) -> bool: pass @abstractmethod def __eq__(self: T, other: object) -> bool: pass T = TypeVar("T", bound=Comparable) class Heap(Generic[T]): """A Max Heap Implementation >>> unsorted = [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5] >>> h = Heap() >>> h.build_max_heap(unsorted) >>> h [209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5] >>> >>> h.extract_max() 209 >>> h [201, 107, 25, 103, 11, 15, 1, 9, 7, 5] >>> >>> h.insert(100) >>> h [201, 107, 25, 103, 100, 15, 1, 9, 7, 5, 11] >>> >>> h.heap_sort() >>> h [1, 5, 7, 9, 11, 15, 25, 100, 103, 107, 201] """ def __init__(self) -> None: self.h: list[T] = [] self.heap_size: int = 0 def __repr__(self) -> str: return str(self.h) def parent_index(self, child_idx: int) -> int | None: """ returns the parent index based on the given child index >>> h = Heap() >>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5]) >>> h [209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5] >>> h.parent_index(-1) # returns none if index is <=0 >>> h.parent_index(0) # returns none if index is <=0 >>> h.parent_index(1) 0 >>> h.parent_index(2) 0 >>> h.parent_index(3) 1 >>> h.parent_index(4) 1 >>> h.parent_index(5) 2 >>> h.parent_index(10.5) 4.0 >>> h.parent_index(209.0) 104.0 >>> h.parent_index("Test") Traceback (most recent call last): ... TypeError: '>' not supported between instances of 'str' and 'int' """ if child_idx > 0: return (child_idx - 1) // 2 return None def left_child_idx(self, parent_idx: int) -> int | None: """ return the left child index if the left child exists. if not, return None. """ left_child_index = 2 * parent_idx + 1 if left_child_index < self.heap_size: return left_child_index return None def right_child_idx(self, parent_idx: int) -> int | None: """ return the right child index if the right child exists. if not, return None. """ right_child_index = 2 * parent_idx + 2 if right_child_index < self.heap_size: return right_child_index return None def max_heapify(self, index: int) -> None: """ correct a single violation of the heap property in a subtree's root. It is the function that is responsible for restoring the property of Max heap i.e the maximum element is always at top. """ if index < self.heap_size: violation: int = index left_child = self.left_child_idx(index) right_child = self.right_child_idx(index) # check which child is larger than its parent if left_child is not None and self.h[left_child] > self.h[violation]: violation = left_child if right_child is not None and self.h[right_child] > self.h[violation]: violation = right_child # if violation indeed exists if violation != index: # swap to fix the violation self.h[violation], self.h[index] = self.h[index], self.h[violation] # fix the subsequent violation recursively if any self.max_heapify(violation) def build_max_heap(self, collection: Iterable[T]) -> None: """ build max heap from an unsorted array >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) >>> h [50, 40, 20, 20, 10] >>> h = Heap() >>> h.build_max_heap([1,2,3,4,5,6,7,8,9,0]) >>> h [9, 8, 7, 4, 5, 6, 3, 2, 1, 0] >>> h = Heap() >>> h.build_max_heap([514,5,61,57,8,99,105]) >>> h [514, 57, 105, 5, 8, 99, 61] >>> h = Heap() >>> h.build_max_heap([514,5,61.6,57,8,9.9,105]) >>> h [514, 57, 105, 5, 8, 9.9, 61.6] """ self.h = list(collection) self.heap_size = len(self.h) if self.heap_size > 1: # max_heapify from right to left but exclude leaves (last level) for i in range(self.heap_size // 2 - 1, -1, -1): self.max_heapify(i) def extract_max(self) -> T: """ get and remove max from heap >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) >>> h.extract_max() 50 >>> h = Heap() >>> h.build_max_heap([514,5,61,57,8,99,105]) >>> h.extract_max() 514 >>> h = Heap() >>> h.build_max_heap([1,2,3,4,5,6,7,8,9,0]) >>> h.extract_max() 9 """ if self.heap_size >= 2: me = self.h[0] self.h[0] = self.h.pop(-1) self.heap_size -= 1 self.max_heapify(0) return me elif self.heap_size == 1: self.heap_size -= 1 return self.h.pop(-1) else: raise Exception("Empty heap") def insert(self, value: T) -> None: """ insert a new value into the max heap >>> h = Heap() >>> h.insert(10) >>> h [10] >>> h = Heap() >>> h.insert(10) >>> h.insert(10) >>> h [10, 10] >>> h = Heap() >>> h.insert(10) >>> h.insert(10.1) >>> h [10.1, 10] >>> h = Heap() >>> h.insert(0.1) >>> h.insert(0) >>> h.insert(9) >>> h.insert(5) >>> h [9, 5, 0.1, 0] """ self.h.append(value) idx = (self.heap_size - 1) // 2 self.heap_size += 1 while idx >= 0: self.max_heapify(idx) idx = (idx - 1) // 2 def heap_sort(self) -> None: size = self.heap_size for j in range(size - 1, 0, -1): self.h[0], self.h[j] = self.h[j], self.h[0] self.heap_size -= 1 self.max_heapify(0) self.heap_size = size if __name__ == "__main__": import doctest # run doc test doctest.testmod() # demo for unsorted in [ [0], [2], [3, 5], [5, 3], [5, 5], [0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 3, 5], [0, 2, 2, 3, 5], [2, 5, 3, 0, 2, 3, 0, 3], [6, 1, 2, 7, 9, 3, 4, 5, 10, 8], [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5], [-45, -2, -5], ]: print(f"unsorted array: {unsorted}") heap: Heap[int] = Heap() heap.build_max_heap(unsorted) print(f"after build heap: {heap}") print(f"max value: {heap.extract_max()}") print(f"after max value removed: {heap}") heap.insert(100) print(f"after new value 100 inserted: {heap}") heap.heap_sort() print(f"heap-sorted array: {heap}\n")
A generic Heap class, can be used as min or max by passing the key function accordingly. Stores actual heap items. Stores indexes of each item for supporting updates and deletion. Stores current size of heap. Stores function used to evaluate the score of an item on which basis ordering will be done. Returns parent index of given index if exists else None return inti 1 2 if i 0 else None def leftself, i: int int None: Returns rightchildindex of given index if exists else None right int2 i 2 return right if 0 right self.size else None def swapself, i: int, j: int None: First update the indexes of the items in index map. Then swap the items in the list. Compares the two items using default comparison return self.arri1 self.arrj1 def getvalidparentself, i: int int: left self.lefti right self.righti validparent i if left is not None and not self.cmpleft, validparent: validparent left if right is not None and not self.cmpright, validparent: validparent right return validparent def heapifyupself, index: int None: Fixes the heap in downward direction of given index validparent self.getvalidparentindex while validparent ! index: self.swapindex, validparent index, validparent validparent, self.getvalidparentvalidparent def updateitemself, item: int, itemvalue: int None: Make sure heap is right in both up and down direction. Ideally only one of them will make any change. Deletes given item from heap if present if item not in self.posmap: return index self.posmapitem del self.posmapitem self.arrindex self.arrself.size 1 self.posmapself.arrself.size 10 index self.size 1 Make sure heap is right in both up and down direction. Ideally only one of them will make any change so no performance loss in calling both. if self.size index: self.heapifyupindex self.heapifydownindex def insertitemself, item: int, itemvalue: int None: Returns top item tuple Calculated value, item from heap if present return self.arr0 if self.size else None def extracttopself tuple None: topitemtuple self.gettop if topitemtuple: self.deleteitemtopitemtuple0 return topitemtuple def testheap None: if name main: import doctest doctest.testmod
from collections.abc import Callable class Heap: """ A generic Heap class, can be used as min or max by passing the key function accordingly. """ def __init__(self, key: Callable | None = None) -> None: # Stores actual heap items. self.arr: list = [] # Stores indexes of each item for supporting updates and deletion. self.pos_map: dict = {} # Stores current size of heap. self.size = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. self.key = key or (lambda x: x) def _parent(self, i: int) -> int | None: """Returns parent index of given index if exists else None""" return int((i - 1) / 2) if i > 0 else None def _left(self, i: int) -> int | None: """Returns left-child-index of given index if exists else None""" left = int(2 * i + 1) return left if 0 < left < self.size else None def _right(self, i: int) -> int | None: """Returns right-child-index of given index if exists else None""" right = int(2 * i + 2) return right if 0 < right < self.size else None def _swap(self, i: int, j: int) -> None: """Performs changes required for swapping two elements in the heap""" # First update the indexes of the items in index map. self.pos_map[self.arr[i][0]], self.pos_map[self.arr[j][0]] = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. self.arr[i], self.arr[j] = self.arr[j], self.arr[i] def _cmp(self, i: int, j: int) -> bool: """Compares the two items using default comparison""" return self.arr[i][1] < self.arr[j][1] def _get_valid_parent(self, i: int) -> int: """ Returns index of valid parent as per desired ordering among given index and both it's children """ left = self._left(i) right = self._right(i) valid_parent = i if left is not None and not self._cmp(left, valid_parent): valid_parent = left if right is not None and not self._cmp(right, valid_parent): valid_parent = right return valid_parent def _heapify_up(self, index: int) -> None: """Fixes the heap in upward direction of given index""" parent = self._parent(index) while parent is not None and not self._cmp(index, parent): self._swap(index, parent) index, parent = parent, self._parent(parent) def _heapify_down(self, index: int) -> None: """Fixes the heap in downward direction of given index""" valid_parent = self._get_valid_parent(index) while valid_parent != index: self._swap(index, valid_parent) index, valid_parent = valid_parent, self._get_valid_parent(valid_parent) def update_item(self, item: int, item_value: int) -> None: """Updates given item value in heap if present""" if item not in self.pos_map: return index = self.pos_map[item] self.arr[index] = [item, self.key(item_value)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(index) self._heapify_down(index) def delete_item(self, item: int) -> None: """Deletes given item from heap if present""" if item not in self.pos_map: return index = self.pos_map[item] del self.pos_map[item] self.arr[index] = self.arr[self.size - 1] self.pos_map[self.arr[self.size - 1][0]] = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(index) self._heapify_down(index) def insert_item(self, item: int, item_value: int) -> None: """Inserts given item with given value in heap""" arr_len = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(item_value)]) else: self.arr[self.size] = [item, self.key(item_value)] self.pos_map[item] = self.size self.size += 1 self._heapify_up(self.size - 1) def get_top(self) -> tuple | None: """Returns top item tuple (Calculated value, item) from heap if present""" return self.arr[0] if self.size else None def extract_top(self) -> tuple | None: """ Return top item tuple (Calculated value, item) from heap and removes it as well if present """ top_item_tuple = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def test_heap() -> None: """ >>> h = Heap() # Max-heap >>> h.insert_item(5, 34) >>> h.insert_item(6, 31) >>> h.insert_item(7, 37) >>> h.get_top() [7, 37] >>> h.extract_top() [7, 37] >>> h.extract_top() [5, 34] >>> h.extract_top() [6, 31] >>> h = Heap(key=lambda x: -x) # Min heap >>> h.insert_item(5, 34) >>> h.insert_item(6, 31) >>> h.insert_item(7, 37) >>> h.get_top() [6, -31] >>> h.extract_top() [6, -31] >>> h.extract_top() [5, -34] >>> h.extract_top() [7, -37] >>> h.insert_item(8, 45) >>> h.insert_item(9, 40) >>> h.insert_item(10, 50) >>> h.get_top() [9, -40] >>> h.update_item(10, 30) >>> h.get_top() [10, -30] >>> h.delete_item(10) >>> h.get_top() [9, -40] """ if __name__ == "__main__": import doctest doctest.testmod()
A maxheap implementation in Python binaryheap BinaryHeap binaryheap.insert6 binaryheap.insert10 binaryheap.insert15 binaryheap.insert12 binaryheap.pop 15 binaryheap.pop 12 binaryheap.getlist 10, 6 lenbinaryheap 2 Swap the element up temporary self.heapi while i 2 0: if self.heapi self.heapi 2: self.heapi self.heapi 2 self.heapi 2 temporary i 2 def insertself, value: int None: Swap the element down while self.size 2 i: if 2 i 1 self.size: biggerchild 2 i else: if self.heap2 i self.heap2 i 1: biggerchild 2 i else: biggerchild 2 i 1 temporary self.heapi if self.heapi self.heapbiggerchild: self.heapi self.heapbiggerchild self.heapbiggerchild temporary i biggerchild def popself int: Length of the array return self.size if name main: import doctest doctest.testmod create an instance of BinaryHeap binaryheap BinaryHeap binaryheap.insert6 binaryheap.insert10 binaryheap.insert15 binaryheap.insert12 pop rootmaxvalues because it is max heap printbinaryheap.pop 15 printbinaryheap.pop 12 get the list and size after operations printbinaryheap.getlist printlenbinaryheap
class BinaryHeap: """ A max-heap implementation in Python >>> binary_heap = BinaryHeap() >>> binary_heap.insert(6) >>> binary_heap.insert(10) >>> binary_heap.insert(15) >>> binary_heap.insert(12) >>> binary_heap.pop() 15 >>> binary_heap.pop() 12 >>> binary_heap.get_list [10, 6] >>> len(binary_heap) 2 """ def __init__(self): self.__heap = [0] self.__size = 0 def __swap_up(self, i: int) -> None: """Swap the element up""" temporary = self.__heap[i] while i // 2 > 0: if self.__heap[i] > self.__heap[i // 2]: self.__heap[i] = self.__heap[i // 2] self.__heap[i // 2] = temporary i //= 2 def insert(self, value: int) -> None: """Insert new element""" self.__heap.append(value) self.__size += 1 self.__swap_up(self.__size) def __swap_down(self, i: int) -> None: """Swap the element down""" while self.__size >= 2 * i: if 2 * i + 1 > self.__size: bigger_child = 2 * i else: if self.__heap[2 * i] > self.__heap[2 * i + 1]: bigger_child = 2 * i else: bigger_child = 2 * i + 1 temporary = self.__heap[i] if self.__heap[i] < self.__heap[bigger_child]: self.__heap[i] = self.__heap[bigger_child] self.__heap[bigger_child] = temporary i = bigger_child def pop(self) -> int: """Pop the root element""" max_value = self.__heap[1] self.__heap[1] = self.__heap[self.__size] self.__size -= 1 self.__heap.pop() self.__swap_down(1) return max_value @property def get_list(self): return self.__heap[1:] def __len__(self): """Length of the array""" return self.__size if __name__ == "__main__": import doctest doctest.testmod() # create an instance of BinaryHeap binary_heap = BinaryHeap() binary_heap.insert(6) binary_heap.insert(10) binary_heap.insert(15) binary_heap.insert(12) # pop root(max-values because it is max heap) print(binary_heap.pop()) # 15 print(binary_heap.pop()) # 12 # get the list and size after operations print(binary_heap.get_list) print(len(binary_heap))
Min heap data structure with decrease key functionality in Ologn time r NodeR, 1 b NodeB, 6 a NodeA, 3 x NodeX, 1 e NodeE, 4 printb NodeB, 6 myMinHeap MinHeapr, b, a, x, e myMinHeap.decreasekeyb, 17 printb NodeB, 17 myMinHeapB 17 this is minheapify method USAGE Use one of these two ways to generate MinHeap Generating MinHeap from array Generating MinHeap by Insert method myMinHeap.inserta myMinHeap.insertb myMinHeap.insertx myMinHeap.insertr myMinHeap.inserte Before After
# Min heap data structure # with decrease key functionality - in O(log(n)) time class Node: def __init__(self, name, val): self.name = name self.val = val def __str__(self): return f"{self.__class__.__name__}({self.name}, {self.val})" def __lt__(self, other): return self.val < other.val class MinHeap: """ >>> r = Node("R", -1) >>> b = Node("B", 6) >>> a = Node("A", 3) >>> x = Node("X", 1) >>> e = Node("E", 4) >>> print(b) Node(B, 6) >>> myMinHeap = MinHeap([r, b, a, x, e]) >>> myMinHeap.decrease_key(b, -17) >>> print(b) Node(B, -17) >>> myMinHeap["B"] -17 """ def __init__(self, array): self.idx_of_element = {} self.heap_dict = {} self.heap = self.build_heap(array) def __getitem__(self, key): return self.get_value(key) def get_parent_idx(self, idx): return (idx - 1) // 2 def get_left_child_idx(self, idx): return idx * 2 + 1 def get_right_child_idx(self, idx): return idx * 2 + 2 def get_value(self, key): return self.heap_dict[key] def build_heap(self, array): last_idx = len(array) - 1 start_from = self.get_parent_idx(last_idx) for idx, i in enumerate(array): self.idx_of_element[i] = idx self.heap_dict[i.name] = i.val for i in range(start_from, -1, -1): self.sift_down(i, array) return array # this is min-heapify method def sift_down(self, idx, array): while True: l = self.get_left_child_idx(idx) # noqa: E741 r = self.get_right_child_idx(idx) smallest = idx if l < len(array) and array[l] < array[idx]: smallest = l if r < len(array) and array[r] < array[smallest]: smallest = r if smallest != idx: array[idx], array[smallest] = array[smallest], array[idx] ( self.idx_of_element[array[idx]], self.idx_of_element[array[smallest]], ) = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) idx = smallest else: break def sift_up(self, idx): p = self.get_parent_idx(idx) while p >= 0 and self.heap[p] > self.heap[idx]: self.heap[p], self.heap[idx] = self.heap[idx], self.heap[p] self.idx_of_element[self.heap[p]], self.idx_of_element[self.heap[idx]] = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) idx = p p = self.get_parent_idx(idx) def peek(self): return self.heap[0] def remove(self): self.heap[0], self.heap[-1] = self.heap[-1], self.heap[0] self.idx_of_element[self.heap[0]], self.idx_of_element[self.heap[-1]] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) x = self.heap.pop() del self.idx_of_element[x] self.sift_down(0, self.heap) return x def insert(self, node): self.heap.append(node) self.idx_of_element[node] = len(self.heap) - 1 self.heap_dict[node.name] = node.val self.sift_up(len(self.heap) - 1) def is_empty(self): return len(self.heap) == 0 def decrease_key(self, node, new_value): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" node.val = new_value self.heap_dict[node.name] = new_value self.sift_up(self.idx_of_element[node]) # USAGE r = Node("R", -1) b = Node("B", 6) a = Node("A", 3) x = Node("X", 1) e = Node("E", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array my_min_heap = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("Min Heap - before decrease key") for i in my_min_heap.heap: print(i) print("Min Heap - After decrease key of node [B -> -17]") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
!usrbinenv python3 One node of the randomized heap. Contains the value and references to two children. Return the value of the node. rhn RandomizedHeapNode10 rhn.value 10 rhn RandomizedHeapNode10 rhn.value 10 Merge 2 nodes together. rhn1 RandomizedHeapNode10 rhn2 RandomizedHeapNode20 RandomizedHeapNode.mergerhn1, rhn2.value 10 rhn1 RandomizedHeapNode20 rhn2 RandomizedHeapNode10 RandomizedHeapNode.mergerhn1, rhn2.value 10 rhn1 RandomizedHeapNode5 rhn2 RandomizedHeapNode0 RandomizedHeapNode.mergerhn1, rhn2.value 0 A data structure that allows inserting a new value and to pop the smallest values. Both operations take OlogN time where N is the size of the structure. Wiki: https:en.wikipedia.orgwikiRandomizedmeldableheap RandomizedHeap2, 3, 1, 5, 1, 7.tosortedlist 1, 1, 2, 3, 5, 7 rh RandomizedHeap rh.pop Traceback most recent call last: ... IndexError: Can't get top element for the empty heap. rh.insert1 rh.insert1 rh.insert0 rh.tosortedlist 1, 0, 1 rh RandomizedHeap3, 1, 3, 7 rh.tosortedlist 1, 3, 3, 7 Insert the value into the heap. rh RandomizedHeap rh.insert3 rh.insert1 rh.insert3 rh.insert7 rh.tosortedlist 1, 3, 3, 7 Pop the smallest value from the heap and return it. rh RandomizedHeap3, 1, 3, 7 rh.pop 1 rh.pop 3 rh.pop 3 rh.pop 7 rh.pop Traceback most recent call last: ... IndexError: Can't get top element for the empty heap. Return the smallest value from the heap. rh RandomizedHeap rh.insert3 rh.top 3 rh.insert1 rh.top 1 rh.insert3 rh.top 1 rh.insert7 rh.top 1 Clear the heap. rh RandomizedHeap3, 1, 3, 7 rh.clear rh.pop Traceback most recent call last: ... IndexError: Can't get top element for the empty heap. Returns sorted list containing all the values in the heap. rh RandomizedHeap3, 1, 3, 7 rh.tosortedlist 1, 3, 3, 7 Check if the heap is not empty. rh RandomizedHeap boolrh False rh.insert1 boolrh True rh.clear boolrh False
#!/usr/bin/env python3 from __future__ import annotations import random from collections.abc import Iterable from typing import Any, Generic, TypeVar T = TypeVar("T", bound=bool) class RandomizedHeapNode(Generic[T]): """ One node of the randomized heap. Contains the value and references to two children. """ def __init__(self, value: T) -> None: self._value: T = value self.left: RandomizedHeapNode[T] | None = None self.right: RandomizedHeapNode[T] | None = None @property def value(self) -> T: """ Return the value of the node. >>> rhn = RandomizedHeapNode(10) >>> rhn.value 10 >>> rhn = RandomizedHeapNode(-10) >>> rhn.value -10 """ return self._value @staticmethod def merge( root1: RandomizedHeapNode[T] | None, root2: RandomizedHeapNode[T] | None ) -> RandomizedHeapNode[T] | None: """ Merge 2 nodes together. >>> rhn1 = RandomizedHeapNode(10) >>> rhn2 = RandomizedHeapNode(20) >>> RandomizedHeapNode.merge(rhn1, rhn2).value 10 >>> rhn1 = RandomizedHeapNode(20) >>> rhn2 = RandomizedHeapNode(10) >>> RandomizedHeapNode.merge(rhn1, rhn2).value 10 >>> rhn1 = RandomizedHeapNode(5) >>> rhn2 = RandomizedHeapNode(0) >>> RandomizedHeapNode.merge(rhn1, rhn2).value 0 """ if not root1: return root2 if not root2: return root1 if root1.value > root2.value: root1, root2 = root2, root1 if random.choice([True, False]): root1.left, root1.right = root1.right, root1.left root1.left = RandomizedHeapNode.merge(root1.left, root2) return root1 class RandomizedHeap(Generic[T]): """ A data structure that allows inserting a new value and to pop the smallest values. Both operations take O(logN) time where N is the size of the structure. Wiki: https://en.wikipedia.org/wiki/Randomized_meldable_heap >>> RandomizedHeap([2, 3, 1, 5, 1, 7]).to_sorted_list() [1, 1, 2, 3, 5, 7] >>> rh = RandomizedHeap() >>> rh.pop() Traceback (most recent call last): ... IndexError: Can't get top element for the empty heap. >>> rh.insert(1) >>> rh.insert(-1) >>> rh.insert(0) >>> rh.to_sorted_list() [-1, 0, 1] """ def __init__(self, data: Iterable[T] | None = ()) -> None: """ >>> rh = RandomizedHeap([3, 1, 3, 7]) >>> rh.to_sorted_list() [1, 3, 3, 7] """ self._root: RandomizedHeapNode[T] | None = None if data: for item in data: self.insert(item) def insert(self, value: T) -> None: """ Insert the value into the heap. >>> rh = RandomizedHeap() >>> rh.insert(3) >>> rh.insert(1) >>> rh.insert(3) >>> rh.insert(7) >>> rh.to_sorted_list() [1, 3, 3, 7] """ self._root = RandomizedHeapNode.merge(self._root, RandomizedHeapNode(value)) def pop(self) -> T | None: """ Pop the smallest value from the heap and return it. >>> rh = RandomizedHeap([3, 1, 3, 7]) >>> rh.pop() 1 >>> rh.pop() 3 >>> rh.pop() 3 >>> rh.pop() 7 >>> rh.pop() Traceback (most recent call last): ... IndexError: Can't get top element for the empty heap. """ result = self.top() if self._root is None: return None self._root = RandomizedHeapNode.merge(self._root.left, self._root.right) return result def top(self) -> T: """ Return the smallest value from the heap. >>> rh = RandomizedHeap() >>> rh.insert(3) >>> rh.top() 3 >>> rh.insert(1) >>> rh.top() 1 >>> rh.insert(3) >>> rh.top() 1 >>> rh.insert(7) >>> rh.top() 1 """ if not self._root: raise IndexError("Can't get top element for the empty heap.") return self._root.value def clear(self) -> None: """ Clear the heap. >>> rh = RandomizedHeap([3, 1, 3, 7]) >>> rh.clear() >>> rh.pop() Traceback (most recent call last): ... IndexError: Can't get top element for the empty heap. """ self._root = None def to_sorted_list(self) -> list[Any]: """ Returns sorted list containing all the values in the heap. >>> rh = RandomizedHeap([3, 1, 3, 7]) >>> rh.to_sorted_list() [1, 3, 3, 7] """ result = [] while self: result.append(self.pop()) return result def __bool__(self) -> bool: """ Check if the heap is not empty. >>> rh = RandomizedHeap() >>> bool(rh) False >>> rh.insert(1) >>> bool(rh) True >>> rh.clear() >>> bool(rh) False """ return self._root is not None if __name__ == "__main__": import doctest doctest.testmod()
!usrbinenv python3 One node of the skew heap. Contains the value and references to two children. Return the value of the node. return self.value staticmethod def merge root1: SkewNodeT None, root2: SkewNodeT None SkewNodeT None: A data structure that allows inserting a new value and to pop the smallest values. Both operations take OlogN time where N is the size of the structure. Wiki: https:en.wikipedia.orgwikiSkewheap Visualization: https:www.cs.usfca.edugallesvisualizationSkewHeap.html listSkewHeap2, 3, 1, 5, 1, 7 1, 1, 2, 3, 5, 7 sh SkewHeap sh.pop Traceback most recent call last: ... IndexError: Can't get top element for the empty heap. sh.insert1 sh.insert1 sh.insert0 listsh 1, 0, 1 sh SkewHeap3, 1, 3, 7 listsh 1, 3, 3, 7 Check if the heap is not empty. sh SkewHeap boolsh False sh.insert1 boolsh True sh.clear boolsh False Returns sorted list containing all the values in the heap. sh SkewHeap3, 1, 3, 7 listsh 1, 3, 3, 7 Pushing items back to the heap not to clear it. Insert the value into the heap. sh SkewHeap sh.insert3 sh.insert1 sh.insert3 sh.insert7 listsh 1, 3, 3, 7 Pop the smallest value from the heap and return it. sh SkewHeap3, 1, 3, 7 sh.pop 1 sh.pop 3 sh.pop 3 sh.pop 7 sh.pop Traceback most recent call last: ... IndexError: Can't get top element for the empty heap. Return the smallest value from the heap. sh SkewHeap sh.insert3 sh.top 3 sh.insert1 sh.top 1 sh.insert3 sh.top 1 sh.insert7 sh.top 1 Clear the heap. sh SkewHeap3, 1, 3, 7 sh.clear sh.pop Traceback most recent call last: ... IndexError: Can't get top element for the empty heap.
#!/usr/bin/env python3 from __future__ import annotations from collections.abc import Iterable, Iterator from typing import Any, Generic, TypeVar T = TypeVar("T", bound=bool) class SkewNode(Generic[T]): """ One node of the skew heap. Contains the value and references to two children. """ def __init__(self, value: T) -> None: self._value: T = value self.left: SkewNode[T] | None = None self.right: SkewNode[T] | None = None @property def value(self) -> T: """Return the value of the node.""" return self._value @staticmethod def merge( root1: SkewNode[T] | None, root2: SkewNode[T] | None ) -> SkewNode[T] | None: """Merge 2 nodes together.""" if not root1: return root2 if not root2: return root1 if root1.value > root2.value: root1, root2 = root2, root1 result = root1 temp = root1.right result.right = root1.left result.left = SkewNode.merge(temp, root2) return result class SkewHeap(Generic[T]): """ A data structure that allows inserting a new value and to pop the smallest values. Both operations take O(logN) time where N is the size of the structure. Wiki: https://en.wikipedia.org/wiki/Skew_heap Visualization: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html >>> list(SkewHeap([2, 3, 1, 5, 1, 7])) [1, 1, 2, 3, 5, 7] >>> sh = SkewHeap() >>> sh.pop() Traceback (most recent call last): ... IndexError: Can't get top element for the empty heap. >>> sh.insert(1) >>> sh.insert(-1) >>> sh.insert(0) >>> list(sh) [-1, 0, 1] """ def __init__(self, data: Iterable[T] | None = ()) -> None: """ >>> sh = SkewHeap([3, 1, 3, 7]) >>> list(sh) [1, 3, 3, 7] """ self._root: SkewNode[T] | None = None if data: for item in data: self.insert(item) def __bool__(self) -> bool: """ Check if the heap is not empty. >>> sh = SkewHeap() >>> bool(sh) False >>> sh.insert(1) >>> bool(sh) True >>> sh.clear() >>> bool(sh) False """ return self._root is not None def __iter__(self) -> Iterator[T]: """ Returns sorted list containing all the values in the heap. >>> sh = SkewHeap([3, 1, 3, 7]) >>> list(sh) [1, 3, 3, 7] """ result: list[Any] = [] while self: result.append(self.pop()) # Pushing items back to the heap not to clear it. for item in result: self.insert(item) return iter(result) def insert(self, value: T) -> None: """ Insert the value into the heap. >>> sh = SkewHeap() >>> sh.insert(3) >>> sh.insert(1) >>> sh.insert(3) >>> sh.insert(7) >>> list(sh) [1, 3, 3, 7] """ self._root = SkewNode.merge(self._root, SkewNode(value)) def pop(self) -> T | None: """ Pop the smallest value from the heap and return it. >>> sh = SkewHeap([3, 1, 3, 7]) >>> sh.pop() 1 >>> sh.pop() 3 >>> sh.pop() 3 >>> sh.pop() 7 >>> sh.pop() Traceback (most recent call last): ... IndexError: Can't get top element for the empty heap. """ result = self.top() self._root = ( SkewNode.merge(self._root.left, self._root.right) if self._root else None ) return result def top(self) -> T: """ Return the smallest value from the heap. >>> sh = SkewHeap() >>> sh.insert(3) >>> sh.top() 3 >>> sh.insert(1) >>> sh.top() 1 >>> sh.insert(3) >>> sh.top() 1 >>> sh.insert(7) >>> sh.top() 1 """ if not self._root: raise IndexError("Can't get top element for the empty heap.") return self._root.value def clear(self) -> None: """ Clear the heap. >>> sh = SkewHeap([3, 1, 3, 7]) >>> sh.clear() >>> sh.pop() Traceback (most recent call last): ... IndexError: Can't get top element for the empty heap. """ self._root = None if __name__ == "__main__": import doctest doctest.testmod()
Linked Lists consists of Nodes. Nodes contain data and also may link to other nodes: Head Node: First node, the address of the head node gives us access of the complete list Last node: points to null Add an item to the LinkedList at the specified position. Default position is 0 the head. Args: item Any: The item to add to the LinkedList. position int, optional: The position at which to add the item. Defaults to 0. Raises: ValueError: If the position is negative or out of bounds. linkedlist LinkedList linkedlist.add1 linkedlist.add2 linkedlist.add3 linkedlist.add4, 2 printlinkedlist 3 2 4 1 Test adding to a negative position linkedlist.add5, 3 Traceback most recent call last: ... ValueError: Position must be nonnegative Test adding to an outofbounds position linkedlist.add5,7 Traceback most recent call last: ... ValueError: Out of bounds linkedlist.add5, 4 printlinkedlist 3 2 4 1 5 Switched 'self.isempty' to 'self.head is None' because mypy was considering the possibility that 'self.head' can be None in below else part and giving error linkedlist LinkedList linkedlist.add23 linkedlist.add14 linkedlist.add9 printlinkedlist 9 14 23 linkedlist LinkedList lenlinkedlist 0 linkedlist.adda lenlinkedlist 1 linkedlist.addb lenlinkedlist 2 linkedlist.remove lenlinkedlist 1 linkedlist.remove lenlinkedlist 0
from __future__ import annotations from typing import Any class Node: def __init__(self, item: Any, next: Any) -> None: # noqa: A002 self.item = item self.next = next class LinkedList: def __init__(self) -> None: self.head: Node | None = None self.size = 0 def add(self, item: Any, position: int = 0) -> None: """ Add an item to the LinkedList at the specified position. Default position is 0 (the head). Args: item (Any): The item to add to the LinkedList. position (int, optional): The position at which to add the item. Defaults to 0. Raises: ValueError: If the position is negative or out of bounds. >>> linked_list = LinkedList() >>> linked_list.add(1) >>> linked_list.add(2) >>> linked_list.add(3) >>> linked_list.add(4, 2) >>> print(linked_list) 3 --> 2 --> 4 --> 1 # Test adding to a negative position >>> linked_list.add(5, -3) Traceback (most recent call last): ... ValueError: Position must be non-negative # Test adding to an out-of-bounds position >>> linked_list.add(5,7) Traceback (most recent call last): ... ValueError: Out of bounds >>> linked_list.add(5, 4) >>> print(linked_list) 3 --> 2 --> 4 --> 1 --> 5 """ if position < 0: raise ValueError("Position must be non-negative") if position == 0 or self.head is None: new_node = Node(item, self.head) self.head = new_node else: current = self.head for _ in range(position - 1): current = current.next if current is None: raise ValueError("Out of bounds") new_node = Node(item, current.next) current.next = new_node self.size += 1 def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' # because mypy was considering the possibility that 'self.head' # can be None in below else part and giving error if self.head is None: return None else: item = self.head.item self.head = self.head.next self.size -= 1 return item def is_empty(self) -> bool: return self.head is None def __str__(self) -> str: """ >>> linked_list = LinkedList() >>> linked_list.add(23) >>> linked_list.add(14) >>> linked_list.add(9) >>> print(linked_list) 9 --> 14 --> 23 """ if self.is_empty(): return "" else: iterate = self.head item_str = "" item_list: list[str] = [] while iterate: item_list.append(str(iterate.item)) iterate = iterate.next item_str = " --> ".join(item_list) return item_str def __len__(self) -> int: """ >>> linked_list = LinkedList() >>> len(linked_list) 0 >>> linked_list.add("a") >>> len(linked_list) 1 >>> linked_list.add("b") >>> len(linked_list) 2 >>> _ = linked_list.remove() >>> len(linked_list) 1 >>> _ = linked_list.remove() >>> len(linked_list) 0 """ return self.size
Iterate through all nodes in the Circular Linked List yielding their data. Yields: The data of each node in the linked list. Get the length number of nodes in the Circular Linked List. Generate a string representation of the Circular Linked List. Returns: A string of the format 12....N. Insert a node with the given data at the end of the Circular Linked List. Insert a node with the given data at the beginning of the Circular Linked List. Insert the data of the node at the nth pos in the Circular Linked List. Args: index: The index at which the data should be inserted. data: The data to be inserted. Raises: IndexError: If the index is out of range. Delete and return the data of the node at the front of the Circular Linked List. Raises: IndexError: If the list is empty. Delete and return the data of the node at the end of the Circular Linked List. Returns: Any: The data of the deleted node. Raises: IndexError: If the index is out of range. Delete and return the data of the node at the nth pos in Circular Linked List. Args: index int: The index of the node to be deleted. Defaults to 0. Returns: Any: The data of the deleted node. Raises: IndexError: If the index is out of range. Check if the Circular Linked List is empty. Returns: bool: True if the list is empty, False otherwise. Test cases for the CircularLinkedList class. testcircularlinkedlist
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass from typing import Any @dataclass class Node: data: Any next_node: Node | None = None @dataclass class CircularLinkedList: head: Node | None = None # Reference to the head (first node) tail: Node | None = None # Reference to the tail (last node) def __iter__(self) -> Iterator[Any]: """ Iterate through all nodes in the Circular Linked List yielding their data. Yields: The data of each node in the linked list. """ node = self.head while node: yield node.data node = node.next_node if node == self.head: break def __len__(self) -> int: """ Get the length (number of nodes) in the Circular Linked List. """ return sum(1 for _ in self) def __repr__(self) -> str: """ Generate a string representation of the Circular Linked List. Returns: A string of the format "1->2->....->N". """ return "->".join(str(item) for item in iter(self)) def insert_tail(self, data: Any) -> None: """ Insert a node with the given data at the end of the Circular Linked List. """ self.insert_nth(len(self), data) def insert_head(self, data: Any) -> None: """ Insert a node with the given data at the beginning of the Circular Linked List. """ self.insert_nth(0, data) def insert_nth(self, index: int, data: Any) -> None: """ Insert the data of the node at the nth pos in the Circular Linked List. Args: index: The index at which the data should be inserted. data: The data to be inserted. Raises: IndexError: If the index is out of range. """ if index < 0 or index > len(self): raise IndexError("list index out of range.") new_node: Node = Node(data) if self.head is None: new_node.next_node = new_node # First node points to itself self.tail = self.head = new_node elif index == 0: # Insert at the head new_node.next_node = self.head assert self.tail is not None # List is not empty, tail exists self.head = self.tail.next_node = new_node else: temp: Node | None = self.head for _ in range(index - 1): assert temp is not None temp = temp.next_node assert temp is not None new_node.next_node = temp.next_node temp.next_node = new_node if index == len(self) - 1: # Insert at the tail self.tail = new_node def delete_front(self) -> Any: """ Delete and return the data of the node at the front of the Circular Linked List. Raises: IndexError: If the list is empty. """ return self.delete_nth(0) def delete_tail(self) -> Any: """ Delete and return the data of the node at the end of the Circular Linked List. Returns: Any: The data of the deleted node. Raises: IndexError: If the index is out of range. """ return self.delete_nth(len(self) - 1) def delete_nth(self, index: int = 0) -> Any: """ Delete and return the data of the node at the nth pos in Circular Linked List. Args: index (int): The index of the node to be deleted. Defaults to 0. Returns: Any: The data of the deleted node. Raises: IndexError: If the index is out of range. """ if not 0 <= index < len(self): raise IndexError("list index out of range.") assert self.head is not None assert self.tail is not None delete_node: Node = self.head if self.head == self.tail: # Just one node self.head = self.tail = None elif index == 0: # Delete head node assert self.tail.next_node is not None self.tail.next_node = self.tail.next_node.next_node self.head = self.head.next_node else: temp: Node | None = self.head for _ in range(index - 1): assert temp is not None temp = temp.next_node assert temp is not None assert temp.next_node is not None delete_node = temp.next_node temp.next_node = temp.next_node.next_node if index == len(self) - 1: # Delete at tail self.tail = temp return delete_node.data def is_empty(self) -> bool: """ Check if the Circular Linked List is empty. Returns: bool: True if the list is empty, False otherwise. """ return len(self) == 0 def test_circular_linked_list() -> None: """ Test cases for the CircularLinkedList class. >>> test_circular_linked_list() """ circular_linked_list = CircularLinkedList() assert len(circular_linked_list) == 0 assert circular_linked_list.is_empty() is True assert str(circular_linked_list) == "" try: circular_linked_list.delete_front() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_tail() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_nth(-1) raise AssertionError except IndexError: assert True try: circular_linked_list.delete_nth(0) raise AssertionError except IndexError: assert True assert circular_linked_list.is_empty() is True for i in range(5): assert len(circular_linked_list) == i circular_linked_list.insert_nth(i, i + 1) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) circular_linked_list.insert_tail(6) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7)) circular_linked_list.insert_head(0) assert str(circular_linked_list) == "->".join(str(i) for i in range(7)) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) assert circular_linked_list.delete_nth(2) == 3 circular_linked_list.insert_nth(2, 3) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) assert circular_linked_list.is_empty() is False if __name__ == "__main__": import doctest doctest.testmod()
Implementing Deque using DoublyLinkedList ... Operations: 1. insertion in the front O1 2. insertion in the end O1 3. remove from the front O1 4. remove from the end O1 A Private class to be inherited class Node: slots prev, data, next def initself, linkp, element, linkn: self.prev linkp self.data element self.next linkn def hasnextandprevself: return f Prev self.prev is not None, Next self.next is not None def initself: self.header self.NodeNone, None, None self.trailer self.NodeNone, None, None self.header.next self.trailer self.trailer.prev self.header self.size 0 def lenself: return self.size def isemptyself: return self.len 0 def insertself, predecessor, e, successor: Create newnode by setting it's prev.link header setting it's next.link trailer newnode self.Nodepredecessor, e, successor predecessor.next newnode successor.prev newnode self.size 1 return self def deleteself, node: predecessor node.prev successor node.next predecessor.next successor successor.prev predecessor self.size 1 temp node.data node.prev node.next node.data None del node return temp class LinkedDequeDoublyLinkedBase: def firstself: if self.isempty: raise ExceptionList is empty return self.header.next.data def lastself: if self.isempty: raise ExceptionList is empty return self.trailer.prev.data DEque Insert Operations At the front, At the end def addfirstself, element: return self.insertself.header, element, self.header.next def addlastself, element: return self.insertself.trailer.prev, element, self.trailer DEqueu Remove Operations At the front, At the end def removefirstself: if self.isempty: raise IndexErrorremovefirst from empty list return self.deleteself.header.next def removelastself: if self.isempty: raise IndexErrorremovefirst from empty list return self.deleteself.trailer.prev
class _DoublyLinkedBase: """A Private class (to be inherited)""" class _Node: __slots__ = "_prev", "_data", "_next" def __init__(self, link_p, element, link_n): self._prev = link_p self._data = element self._next = link_n def has_next_and_prev(self): return ( f" Prev -> {self._prev is not None}, Next -> {self._next is not None}" ) def __init__(self): self._header = self._Node(None, None, None) self._trailer = self._Node(None, None, None) self._header._next = self._trailer self._trailer._prev = self._header self._size = 0 def __len__(self): return self._size def is_empty(self): return self.__len__() == 0 def _insert(self, predecessor, e, successor): # Create new_node by setting it's prev.link -> header # setting it's next.link -> trailer new_node = self._Node(predecessor, e, successor) predecessor._next = new_node successor._prev = new_node self._size += 1 return self def _delete(self, node): predecessor = node._prev successor = node._next predecessor._next = successor successor._prev = predecessor self._size -= 1 temp = node._data node._prev = node._next = node._data = None del node return temp class LinkedDeque(_DoublyLinkedBase): def first(self): """return first element >>> d = LinkedDeque() >>> d.add_first('A').first() 'A' >>> d.add_first('B').first() 'B' """ if self.is_empty(): raise Exception("List is empty") return self._header._next._data def last(self): """return last element >>> d = LinkedDeque() >>> d.add_last('A').last() 'A' >>> d.add_last('B').last() 'B' """ if self.is_empty(): raise Exception("List is empty") return self._trailer._prev._data # DEque Insert Operations (At the front, At the end) def add_first(self, element): """insertion in the front >>> LinkedDeque().add_first('AV').first() 'AV' """ return self._insert(self._header, element, self._header._next) def add_last(self, element): """insertion in the end >>> LinkedDeque().add_last('B').last() 'B' """ return self._insert(self._trailer._prev, element, self._trailer) # DEqueu Remove Operations (At the front, At the end) def remove_first(self): """removal from the front >>> d = LinkedDeque() >>> d.is_empty() True >>> d.remove_first() Traceback (most recent call last): ... IndexError: remove_first from empty list >>> d.add_first('A') # doctest: +ELLIPSIS <data_structures.linked_list.deque_doubly.LinkedDeque object at ... >>> d.remove_first() 'A' >>> d.is_empty() True """ if self.is_empty(): raise IndexError("remove_first from empty list") return self._delete(self._header._next) def remove_last(self): """removal in the end >>> d = LinkedDeque() >>> d.is_empty() True >>> d.remove_last() Traceback (most recent call last): ... IndexError: remove_first from empty list >>> d.add_first('A') # doctest: +ELLIPSIS <data_structures.linked_list.deque_doubly.LinkedDeque object at ... >>> d.remove_last() 'A' >>> d.is_empty() True """ if self.is_empty(): raise IndexError("remove_first from empty list") return self._delete(self._trailer._prev)
https:en.wikipedia.orgwikiDoublylinkedlist linkedlist DoublyLinkedList linkedlist.insertathead'b' linkedlist.insertathead'a' linkedlist.insertattail'c' tuplelinkedlist 'a', 'b', 'c' linkedlist DoublyLinkedList linkedlist.insertattail'a' linkedlist.insertattail'b' linkedlist.insertattail'c' strlinkedlist 'abc' linkedlist DoublyLinkedList for i in range0, 5: ... linkedlist.insertatnthi, i 1 lenlinkedlist 5 True linkedlist DoublyLinkedList linkedlist.insertatnth1, 666 Traceback most recent call last: .... IndexError: list index out of range linkedlist.insertatnth1, 666 Traceback most recent call last: .... IndexError: list index out of range linkedlist.insertatnth0, 2 linkedlist.insertatnth0, 1 linkedlist.insertatnth2, 4 linkedlist.insertatnth2, 3 strlinkedlist '1234' linkedlist.insertatnth5, 5 Traceback most recent call last: .... IndexError: list index out of range linkedlist DoublyLinkedList linkedlist.deleteatnth0 Traceback most recent call last: .... IndexError: list index out of range for i in range0, 5: ... linkedlist.insertatnthi, i 1 linkedlist.deleteatnth0 1 True linkedlist.deleteatnth3 5 True linkedlist.deleteatnth1 3 True strlinkedlist '24' linkedlist.deleteatnth2 Traceback most recent call last: .... IndexError: list index out of range linkedlist DoublyLinkedList linkedlist.isempty True linkedlist.insertattail1 linkedlist.isempty False testdoublylinkedlist
class Node: def __init__(self, data): self.data = data self.previous = None self.next = None def __str__(self): return f"{self.data}" class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def __iter__(self): """ >>> linked_list = DoublyLinkedList() >>> linked_list.insert_at_head('b') >>> linked_list.insert_at_head('a') >>> linked_list.insert_at_tail('c') >>> tuple(linked_list) ('a', 'b', 'c') """ node = self.head while node: yield node.data node = node.next def __str__(self): """ >>> linked_list = DoublyLinkedList() >>> linked_list.insert_at_tail('a') >>> linked_list.insert_at_tail('b') >>> linked_list.insert_at_tail('c') >>> str(linked_list) 'a->b->c' """ return "->".join([str(item) for item in self]) def __len__(self): """ >>> linked_list = DoublyLinkedList() >>> for i in range(0, 5): ... linked_list.insert_at_nth(i, i + 1) >>> len(linked_list) == 5 True """ return sum(1 for _ in self) def insert_at_head(self, data): self.insert_at_nth(0, data) def insert_at_tail(self, data): self.insert_at_nth(len(self), data) def insert_at_nth(self, index: int, data): """ >>> linked_list = DoublyLinkedList() >>> linked_list.insert_at_nth(-1, 666) Traceback (most recent call last): .... IndexError: list index out of range >>> linked_list.insert_at_nth(1, 666) Traceback (most recent call last): .... IndexError: list index out of range >>> linked_list.insert_at_nth(0, 2) >>> linked_list.insert_at_nth(0, 1) >>> linked_list.insert_at_nth(2, 4) >>> linked_list.insert_at_nth(2, 3) >>> str(linked_list) '1->2->3->4' >>> linked_list.insert_at_nth(5, 5) Traceback (most recent call last): .... IndexError: list index out of range """ length = len(self) if not 0 <= index <= length: raise IndexError("list index out of range") new_node = Node(data) if self.head is None: self.head = self.tail = new_node elif index == 0: self.head.previous = new_node new_node.next = self.head self.head = new_node elif index == length: self.tail.next = new_node new_node.previous = self.tail self.tail = new_node else: temp = self.head for _ in range(index): temp = temp.next temp.previous.next = new_node new_node.previous = temp.previous new_node.next = temp temp.previous = new_node def delete_head(self): return self.delete_at_nth(0) def delete_tail(self): return self.delete_at_nth(len(self) - 1) def delete_at_nth(self, index: int): """ >>> linked_list = DoublyLinkedList() >>> linked_list.delete_at_nth(0) Traceback (most recent call last): .... IndexError: list index out of range >>> for i in range(0, 5): ... linked_list.insert_at_nth(i, i + 1) >>> linked_list.delete_at_nth(0) == 1 True >>> linked_list.delete_at_nth(3) == 5 True >>> linked_list.delete_at_nth(1) == 3 True >>> str(linked_list) '2->4' >>> linked_list.delete_at_nth(2) Traceback (most recent call last): .... IndexError: list index out of range """ length = len(self) if not 0 <= index <= length - 1: raise IndexError("list index out of range") delete_node = self.head # default first node if length == 1: self.head = self.tail = None elif index == 0: self.head = self.head.next self.head.previous = None elif index == length - 1: delete_node = self.tail self.tail = self.tail.previous self.tail.next = None else: temp = self.head for _ in range(index): temp = temp.next delete_node = temp temp.next.previous = temp.previous temp.previous.next = temp.next return delete_node.data def delete(self, data) -> str: current = self.head while current.data != data: # Find the position to delete if current.next: current = current.next else: # We have reached the end an no value matches raise ValueError("No data matching given value") if current == self.head: self.delete_head() elif current == self.tail: self.delete_tail() else: # Before: 1 <--> 2(current) <--> 3 current.previous.next = current.next # 1 --> 3 current.next.previous = current.previous # 1 <--> 3 return data def is_empty(self): """ >>> linked_list = DoublyLinkedList() >>> linked_list.is_empty() True >>> linked_list.insert_at_tail(1) >>> linked_list.is_empty() False """ return len(self) == 0 def test_doubly_linked_list() -> None: """ >>> test_doubly_linked_list() """ linked_list = DoublyLinkedList() assert linked_list.is_empty() is True assert str(linked_list) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10): assert len(linked_list) == i linked_list.insert_at_nth(i, i + 1) assert str(linked_list) == "->".join(str(i) for i in range(1, 11)) linked_list.insert_at_head(0) linked_list.insert_at_tail(11) assert str(linked_list) == "->".join(str(i) for i in range(12)) assert linked_list.delete_head() == 0 assert linked_list.delete_at_nth(9) == 10 assert linked_list.delete_tail() == 11 assert len(linked_list) == 9 assert str(linked_list) == "->".join(str(i) for i in range(1, 10)) if __name__ == "__main__": from doctest import testmod testmod()
A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes. This is an example of a double ended, doubly linked list. Each link references the next link and the previous one. A Doubly Linked List DLL contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. Advantages over SLL It can be traversed in both forward and backward direction. Delete operation is more efficient newlinkedlist LinkedList newlinkedlist.getheaddata is None True newlinkedlist.gettaildata is None True newlinkedlist.isempty True newlinkedlist.insert10 newlinkedlist.getheaddata 10 newlinkedlist.gettaildata 10 newlinkedlist.insertatpositionposition3, value20 newlinkedlist.getheaddata 10 newlinkedlist.gettaildata 20 newlinkedlist.setheadNode1000 newlinkedlist.getheaddata 1000 newlinkedlist.gettaildata 20 newlinkedlist.settailNode2000 newlinkedlist.getheaddata 1000 newlinkedlist.gettaildata 2000 for value in newlinkedlist: ... printvalue 1000 10 20 2000 newlinkedlist.isempty False for value in newlinkedlist: ... printvalue 1000 10 20 2000 10 in newlinkedlist True newlinkedlist.deletevaluevalue10 10 in newlinkedlist False newlinkedlist.deletevaluevalue2000 newlinkedlist.gettaildata 20 newlinkedlist.deletevaluevalue1000 newlinkedlist.gettaildata 20 newlinkedlist.getheaddata 20 for value in newlinkedlist: ... printvalue 20 newlinkedlist.deletevaluevalue20 for value in newlinkedlist: ... printvalue for value in range1,10: ... newlinkedlist.insertvaluevalue for value in newlinkedlist: ... printvalue 1 2 3 4 5 6 7 8 9
class Node: def __init__(self, data: int, previous=None, next_node=None): self.data = data self.previous = previous self.next = next_node def __str__(self) -> str: return f"{self.data}" def get_data(self) -> int: return self.data def get_next(self): return self.next def get_previous(self): return self.previous class LinkedListIterator: def __init__(self, head): self.current = head def __iter__(self): return self def __next__(self): if not self.current: raise StopIteration else: value = self.current.get_data() self.current = self.current.get_next() return value class LinkedList: def __init__(self): self.head = None # First node in list self.tail = None # Last node in list def __str__(self): current = self.head nodes = [] while current is not None: nodes.append(current.get_data()) current = current.get_next() return " ".join(str(node) for node in nodes) def __contains__(self, value: int): current = self.head while current: if current.get_data() == value: return True current = current.get_next() return False def __iter__(self): return LinkedListIterator(self.head) def get_head_data(self): if self.head: return self.head.get_data() return None def get_tail_data(self): if self.tail: return self.tail.get_data() return None def set_head(self, node: Node) -> None: if self.head is None: self.head = node self.tail = node else: self.insert_before_node(self.head, node) def set_tail(self, node: Node) -> None: if self.head is None: self.set_head(node) else: self.insert_after_node(self.tail, node) def insert(self, value: int) -> None: node = Node(value) if self.head is None: self.set_head(node) else: self.set_tail(node) def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.next = node node_to_insert.previous = node.previous if node.get_previous() is None: self.head = node_to_insert else: node.previous.next = node_to_insert node.previous = node_to_insert def insert_after_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.previous = node node_to_insert.next = node.next if node.get_next() is None: self.tail = node_to_insert else: node.next.previous = node_to_insert node.next = node_to_insert def insert_at_position(self, position: int, value: int) -> None: current_position = 1 new_node = Node(value) node = self.head while node: if current_position == position: self.insert_before_node(node, new_node) return current_position += 1 node = node.next self.insert_after_node(self.tail, new_node) def get_node(self, item: int) -> Node: node = self.head while node: if node.get_data() == item: return node node = node.get_next() raise Exception("Node not found") def delete_value(self, value): if (node := self.get_node(value)) is not None: if node == self.head: self.head = self.head.get_next() if node == self.tail: self.tail = self.tail.get_previous() self.remove_node_pointers(node) @staticmethod def remove_node_pointers(node: Node) -> None: if node.get_next(): node.next.previous = node.previous if node.get_previous(): node.previous.next = node.next node.next = None node.previous = None def is_empty(self): return self.head is None def create_linked_list() -> None: """ >>> new_linked_list = LinkedList() >>> new_linked_list.get_head_data() is None True >>> new_linked_list.get_tail_data() is None True >>> new_linked_list.is_empty() True >>> new_linked_list.insert(10) >>> new_linked_list.get_head_data() 10 >>> new_linked_list.get_tail_data() 10 >>> new_linked_list.insert_at_position(position=3, value=20) >>> new_linked_list.get_head_data() 10 >>> new_linked_list.get_tail_data() 20 >>> new_linked_list.set_head(Node(1000)) >>> new_linked_list.get_head_data() 1000 >>> new_linked_list.get_tail_data() 20 >>> new_linked_list.set_tail(Node(2000)) >>> new_linked_list.get_head_data() 1000 >>> new_linked_list.get_tail_data() 2000 >>> for value in new_linked_list: ... print(value) 1000 10 20 2000 >>> new_linked_list.is_empty() False >>> for value in new_linked_list: ... print(value) 1000 10 20 2000 >>> 10 in new_linked_list True >>> new_linked_list.delete_value(value=10) >>> 10 in new_linked_list False >>> new_linked_list.delete_value(value=2000) >>> new_linked_list.get_tail_data() 20 >>> new_linked_list.delete_value(value=1000) >>> new_linked_list.get_tail_data() 20 >>> new_linked_list.get_head_data() 20 >>> for value in new_linked_list: ... print(value) 20 >>> new_linked_list.delete_value(value=20) >>> for value in new_linked_list: ... print(value) >>> for value in range(1,10): ... new_linked_list.insert(value=value) >>> for value in new_linked_list: ... print(value) 1 2 3 4 5 6 7 8 9 """ if __name__ == "__main__": import doctest doctest.testmod()
Floyd's cycle detection algorithm is a popular algorithm used to detect cycles in a linked list. It uses two pointers, a slow pointer and a fast pointer, to traverse the linked list. The slow pointer moves one node at a time while the fast pointer moves two nodes at a time. If there is a cycle in the linked list, the fast pointer will eventually catch up to the slow pointer and they will meet at the same node. If there is no cycle, the fast pointer will reach the end of the linked list and the algorithm will terminate. For more information: https:en.wikipedia.orgwikiCycledetectionFloyd'stortoiseandhare A class representing a node in a singly linked list. A class representing a singly linked list. Iterates through the linked list. Returns: Iterator: An iterator over the linked list. Examples: linkedlist LinkedList listlinkedlist linkedlist.addnode1 tuplelinkedlist 1, Avoid infinite loop in there's a cycle Adds a new node to the end of the linked list. Args: data Any: The data to be stored in the new node. Examples: linkedlist LinkedList linkedlist.addnode1 linkedlist.addnode2 linkedlist.addnode3 linkedlist.addnode4 tuplelinkedlist 1, 2, 3, 4 Detects if there is a cycle in the linked list using Floyd's cycle detection algorithm. Returns: bool: True if there is a cycle, False otherwise. Examples: linkedlist LinkedList linkedlist.addnode1 linkedlist.addnode2 linkedlist.addnode3 linkedlist.addnode4 linkedlist.detectcycle False Create a cycle in the linked list linkedlist.head.nextnode.nextnode.nextnode linkedlist.head.nextnode linkedlist.detectcycle True Create a cycle in the linked list It first checks if the head, nextnode, and nextnode.nextnode attributes of the linked list are not None to avoid any potential type errors.
from collections.abc import Iterator from dataclasses import dataclass from typing import Any, Self @dataclass class Node: """ A class representing a node in a singly linked list. """ data: Any next_node: Self | None = None @dataclass class LinkedList: """ A class representing a singly linked list. """ head: Node | None = None def __iter__(self) -> Iterator: """ Iterates through the linked list. Returns: Iterator: An iterator over the linked list. Examples: >>> linked_list = LinkedList() >>> list(linked_list) [] >>> linked_list.add_node(1) >>> tuple(linked_list) (1,) """ visited = [] node = self.head while node: # Avoid infinite loop in there's a cycle if node in visited: return visited.append(node) yield node.data node = node.next_node def add_node(self, data: Any) -> None: """ Adds a new node to the end of the linked list. Args: data (Any): The data to be stored in the new node. Examples: >>> linked_list = LinkedList() >>> linked_list.add_node(1) >>> linked_list.add_node(2) >>> linked_list.add_node(3) >>> linked_list.add_node(4) >>> tuple(linked_list) (1, 2, 3, 4) """ new_node = Node(data) if self.head is None: self.head = new_node return current_node = self.head while current_node.next_node is not None: current_node = current_node.next_node current_node.next_node = new_node def detect_cycle(self) -> bool: """ Detects if there is a cycle in the linked list using Floyd's cycle detection algorithm. Returns: bool: True if there is a cycle, False otherwise. Examples: >>> linked_list = LinkedList() >>> linked_list.add_node(1) >>> linked_list.add_node(2) >>> linked_list.add_node(3) >>> linked_list.add_node(4) >>> linked_list.detect_cycle() False # Create a cycle in the linked list >>> linked_list.head.next_node.next_node.next_node = linked_list.head.next_node >>> linked_list.detect_cycle() True """ if self.head is None: return False slow_pointer: Node | None = self.head fast_pointer: Node | None = self.head while fast_pointer is not None and fast_pointer.next_node is not None: slow_pointer = slow_pointer.next_node if slow_pointer else None fast_pointer = fast_pointer.next_node.next_node if slow_pointer == fast_pointer: return True return False if __name__ == "__main__": import doctest doctest.testmod() linked_list = LinkedList() linked_list.add_node(1) linked_list.add_node(2) linked_list.add_node(3) linked_list.add_node(4) # Create a cycle in the linked list # It first checks if the head, next_node, and next_node.next_node attributes of the # linked list are not None to avoid any potential type errors. if ( linked_list.head and linked_list.head.next_node and linked_list.head.next_node.next_node ): linked_list.head.next_node.next_node.next_node = linked_list.head.next_node has_cycle = linked_list.detect_cycle() print(has_cycle) # Output: True
Recursive Prorgam to create a Linked List from a sequence and print a string representation of it. Returns a visual representation of the node and all its following nodes. stringrep temp self while temp: stringrep ftemp.data temp temp.next stringrep END return stringrep def makelinkedlistelementslist: if elementslist is empty Set first element as Head Loop through elements from position 1
# Recursive Prorgam to create a Linked List from a sequence and # print a string representation of it. class Node: def __init__(self, data=None): self.data = data self.next = None def __repr__(self): """Returns a visual representation of the node and all its following nodes.""" string_rep = "" temp = self while temp: string_rep += f"<{temp.data}> ---> " temp = temp.next string_rep += "<END>" return string_rep def make_linked_list(elements_list): """Creates a Linked List from the elements of the given sequence (list/tuple) and returns the head of the Linked List.""" # if elements_list is empty if not elements_list: raise Exception("The Elements List is empty") # Set first element as Head head = Node(elements_list[0]) current = head # Loop through elements from position 1 for data in elements_list[1:]: current.next = Node(data) current = current.next return head list_data = [1, 3, 5, 32, 44, 12, 43] print(f"List: {list_data}") print("Creating Linked List from List.") linked_list = make_linked_list(list_data) print("Linked List:") print(linked_list)
A loop is when the exact same Node appears more than once in a linked list. rootnode Node1 rootnode.nextnode Node2 rootnode.nextnode.nextnode Node3 rootnode.nextnode.nextnode.nextnode Node4 rootnode.hasloop False rootnode.nextnode.nextnode.nextnode rootnode.nextnode rootnode.hasloop True
from __future__ import annotations from typing import Any class ContainsLoopError(Exception): pass class Node: def __init__(self, data: Any) -> None: self.data: Any = data self.next_node: Node | None = None def __iter__(self): node = self visited = [] while node: if node in visited: raise ContainsLoopError visited.append(node) yield node.data node = node.next_node @property def has_loop(self) -> bool: """ A loop is when the exact same Node appears more than once in a linked list. >>> root_node = Node(1) >>> root_node.next_node = Node(2) >>> root_node.next_node.next_node = Node(3) >>> root_node.next_node.next_node.next_node = Node(4) >>> root_node.has_loop False >>> root_node.next_node.next_node.next_node = root_node.next_node >>> root_node.has_loop True """ try: list(self) return False except ContainsLoopError: return True if __name__ == "__main__": root_node = Node(1) root_node.next_node = Node(2) root_node.next_node.next_node = Node(3) root_node.next_node.next_node.next_node = Node(4) print(root_node.has_loop) # False root_node.next_node.next_node.next_node = root_node.next_node print(root_node.has_loop) # True root_node = Node(5) root_node.next_node = Node(6) root_node.next_node.next_node = Node(5) root_node.next_node.next_node.next_node = Node(6) print(root_node.has_loop) # False root_node = Node(1) print(root_node.has_loop) # False
Check if a linked list is a palindrome. Args: head: The head of the linked list. Returns: bool: True if the linked list is a palindrome, False otherwise. Examples: ispalindromeNone True ispalindromeListNode1 True ispalindromeListNode1, ListNode2 False ispalindromeListNode1, ListNode2, ListNode1 True ispalindromeListNode1, ListNode2, ListNode2, ListNode1 True split the list to two parts slow will always be defined, adding this check to resolve mypy static check reverse the second part compare two parts second part has the same or one less node Check if a linked list is a palindrome using a stack. Args: head ListNode: The head of the linked list. Returns: bool: True if the linked list is a palindrome, False otherwise. Examples: ispalindromestackNone True ispalindromestackListNode1 True ispalindromestackListNode1, ListNode2 False ispalindromestackListNode1, ListNode2, ListNode1 True ispalindromestackListNode1, ListNode2, ListNode2, ListNode1 True 1. Get the midpoint slow slow will always be defined, adding this check to resolve mypy static check 2. Push the second half into the stack 3. Comparison Check if a linked list is a palindrome using a dictionary. Args: head ListNode: The head of the linked list. Returns: bool: True if the linked list is a palindrome, False otherwise. Examples: ispalindromedictNone True ispalindromedictListNode1 True ispalindromedictListNode1, ListNode2 False ispalindromedictListNode1, ListNode2, ListNode1 True ispalindromedictListNode1, ListNode2, ListNode2, ListNode1 True ispalindromedict ... ListNode ... 1, ListNode2, ListNode1, ListNode3, ListNode2, ListNode1 ... ... False
from __future__ import annotations from dataclasses import dataclass @dataclass class ListNode: val: int = 0 next_node: ListNode | None = None def is_palindrome(head: ListNode | None) -> bool: """ Check if a linked list is a palindrome. Args: head: The head of the linked list. Returns: bool: True if the linked list is a palindrome, False otherwise. Examples: >>> is_palindrome(None) True >>> is_palindrome(ListNode(1)) True >>> is_palindrome(ListNode(1, ListNode(2))) False >>> is_palindrome(ListNode(1, ListNode(2, ListNode(1)))) True >>> is_palindrome(ListNode(1, ListNode(2, ListNode(2, ListNode(1))))) True """ if not head: return True # split the list to two parts fast: ListNode | None = head.next_node slow: ListNode | None = head while fast and fast.next_node: fast = fast.next_node.next_node slow = slow.next_node if slow else None if slow: # slow will always be defined, # adding this check to resolve mypy static check second = slow.next_node slow.next_node = None # Don't forget here! But forget still works! # reverse the second part node: ListNode | None = None while second: nxt = second.next_node second.next_node = node node = second second = nxt # compare two parts # second part has the same or one less node while node and head: if node.val != head.val: return False node = node.next_node head = head.next_node return True def is_palindrome_stack(head: ListNode | None) -> bool: """ Check if a linked list is a palindrome using a stack. Args: head (ListNode): The head of the linked list. Returns: bool: True if the linked list is a palindrome, False otherwise. Examples: >>> is_palindrome_stack(None) True >>> is_palindrome_stack(ListNode(1)) True >>> is_palindrome_stack(ListNode(1, ListNode(2))) False >>> is_palindrome_stack(ListNode(1, ListNode(2, ListNode(1)))) True >>> is_palindrome_stack(ListNode(1, ListNode(2, ListNode(2, ListNode(1))))) True """ if not head or not head.next_node: return True # 1. Get the midpoint (slow) slow: ListNode | None = head fast: ListNode | None = head while fast and fast.next_node: fast = fast.next_node.next_node slow = slow.next_node if slow else None # slow will always be defined, # adding this check to resolve mypy static check if slow: stack = [slow.val] # 2. Push the second half into the stack while slow.next_node: slow = slow.next_node stack.append(slow.val) # 3. Comparison cur: ListNode | None = head while stack and cur: if stack.pop() != cur.val: return False cur = cur.next_node return True def is_palindrome_dict(head: ListNode | None) -> bool: """ Check if a linked list is a palindrome using a dictionary. Args: head (ListNode): The head of the linked list. Returns: bool: True if the linked list is a palindrome, False otherwise. Examples: >>> is_palindrome_dict(None) True >>> is_palindrome_dict(ListNode(1)) True >>> is_palindrome_dict(ListNode(1, ListNode(2))) False >>> is_palindrome_dict(ListNode(1, ListNode(2, ListNode(1)))) True >>> is_palindrome_dict(ListNode(1, ListNode(2, ListNode(2, ListNode(1))))) True >>> is_palindrome_dict( ... ListNode( ... 1, ListNode(2, ListNode(1, ListNode(3, ListNode(2, ListNode(1))))) ... ) ... ) False """ if not head or not head.next_node: return True d: dict[int, list[int]] = {} pos = 0 while head: if head.val in d: d[head.val].append(pos) else: d[head.val] = [pos] head = head.next_node pos += 1 checksum = pos - 1 middle = 0 for v in d.values(): if len(v) % 2 != 0: middle += 1 else: step = 0 for i in range(len(v)): if v[i] + v[len(v) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True if __name__ == "__main__": import doctest doctest.testmod()
Algorithm that merges two sorted linked lists into one sorted linked list. tupleSortedLinkedListtestdataodd tuplesortedtestdataodd True tupleSortedLinkedListtestdataeven tuplesortedtestdataeven True for i in range3: ... lenSortedLinkedListrangei i True True True lenSortedLinkedListtestdataodd 8 strSortedLinkedList '' strSortedLinkedListtestdataodd '11 1 0 1 3 5 7 9' strSortedLinkedListtestdataeven '2 0 2 3 4 6 8 10' SSL SortedLinkedList merged mergelistsSSLtestdataodd, SSLtestdataeven lenmerged 16 strmerged '11 2 1 0 0 1 2 3 3 4 5 6 7 8 9 10' listmerged listsortedtestdataodd testdataeven True
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass test_data_odd = (3, 9, -11, 0, 7, 5, 1, -1) test_data_even = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class Node: data: int next_node: Node | None class SortedLinkedList: def __init__(self, ints: Iterable[int]) -> None: self.head: Node | None = None for i in sorted(ints, reverse=True): self.head = Node(i, self.head) def __iter__(self) -> Iterator[int]: """ >>> tuple(SortedLinkedList(test_data_odd)) == tuple(sorted(test_data_odd)) True >>> tuple(SortedLinkedList(test_data_even)) == tuple(sorted(test_data_even)) True """ node = self.head while node: yield node.data node = node.next_node def __len__(self) -> int: """ >>> for i in range(3): ... len(SortedLinkedList(range(i))) == i True True True >>> len(SortedLinkedList(test_data_odd)) 8 """ return sum(1 for _ in self) def __str__(self) -> str: """ >>> str(SortedLinkedList([])) '' >>> str(SortedLinkedList(test_data_odd)) '-11 -> -1 -> 0 -> 1 -> 3 -> 5 -> 7 -> 9' >>> str(SortedLinkedList(test_data_even)) '-2 -> 0 -> 2 -> 3 -> 4 -> 6 -> 8 -> 10' """ return " -> ".join([str(node) for node in self]) def merge_lists( sll_one: SortedLinkedList, sll_two: SortedLinkedList ) -> SortedLinkedList: """ >>> SSL = SortedLinkedList >>> merged = merge_lists(SSL(test_data_odd), SSL(test_data_even)) >>> len(merged) 16 >>> str(merged) '-11 -> -2 -> -1 -> 0 -> 0 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10' >>> list(merged) == list(sorted(test_data_odd + test_data_even)) True """ return SortedLinkedList(list(sll_one) + list(sll_two)) if __name__ == "__main__": import doctest doctest.testmod() SSL = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
link LinkedList link.middleelement No element found. link.push5 5 link.push6 6 link.push8 8 link.push8 8 link.push10 10 link.push12 12 link.push17 17 link.push7 7 link.push3 3 link.push20 20 link.push20 20 link.middleelement 12
from __future__ import annotations class Node: def __init__(self, data: int) -> None: self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def push(self, new_data: int) -> int: new_node = Node(new_data) new_node.next = self.head self.head = new_node return self.head.data def middle_element(self) -> int | None: """ >>> link = LinkedList() >>> link.middle_element() No element found. >>> link.push(5) 5 >>> link.push(6) 6 >>> link.push(8) 8 >>> link.push(8) 8 >>> link.push(10) 10 >>> link.push(12) 12 >>> link.push(17) 17 >>> link.push(7) 7 >>> link.push(3) 3 >>> link.push(20) 20 >>> link.push(-20) -20 >>> link.middle_element() 12 >>> """ slow_pointer = self.head fast_pointer = self.head if self.head: while fast_pointer and fast_pointer.next: fast_pointer = fast_pointer.next.next slow_pointer = slow_pointer.next return slow_pointer.data else: print("No element found.") return None if __name__ == "__main__": link = LinkedList() for _ in range(int(input().strip())): data = int(input().strip()) link.push(data) print(link.middle_element())
A class to represent a Linked List. Use a tail pointer to speed up the append operation. Initialize a LinkedList with the head node set to None. linkedlist LinkedList linkedlist.head, linkedlist.tail None, None Iterate the LinkedList yielding each Node's data. linkedlist LinkedList items 1, 2, 3, 4, 5 linkedlist.extenditems tuplelinkedlist items True Returns a string representation of the LinkedList. linkedlist LinkedList strlinkedlist '' linkedlist.append1 strlinkedlist '1' linkedlist.extend2, 3, 4, 5 strlinkedlist '1 2 3 4 5' Appends a new node with the given data to the end of the LinkedList. linkedlist LinkedList strlinkedlist '' linkedlist.append1 strlinkedlist '1' linkedlist.append2 strlinkedlist '1 2' Appends each item to the end of the LinkedList. linkedlist LinkedList linkedlist.extend strlinkedlist '' linkedlist.extend1, 2 strlinkedlist '1 2' linkedlist.extend3,4 strlinkedlist '1 2 3 4' Creates a Linked List from the elements of the given sequence listtuple and returns the head of the Linked List. makelinkedlist Traceback most recent call last: ... Exception: The Elements List is empty makelinkedlist7 7 makelinkedlist'abc' abc makelinkedlist7, 25 7 25 Prints the elements of the given Linked List in reverse order inreverseLinkedList '' inreversemakelinkedlist69, 88, 73 '73 88 69'
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass @dataclass class Node: data: int next_node: Node | None = None class LinkedList: """A class to represent a Linked List. Use a tail pointer to speed up the append() operation. """ def __init__(self) -> None: """Initialize a LinkedList with the head node set to None. >>> linked_list = LinkedList() >>> (linked_list.head, linked_list.tail) (None, None) """ self.head: Node | None = None self.tail: Node | None = None # Speeds up the append() operation def __iter__(self) -> Iterator[int]: """Iterate the LinkedList yielding each Node's data. >>> linked_list = LinkedList() >>> items = (1, 2, 3, 4, 5) >>> linked_list.extend(items) >>> tuple(linked_list) == items True """ node = self.head while node: yield node.data node = node.next_node def __repr__(self) -> str: """Returns a string representation of the LinkedList. >>> linked_list = LinkedList() >>> str(linked_list) '' >>> linked_list.append(1) >>> str(linked_list) '1' >>> linked_list.extend([2, 3, 4, 5]) >>> str(linked_list) '1 -> 2 -> 3 -> 4 -> 5' """ return " -> ".join([str(data) for data in self]) def append(self, data: int) -> None: """Appends a new node with the given data to the end of the LinkedList. >>> linked_list = LinkedList() >>> str(linked_list) '' >>> linked_list.append(1) >>> str(linked_list) '1' >>> linked_list.append(2) >>> str(linked_list) '1 -> 2' """ if self.tail: self.tail.next_node = self.tail = Node(data) else: self.head = self.tail = Node(data) def extend(self, items: Iterable[int]) -> None: """Appends each item to the end of the LinkedList. >>> linked_list = LinkedList() >>> linked_list.extend([]) >>> str(linked_list) '' >>> linked_list.extend([1, 2]) >>> str(linked_list) '1 -> 2' >>> linked_list.extend([3,4]) >>> str(linked_list) '1 -> 2 -> 3 -> 4' """ for item in items: self.append(item) def make_linked_list(elements_list: Iterable[int]) -> LinkedList: """Creates a Linked List from the elements of the given sequence (list/tuple) and returns the head of the Linked List. >>> make_linked_list([]) Traceback (most recent call last): ... Exception: The Elements List is empty >>> make_linked_list([7]) 7 >>> make_linked_list(['abc']) abc >>> make_linked_list([7, 25]) 7 -> 25 """ if not elements_list: raise Exception("The Elements List is empty") linked_list = LinkedList() linked_list.extend(elements_list) return linked_list def in_reverse(linked_list: LinkedList) -> str: """Prints the elements of the given Linked List in reverse order >>> in_reverse(LinkedList()) '' >>> in_reverse(make_linked_list([69, 88, 73])) '73 <- 88 <- 69' """ return " <- ".join(str(line) for line in reversed(tuple(linked_list))) if __name__ == "__main__": from doctest import testmod testmod() linked_list = make_linked_list((14, 52, 14, 12, 43)) print(f"Linked List: {linked_list}") print(f"Reverse List: {in_reverse(linked_list)}")
ints listLinkedListints ints True ints tuplerange5 tupleLinkedListints ints True for i in range3: ... lenLinkedListrangei i True True True lenLinkedListabcdefgh 8 strLinkedList '' strLinkedListrange5 '0 1 2 3 4' ll LinkedList1, 2 tuplell 1, 2 ll.append3 tuplell 1, 2, 3 ll.append4 tuplell 1, 2, 3, 4 lenll 4 reverse nodes within groups of size k ll LinkedList1, 2, 3, 4, 5 ll.reverseknodes2 tuplell 2, 1, 4, 3, 5 strll '2 1 4 3 5'
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass @dataclass class Node: data: int next_node: Node | None = None class LinkedList: def __init__(self, ints: Iterable[int]) -> None: self.head: Node | None = None for i in ints: self.append(i) def __iter__(self) -> Iterator[int]: """ >>> ints = [] >>> list(LinkedList(ints)) == ints True >>> ints = tuple(range(5)) >>> tuple(LinkedList(ints)) == ints True """ node = self.head while node: yield node.data node = node.next_node def __len__(self) -> int: """ >>> for i in range(3): ... len(LinkedList(range(i))) == i True True True >>> len(LinkedList("abcdefgh")) 8 """ return sum(1 for _ in self) def __str__(self) -> str: """ >>> str(LinkedList([])) '' >>> str(LinkedList(range(5))) '0 -> 1 -> 2 -> 3 -> 4' """ return " -> ".join([str(node) for node in self]) def append(self, data: int) -> None: """ >>> ll = LinkedList([1, 2]) >>> tuple(ll) (1, 2) >>> ll.append(3) >>> tuple(ll) (1, 2, 3) >>> ll.append(4) >>> tuple(ll) (1, 2, 3, 4) >>> len(ll) 4 """ if not self.head: self.head = Node(data) return node = self.head while node.next_node: node = node.next_node node.next_node = Node(data) def reverse_k_nodes(self, group_size: int) -> None: """ reverse nodes within groups of size k >>> ll = LinkedList([1, 2, 3, 4, 5]) >>> ll.reverse_k_nodes(2) >>> tuple(ll) (2, 1, 4, 3, 5) >>> str(ll) '2 -> 1 -> 4 -> 3 -> 5' """ if self.head is None or self.head.next_node is None: return length = len(self) dummy_head = Node(0) dummy_head.next_node = self.head previous_node = dummy_head while length >= group_size: current_node = previous_node.next_node assert current_node next_node = current_node.next_node for _ in range(1, group_size): assert next_node, current_node current_node.next_node = next_node.next_node assert previous_node next_node.next_node = previous_node.next_node previous_node.next_node = next_node next_node = current_node.next_node previous_node = current_node length -= group_size self.head = dummy_head.next_node if __name__ == "__main__": import doctest doctest.testmod() ll = LinkedList([1, 2, 3, 4, 5]) print(f"Original Linked List: {ll}") k = 2 ll.reverse_k_nodes(k) print(f"After reversing groups of size {k}: {ll}")
Print the entire linked list iteratively. This function prints the elements of a linked list separated by ''. Parameters: head Node None: The head of the linked list to be printed, or None if the linked list is empty. head insertnodeNone, 0 head insertnodehead, 2 head insertnodehead, 1 printlinkedlisthead 021 head insertnodehead, 4 head insertnodehead, 5 printlinkedlisthead 02145 Insert a new node at the end of a linked list and return the new head. Parameters: head Node None: The head of the linked list. data int: The data to be inserted into the new node. Returns: Node: The new head of the linked list. head insertnodeNone, 10 head insertnodehead, 9 head insertnodehead, 8 printlinkedlisthead 1098 If the linked list is empty, the newnode becomes the head Rotate a linked list to the right by places times. Parameters: head: The head of the linked list. places: The number of places to rotate. Returns: Node: The head of the rotated linked list. rotatetotherightNone, places1 Traceback most recent call last: ... ValueError: The linked list is empty. head insertnodeNone, 1 rotatetotherighthead, places1 head True head insertnodeNone, 1 head insertnodehead, 2 head insertnodehead, 3 head insertnodehead, 4 head insertnodehead, 5 newhead rotatetotherighthead, places2 printlinkedlistnewhead 45123 Check if the list is empty or has only one element Calculate the length of the linked list Adjust the value of places to avoid places longer than the list. Find the new head position after rotation. Traverse to the new head position Update pointers to perform rotation
from __future__ import annotations from dataclasses import dataclass @dataclass class Node: data: int next_node: Node | None = None def print_linked_list(head: Node | None) -> None: """ Print the entire linked list iteratively. This function prints the elements of a linked list separated by '->'. Parameters: head (Node | None): The head of the linked list to be printed, or None if the linked list is empty. >>> head = insert_node(None, 0) >>> head = insert_node(head, 2) >>> head = insert_node(head, 1) >>> print_linked_list(head) 0->2->1 >>> head = insert_node(head, 4) >>> head = insert_node(head, 5) >>> print_linked_list(head) 0->2->1->4->5 """ if head is None: return while head.next_node is not None: print(head.data, end="->") head = head.next_node print(head.data) def insert_node(head: Node | None, data: int) -> Node: """ Insert a new node at the end of a linked list and return the new head. Parameters: head (Node | None): The head of the linked list. data (int): The data to be inserted into the new node. Returns: Node: The new head of the linked list. >>> head = insert_node(None, 10) >>> head = insert_node(head, 9) >>> head = insert_node(head, 8) >>> print_linked_list(head) 10->9->8 """ new_node = Node(data) # If the linked list is empty, the new_node becomes the head if head is None: return new_node temp_node = head while temp_node.next_node: temp_node = temp_node.next_node temp_node.next_node = new_node # type: ignore return head def rotate_to_the_right(head: Node, places: int) -> Node: """ Rotate a linked list to the right by places times. Parameters: head: The head of the linked list. places: The number of places to rotate. Returns: Node: The head of the rotated linked list. >>> rotate_to_the_right(None, places=1) Traceback (most recent call last): ... ValueError: The linked list is empty. >>> head = insert_node(None, 1) >>> rotate_to_the_right(head, places=1) == head True >>> head = insert_node(None, 1) >>> head = insert_node(head, 2) >>> head = insert_node(head, 3) >>> head = insert_node(head, 4) >>> head = insert_node(head, 5) >>> new_head = rotate_to_the_right(head, places=2) >>> print_linked_list(new_head) 4->5->1->2->3 """ # Check if the list is empty or has only one element if not head: raise ValueError("The linked list is empty.") if head.next_node is None: return head # Calculate the length of the linked list length = 1 temp_node = head while temp_node.next_node is not None: length += 1 temp_node = temp_node.next_node # Adjust the value of places to avoid places longer than the list. places %= length if places == 0: return head # As no rotation is needed. # Find the new head position after rotation. new_head_index = length - places # Traverse to the new head position temp_node = head for _ in range(new_head_index - 1): assert temp_node.next_node temp_node = temp_node.next_node # Update pointers to perform rotation assert temp_node.next_node new_head = temp_node.next_node temp_node.next_node = None temp_node = new_head while temp_node.next_node: temp_node = temp_node.next_node temp_node.next_node = head assert new_head return new_head if __name__ == "__main__": import doctest doctest.testmod() head = insert_node(None, 5) head = insert_node(head, 1) head = insert_node(head, 2) head = insert_node(head, 4) head = insert_node(head, 3) print("Original list: ", end="") print_linked_list(head) places = 3 new_head = rotate_to_the_right(head, places) print(f"After {places} iterations: ", end="") print_linked_list(new_head)
Create and initialize Node class instance. Node20 Node20 NodeHello, world! NodeHello, world! NodeNone NodeNone NodeTrue NodeTrue Get the string representation of this node. Node10.repr 'Node10' reprNode10 'Node10' strNode10 'Node10' Node10 Node10 Create and initialize LinkedList class instance. linkedlist LinkedList linkedlist.head is None True This function is intended for iterators to access and iterate through data inside linked list. linkedlist LinkedList linkedlist.inserttailtail linkedlist.inserttailtail1 linkedlist.inserttailtail2 for node in linkedlist: iter used here. ... node 'tail' 'tail1' 'tail2' Return length of linked list i.e. number of nodes linkedlist LinkedList lenlinkedlist 0 linkedlist.inserttailtail lenlinkedlist 1 linkedlist.insertheadhead lenlinkedlist 2 linkedlist.deletetail lenlinkedlist 1 linkedlist.deletehead lenlinkedlist 0 String representationvisualization of a Linked Lists linkedlist LinkedList linkedlist.inserttail1 linkedlist.inserttail3 linkedlist.repr '1 3' reprlinkedlist '1 3' strlinkedlist '1 3' linkedlist.inserttail5 flinkedlist '1 3 5' Indexing Support. Used to get a node at particular position linkedlist LinkedList for i in range0, 10: ... linkedlist.insertnthi, i allstrlinkedlisti stri for i in range0, 10 True linkedlist10 Traceback most recent call last: ... ValueError: list index out of range. linkedlistlenlinkedlist Traceback most recent call last: ... ValueError: list index out of range. Used to change the data of a particular node linkedlist LinkedList for i in range0, 10: ... linkedlist.insertnthi, i linkedlist0 666 linkedlist0 666 linkedlist5 666 linkedlist5 666 linkedlist10 666 Traceback most recent call last: ... ValueError: list index out of range. linkedlistlenlinkedlist 666 Traceback most recent call last: ... ValueError: list index out of range. Insert data to the end of linked list. linkedlist LinkedList linkedlist.inserttailtail linkedlist tail linkedlist.inserttailtail2 linkedlist tail tail2 linkedlist.inserttailtail3 linkedlist tail tail2 tail3 Insert data to the beginning of linked list. linkedlist LinkedList linkedlist.insertheadhead linkedlist head linkedlist.insertheadhead2 linkedlist head2 head linkedlist.insertheadhead3 linkedlist head3 head2 head Insert data at given index. linkedlist LinkedList linkedlist.inserttailfirst linkedlist.inserttailsecond linkedlist.inserttailthird linkedlist first second third linkedlist.insertnth1, fourth linkedlist first fourth second third linkedlist.insertnth3, fifth linkedlist first fourth second fifth third This method prints every node data. linkedlist LinkedList linkedlist.inserttailfirst linkedlist.inserttailsecond linkedlist.inserttailthird linkedlist first second third Delete the first node and return the node's data. linkedlist LinkedList linkedlist.inserttailfirst linkedlist.inserttailsecond linkedlist.inserttailthird linkedlist first second third linkedlist.deletehead 'first' linkedlist second third linkedlist.deletehead 'second' linkedlist third linkedlist.deletehead 'third' linkedlist.deletehead Traceback most recent call last: ... IndexError: List index out of range. Delete the tail end node and return the node's data. linkedlist LinkedList linkedlist.inserttailfirst linkedlist.inserttailsecond linkedlist.inserttailthird linkedlist first second third linkedlist.deletetail 'third' linkedlist first second linkedlist.deletetail 'second' linkedlist first linkedlist.deletetail 'first' linkedlist.deletetail Traceback most recent call last: ... IndexError: List index out of range. Delete node at given index and return the node's data. linkedlist LinkedList linkedlist.inserttailfirst linkedlist.inserttailsecond linkedlist.inserttailthird linkedlist first second third linkedlist.deletenth1 delete middle 'second' linkedlist first third linkedlist.deletenth5 this raises error Traceback most recent call last: ... IndexError: List index out of range. linkedlist.deletenth1 this also raises error Traceback most recent call last: ... IndexError: List index out of range. Check if linked list is empty. linkedlist LinkedList linkedlist.isempty True linkedlist.insertheadfirst linkedlist.isempty False This reverses the linked list order. linkedlist LinkedList linkedlist.inserttailfirst linkedlist.inserttailsecond linkedlist.inserttailthird linkedlist first second third linkedlist.reverse linkedlist third second first Store the current node's next node. Make the current node's nextnode point backwards Make the previous node be the current node Make the current node the nextnode node to progress iteration Return prev in order to put the head at the end testsinglylinkedlist This section of the test used varying data types for input. testsinglylinkedlist2 Check if it's empty or not Delete the head Delete the tail Delete a node in specific location in linked list Add a Node instance to its head Add None to its tail Reverse the linked list
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass from typing import Any @dataclass class Node: """ Create and initialize Node class instance. >>> Node(20) Node(20) >>> Node("Hello, world!") Node(Hello, world!) >>> Node(None) Node(None) >>> Node(True) Node(True) """ data: Any next_node: Node | None = None def __repr__(self) -> str: """ Get the string representation of this node. >>> Node(10).__repr__() 'Node(10)' >>> repr(Node(10)) 'Node(10)' >>> str(Node(10)) 'Node(10)' >>> Node(10) Node(10) """ return f"Node({self.data})" class LinkedList: def __init__(self): """ Create and initialize LinkedList class instance. >>> linked_list = LinkedList() >>> linked_list.head is None True """ self.head = None def __iter__(self) -> Iterator[Any]: """ This function is intended for iterators to access and iterate through data inside linked list. >>> linked_list = LinkedList() >>> linked_list.insert_tail("tail") >>> linked_list.insert_tail("tail_1") >>> linked_list.insert_tail("tail_2") >>> for node in linked_list: # __iter__ used here. ... node 'tail' 'tail_1' 'tail_2' """ node = self.head while node: yield node.data node = node.next_node def __len__(self) -> int: """ Return length of linked list i.e. number of nodes >>> linked_list = LinkedList() >>> len(linked_list) 0 >>> linked_list.insert_tail("tail") >>> len(linked_list) 1 >>> linked_list.insert_head("head") >>> len(linked_list) 2 >>> _ = linked_list.delete_tail() >>> len(linked_list) 1 >>> _ = linked_list.delete_head() >>> len(linked_list) 0 """ return sum(1 for _ in self) def __repr__(self) -> str: """ String representation/visualization of a Linked Lists >>> linked_list = LinkedList() >>> linked_list.insert_tail(1) >>> linked_list.insert_tail(3) >>> linked_list.__repr__() '1 -> 3' >>> repr(linked_list) '1 -> 3' >>> str(linked_list) '1 -> 3' >>> linked_list.insert_tail(5) >>> f"{linked_list}" '1 -> 3 -> 5' """ return " -> ".join([str(item) for item in self]) def __getitem__(self, index: int) -> Any: """ Indexing Support. Used to get a node at particular position >>> linked_list = LinkedList() >>> for i in range(0, 10): ... linked_list.insert_nth(i, i) >>> all(str(linked_list[i]) == str(i) for i in range(0, 10)) True >>> linked_list[-10] Traceback (most recent call last): ... ValueError: list index out of range. >>> linked_list[len(linked_list)] Traceback (most recent call last): ... ValueError: list index out of range. """ if not 0 <= index < len(self): raise ValueError("list index out of range.") for i, node in enumerate(self): if i == index: return node return None # Used to change the data of a particular node def __setitem__(self, index: int, data: Any) -> None: """ >>> linked_list = LinkedList() >>> for i in range(0, 10): ... linked_list.insert_nth(i, i) >>> linked_list[0] = 666 >>> linked_list[0] 666 >>> linked_list[5] = -666 >>> linked_list[5] -666 >>> linked_list[-10] = 666 Traceback (most recent call last): ... ValueError: list index out of range. >>> linked_list[len(linked_list)] = 666 Traceback (most recent call last): ... ValueError: list index out of range. """ if not 0 <= index < len(self): raise ValueError("list index out of range.") current = self.head for _ in range(index): current = current.next_node current.data = data def insert_tail(self, data: Any) -> None: """ Insert data to the end of linked list. >>> linked_list = LinkedList() >>> linked_list.insert_tail("tail") >>> linked_list tail >>> linked_list.insert_tail("tail_2") >>> linked_list tail -> tail_2 >>> linked_list.insert_tail("tail_3") >>> linked_list tail -> tail_2 -> tail_3 """ self.insert_nth(len(self), data) def insert_head(self, data: Any) -> None: """ Insert data to the beginning of linked list. >>> linked_list = LinkedList() >>> linked_list.insert_head("head") >>> linked_list head >>> linked_list.insert_head("head_2") >>> linked_list head_2 -> head >>> linked_list.insert_head("head_3") >>> linked_list head_3 -> head_2 -> head """ self.insert_nth(0, data) def insert_nth(self, index: int, data: Any) -> None: """ Insert data at given index. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_list.insert_nth(1, "fourth") >>> linked_list first -> fourth -> second -> third >>> linked_list.insert_nth(3, "fifth") >>> linked_list first -> fourth -> second -> fifth -> third """ if not 0 <= index <= len(self): raise IndexError("list index out of range") new_node = Node(data) if self.head is None: self.head = new_node elif index == 0: new_node.next_node = self.head # link new_node to head self.head = new_node else: temp = self.head for _ in range(index - 1): temp = temp.next_node new_node.next_node = temp.next_node temp.next_node = new_node def print_list(self) -> None: # print every node data """ This method prints every node data. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third """ print(self) def delete_head(self) -> Any: """ Delete the first node and return the node's data. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_list.delete_head() 'first' >>> linked_list second -> third >>> linked_list.delete_head() 'second' >>> linked_list third >>> linked_list.delete_head() 'third' >>> linked_list.delete_head() Traceback (most recent call last): ... IndexError: List index out of range. """ return self.delete_nth(0) def delete_tail(self) -> Any: # delete from tail """ Delete the tail end node and return the node's data. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_list.delete_tail() 'third' >>> linked_list first -> second >>> linked_list.delete_tail() 'second' >>> linked_list first >>> linked_list.delete_tail() 'first' >>> linked_list.delete_tail() Traceback (most recent call last): ... IndexError: List index out of range. """ return self.delete_nth(len(self) - 1) def delete_nth(self, index: int = 0) -> Any: """ Delete node at given index and return the node's data. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_list.delete_nth(1) # delete middle 'second' >>> linked_list first -> third >>> linked_list.delete_nth(5) # this raises error Traceback (most recent call last): ... IndexError: List index out of range. >>> linked_list.delete_nth(-1) # this also raises error Traceback (most recent call last): ... IndexError: List index out of range. """ if not 0 <= index <= len(self) - 1: # test if index is valid raise IndexError("List index out of range.") delete_node = self.head # default first node if index == 0: self.head = self.head.next_node else: temp = self.head for _ in range(index - 1): temp = temp.next_node delete_node = temp.next_node temp.next_node = temp.next_node.next_node return delete_node.data def is_empty(self) -> bool: """ Check if linked list is empty. >>> linked_list = LinkedList() >>> linked_list.is_empty() True >>> linked_list.insert_head("first") >>> linked_list.is_empty() False """ return self.head is None def reverse(self) -> None: """ This reverses the linked list order. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_list.reverse() >>> linked_list third -> second -> first """ prev = None current = self.head while current: # Store the current node's next node. next_node = current.next_node # Make the current node's next_node point backwards current.next_node = prev # Make the previous node be the current node prev = current # Make the current node the next_node node (to progress iteration) current = next_node # Return prev in order to put the head at the end self.head = prev def test_singly_linked_list() -> None: """ >>> test_singly_linked_list() """ linked_list = LinkedList() assert linked_list.is_empty() is True assert str(linked_list) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10): assert len(linked_list) == i linked_list.insert_nth(i, i + 1) assert str(linked_list) == " -> ".join(str(i) for i in range(1, 11)) linked_list.insert_head(0) linked_list.insert_tail(11) assert str(linked_list) == " -> ".join(str(i) for i in range(12)) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9) == 10 assert linked_list.delete_tail() == 11 assert len(linked_list) == 9 assert str(linked_list) == " -> ".join(str(i) for i in range(1, 10)) assert all(linked_list[i] == i + 1 for i in range(9)) is True for i in range(9): linked_list[i] = -i assert all(linked_list[i] == -i for i in range(9)) is True linked_list.reverse() assert str(linked_list) == " -> ".join(str(i) for i in range(-8, 1)) def test_singly_linked_list_2() -> None: """ This section of the test used varying data types for input. >>> test_singly_linked_list_2() """ test_input = [ -9, 100, Node(77345112), "dlrow olleH", 7, 5555, 0, -192.55555, "Hello, world!", 77.9, Node(10), None, None, 12.20, ] linked_list = LinkedList() for i in test_input: linked_list.insert_tail(i) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(linked_list) == "-9 -> 100 -> Node(77345112) -> dlrow olleH -> 7 -> 5555 -> " "0 -> -192.55555 -> Hello, world! -> 77.9 -> Node(10) -> None -> None -> 12.2" ) # Delete the head result = linked_list.delete_head() assert result == -9 assert ( str(linked_list) == "100 -> Node(77345112) -> dlrow olleH -> 7 -> 5555 -> 0 -> " "-192.55555 -> Hello, world! -> 77.9 -> Node(10) -> None -> None -> 12.2" ) # Delete the tail result = linked_list.delete_tail() assert result == 12.2 assert ( str(linked_list) == "100 -> Node(77345112) -> dlrow olleH -> 7 -> 5555 -> 0 -> " "-192.55555 -> Hello, world! -> 77.9 -> Node(10) -> None -> None" ) # Delete a node in specific location in linked list result = linked_list.delete_nth(10) assert result is None assert ( str(linked_list) == "100 -> Node(77345112) -> dlrow olleH -> 7 -> 5555 -> 0 -> " "-192.55555 -> Hello, world! -> 77.9 -> Node(10) -> None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!")) assert ( str(linked_list) == "Node(Hello again, world!) -> 100 -> Node(77345112) -> dlrow olleH -> " "7 -> 5555 -> 0 -> -192.55555 -> Hello, world! -> 77.9 -> Node(10) -> None" ) # Add None to its tail linked_list.insert_tail(None) assert ( str(linked_list) == "Node(Hello again, world!) -> 100 -> Node(77345112) -> dlrow olleH -> 7 -> " "5555 -> 0 -> -192.55555 -> Hello, world! -> 77.9 -> Node(10) -> None -> None" ) # Reverse the linked list linked_list.reverse() assert ( str(linked_list) == "None -> None -> Node(10) -> 77.9 -> Hello, world! -> -192.55555 -> 0 -> " "5555 -> 7 -> dlrow olleH -> Node(77345112) -> 100 -> Node(Hello again, world!)" ) def main(): from doctest import testmod testmod() linked_list = LinkedList() linked_list.insert_head(input("Inserting 1st at head ").strip()) linked_list.insert_head(input("Inserting 2nd at head ").strip()) print("\nPrint list:") linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail ").strip()) linked_list.insert_tail(input("Inserting 2nd at tail ").strip()) print("\nPrint list:") linked_list.print_list() print("\nDelete head") linked_list.delete_head() print("Delete tail") linked_list.delete_tail() print("\nPrint list:") linked_list.print_list() print("\nReverse linked list") linked_list.reverse() print("\nPrint list:") linked_list.print_list() print("\nString representation of linked list:") print(linked_list) print("\nReading/changing Node data using indexing:") print(f"Element at Position 1: {linked_list[1]}") linked_list[1] = input("Enter New Value: ").strip() print("New list:") print(linked_list) print(f"length of linked_list is : {len(linked_list)}") if __name__ == "__main__": main()
Based on Skip Lists: A Probabilistic Alternative to Balanced Trees by William Pugh https:epaperpress.comsortsearchdownloadskiplist.pdf :return: Visual representation of Node node NodeKey, 2 reprnode 'NodeKey: 2' :return: Number of forward references node NodeKey, 2 node.level 0 node.forward.appendNodeKey2, 4 node.level 1 node.forward.appendNodeKey3, 6 node.level 2 :return: Visual representation of SkipList skiplist SkipList printskiplist SkipListlevel0 skiplist.insertKey1, Value printskiplist doctest: ELLIPSIS SkipListlevel... root... Key1Key1... None ... skiplist.insertKey2, OtherValue printskiplist doctest: ELLIPSIS SkipListlevel... root... Key1Key1... Key2Key2... None ... :return: Random level from 1, self.maxlevel interval. Higher values are less likely. :param key: Searched key, :return: Tuple with searched node or None if given key is not present and list of nodes that refer if key is present of should refer to given node. Nodes with refer or should refer to output node i node.level When node level is lesser than i decrement i. node.forwardi.key key Jumping to node with key value higher or equal to searched key would result in skipping searched key. Each leftmost node relative to searched node will potentially have to be updated. lennode.forward ! 0 If current node doesn't contain any further references then searched key is not present. node.forward0.key key Next node key should be equal to search key if key is present. :param key: Key to remove from list. skiplist SkipList skiplist.insert2, Two skiplist.insert1, One skiplist.insert3, Three listskiplist 1, 2, 3 skiplist.delete2 listskiplist 1, 3 Remove or replace all references to removed node. :param key: Key to insert. :param value: Value associated with given key. skiplist SkipList skiplist.insert2, Two skiplist.find2 'Two' listskiplist 2 After level increase we have to add additional nodes to head. Change references to pass through new node. :param key: Search key. :return: Value associated with given key or None if given key is not present. skiplist SkipList skiplist.find2 skiplist.insert2, Two skiplist.find2 'Two' skiplist.insert2, Three skiplist.find2 'Three' Repeat test 100 times due to the probabilistic nature of skip list random values random bugs pytests
from __future__ import annotations from random import random from typing import Generic, TypeVar KT = TypeVar("KT") VT = TypeVar("VT") class Node(Generic[KT, VT]): def __init__(self, key: KT | str = "root", value: VT | None = None): self.key = key self.value = value self.forward: list[Node[KT, VT]] = [] def __repr__(self) -> str: """ :return: Visual representation of Node >>> node = Node("Key", 2) >>> repr(node) 'Node(Key: 2)' """ return f"Node({self.key}: {self.value})" @property def level(self) -> int: """ :return: Number of forward references >>> node = Node("Key", 2) >>> node.level 0 >>> node.forward.append(Node("Key2", 4)) >>> node.level 1 >>> node.forward.append(Node("Key3", 6)) >>> node.level 2 """ return len(self.forward) class SkipList(Generic[KT, VT]): def __init__(self, p: float = 0.5, max_level: int = 16): self.head: Node[KT, VT] = Node[KT, VT]() self.level = 0 self.p = p self.max_level = max_level def __str__(self) -> str: """ :return: Visual representation of SkipList >>> skip_list = SkipList() >>> print(skip_list) SkipList(level=0) >>> skip_list.insert("Key1", "Value") >>> print(skip_list) # doctest: +ELLIPSIS SkipList(level=... [root]--... [Key1]--Key1... None *... >>> skip_list.insert("Key2", "OtherValue") >>> print(skip_list) # doctest: +ELLIPSIS SkipList(level=... [root]--... [Key1]--Key1... [Key2]--Key2... None *... """ items = list(self) if len(items) == 0: return f"SkipList(level={self.level})" label_size = max((len(str(item)) for item in items), default=4) label_size = max(label_size, 4) + 4 node = self.head lines = [] forwards = node.forward.copy() lines.append(f"[{node.key}]".ljust(label_size, "-") + "* " * len(forwards)) lines.append(" " * label_size + "| " * len(forwards)) while len(node.forward) != 0: node = node.forward[0] lines.append( f"[{node.key}]".ljust(label_size, "-") + " ".join(str(n.key) if n.key == node.key else "|" for n in forwards) ) lines.append(" " * label_size + "| " * len(forwards)) forwards[: node.level] = node.forward lines.append("None".ljust(label_size) + "* " * len(forwards)) return f"SkipList(level={self.level})\n" + "\n".join(lines) def __iter__(self): node = self.head while len(node.forward) != 0: yield node.forward[0].key node = node.forward[0] def random_level(self) -> int: """ :return: Random level from [1, self.max_level] interval. Higher values are less likely. """ level = 1 while random() < self.p and level < self.max_level: level += 1 return level def _locate_node(self, key) -> tuple[Node[KT, VT] | None, list[Node[KT, VT]]]: """ :param key: Searched key, :return: Tuple with searched node (or None if given key is not present) and list of nodes that refer (if key is present) of should refer to given node. """ # Nodes with refer or should refer to output node update_vector = [] node = self.head for i in reversed(range(self.level)): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: node = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(node) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def delete(self, key: KT): """ :param key: Key to remove from list. >>> skip_list = SkipList() >>> skip_list.insert(2, "Two") >>> skip_list.insert(1, "One") >>> skip_list.insert(3, "Three") >>> list(skip_list) [1, 2, 3] >>> skip_list.delete(2) >>> list(skip_list) [1, 3] """ node, update_vector = self._locate_node(key) if node is not None: for i, update_node in enumerate(update_vector): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: update_node.forward[i] = node.forward[i] else: update_node.forward = update_node.forward[:i] def insert(self, key: KT, value: VT): """ :param key: Key to insert. :param value: Value associated with given key. >>> skip_list = SkipList() >>> skip_list.insert(2, "Two") >>> skip_list.find(2) 'Two' >>> list(skip_list) [2] """ node, update_vector = self._locate_node(key) if node is not None: node.value = value else: level = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1, level): update_vector.append(self.head) self.level = level new_node = Node(key, value) for i, update_node in enumerate(update_vector[:level]): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i]) if update_node.level < i + 1: update_node.forward.append(new_node) else: update_node.forward[i] = new_node def find(self, key: VT) -> VT | None: """ :param key: Search key. :return: Value associated with given key or None if given key is not present. >>> skip_list = SkipList() >>> skip_list.find(2) >>> skip_list.insert(2, "Two") >>> skip_list.find(2) 'Two' >>> skip_list.insert(2, "Three") >>> skip_list.find(2) 'Three' """ node, _ = self._locate_node(key) if node is not None: return node.value return None def test_insert(): skip_list = SkipList() skip_list.insert("Key1", 3) skip_list.insert("Key2", 12) skip_list.insert("Key3", 41) skip_list.insert("Key4", -19) node = skip_list.head all_values = {} while node.level != 0: node = node.forward[0] all_values[node.key] = node.value assert len(all_values) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19 def test_insert_overrides_existing_value(): skip_list = SkipList() skip_list.insert("Key1", 10) skip_list.insert("Key1", 12) skip_list.insert("Key5", 7) skip_list.insert("Key7", 10) skip_list.insert("Key10", 5) skip_list.insert("Key7", 7) skip_list.insert("Key5", 5) skip_list.insert("Key10", 10) node = skip_list.head all_values = {} while node.level != 0: node = node.forward[0] all_values[node.key] = node.value if len(all_values) != 4: print() assert len(all_values) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10 def test_searching_empty_list_returns_none(): skip_list = SkipList() assert skip_list.find("Some key") is None def test_search(): skip_list = SkipList() skip_list.insert("Key2", 20) assert skip_list.find("Key2") == 20 skip_list.insert("Some Key", 10) skip_list.insert("Key2", 8) skip_list.insert("V", 13) assert skip_list.find("Y") is None assert skip_list.find("Key2") == 8 assert skip_list.find("Some Key") == 10 assert skip_list.find("V") == 13 def test_deleting_item_from_empty_list_do_nothing(): skip_list = SkipList() skip_list.delete("Some key") assert len(skip_list.head.forward) == 0 def test_deleted_items_are_not_founded_by_find_method(): skip_list = SkipList() skip_list.insert("Key1", 12) skip_list.insert("V", 13) skip_list.insert("X", 14) skip_list.insert("Key2", 15) skip_list.delete("V") skip_list.delete("Key2") assert skip_list.find("V") is None assert skip_list.find("Key2") is None def test_delete_removes_only_given_key(): skip_list = SkipList() skip_list.insert("Key1", 12) skip_list.insert("V", 13) skip_list.insert("X", 14) skip_list.insert("Key2", 15) skip_list.delete("V") assert skip_list.find("V") is None assert skip_list.find("X") == 14 assert skip_list.find("Key1") == 12 assert skip_list.find("Key2") == 15 skip_list.delete("X") assert skip_list.find("V") is None assert skip_list.find("X") is None assert skip_list.find("Key1") == 12 assert skip_list.find("Key2") == 15 skip_list.delete("Key1") assert skip_list.find("V") is None assert skip_list.find("X") is None assert skip_list.find("Key1") is None assert skip_list.find("Key2") == 15 skip_list.delete("Key2") assert skip_list.find("V") is None assert skip_list.find("X") is None assert skip_list.find("Key1") is None assert skip_list.find("Key2") is None def test_delete_doesnt_leave_dead_nodes(): skip_list = SkipList() skip_list.insert("Key1", 12) skip_list.insert("V", 13) skip_list.insert("X", 142) skip_list.insert("Key2", 15) skip_list.delete("X") def traverse_keys(node): yield node.key for forward_node in node.forward: yield from traverse_keys(forward_node) assert len(set(traverse_keys(skip_list.head))) == 4 def test_iter_always_yields_sorted_values(): def is_sorted(lst): return all(next_item >= item for item, next_item in zip(lst, lst[1:])) skip_list = SkipList() for i in range(10): skip_list.insert(i, i) assert is_sorted(list(skip_list)) skip_list.delete(5) skip_list.delete(8) skip_list.delete(2) assert is_sorted(list(skip_list)) skip_list.insert(-12, -12) skip_list.insert(77, 77) assert is_sorted(list(skip_list)) def pytests(): for _ in range(100): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def main(): """ >>> pytests() """ skip_list = SkipList() skip_list.insert(2, "2") skip_list.insert(4, "4") skip_list.insert(6, "4") skip_list.insert(4, "5") skip_list.insert(8, "4") skip_list.insert(9, "4") skip_list.delete(4) print(skip_list) if __name__ == "__main__": import doctest doctest.testmod() main()
linkedlist LinkedList listlinkedlist linkedlist.push0 tuplelinkedlist 0, linkedlist LinkedList lenlinkedlist 0 linkedlist.push0 lenlinkedlist 1 Add a new node with the given data to the beginning of the Linked List. Args: newdata Any: The data to be added to the new node. Returns: None Examples: linkedlist LinkedList linkedlist.push5 linkedlist.push4 linkedlist.push3 linkedlist.push2 linkedlist.push1 listlinkedlist 1, 2, 3, 4, 5 Swap the positions of two nodes in the Linked List based on their data values. Args: nodedata1: Data value of the first node to be swapped. nodedata2: Data value of the second node to be swapped. Note: If either of the specified data values isn't found then, no swapping occurs. Examples: When both values are present in a linked list. linkedlist LinkedList linkedlist.push5 linkedlist.push4 linkedlist.push3 linkedlist.push2 linkedlist.push1 listlinkedlist 1, 2, 3, 4, 5 linkedlist.swapnodes1, 5 tuplelinkedlist 5, 2, 3, 4, 1 When one value is present and the other isn't in the linked list. secondlist LinkedList secondlist.push6 secondlist.push7 secondlist.push8 secondlist.push9 secondlist.swapnodes1, 6 is None True When both values are absent in the linked list. secondlist LinkedList secondlist.push10 secondlist.push9 secondlist.push8 secondlist.push7 secondlist.swapnodes1, 3 is None True When linkedlist is empty. secondlist LinkedList secondlist.swapnodes1, 3 is None True Returns: None Swap the data values of the two nodes Python script that outputs the swap of nodes in a linked list.
from __future__ import annotations from collections.abc import Iterator from dataclasses import dataclass from typing import Any @dataclass class Node: data: Any next_node: Node | None = None @dataclass class LinkedList: head: Node | None = None def __iter__(self) -> Iterator: """ >>> linked_list = LinkedList() >>> list(linked_list) [] >>> linked_list.push(0) >>> tuple(linked_list) (0,) """ node = self.head while node: yield node.data node = node.next_node def __len__(self) -> int: """ >>> linked_list = LinkedList() >>> len(linked_list) 0 >>> linked_list.push(0) >>> len(linked_list) 1 """ return sum(1 for _ in self) def push(self, new_data: Any) -> None: """ Add a new node with the given data to the beginning of the Linked List. Args: new_data (Any): The data to be added to the new node. Returns: None Examples: >>> linked_list = LinkedList() >>> linked_list.push(5) >>> linked_list.push(4) >>> linked_list.push(3) >>> linked_list.push(2) >>> linked_list.push(1) >>> list(linked_list) [1, 2, 3, 4, 5] """ new_node = Node(new_data) new_node.next_node = self.head self.head = new_node def swap_nodes(self, node_data_1: Any, node_data_2: Any) -> None: """ Swap the positions of two nodes in the Linked List based on their data values. Args: node_data_1: Data value of the first node to be swapped. node_data_2: Data value of the second node to be swapped. Note: If either of the specified data values isn't found then, no swapping occurs. Examples: When both values are present in a linked list. >>> linked_list = LinkedList() >>> linked_list.push(5) >>> linked_list.push(4) >>> linked_list.push(3) >>> linked_list.push(2) >>> linked_list.push(1) >>> list(linked_list) [1, 2, 3, 4, 5] >>> linked_list.swap_nodes(1, 5) >>> tuple(linked_list) (5, 2, 3, 4, 1) When one value is present and the other isn't in the linked list. >>> second_list = LinkedList() >>> second_list.push(6) >>> second_list.push(7) >>> second_list.push(8) >>> second_list.push(9) >>> second_list.swap_nodes(1, 6) is None True When both values are absent in the linked list. >>> second_list = LinkedList() >>> second_list.push(10) >>> second_list.push(9) >>> second_list.push(8) >>> second_list.push(7) >>> second_list.swap_nodes(1, 3) is None True When linkedlist is empty. >>> second_list = LinkedList() >>> second_list.swap_nodes(1, 3) is None True Returns: None """ if node_data_1 == node_data_2: return node_1 = self.head while node_1 and node_1.data != node_data_1: node_1 = node_1.next_node node_2 = self.head while node_2 and node_2.data != node_data_2: node_2 = node_2.next_node if node_1 is None or node_2 is None: return # Swap the data values of the two nodes node_1.data, node_2.data = node_2.data, node_1.data if __name__ == "__main__": """ Python script that outputs the swap of nodes in a linked list. """ from doctest import testmod testmod() linked_list = LinkedList() for i in range(5, 0, -1): linked_list.push(i) print(f"Original Linked List: {list(linked_list)}") linked_list.swap_nodes(1, 4) print(f"Modified Linked List: {list(linked_list)}") print("After swapping the nodes whose data is 1 and 4.")
Implementation of Circular Queue using Python lists Circular FIFO queue with a fixed capacity def initself, n: int: self.n n self.array None self.n self.front 0 index of the first element self.rear 0 self.size 0 def lenself int: return self.size def isemptyself bool: return self.size 0 def firstself: return False if self.isempty else self.arrayself.front def enqueueself, data: if self.size self.n: raise ExceptionQUEUE IS FULL self.arrayself.rear data self.rear self.rear 1 self.n self.size 1 return self def dequeueself: if self.size 0: raise ExceptionUNDERFLOW temp self.arrayself.front self.arrayself.front None self.front self.front 1 self.n self.size 1 return temp
# Implementation of Circular Queue (using Python lists) class CircularQueue: """Circular FIFO queue with a fixed capacity""" def __init__(self, n: int): self.n = n self.array = [None] * self.n self.front = 0 # index of the first element self.rear = 0 self.size = 0 def __len__(self) -> int: """ >>> cq = CircularQueue(5) >>> len(cq) 0 >>> cq.enqueue("A") # doctest: +ELLIPSIS <data_structures.queue.circular_queue.CircularQueue object at ... >>> len(cq) 1 """ return self.size def is_empty(self) -> bool: """ >>> cq = CircularQueue(5) >>> cq.is_empty() True >>> cq.enqueue("A").is_empty() False """ return self.size == 0 def first(self): """ >>> cq = CircularQueue(5) >>> cq.first() False >>> cq.enqueue("A").first() 'A' """ return False if self.is_empty() else self.array[self.front] def enqueue(self, data): """ This function insert an element in the queue using self.rear value as an index >>> cq = CircularQueue(5) >>> cq.enqueue("A") # doctest: +ELLIPSIS <data_structures.queue.circular_queue.CircularQueue object at ... >>> (cq.size, cq.first()) (1, 'A') >>> cq.enqueue("B") # doctest: +ELLIPSIS <data_structures.queue.circular_queue.CircularQueue object at ... >>> (cq.size, cq.first()) (2, 'A') """ if self.size >= self.n: raise Exception("QUEUE IS FULL") self.array[self.rear] = data self.rear = (self.rear + 1) % self.n self.size += 1 return self def dequeue(self): """ This function removes an element from the queue using on self.front value as an index >>> cq = CircularQueue(5) >>> cq.dequeue() Traceback (most recent call last): ... Exception: UNDERFLOW >>> cq.enqueue("A").enqueue("B").dequeue() 'A' >>> (cq.size, cq.first()) (1, 'B') >>> cq.dequeue() 'B' >>> cq.dequeue() Traceback (most recent call last): ... Exception: UNDERFLOW """ if self.size == 0: raise Exception("UNDERFLOW") temp = self.array[self.front] self.array[self.front] = None self.front = (self.front + 1) % self.n self.size -= 1 return temp
Implementation of Circular Queue using linked lists https:en.wikipedia.orgwikiCircularbuffer Circular FIFO list with the given capacity default queue length : 6 cq CircularQueueLinkedList2 cq.enqueue'a' cq.enqueue'b' cq.enqueue'c' Traceback most recent call last: ... Exception: Full Queue Checks where the queue is empty or not cq CircularQueueLinkedList cq.isempty True cq.enqueue'a' cq.isempty False cq.dequeue 'a' cq.isempty True Returns the first element of the queue cq CircularQueueLinkedList cq.first Traceback most recent call last: ... Exception: Empty Queue cq.enqueue'a' cq.first 'a' cq.dequeue 'a' cq.first Traceback most recent call last: ... Exception: Empty Queue cq.enqueue'b' cq.enqueue'c' cq.first 'b' Saves data at the end of the queue cq CircularQueueLinkedList cq.enqueue'a' cq.enqueue'b' cq.dequeue 'a' cq.dequeue 'b' cq.dequeue Traceback most recent call last: ... Exception: Empty Queue Removes and retrieves the first element of the queue cq CircularQueueLinkedList cq.dequeue Traceback most recent call last: ... Exception: Empty Queue cq.enqueue'a' cq.dequeue 'a' cq.dequeue Traceback most recent call last: ... Exception: Empty Queue
# Implementation of Circular Queue using linked lists # https://en.wikipedia.org/wiki/Circular_buffer from __future__ import annotations from typing import Any class CircularQueueLinkedList: """ Circular FIFO list with the given capacity (default queue length : 6) >>> cq = CircularQueueLinkedList(2) >>> cq.enqueue('a') >>> cq.enqueue('b') >>> cq.enqueue('c') Traceback (most recent call last): ... Exception: Full Queue """ def __init__(self, initial_capacity: int = 6) -> None: self.front: Node | None = None self.rear: Node | None = None self.create_linked_list(initial_capacity) def create_linked_list(self, initial_capacity: int) -> None: current_node = Node() self.front = current_node self.rear = current_node previous_node = current_node for _ in range(1, initial_capacity): current_node = Node() previous_node.next = current_node current_node.prev = previous_node previous_node = current_node previous_node.next = self.front self.front.prev = previous_node def is_empty(self) -> bool: """ Checks where the queue is empty or not >>> cq = CircularQueueLinkedList() >>> cq.is_empty() True >>> cq.enqueue('a') >>> cq.is_empty() False >>> cq.dequeue() 'a' >>> cq.is_empty() True """ return ( self.front == self.rear and self.front is not None and self.front.data is None ) def first(self) -> Any | None: """ Returns the first element of the queue >>> cq = CircularQueueLinkedList() >>> cq.first() Traceback (most recent call last): ... Exception: Empty Queue >>> cq.enqueue('a') >>> cq.first() 'a' >>> cq.dequeue() 'a' >>> cq.first() Traceback (most recent call last): ... Exception: Empty Queue >>> cq.enqueue('b') >>> cq.enqueue('c') >>> cq.first() 'b' """ self.check_can_perform_operation() return self.front.data if self.front else None def enqueue(self, data: Any) -> None: """ Saves data at the end of the queue >>> cq = CircularQueueLinkedList() >>> cq.enqueue('a') >>> cq.enqueue('b') >>> cq.dequeue() 'a' >>> cq.dequeue() 'b' >>> cq.dequeue() Traceback (most recent call last): ... Exception: Empty Queue """ if self.rear is None: return self.check_is_full() if not self.is_empty(): self.rear = self.rear.next if self.rear: self.rear.data = data def dequeue(self) -> Any: """ Removes and retrieves the first element of the queue >>> cq = CircularQueueLinkedList() >>> cq.dequeue() Traceback (most recent call last): ... Exception: Empty Queue >>> cq.enqueue('a') >>> cq.dequeue() 'a' >>> cq.dequeue() Traceback (most recent call last): ... Exception: Empty Queue """ self.check_can_perform_operation() if self.rear is None or self.front is None: return None if self.front == self.rear: data = self.front.data self.front.data = None return data old_front = self.front self.front = old_front.next data = old_front.data old_front.data = None return data def check_can_perform_operation(self) -> None: if self.is_empty(): raise Exception("Empty Queue") def check_is_full(self) -> None: if self.rear and self.rear.next == self.front: raise Exception("Full Queue") class Node: def __init__(self) -> None: self.data: Any | None = None self.next: Node | None = None self.prev: Node | None = None if __name__ == "__main__": import doctest doctest.testmod()
Implementation of double ended queue. Deque data structure. Operations appendval: Any None appendleftval: Any None extenditerable: Iterable None extendleftiterable: Iterable None pop Any popleft Any Observers isempty bool Attributes front: Node front of the deque a.k.a. the first element back: Node back of the element a.k.a. the last element len: int the number of nodes Representation of a node. Contains a value and a pointer to the next node as well as to the previous one. Helper class for iteration. Will be used to implement iteration. Attributes cur: Node the current node of the iteration. ourdeque Deque1, 2, 3 iterator iterourdeque ourdeque Deque1, 2, 3 iterator iterourdeque nextiterator 1 nextiterator 2 nextiterator 3 finished iterating append every value to the deque Adds val to the end of the deque. Time complexity: O1 ourdeque1 Deque1, 2, 3 ourdeque1.append4 ourdeque1 1, 2, 3, 4 ourdeque2 Deque'ab' ourdeque2.append'c' ourdeque2 'a', 'b', 'c' from collections import deque dequecollections1 deque1, 2, 3 dequecollections1.append4 dequecollections1 deque1, 2, 3, 4 dequecollections2 deque'ab' dequecollections2.append'c' dequecollections2 deque'a', 'b', 'c' listourdeque1 listdequecollections1 True listourdeque2 listdequecollections2 True front back connect nodes make sure there were no errors Adds val to the beginning of the deque. Time complexity: O1 ourdeque1 Deque2, 3 ourdeque1.appendleft1 ourdeque1 1, 2, 3 ourdeque2 Deque'bc' ourdeque2.appendleft'a' ourdeque2 'a', 'b', 'c' from collections import deque dequecollections1 deque2, 3 dequecollections1.appendleft1 dequecollections1 deque1, 2, 3 dequecollections2 deque'bc' dequecollections2.appendleft'a' dequecollections2 deque'a', 'b', 'c' listourdeque1 listdequecollections1 True listourdeque2 listdequecollections2 True front back connect nodes make sure there were no errors Appends every value of iterable to the end of the deque. Time complexity: On ourdeque1 Deque1, 2, 3 ourdeque1.extend4, 5 ourdeque1 1, 2, 3, 4, 5 ourdeque2 Deque'ab' ourdeque2.extend'cd' ourdeque2 'a', 'b', 'c', 'd' from collections import deque dequecollections1 deque1, 2, 3 dequecollections1.extend4, 5 dequecollections1 deque1, 2, 3, 4, 5 dequecollections2 deque'ab' dequecollections2.extend'cd' dequecollections2 deque'a', 'b', 'c', 'd' listourdeque1 listdequecollections1 True listourdeque2 listdequecollections2 True Appends every value of iterable to the beginning of the deque. Time complexity: On ourdeque1 Deque1, 2, 3 ourdeque1.extendleft0, 1 ourdeque1 1, 0, 1, 2, 3 ourdeque2 Deque'cd' ourdeque2.extendleft'ba' ourdeque2 'a', 'b', 'c', 'd' from collections import deque dequecollections1 deque1, 2, 3 dequecollections1.extendleft0, 1 dequecollections1 deque1, 0, 1, 2, 3 dequecollections2 deque'cd' dequecollections2.extendleft'ba' dequecollections2 deque'a', 'b', 'c', 'd' listourdeque1 listdequecollections1 True listourdeque2 listdequecollections2 True Removes the last element of the deque and returns it. Time complexity: O1 returns topop.val: the value of the node to pop. ourdeque1 Deque1 ourpopped1 ourdeque1.pop ourpopped1 1 ourdeque1 ourdeque2 Deque1, 2, 3, 15182 ourpopped2 ourdeque2.pop ourpopped2 15182 ourdeque2 1, 2, 3 from collections import deque dequecollections deque1, 2, 3, 15182 collectionspopped dequecollections.pop collectionspopped 15182 dequecollections deque1, 2, 3 listourdeque2 listdequecollections True ourpopped2 collectionspopped True make sure the deque has elements to pop if only one element in the queue: point the front and back to None else remove one element from back drop the last node, python will deallocate memory automatically Removes the first element of the deque and returns it. Time complexity: O1 returns topop.val: the value of the node to pop. ourdeque1 Deque1 ourpopped1 ourdeque1.pop ourpopped1 1 ourdeque1 ourdeque2 Deque15182, 1, 2, 3 ourpopped2 ourdeque2.popleft ourpopped2 15182 ourdeque2 1, 2, 3 from collections import deque dequecollections deque15182, 1, 2, 3 collectionspopped dequecollections.popleft collectionspopped 15182 dequecollections deque1, 2, 3 listourdeque2 listdequecollections True ourpopped2 collectionspopped True make sure the deque has elements to pop if only one element in the queue: point the front and back to None else remove one element from front Checks if the deque is empty. Time complexity: O1 ourdeque Deque1, 2, 3 ourdeque.isempty False ouremptydeque Deque ouremptydeque.isempty True from collections import deque emptydequecollections deque listouremptydeque listemptydequecollections True Implements len function. Returns the length of the deque. Time complexity: O1 ourdeque Deque1, 2, 3 lenourdeque 3 ouremptydeque Deque lenouremptydeque 0 from collections import deque dequecollections deque1, 2, 3 lendequecollections 3 emptydequecollections deque lenemptydequecollections 0 lenouremptydeque lenemptydequecollections True Implements operator. Returns if self is equal to other. Time complexity: On ourdeque1 Deque1, 2, 3 ourdeque2 Deque1, 2, 3 ourdeque1 ourdeque2 True ourdeque3 Deque1, 2 ourdeque1 ourdeque3 False from collections import deque dequecollections1 deque1, 2, 3 dequecollections2 deque1, 2, 3 dequecollections1 dequecollections2 True dequecollections3 deque1, 2 dequecollections1 dequecollections3 False ourdeque1 ourdeque2 dequecollections1 dequecollections2 True ourdeque1 ourdeque3 dequecollections1 dequecollections3 True if the length of the dequeues are not the same, they are not equal compare every value Implements iteration. Time complexity: O1 ourdeque Deque1, 2, 3 for v in ourdeque: ... printv 1 2 3 from collections import deque dequecollections deque1, 2, 3 for v in dequecollections: ... printv 1 2 3 Implements representation of the deque. Represents it as a list, with its values between '' and ''. Time complexity: On ourdeque Deque1, 2, 3 ourdeque 1, 2, 3 append the values in a list to display
from __future__ import annotations from collections.abc import Iterable from dataclasses import dataclass from typing import Any class Deque: """ Deque data structure. Operations ---------- append(val: Any) -> None appendleft(val: Any) -> None extend(iterable: Iterable) -> None extendleft(iterable: Iterable) -> None pop() -> Any popleft() -> Any Observers --------- is_empty() -> bool Attributes ---------- _front: _Node front of the deque a.k.a. the first element _back: _Node back of the element a.k.a. the last element _len: int the number of nodes """ __slots__ = ("_front", "_back", "_len") @dataclass class _Node: """ Representation of a node. Contains a value and a pointer to the next node as well as to the previous one. """ val: Any = None next_node: Deque._Node | None = None prev_node: Deque._Node | None = None class _Iterator: """ Helper class for iteration. Will be used to implement iteration. Attributes ---------- _cur: _Node the current node of the iteration. """ __slots__ = ("_cur",) def __init__(self, cur: Deque._Node | None) -> None: self._cur = cur def __iter__(self) -> Deque._Iterator: """ >>> our_deque = Deque([1, 2, 3]) >>> iterator = iter(our_deque) """ return self def __next__(self) -> Any: """ >>> our_deque = Deque([1, 2, 3]) >>> iterator = iter(our_deque) >>> next(iterator) 1 >>> next(iterator) 2 >>> next(iterator) 3 """ if self._cur is None: # finished iterating raise StopIteration val = self._cur.val self._cur = self._cur.next_node return val def __init__(self, iterable: Iterable[Any] | None = None) -> None: self._front: Any = None self._back: Any = None self._len: int = 0 if iterable is not None: # append every value to the deque for val in iterable: self.append(val) def append(self, val: Any) -> None: """ Adds val to the end of the deque. Time complexity: O(1) >>> our_deque_1 = Deque([1, 2, 3]) >>> our_deque_1.append(4) >>> our_deque_1 [1, 2, 3, 4] >>> our_deque_2 = Deque('ab') >>> our_deque_2.append('c') >>> our_deque_2 ['a', 'b', 'c'] >>> from collections import deque >>> deque_collections_1 = deque([1, 2, 3]) >>> deque_collections_1.append(4) >>> deque_collections_1 deque([1, 2, 3, 4]) >>> deque_collections_2 = deque('ab') >>> deque_collections_2.append('c') >>> deque_collections_2 deque(['a', 'b', 'c']) >>> list(our_deque_1) == list(deque_collections_1) True >>> list(our_deque_2) == list(deque_collections_2) True """ node = self._Node(val, None, None) if self.is_empty(): # front = back self._front = self._back = node self._len = 1 else: # connect nodes self._back.next_node = node node.prev_node = self._back self._back = node # assign new back to the new node self._len += 1 # make sure there were no errors assert not self.is_empty(), "Error on appending value." def appendleft(self, val: Any) -> None: """ Adds val to the beginning of the deque. Time complexity: O(1) >>> our_deque_1 = Deque([2, 3]) >>> our_deque_1.appendleft(1) >>> our_deque_1 [1, 2, 3] >>> our_deque_2 = Deque('bc') >>> our_deque_2.appendleft('a') >>> our_deque_2 ['a', 'b', 'c'] >>> from collections import deque >>> deque_collections_1 = deque([2, 3]) >>> deque_collections_1.appendleft(1) >>> deque_collections_1 deque([1, 2, 3]) >>> deque_collections_2 = deque('bc') >>> deque_collections_2.appendleft('a') >>> deque_collections_2 deque(['a', 'b', 'c']) >>> list(our_deque_1) == list(deque_collections_1) True >>> list(our_deque_2) == list(deque_collections_2) True """ node = self._Node(val, None, None) if self.is_empty(): # front = back self._front = self._back = node self._len = 1 else: # connect nodes node.next_node = self._front self._front.prev_node = node self._front = node # assign new front to the new node self._len += 1 # make sure there were no errors assert not self.is_empty(), "Error on appending value." def extend(self, iterable: Iterable[Any]) -> None: """ Appends every value of iterable to the end of the deque. Time complexity: O(n) >>> our_deque_1 = Deque([1, 2, 3]) >>> our_deque_1.extend([4, 5]) >>> our_deque_1 [1, 2, 3, 4, 5] >>> our_deque_2 = Deque('ab') >>> our_deque_2.extend('cd') >>> our_deque_2 ['a', 'b', 'c', 'd'] >>> from collections import deque >>> deque_collections_1 = deque([1, 2, 3]) >>> deque_collections_1.extend([4, 5]) >>> deque_collections_1 deque([1, 2, 3, 4, 5]) >>> deque_collections_2 = deque('ab') >>> deque_collections_2.extend('cd') >>> deque_collections_2 deque(['a', 'b', 'c', 'd']) >>> list(our_deque_1) == list(deque_collections_1) True >>> list(our_deque_2) == list(deque_collections_2) True """ for val in iterable: self.append(val) def extendleft(self, iterable: Iterable[Any]) -> None: """ Appends every value of iterable to the beginning of the deque. Time complexity: O(n) >>> our_deque_1 = Deque([1, 2, 3]) >>> our_deque_1.extendleft([0, -1]) >>> our_deque_1 [-1, 0, 1, 2, 3] >>> our_deque_2 = Deque('cd') >>> our_deque_2.extendleft('ba') >>> our_deque_2 ['a', 'b', 'c', 'd'] >>> from collections import deque >>> deque_collections_1 = deque([1, 2, 3]) >>> deque_collections_1.extendleft([0, -1]) >>> deque_collections_1 deque([-1, 0, 1, 2, 3]) >>> deque_collections_2 = deque('cd') >>> deque_collections_2.extendleft('ba') >>> deque_collections_2 deque(['a', 'b', 'c', 'd']) >>> list(our_deque_1) == list(deque_collections_1) True >>> list(our_deque_2) == list(deque_collections_2) True """ for val in iterable: self.appendleft(val) def pop(self) -> Any: """ Removes the last element of the deque and returns it. Time complexity: O(1) @returns topop.val: the value of the node to pop. >>> our_deque1 = Deque([1]) >>> our_popped1 = our_deque1.pop() >>> our_popped1 1 >>> our_deque1 [] >>> our_deque2 = Deque([1, 2, 3, 15182]) >>> our_popped2 = our_deque2.pop() >>> our_popped2 15182 >>> our_deque2 [1, 2, 3] >>> from collections import deque >>> deque_collections = deque([1, 2, 3, 15182]) >>> collections_popped = deque_collections.pop() >>> collections_popped 15182 >>> deque_collections deque([1, 2, 3]) >>> list(our_deque2) == list(deque_collections) True >>> our_popped2 == collections_popped True """ # make sure the deque has elements to pop assert not self.is_empty(), "Deque is empty." topop = self._back # if only one element in the queue: point the front and back to None # else remove one element from back if self._front == self._back: self._front = None self._back = None else: self._back = self._back.prev_node # set new back # drop the last node, python will deallocate memory automatically self._back.next_node = None self._len -= 1 return topop.val def popleft(self) -> Any: """ Removes the first element of the deque and returns it. Time complexity: O(1) @returns topop.val: the value of the node to pop. >>> our_deque1 = Deque([1]) >>> our_popped1 = our_deque1.pop() >>> our_popped1 1 >>> our_deque1 [] >>> our_deque2 = Deque([15182, 1, 2, 3]) >>> our_popped2 = our_deque2.popleft() >>> our_popped2 15182 >>> our_deque2 [1, 2, 3] >>> from collections import deque >>> deque_collections = deque([15182, 1, 2, 3]) >>> collections_popped = deque_collections.popleft() >>> collections_popped 15182 >>> deque_collections deque([1, 2, 3]) >>> list(our_deque2) == list(deque_collections) True >>> our_popped2 == collections_popped True """ # make sure the deque has elements to pop assert not self.is_empty(), "Deque is empty." topop = self._front # if only one element in the queue: point the front and back to None # else remove one element from front if self._front == self._back: self._front = None self._back = None else: self._front = self._front.next_node # set new front and drop the first node self._front.prev_node = None self._len -= 1 return topop.val def is_empty(self) -> bool: """ Checks if the deque is empty. Time complexity: O(1) >>> our_deque = Deque([1, 2, 3]) >>> our_deque.is_empty() False >>> our_empty_deque = Deque() >>> our_empty_deque.is_empty() True >>> from collections import deque >>> empty_deque_collections = deque() >>> list(our_empty_deque) == list(empty_deque_collections) True """ return self._front is None def __len__(self) -> int: """ Implements len() function. Returns the length of the deque. Time complexity: O(1) >>> our_deque = Deque([1, 2, 3]) >>> len(our_deque) 3 >>> our_empty_deque = Deque() >>> len(our_empty_deque) 0 >>> from collections import deque >>> deque_collections = deque([1, 2, 3]) >>> len(deque_collections) 3 >>> empty_deque_collections = deque() >>> len(empty_deque_collections) 0 >>> len(our_empty_deque) == len(empty_deque_collections) True """ return self._len def __eq__(self, other: object) -> bool: """ Implements "==" operator. Returns if *self* is equal to *other*. Time complexity: O(n) >>> our_deque_1 = Deque([1, 2, 3]) >>> our_deque_2 = Deque([1, 2, 3]) >>> our_deque_1 == our_deque_2 True >>> our_deque_3 = Deque([1, 2]) >>> our_deque_1 == our_deque_3 False >>> from collections import deque >>> deque_collections_1 = deque([1, 2, 3]) >>> deque_collections_2 = deque([1, 2, 3]) >>> deque_collections_1 == deque_collections_2 True >>> deque_collections_3 = deque([1, 2]) >>> deque_collections_1 == deque_collections_3 False >>> (our_deque_1 == our_deque_2) == (deque_collections_1 == deque_collections_2) True >>> (our_deque_1 == our_deque_3) == (deque_collections_1 == deque_collections_3) True """ if not isinstance(other, Deque): return NotImplemented me = self._front oth = other._front # if the length of the dequeues are not the same, they are not equal if len(self) != len(other): return False while me is not None and oth is not None: # compare every value if me.val != oth.val: return False me = me.next_node oth = oth.next_node return True def __iter__(self) -> Deque._Iterator: """ Implements iteration. Time complexity: O(1) >>> our_deque = Deque([1, 2, 3]) >>> for v in our_deque: ... print(v) 1 2 3 >>> from collections import deque >>> deque_collections = deque([1, 2, 3]) >>> for v in deque_collections: ... print(v) 1 2 3 """ return Deque._Iterator(self._front) def __repr__(self) -> str: """ Implements representation of the deque. Represents it as a list, with its values between '[' and ']'. Time complexity: O(n) >>> our_deque = Deque([1, 2, 3]) >>> our_deque [1, 2, 3] """ values_list = [] aux = self._front while aux is not None: # append the values in a list to display values_list.append(aux.val) aux = aux.next_node return f"[{', '.join(repr(val) for val in values_list)}]" if __name__ == "__main__": import doctest doctest.testmod() dq = Deque([3]) dq.pop()
A Queue using a linked list like structure from future import annotations from collections.abc import Iterator from typing import Any class Node: def initself, data: Any None: self.data: Any data self.next: Node None None def strself str: return fself.data class LinkedQueue: def initself None: self.front: Node None None self.rear: Node None None def iterself IteratorAny: node self.front while node: yield node.data node node.next def lenself int: return lentupleiterself def strself str: return .joinstritem for item in self def isemptyself bool: return lenself 0 def putself, item: Any None: node Nodeitem if self.isempty: self.front self.rear node else: assert isinstanceself.rear, Node self.rear.next node self.rear node def getself Any: if self.isempty: raise IndexErrordequeue from empty queue assert isinstanceself.front, Node node self.front self.front self.front.next if self.front is None: self.rear None return node.data def clearself None: self.front self.rear None if name main: from doctest import testmod testmod
from __future__ import annotations from collections.abc import Iterator from typing import Any class Node: def __init__(self, data: Any) -> None: self.data: Any = data self.next: Node | None = None def __str__(self) -> str: return f"{self.data}" class LinkedQueue: """ >>> queue = LinkedQueue() >>> queue.is_empty() True >>> queue.put(5) >>> queue.put(9) >>> queue.put('python') >>> queue.is_empty() False >>> queue.get() 5 >>> queue.put('algorithms') >>> queue.get() 9 >>> queue.get() 'python' >>> queue.get() 'algorithms' >>> queue.is_empty() True >>> queue.get() Traceback (most recent call last): ... IndexError: dequeue from empty queue """ def __init__(self) -> None: self.front: Node | None = None self.rear: Node | None = None def __iter__(self) -> Iterator[Any]: node = self.front while node: yield node.data node = node.next def __len__(self) -> int: """ >>> queue = LinkedQueue() >>> for i in range(1, 6): ... queue.put(i) >>> len(queue) 5 >>> for i in range(1, 6): ... assert len(queue) == 6 - i ... _ = queue.get() >>> len(queue) 0 """ return len(tuple(iter(self))) def __str__(self) -> str: """ >>> queue = LinkedQueue() >>> for i in range(1, 4): ... queue.put(i) >>> queue.put("Python") >>> queue.put(3.14) >>> queue.put(True) >>> str(queue) '1 <- 2 <- 3 <- Python <- 3.14 <- True' """ return " <- ".join(str(item) for item in self) def is_empty(self) -> bool: """ >>> queue = LinkedQueue() >>> queue.is_empty() True >>> for i in range(1, 6): ... queue.put(i) >>> queue.is_empty() False """ return len(self) == 0 def put(self, item: Any) -> None: """ >>> queue = LinkedQueue() >>> queue.get() Traceback (most recent call last): ... IndexError: dequeue from empty queue >>> for i in range(1, 6): ... queue.put(i) >>> str(queue) '1 <- 2 <- 3 <- 4 <- 5' """ node = Node(item) if self.is_empty(): self.front = self.rear = node else: assert isinstance(self.rear, Node) self.rear.next = node self.rear = node def get(self) -> Any: """ >>> queue = LinkedQueue() >>> queue.get() Traceback (most recent call last): ... IndexError: dequeue from empty queue >>> queue = LinkedQueue() >>> for i in range(1, 6): ... queue.put(i) >>> for i in range(1, 6): ... assert queue.get() == i >>> len(queue) 0 """ if self.is_empty(): raise IndexError("dequeue from empty queue") assert isinstance(self.front, Node) node = self.front self.front = self.front.next if self.front is None: self.rear = None return node.data def clear(self) -> None: """ >>> queue = LinkedQueue() >>> for i in range(1, 6): ... queue.put(i) >>> queue.clear() >>> len(queue) 0 >>> str(queue) '' """ self.front = self.rear = None if __name__ == "__main__": from doctest import testmod testmod()
Pure Python implementations of a Fixed Priority Queue and an Element Priority Queue using Python lists. Tasks can be added to a Priority Queue at any time and in any order but when Tasks are removed then the Task with the highest priority is removed in FIFO order. In code we will use three levels of priority with priority zero Tasks being the most urgent high priority and priority 2 tasks being the least urgent. Examples fpq FixedPriorityQueue fpq.enqueue0, 10 fpq.enqueue1, 70 fpq.enqueue0, 100 fpq.enqueue2, 1 fpq.enqueue2, 5 fpq.enqueue1, 7 fpq.enqueue2, 4 fpq.enqueue1, 64 fpq.enqueue0, 128 printfpq Priority 0: 10, 100, 128 Priority 1: 70, 7, 64 Priority 2: 1, 5, 4 fpq.dequeue 10 fpq.dequeue 100 fpq.dequeue 128 fpq.dequeue 70 fpq.dequeue 7 printfpq Priority 0: Priority 1: 64 Priority 2: 1, 5, 4 fpq.dequeue 64 fpq.dequeue 1 fpq.dequeue 5 fpq.dequeue 4 fpq.dequeue Traceback most recent call last: ... datastructures.queue.priorityqueueusinglist.UnderFlowError: All queues are empty printfpq Priority 0: Priority 1: Priority 2: Add an element to a queue based on its priority. If the priority is invalid ValueError is raised. If the queue is full an OverFlowError is raised. Return the highest priority element in FIFO order. If the queue is empty then an under flow exception is raised. Element Priority Queue is the same as Fixed Priority Queue except that the value of the element itself is the priority. The rules for priorities are the same the as Fixed Priority Queue. epq ElementPriorityQueue epq.enqueue10 epq.enqueue70 epq.enqueue4 epq.enqueue1 epq.enqueue5 epq.enqueue7 epq.enqueue4 epq.enqueue64 epq.enqueue128 printepq 10, 70, 4, 1, 5, 7, 4, 64, 128 epq.dequeue 1 epq.dequeue 4 epq.dequeue 4 epq.dequeue 5 epq.dequeue 7 epq.dequeue 10 printepq 70, 64, 128 epq.dequeue 64 epq.dequeue 70 epq.dequeue 128 epq.dequeue Traceback most recent call last: ... datastructures.queue.priorityqueueusinglist.UnderFlowError: The queue is empty printepq This function enters the element into the queue If the queue is full an Exception is raised saying Over Flow! Return the highest priority element in FIFO order. If the queue is empty then an under flow exception is raised. Prints all the elements within the Element Priority Queue
class OverFlowError(Exception): pass class UnderFlowError(Exception): pass class FixedPriorityQueue: """ Tasks can be added to a Priority Queue at any time and in any order but when Tasks are removed then the Task with the highest priority is removed in FIFO order. In code we will use three levels of priority with priority zero Tasks being the most urgent (high priority) and priority 2 tasks being the least urgent. Examples >>> fpq = FixedPriorityQueue() >>> fpq.enqueue(0, 10) >>> fpq.enqueue(1, 70) >>> fpq.enqueue(0, 100) >>> fpq.enqueue(2, 1) >>> fpq.enqueue(2, 5) >>> fpq.enqueue(1, 7) >>> fpq.enqueue(2, 4) >>> fpq.enqueue(1, 64) >>> fpq.enqueue(0, 128) >>> print(fpq) Priority 0: [10, 100, 128] Priority 1: [70, 7, 64] Priority 2: [1, 5, 4] >>> fpq.dequeue() 10 >>> fpq.dequeue() 100 >>> fpq.dequeue() 128 >>> fpq.dequeue() 70 >>> fpq.dequeue() 7 >>> print(fpq) Priority 0: [] Priority 1: [64] Priority 2: [1, 5, 4] >>> fpq.dequeue() 64 >>> fpq.dequeue() 1 >>> fpq.dequeue() 5 >>> fpq.dequeue() 4 >>> fpq.dequeue() Traceback (most recent call last): ... data_structures.queue.priority_queue_using_list.UnderFlowError: All queues are empty >>> print(fpq) Priority 0: [] Priority 1: [] Priority 2: [] """ def __init__(self): self.queues = [ [], [], [], ] def enqueue(self, priority: int, data: int) -> None: """ Add an element to a queue based on its priority. If the priority is invalid ValueError is raised. If the queue is full an OverFlowError is raised. """ try: if len(self.queues[priority]) >= 100: raise OverflowError("Maximum queue size is 100") self.queues[priority].append(data) except IndexError: raise ValueError("Valid priorities are 0, 1, and 2") def dequeue(self) -> int: """ Return the highest priority element in FIFO order. If the queue is empty then an under flow exception is raised. """ for queue in self.queues: if queue: return queue.pop(0) raise UnderFlowError("All queues are empty") def __str__(self) -> str: return "\n".join(f"Priority {i}: {q}" for i, q in enumerate(self.queues)) class ElementPriorityQueue: """ Element Priority Queue is the same as Fixed Priority Queue except that the value of the element itself is the priority. The rules for priorities are the same the as Fixed Priority Queue. >>> epq = ElementPriorityQueue() >>> epq.enqueue(10) >>> epq.enqueue(70) >>> epq.enqueue(4) >>> epq.enqueue(1) >>> epq.enqueue(5) >>> epq.enqueue(7) >>> epq.enqueue(4) >>> epq.enqueue(64) >>> epq.enqueue(128) >>> print(epq) [10, 70, 4, 1, 5, 7, 4, 64, 128] >>> epq.dequeue() 1 >>> epq.dequeue() 4 >>> epq.dequeue() 4 >>> epq.dequeue() 5 >>> epq.dequeue() 7 >>> epq.dequeue() 10 >>> print(epq) [70, 64, 128] >>> epq.dequeue() 64 >>> epq.dequeue() 70 >>> epq.dequeue() 128 >>> epq.dequeue() Traceback (most recent call last): ... data_structures.queue.priority_queue_using_list.UnderFlowError: The queue is empty >>> print(epq) [] """ def __init__(self): self.queue = [] def enqueue(self, data: int) -> None: """ This function enters the element into the queue If the queue is full an Exception is raised saying Over Flow! """ if len(self.queue) == 100: raise OverFlowError("Maximum queue size is 100") self.queue.append(data) def dequeue(self) -> int: """ Return the highest priority element in FIFO order. If the queue is empty then an under flow exception is raised. """ if not self.queue: raise UnderFlowError("The queue is empty") else: data = min(self.queue) self.queue.remove(data) return data def __str__(self) -> str: """ Prints all the elements within the Element Priority Queue """ return str(self.queue) def fixed_priority_queue(): fpq = FixedPriorityQueue() fpq.enqueue(0, 10) fpq.enqueue(1, 70) fpq.enqueue(0, 100) fpq.enqueue(2, 1) fpq.enqueue(2, 5) fpq.enqueue(1, 7) fpq.enqueue(2, 4) fpq.enqueue(1, 64) fpq.enqueue(0, 128) print(fpq) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq.dequeue()) print(fpq.dequeue()) def element_priority_queue(): epq = ElementPriorityQueue() epq.enqueue(10) epq.enqueue(70) epq.enqueue(100) epq.enqueue(1) epq.enqueue(5) epq.enqueue(7) epq.enqueue(4) epq.enqueue(64) epq.enqueue(128) print(epq) print(epq.dequeue()) print(epq.dequeue()) print(epq.dequeue()) print(epq.dequeue()) print(epq.dequeue()) print(epq) print(epq.dequeue()) print(epq.dequeue()) print(epq.dequeue()) print(epq.dequeue()) print(epq.dequeue()) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
Queue represented by a Python list from collections.abc import Iterable from typing import Generic, TypeVar T TypeVarT class QueueByListGenericT: def initself, iterable: IterableT None None None: self.entries: listT listiterable or def lenself int: return lenself.entries def reprself str: return fQueuetupleself.entries def putself, item: T None: self.entries.appenditem def getself T: if not self.entries: raise IndexErrorQueue is empty return self.entries.pop0 def rotateself, rotation: int None: put self.entries.append get self.entries.pop for in rangerotation: putget0 def getfrontself T: return self.entries0 if name main: from doctest import testmod testmod
from collections.abc import Iterable from typing import Generic, TypeVar _T = TypeVar("_T") class QueueByList(Generic[_T]): def __init__(self, iterable: Iterable[_T] | None = None) -> None: """ >>> QueueByList() Queue(()) >>> QueueByList([10, 20, 30]) Queue((10, 20, 30)) >>> QueueByList((i**2 for i in range(1, 4))) Queue((1, 4, 9)) """ self.entries: list[_T] = list(iterable or []) def __len__(self) -> int: """ >>> len(QueueByList()) 0 >>> from string import ascii_lowercase >>> len(QueueByList(ascii_lowercase)) 26 >>> queue = QueueByList() >>> for i in range(1, 11): ... queue.put(i) >>> len(queue) 10 >>> for i in range(2): ... queue.get() 1 2 >>> len(queue) 8 """ return len(self.entries) def __repr__(self) -> str: """ >>> queue = QueueByList() >>> queue Queue(()) >>> str(queue) 'Queue(())' >>> queue.put(10) >>> queue Queue((10,)) >>> queue.put(20) >>> queue.put(30) >>> queue Queue((10, 20, 30)) """ return f"Queue({tuple(self.entries)})" def put(self, item: _T) -> None: """Put `item` to the Queue >>> queue = QueueByList() >>> queue.put(10) >>> queue.put(20) >>> len(queue) 2 >>> queue Queue((10, 20)) """ self.entries.append(item) def get(self) -> _T: """ Get `item` from the Queue >>> queue = QueueByList((10, 20, 30)) >>> queue.get() 10 >>> queue.put(40) >>> queue.get() 20 >>> queue.get() 30 >>> len(queue) 1 >>> queue.get() 40 >>> queue.get() Traceback (most recent call last): ... IndexError: Queue is empty """ if not self.entries: raise IndexError("Queue is empty") return self.entries.pop(0) def rotate(self, rotation: int) -> None: """Rotate the items of the Queue `rotation` times >>> queue = QueueByList([10, 20, 30, 40]) >>> queue Queue((10, 20, 30, 40)) >>> queue.rotate(1) >>> queue Queue((20, 30, 40, 10)) >>> queue.rotate(2) >>> queue Queue((40, 10, 20, 30)) """ put = self.entries.append get = self.entries.pop for _ in range(rotation): put(get(0)) def get_front(self) -> _T: """Get the front item from the Queue >>> queue = QueueByList((10, 20, 30)) >>> queue.get_front() 10 >>> queue Queue((10, 20, 30)) >>> queue.get() 10 >>> queue.get_front() 20 """ return self.entries[0] if __name__ == "__main__": from doctest import testmod testmod()
Queue implementation using two stacks from collections.abc import Iterable from typing import Generic, TypeVar T TypeVarT class QueueByTwoStacksGenericT: def initself, iterable: IterableT None None None: self.stack1: listT listiterable or self.stack2: listT def lenself int: return lenself.stack1 lenself.stack2 def reprself str: return fQueuetupleself.stack2::1 self.stack1 def putself, item: T None: self.stack1.appenditem def getself T: To reduce number of attribute lookups in while loop. stack1pop self.stack1.pop stack2append self.stack2.append if not self.stack2: while self.stack1: stack2appendstack1pop if not self.stack2: raise IndexErrorQueue is empty return self.stack2.pop if name main: from doctest import testmod testmod
from collections.abc import Iterable from typing import Generic, TypeVar _T = TypeVar("_T") class QueueByTwoStacks(Generic[_T]): def __init__(self, iterable: Iterable[_T] | None = None) -> None: """ >>> QueueByTwoStacks() Queue(()) >>> QueueByTwoStacks([10, 20, 30]) Queue((10, 20, 30)) >>> QueueByTwoStacks((i**2 for i in range(1, 4))) Queue((1, 4, 9)) """ self._stack1: list[_T] = list(iterable or []) self._stack2: list[_T] = [] def __len__(self) -> int: """ >>> len(QueueByTwoStacks()) 0 >>> from string import ascii_lowercase >>> len(QueueByTwoStacks(ascii_lowercase)) 26 >>> queue = QueueByTwoStacks() >>> for i in range(1, 11): ... queue.put(i) ... >>> len(queue) 10 >>> for i in range(2): ... queue.get() 1 2 >>> len(queue) 8 """ return len(self._stack1) + len(self._stack2) def __repr__(self) -> str: """ >>> queue = QueueByTwoStacks() >>> queue Queue(()) >>> str(queue) 'Queue(())' >>> queue.put(10) >>> queue Queue((10,)) >>> queue.put(20) >>> queue.put(30) >>> queue Queue((10, 20, 30)) """ return f"Queue({tuple(self._stack2[::-1] + self._stack1)})" def put(self, item: _T) -> None: """ Put `item` into the Queue >>> queue = QueueByTwoStacks() >>> queue.put(10) >>> queue.put(20) >>> len(queue) 2 >>> queue Queue((10, 20)) """ self._stack1.append(item) def get(self) -> _T: """ Get `item` from the Queue >>> queue = QueueByTwoStacks((10, 20, 30)) >>> queue.get() 10 >>> queue.put(40) >>> queue.get() 20 >>> queue.get() 30 >>> len(queue) 1 >>> queue.get() 40 >>> queue.get() Traceback (most recent call last): ... IndexError: Queue is empty """ # To reduce number of attribute look-ups in `while` loop. stack1_pop = self._stack1.pop stack2_append = self._stack2.append if not self._stack2: while self._stack1: stack2_append(stack1_pop()) if not self._stack2: raise IndexError("Queue is empty") return self._stack2.pop() if __name__ == "__main__": from doctest import testmod testmod()
Queue represented by a pseudo stack represented by a list with pop and append from typing import Any class Queue: def initself: self.stack self.length 0 def strself: printed strself.stack1:1 return printed Dequeues code item requirement: self.length 0 return dequeued item that was dequeued def getself Any: self.rotate1 dequeued self.stackself.length 1 self.stack self.stack:1 self.rotateself.length 1 self.length self.length 1 return dequeued Reports item at the front of self return item at front of self.stack def frontself Any: front self.get self.putfront self.rotateself.length 1 return front
from typing import Any class Queue: def __init__(self): self.stack = [] self.length = 0 def __str__(self): printed = "<" + str(self.stack)[1:-1] + ">" return printed """Enqueues {@code item} @param item item to enqueue""" def put(self, item: Any) -> None: self.stack.append(item) self.length = self.length + 1 """Dequeues {@code item} @requirement: |self.length| > 0 @return dequeued item that was dequeued""" def get(self) -> Any: self.rotate(1) dequeued = self.stack[self.length - 1] self.stack = self.stack[:-1] self.rotate(self.length - 1) self.length = self.length - 1 return dequeued """Rotates the queue {@code rotation} times @param rotation number of times to rotate queue""" def rotate(self, rotation: int) -> None: for _ in range(rotation): temp = self.stack[0] self.stack = self.stack[1:] self.put(temp) self.length = self.length - 1 """Reports item at the front of self @return item at front of self.stack""" def front(self) -> Any: front = self.get() self.put(front) self.rotate(self.length - 1) return front """Returns the length of this.stack""" def size(self) -> int: return self.length
Use a stack to check if a string of parentheses is balanced. balancedparentheses True balancedparentheses True balancedparentheses False balancedparentheses1234 True balancedparentheses True
from .stack import Stack def balanced_parentheses(parentheses: str) -> bool: """Use a stack to check if a string of parentheses is balanced. >>> balanced_parentheses("([]{})") True >>> balanced_parentheses("[()]{}{[()()]()}") True >>> balanced_parentheses("[(])") False >>> balanced_parentheses("1+2*3-4") True >>> balanced_parentheses("") True """ stack: Stack[str] = Stack() bracket_pairs = {"(": ")", "[": "]", "{": "}"} for bracket in parentheses: if bracket in bracket_pairs: stack.push(bracket) elif bracket in (")", "]", "}"): if stack.is_empty() or bracket_pairs[stack.pop()] != bracket: return False return stack.is_empty() if __name__ == "__main__": from doctest import testmod testmod() examples = ["((()))", "((())", "(()))"] print("Balanced parentheses demonstration:\n") for example in examples: not_str = "" if balanced_parentheses(example) else "not " print(f"{example} is {not_str}balanced")
Author: Alexander Joslin GitHub: github.comechoaj Explanation: https:medium.comhaleesammarimplementedinjsdijkstras2stack algorithmforevaluatingmathematicalexpressionsfc0837dae1ea We can use Dijkstra's two stack algorithm to solve an equation such as: 5 4 2 2 3 THESE ARE THE ALGORITHM'S RULES: RULE 1: Scan the expression from left to right. When an operand is encountered, push it onto the operand stack. RULE 2: When an operator is encountered in the expression, push it onto the operator stack. RULE 3: When a left parenthesis is encountered in the expression, ignore it. RULE 4: When a right parenthesis is encountered in the expression, pop an operator off the operator stack. The two operands it must operate on must be the last two operands pushed onto the operand stack. We therefore pop the operand stack twice, perform the operation, and push the result back onto the operand stack so it will be available for use as an operand of the next operator popped off the operator stack. RULE 5: When the entire infix expression has been scanned, the value left on the operand stack represents the value of the expression. NOTE: It only works with whole numbers. DocTests dijkstrastwostackalgorithm5 3 8 dijkstrastwostackalgorithm9 2 9 8 1 5 dijkstrastwostackalgorithm3 2 2 3 2 4 3 3 :param equation: a string :return: result: an integer RULE 1 RULE 2 RULE 4 RULE 5 answer 45
__author__ = "Alexander Joslin" import operator as op from .stack import Stack def dijkstras_two_stack_algorithm(equation: str) -> int: """ DocTests >>> dijkstras_two_stack_algorithm("(5 + 3)") 8 >>> dijkstras_two_stack_algorithm("((9 - (2 + 9)) + (8 - 1))") 5 >>> dijkstras_two_stack_algorithm("((((3 - 2) - (2 + 3)) + (2 - 4)) + 3)") -3 :param equation: a string :return: result: an integer """ operators = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub} operand_stack: Stack[int] = Stack() operator_stack: Stack[str] = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(i)) elif i in operators: # RULE 2 operator_stack.push(i) elif i == ")": # RULE 4 opr = operator_stack.peek() operator_stack.pop() num1 = operand_stack.peek() operand_stack.pop() num2 = operand_stack.peek() operand_stack.pop() total = operators[opr](num2, num1) operand_stack.push(total) # RULE 5 return operand_stack.peek() if __name__ == "__main__": equation = "(5 + ((4 * 2) * (2 + 3)))" # answer = 45 print(f"{equation} = {dijkstras_two_stack_algorithm(equation)}")
https:en.wikipedia.orgwikiInfixnotation https:en.wikipedia.orgwikiReversePolishnotation https:en.wikipedia.orgwikiShuntingyardalgorithm Return integer value representing an operator's precedence, or order of operation. https:en.wikipedia.orgwikiOrderofoperations Return the associativity of the operator char. https:en.wikipedia.orgwikiOperatorassociativity infixtopostfix1234 Traceback most recent call last: ... ValueError: Mismatched parentheses infixtopostfix '' infixtopostfix32 '3 2 ' infixtopostfix3456 '3 4 5 6 ' infixtopostfix12345 '1 2 3 4 5 ' infixtopostfixabcdefg 'a b c d e f g ' infixtopostfixxy5z2 'x y 5 z 2 ' infixtopostfix232 '2 3 2 ' Precedences are equal
from typing import Literal from .balanced_parentheses import balanced_parentheses from .stack import Stack PRECEDENCES: dict[str, int] = { "+": 1, "-": 1, "*": 2, "/": 2, "^": 3, } ASSOCIATIVITIES: dict[str, Literal["LR", "RL"]] = { "+": "LR", "-": "LR", "*": "LR", "/": "LR", "^": "RL", } def precedence(char: str) -> int: """ Return integer value representing an operator's precedence, or order of operation. https://en.wikipedia.org/wiki/Order_of_operations """ return PRECEDENCES.get(char, -1) def associativity(char: str) -> Literal["LR", "RL"]: """ Return the associativity of the operator `char`. https://en.wikipedia.org/wiki/Operator_associativity """ return ASSOCIATIVITIES[char] def infix_to_postfix(expression_str: str) -> str: """ >>> infix_to_postfix("(1*(2+3)+4))") Traceback (most recent call last): ... ValueError: Mismatched parentheses >>> infix_to_postfix("") '' >>> infix_to_postfix("3+2") '3 2 +' >>> infix_to_postfix("(3+4)*5-6") '3 4 + 5 * 6 -' >>> infix_to_postfix("(1+2)*3/4-5") '1 2 + 3 * 4 / 5 -' >>> infix_to_postfix("a+b*c+(d*e+f)*g") 'a b c * + d e * f + g * +' >>> infix_to_postfix("x^y/(5*z)+2") 'x y ^ 5 z * / 2 +' >>> infix_to_postfix("2^3^2") '2 3 2 ^ ^' """ if not balanced_parentheses(expression_str): raise ValueError("Mismatched parentheses") stack: Stack[str] = Stack() postfix = [] for char in expression_str: if char.isalpha() or char.isdigit(): postfix.append(char) elif char == "(": stack.push(char) elif char == ")": while not stack.is_empty() and stack.peek() != "(": postfix.append(stack.pop()) stack.pop() else: while True: if stack.is_empty(): stack.push(char) break char_precedence = precedence(char) tos_precedence = precedence(stack.peek()) if char_precedence > tos_precedence: stack.push(char) break if char_precedence < tos_precedence: postfix.append(stack.pop()) continue # Precedences are equal if associativity(char) == "RL": stack.push(char) break postfix.append(stack.pop()) while not stack.is_empty(): postfix.append(stack.pop()) return " ".join(postfix) if __name__ == "__main__": from doctest import testmod testmod() expression = "a+b*(c^d-e)^(f+g*h)-i" print("Infix to Postfix Notation demonstration:\n") print("Infix notation: " + expression) print("Postfix notation: " + infix_to_postfix(expression))
Output: Enter an Infix Equation a b c Symbol Stack Postfix c c c b cb cb a cba cba abc Infix abc Prefix infix2postfixabc doctest: NORMALIZEWHITESPACE Symbol Stack Postfix a a a b ab ab c abc abc abc 'abc' infix2postfix1a2b doctest: NORMALIZEWHITESPACE Symbol Stack Postfix 1 1 1 1 1 1 a 1a 1a 1a 2 1a2 1a2 b 1a2b 1a2b 1a2b '1a2b' infix2postfix Symbol Stack Postfix '' infix2postfix Traceback most recent call last: ... ValueError: invalid expression infix2postfix Traceback most recent call last: ... IndexError: list index out of range Print table header for output infix2prefixabc doctest: NORMALIZEWHITESPACE Symbol Stack Postfix c c c b cb cb a cba cba 'abc' infix2prefix1a2b doctest: NORMALIZEWHITESPACE Symbol Stack Postfix b b b 2 b2 b2 b2 a b2a b2a b2a b2a b2a 1 b2a1 b2a1 '1a2b' infix2prefix'' Symbol Stack Postfix '' infix2prefix'' Traceback most recent call last: ... IndexError: list index out of range infix2prefix'' Traceback most recent call last: ... ValueError: invalid expression call infix2postfix on Infix, return reverse of Postfix
def infix_2_postfix(infix: str) -> str: """ >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ---------------------------- a | | a + | + | a b | + | ab ^ | +^ | ab c | +^ | abc | + | abc^ | | abc^+ 'abc^+' >>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ------------------------------------------- 1 | | 1 * | * | 1 ( | *( | 1 ( | *(( | 1 - | *((- | 1 a | *((- | 1a ) | *( | 1a- * | *(* | 1a- 2 | *(* | 1a-2 + | *(+ | 1a-2* b | *(+ | 1a-2*b ) | * | 1a-2*b+ | | 1a-2*b+* '1a-2*b+*' >>> infix_2_postfix("") Symbol | Stack | Postfix ---------------------------- '' >>> infix_2_postfix("(()") Traceback (most recent call last): ... ValueError: invalid expression >>> infix_2_postfix("())") Traceback (most recent call last): ... IndexError: list index out of range """ stack = [] post_fix = [] priority = { "^": 3, "*": 2, "/": 2, "%": 2, "+": 1, "-": 1, } # Priority of each operator print_width = max(len(infix), 7) # Print table header for output print( "Symbol".center(8), "Stack".center(print_width), "Postfix".center(print_width), sep=" | ", ) print("-" * (print_width * 3 + 7)) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(x) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(x) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered if len(stack) == 0: # close bracket without open bracket raise IndexError("list index out of range") while stack[-1] != "(": post_fix.append(stack.pop()) # Pop stack & add the content to Postfix stack.pop() else: if len(stack) == 0: stack.append(x) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop()) # pop stack & add to Postfix stack.append(x) # push x to stack print( x.center(8), ("".join(stack)).ljust(print_width), ("".join(post_fix)).ljust(print_width), sep=" | ", ) # Output in tabular format while len(stack) > 0: # while stack is not empty if stack[-1] == "(": # open bracket with no close bracket raise ValueError("invalid expression") post_fix.append(stack.pop()) # pop stack & add to Postfix print( " ".center(8), ("".join(stack)).ljust(print_width), ("".join(post_fix)).ljust(print_width), sep=" | ", ) # Output in tabular format return "".join(post_fix) # return Postfix as str def infix_2_prefix(infix: str) -> str: """ >>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ---------------------------- c | | c ^ | ^ | c b | ^ | cb + | + | cb^ a | + | cb^a | | cb^a+ '+a^bc' >>> infix_2_prefix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ------------------------------------------- ( | ( | b | ( | b + | (+ | b 2 | (+ | b2 * | (+* | b2 ( | (+*( | b2 a | (+*( | b2a - | (+*(- | b2a ) | (+* | b2a- ) | | b2a-*+ * | * | b2a-*+ 1 | * | b2a-*+1 | | b2a-*+1* '*1+*-a2b' >>> infix_2_prefix('') Symbol | Stack | Postfix ---------------------------- '' >>> infix_2_prefix('(()') Traceback (most recent call last): ... IndexError: list index out of range >>> infix_2_prefix('())') Traceback (most recent call last): ... ValueError: invalid expression """ reversed_infix = list(infix[::-1]) # reverse the infix equation for i in range(len(reversed_infix)): if reversed_infix[i] == "(": reversed_infix[i] = ")" # change "(" to ")" elif reversed_infix[i] == ")": reversed_infix[i] = "(" # change ")" to "(" # call infix_2_postfix on Infix, return reverse of Postfix return (infix_2_postfix("".join(reversed_infix)))[::-1] if __name__ == "__main__": from doctest import testmod testmod() Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation Infix = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")