Skip to content

Binary Search Tree (BST) in Python – A simple, well-documented implementation of a binary search tree with insert, remove, search, and traversal methods. Includes example usage and test script.

License

Notifications You must be signed in to change notification settings

Ahmedhm1/BST---Binary-Search-Tree-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binary Search Tree in Python

This project implements a Binary Search Tree (BST) in Python with basic operations:

  • Insert values
  • Remove values (remove_value)
  • Traversals (pre-order, in-order, post-order)
  • Search for values
  • Get minimum and maximum values
  • Change a node's value

🐍 1. Basic Usage (easiest way)

If you just want to try the tree without installing anything, copy tree.py into the same folder as your project and use:

from tree import Tree

tree = Tree()
tree.insert(5)
tree.insert(3)
tree.insert(7)
tree.print_in_order()

📦 2. Advanced Usage (install as a package)

If you want to use this as a Python package, you first need Git installed.
👉 Download Git if you don’t already have it.

Then install directly from GitHub using pip:

pip install git+https://github.com/Ahmedhm1/BST---Binary-Search-Tree-Python.git

Now you can import it anywhere:

from bst import Tree

tree = Tree()
tree.insert(10)
tree.insert(4)
tree.insert(12)
tree.print_in_order()

🔎 Example Usage

from bst import Tree

# Create tree and insert values
test = Tree()
test.insert(5)
test.insert(8)
test.insert(6)
test.insert(9)
test.insert(3)
test.insert(4)
test.insert(2)

# Remove values
test.remove_value(5)   # Removes root
test.remove_value(30)  # Does nothing, value not found

# Print in-order traversal (left, root, right)
test.print_in_order()

# Print pre-order traversal (root, left, right)
test.print_pre_order()

# Print post-order traversal (left, right, root)
test.print_post_order()

📂 Project Structure

BST---Binary-Search-Tree-Data-Structure/
│
├── bst/              # Package source code
│   ├── __init__.py   # Exports Tree
│   └── tree.py       # Binary Search Tree implementation
│
├── test.py           # Example test file
├── setup.py          # Package setup script
├── pyproject.toml    # (Optional) modern build file
└── README.md

📖 Source Code

➡️ View tree.py directly if you just want to read the implementation.

About

Binary Search Tree (BST) in Python – A simple, well-documented implementation of a binary search tree with insert, remove, search, and traversal methods. Includes example usage and test script.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages