Binary-Search-Tree-DSA

Binary Search Tree - DSA Project

Overview

This Java project implements a Binary Search Tree (BST) with fundamental operations such as insertion, deletion, and traversal. Designed with efficiency and simplicity in mind, this class provides an intuitive way to manage and manipulate a collection of integers in a hierarchical structure.

Features

Prerequisites

Setup and Compilation

  1. Ensure Java is installed on your system. You can verify this by running java -version in your command line or terminal.
  2. Clone or download this repository to your local machine.
  3. Navigate to the directory containing the BinarySearchTree.java file.
  4. Compile the class using the Java compiler:
    javac BinarySearchTree.java
    

Usage

Creating a BST

Instantiate a BST object in your Java program. You can create a tree in three ways:

Inserting Elements

Add elements to your BST:

bst.insert(5);
bst.insert(15);

Deleting Elements

Remove elements by value:

bst.delete(5);

Searching for Elements

Check if an element exists in the tree:

boolean found = bst.search(15);

Traversing the Tree

Perform different tree traversals:

bst.inorderRec();
bst.preorderRec();
bst.postorderRec();

Finding the K-th Smallest Element

Retrieve the k-th smallest element from the BST:

Node kthSmallest = bst.kthSmallest(3);
if (kthSmallest != null) {
    System.out.println("K-th Smallest: " + kthSmallest.key);
}

Creating a Tree from an Array

Overwrite the current tree with a new tree constructed from an array of integers:

int[] values = {3, 1, 4, 2};
bst.createTree(values);

Main File

There is also a Main.java file, which comprehensively tests all the functions and methods of the BST class. Feel free to run it as follows:

java Main

Contributing

Contributions to improve the Binary Search Tree implementation or extend its functionality are welcome. Please fork the repository, make your changes, and submit a pull request with a clear description of your modifications or additions.


Created with ❤️ by Son Nguyen in 2023.