Skip to main content

A class for binary search trees.

Project description

Binary Search Tree

Description

This project implements a Binary Search Tree (BST) data structure in Python. A BST is a binary tree in which all the nodes follow the property that the value of the left child is less than or equal to the parent node, and the value of the right child is greater than or equal to the parent node. This allows for efficient search, insertion, and deletion operations.

Utility

The Binary Search Tree data structure is useful for organizing and managing data efficiently, especially when it comes to searching for elements within a dataset. It provides logarithmic time complexity for search operations, making it ideal for applications requiring fast retrieval of data.

Install

You can install the binary-search-tree package via pip:

pip install ran-bst

Examples of Basic Uses

from bst import BinarySearchTree as Tree

# It's necessary to pass a data type that supports (>, ==, <).
tree = Tree(int)  

# Creating a trivial tree of integers.
# Add integers from 0 to 29 to the tree.
for num in range(30): tree.add(num)

# Print the height and size of the tree before stabilization.
print("Tree height", tree.height())  # Returns 30.
print("Tree size", tree.size())  # Returns 30.

# Balance the tree.
tree.stabilize()

# Print the height and size of the tree after stabilization.
print("Tree height", tree.height())  # Returns 5.
print("Tree size", tree.size())  # Returns 30.

# Check if an element exists in the tree.
search_result = tree.exist(19)  # Equivalent to: 19 in tree
print("Search result for element 19:", search_result)  # Returns true

# Remove element 19 from the tree.
tree.remove(19)
print("Search result for element 19 after removal:", 19 in tree)  # Returns false

print("Tree size after removal:", tree.size())  # Returns 29


# Returning lists with different orders of traversal.
print("Inorder traversal: ", tree.inorder())
print("Preorder traversal: ", tree.preorder())
print("Postorder traversal: ", tree.postorder())
print("Levelorder traversal: ", tree.levelorder())
print("Spiralorder traversal: ", tree.spiralorder())

# Returning the minimum and maximum elements respectively.
print("Minimum element:", tree.min())
print("Maximum element:", tree.max())

# Accessing a data item:
n = 3
print("Accessing a data item:", tree[n])  # Equivalent to tree.get(n), tree.inorder()[n]

# Iterating over the tree:
for v in tree: print(v) # Equivalent to: for v in tree.inorder(): print(v)

# Subset of trees of the same type:
subtree = Tree(int)

subtree.add(3)
subtree.add(2)
subtree.add(1)

print("'subtree' is contained in 'tree':", subtree in tree)  # Returns true
print("'subtree == 'tree':", subtree == tree)  # Returns false
print("'tree' is contained in 'subtree':", tree in subtree) # Returns false

Contanct

For any inquiries or feedback regarding this project, please contact us on Discord

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

ran-bst-1.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

ran_bst-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file ran-bst-1.0.0.tar.gz.

File metadata

  • Download URL: ran-bst-1.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for ran-bst-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aeec92022b42ce50a9925b3ae66309943af8a2dbaa4a87440c729ba7097ce63c
MD5 8d07794e396d0b7c151e03dec1d915bb
BLAKE2b-256 754d66319746ed8599ad1ed8bb7dfcaf5917675540306c876262948c958f88c9

See more details on using hashes here.

File details

Details for the file ran_bst-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ran_bst-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for ran_bst-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4be7dcfcc8f99cc38d64556c67c38086c2ed094f1a690e07b273154fa038c305
MD5 192a6bc1995db5819e792fac7e6e893e
BLAKE2b-256 60add09da7d1658836c9b4a595ee360167cbe9ee8a156e00e2ec44ef09c2d08c

See more details on using hashes here.

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