GithubHelp home page GithubHelp logo

zyxwvo / binary-search-tree-dsa Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hoangsonww/binary-search-tree-dsa

1.0 1.0 0.0 21 KB

๐ŸŒณ This repository is dedicated to the Binary Search Tree (BST) data structure, featuring a comprehensive demo of all its functionalities including insertion, deletion, search, and traversal operations. It serves as an educational resource for understanding BSTs in depth, offering code examples and explanations suitable for students and developers.

Java 100.00%

binary-search-tree-dsa's Introduction

Binary Search Tree Implementation

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

  • Dynamic Tree Construction: Create a BST either empty, with a single root node, or by parsing an array of integers.
  • Insertion: Add new elements to the tree while maintaining the BST properties.
  • Deletion: Remove elements from the tree, adjusting the structure as necessary to preserve BST properties.
  • Search: Check whether a specific value exists within the tree.
  • Traversal: Inorder, preorder, and postorder traversal methods to explore the tree's contents.
  • K-th Smallest Element: Retrieve the k-th smallest element from the tree, demonstrating an application of inorder traversal.
  • Create Tree from Array: Build a new BST from an array of integers, replacing any existing tree.

Prerequisites

  • Java Development Kit (JDK) 11 or later.

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:

  • Empty tree: BinarySearchTree bst = new BinarySearchTree();
  • Tree with an integer root: BinarySearchTree bst = new BinarySearchTree(10);
  • Tree with a Node as root: BinarySearchTree bst = new BinarySearchTree(new Node(10));

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);

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.


binary-search-tree-dsa's People

Contributors

hoangsonww avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.