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.2.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.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_red_black_trees-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 6df290ff6fd26a1984550c347112993a2b36ab1049bcd3cc475e74db4fdacf01
MD5 4dadae4480f95031f7ce74fdbcc9d29d
BLAKE2b-256 f658e91058777eafb5a9901e287e65933d87a286a4300e628dfc8780d11e054c

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_red_black_trees-0.0.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for python_red_black_trees-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e9203a9824cd5407219a7a104a59cd067c4731c048b8369679136ee79b83ae6b
MD5 f16b061fcd14ae563187555bd5f1e895
BLAKE2b-256 6e895ec05e8a247daa93547be53aaf7ac2159f5e4ff705cdb16e83783ac721a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_red_black_trees-0.0.2-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