Skip to main content

Red-black tree implementation in Python.

Project description

Python red-black trees

Build Status Coverage Status

A Python implementation of red-black trees. This implementation is unique in that instead of hiding the nodes within the implementation, the nodes are easily seen so that the user can inspect the color and relationships between bodes at any time. Highly specialized comparison functions can be specified to determine which bose is greater than or less than another

What is this for?

This implementation was created for MS-CFB, a project where files must be saved in a Red Black Tree, and this tree stricture is to be saved to disk. Most Red Black Tree implementations would use a dictionary sort when comparing which string is larger or smaller than another, but this project requires using the file name length along with converting to uppercase unicode. To accomodate custom sorting, this library requires the user to define dunder methods do imporm the tree which of two nodes is larger or smaller.

Documentation

Standard red-black tree interface

Constructor

A new red-black tree can be constructed as follows:

bst = RedBlackTree()

Insert

Items can be inserted into a tree using the insert method:

bst.insert(5)  # inserts a node with value 5

Delete

Items can be removed from the tree using the delete method. This method will do nothing if there is no item in the tree with the specific key.

bst.delete(5)  # removes a node with value 5

Minimum and maximum

The minimum and maximum value in the tree can be found with the corresponding methods. If the tree is empty, these methods will both return the special value bst.TNULL

bst.minimum()  # returns minimum value
bst.maximum()  # returns maximum value

bst.minimum() == bst.TNULL  # Check whether tree is empty

Tree size

Tree size can be accessed via the size member variable:

bst.size  # contains the tree's size

Search

To find a specific item in the tree, you can use the search method:

bst.search(6)  # returns the node containing 6. Will return bst.TNULL if item is not present.

Predecessor and successor

To get a node's predecessor or sucessor;

bst.predecessor(bst.search(6))  # Gets the predecessor a node containing 6
bst.successor(bst.search(6))  # Gets the successor a node containing 6

Printing methods

To know more about the contents of the tree, you can use various printing methods:

bst.print_tree()  # prints an ASCII representation of the whole tree
bst.preorder()      # prints a preorder traversal
bst.inorder()       # prints an inorder traveral
bst.postorder()     # prints a postorder traversal

Dictionary interface

bst[80] = 4  # Store the value 4 with the key 80
bst[80]      # Retrieve the value associated with the key 80

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_red_black_trees-0.0.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_red_black_trees-0.0.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file python_red_black_trees-0.0.1.tar.gz.

File metadata

  • Download URL: python_red_black_trees-0.0.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_red_black_trees-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9d30ce7eae6326c14021a6ecb31b2e892036e1b7b49e80615274aa2e33433b97
MD5 37286490c3b291650ff38ec5b69ceb67
BLAKE2b-256 0ec9b144d0518905e8428204ff64a8b479c1cb2f79b0da55b4d2ca22b9bf46e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_red_black_trees-0.0.1.tar.gz:

Publisher: python-publish.yml on Beakerboy/python-red-black-trees

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_red_black_trees-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_red_black_trees-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e6e44687a52090f96b40f14482a579125bdc0fd9e4c75c5bf135de6886f824f7
MD5 252757ecede29604c6e375f324d4295d
BLAKE2b-256 398048ea0c2aecf59f0d0e35c75bd1452b1c15ec21998792c51deccb14708f8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_red_black_trees-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on Beakerboy/python-red-black-trees

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page