{"id":4233,"library":"red-black-tree-mod","title":"Flexible Python Red Black Tree Implementation","description":"This library provides a flexible Python implementation of red-black trees, offering a low standard deviation in operation times for insertion, deletion, and lookup. It includes modules for both set-like (enforcing uniqueness) and dictionary-like use. The library is known to work across CPython 2.x, CPython 3.x, PyPy, and Jython. The current version is 1.22, with an infrequent release cadence; the last major update was in December 2023.","status":"active","version":"1.22","language":"en","source_language":"en","source_url":"https://stromberg.dnsalias.org/~strombrg/red-black-tree-mod","tags":["data-structure","red-black-tree","tree","sorted-collection","dict","set","algorithms"],"install":[{"cmd":"pip install red-black-tree-mod","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The library provides multiple modules; 'red_black_dict_mod' is used for dictionary-like functionality. The specific module to import depends on whether dictionary-like or set-like behavior is desired, as implied by the project description.","symbol":"RedBlackTree","correct":"from red_black_dict_mod import RedBlackTree"}],"quickstart":{"code":"from red_black_dict_mod import RedBlackTree\n\n# Dictionary-like usage\nrbd = RedBlackTree()\n\n# Insert items\nfor i in range(10):\n    rbd[i] = f\"value_{i}\"\n\nprint(f\"Tree size: {len(rbd)}\")\nprint(f\"Value for key 5: {rbd[5]}\")\n\n# Check for existence\nprint(f\"Is 7 in tree? {7 in rbd}\")\nprint(f\"Is 100 in tree? {100 in rbd}\")\n\n# Iterate through items (keys are sorted)\nprint(\"Items in sorted order:\")\nfor k, v in rbd.items():\n    print(f\"  {k}: {v}\")\n\n# Delete an item\ndel rbd[5]\nprint(f\"Tree size after deleting 5: {len(rbd)}\")\nprint(f\"Value for key 5 after deletion (should raise KeyError):\\n\")\ntry:\n    print(rbd[5])\nexcept KeyError as e:\n    print(f\"  {e}\")\n\n# Set-like usage (not directly shown in quickstart, but implied by documentation)\n# The documentation states: 'A module is provided for red black trees that enforce uniqueness. \n# They allow for set-like use and dictionary-like use.'\n# It's likely you would import a different module or use RedBlackTree directly for this, \n# but without clear examples, this quickstart focuses on the dictionary aspect.","lang":"python","description":"Demonstrates basic dictionary-like usage, including insertion, lookup, iteration (keys are sorted), and deletion."},"warnings":[{"fix":"Review the PyPI release notes for version 1.21 and later for any unstated behavioral changes. Thoroughly test existing code when upgrading from versions prior to 1.21.","message":"The library's release history shows a significant gap between versions 1.20 (November 2013) and 1.21 (November 2022). While not explicitly documented as breaking, this long period, coupled with the library's Python 2.x and 3.x compatibility, could imply subtle behavioral changes or a lack of explicit updates for new Python idioms introduced in intermediate versions. Users migrating from very old versions should test thoroughly.","severity":"gotcha","affected_versions":"<1.21"},{"fix":"Examine the installed package contents (e.g., `pip show red-black-tree-mod` and explore the source) to identify the correct module for set-like functionality if `red_black_dict_mod` does not suit your needs.","message":"The project description mentions 'a pair of python modules' for implementing red-black trees, one for uniqueness (set-like) and another for dictionary-like use. However, the primary quickstart examples and common usage patterns on PyPI often feature `red_black_dict_mod.RedBlackTree`. The import path or class name for the dedicated 'set-like' module is not explicitly detailed, potentially causing confusion for users seeking only set functionality.","severity":"gotcha","affected_versions":"All"},{"fix":"Evaluate performance requirements. If absolute maximum performance for sorted collections is critical, consider benchmarking against or migrating to libraries like `SortedContainers`.","message":"For use cases requiring very high performance, scalability, or more active maintenance, alternative libraries like `SortedContainers` might be more suitable. This `red-black-tree-mod` library prioritizes flexibility and broad Python version/runtime compatibility, which might come at the cost of peak performance compared to Python 3-optimized alternatives.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}