This project provides an implementation of a Red-Black Tree, a self-balancing binary search tree, written in Java. The Red-Black Tree ensures that the tree remains balanced, resulting in efficient operations for insertion, deletion, and search with a time complexity of O(log n).
Additionally, this implementation includes a JavaFX-based graphical visualization, allowing users to interact with the tree dynamically.
- Insertion of elements while maintaining the Red-Black Tree properties
- Deletion of elements with automatic rebalancing
- Searching for a specific element
- Tree Traversals (in-order, pre-order, post-order)
- Graphical Visualization using JavaFX for an interactive experience
Ensure you have the following installed:
- Java Development Kit (JDK 11 or later)
- JavaFX SDK (included in newer JDKs or available separately)
- An IDE such as IntelliJ IDEA, Eclipse, or NetBeans (recommended for JavaFX development)
- Clone the repository:
git clone https://github.com/mahajialirezaei/RedBlackTree.git
- Open the project in your IDE.
- Ensure JavaFX libraries are correctly configured in the project settings.
- Compile and run the project:
(Replace
javac -d out --module-path <path-to-javafx-lib> --add-modules javafx.controls,javafx.fxml *.java java --module-path <path-to-javafx-lib> --add-modules javafx.controls,javafx.fxml -cp out Main
<path-to-javafx-lib>
with the actual JavaFX SDK path.)
Once the application is running, you can:
- Insert elements by entering values in the graphical interface.
- Delete elements from the tree dynamically.
- Search for a specific element.
- View the tree structure visually with dynamic updates.
The tree maintains the following properties:
- Every node is either red or black.
- The root node is always black.
- Red nodes cannot have red children (no two consecutive red nodes).
- Every path from a node to its descendant NULL nodes must have the same number of black nodes.
RedBlackTree tree = new RedBlackTree();
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(15);
tree.display();
This inserts elements into the tree while maintaining Red-Black properties and displays the tree structure.
Contributions are welcome! Feel free to submit issues or pull requests to improve the implementation.