From 457fb8895ce5b56086c9b1179ff2b16d37e2dc47 Mon Sep 17 00:00:00 2001 From: Karthik LU Date: Tue, 12 Aug 2025 08:41:56 +0530 Subject: [PATCH 1/3] updated the README.md and added a image --- Binary_Search_Tree/README.md | 86 ++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/Binary_Search_Tree/README.md b/Binary_Search_Tree/README.md index 46a7bbf..2cd3ee8 100644 --- a/Binary_Search_Tree/README.md +++ b/Binary_Search_Tree/README.md @@ -1,23 +1,71 @@ ![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) -# Binary Search Tree -- Differ from a regular Binary Tree because of one key property: Nodes must be arranged in order - - the node's left subtree must have values less than the node - - the node's right subtree must have values greater than the node - - this is going based on that every value in the BST must be unique - -# Functions Implemented -- Init - - tree = BST(1,None,None) _creates a tree with one node_ - - A basic constructor that creates a BST with three parameters - - BST(value,left_subtree,right_subtree) -- Add - - tree.add(4) _adds 4 to our previous tree created, giving a right child_ - - Maintains BST properties by adding to either subtree depending on the value - - returns a string telling if insertion was or wasn't successful -- Remove - - tree.remove(1) _removes 1 from our current tree, resulting 4 to be the sole node_ - - Maintains BST properties by restructuring the tree when we remove the value - - returns a string telling if deletion was or wasn't successful + +# Binary Search Tree (BST) Implementation in Python +![Alt text](https://i.pinimg.com/originals/e7/f5/b6/e7f5b60413ff4bedebfd2805f97d8de7.jpg) + +## Overview +A **Binary Search Tree (BST)** is a special kind of **Binary Tree** with one key property: +- The **left subtree** of a node contains values **less than** the node’s value. +- The **right subtree** of a node contains values **greater than** the node’s value. +- **All values in a BST must be unique**. + +This project provides a **Python implementation** of a BST, allowing you to **insert**, **remove**, **search**, and **traverse** nodes while maintaining the BST property. + +--- + +## Features +- **Initialization** — Create a BST with a given root node and optional left/right subtrees. +- **Add** — Insert a new value while keeping the BST property intact. +- **Remove** — Delete a value and restructure the tree if needed. +- **Search** — Find if a value exists in the tree. +- **Traversal** — Inorder, Preorder, and Postorder printing. + +--- + +## BST Property Rule +```text +For any node N: + All values in N's left subtree < N.value + All values in N's right subtree > N.value +```python +from bst import BST # assuming file is bst.py + +# Initialize the BST +tree = BST(1, None, None) +print("Initial Tree:") +tree.print_inorder() # Output: 1 + +# Add elements +print(tree.add(4)) # Adds 4 as the right child +print(tree.add(0)) # Adds 0 as the left child +tree.print_inorder() # Output: 0 1 4 + +# Remove elements +print(tree.remove(1)) # Removes 1, restructures tree +tree.print_inorder() # Output: 0 4 + +# Search +print(tree.search(4)) # True +print(tree.search(10)) # False +# Initial Insertion: 1, 4, 0 + 1 + / \ + 0 4 +# After Removing 1: + 4 + / + 0 +#Time Complexity + +| Operation | Average Case | Worst Case | +| ---------- | ------------ | ---------- | +| **Search** | O(log n) | O(n) | +| **Insert** | O(log n) | O(n) | +| **Delete** | O(log n) | O(n) | + + + + # Author [Tomas Urdinola](https://github.com/tomurdi) From aa9d40b7503644f93197201cba2916e087b4b426 Mon Sep 17 00:00:00 2001 From: Karthik LU Date: Tue, 12 Aug 2025 08:44:04 +0530 Subject: [PATCH 2/3] added a image and updated the README --- Binary_Search_Tree/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Binary_Search_Tree/README.md b/Binary_Search_Tree/README.md index 2cd3ee8..841c0da 100644 --- a/Binary_Search_Tree/README.md +++ b/Binary_Search_Tree/README.md @@ -24,7 +24,7 @@ This project provides a **Python implementation** of a BST, allowing you to **in --- ## BST Property Rule -```text + For any node N: All values in N's left subtree < N.value All values in N's right subtree > N.value From d1d003ab579bf1d3b5b859ee251d1f27c2f8416f Mon Sep 17 00:00:00 2001 From: Karthik LU Date: Tue, 12 Aug 2025 08:44:50 +0530 Subject: [PATCH 3/3] Added image and updated the README --- Binary_Search_Tree/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Binary_Search_Tree/README.md b/Binary_Search_Tree/README.md index 841c0da..9272205 100644 --- a/Binary_Search_Tree/README.md +++ b/Binary_Search_Tree/README.md @@ -23,11 +23,11 @@ This project provides a **Python implementation** of a BST, allowing you to **in --- -## BST Property Rule +## BST Property Rule -For any node N: - All values in N's left subtree < N.value - All values in N's right subtree > N.value +For any node N: + All values in N's left subtree < N.value + All values in N's right subtree > N.value ```python from bst import BST # assuming file is bst.py