Skip to content

Comprehensive Java implementations of N-ary trees, binary search trees, AVL self-balancing trees, and B-Trees.

Notifications You must be signed in to change notification settings

RasHOSub5/TreesForJava

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo

🌳 Data Structures with Trees

This project provides a collection of Java implementations of tree-based data structures, designed both for learning purposes and as utilities that can be extended into larger projects.

📦 What's inside?

Nodes:

Core building blocks for all tree structures:

  • N-Ary Nodes → Basis for N-Ary Trees, where each node can have any number of children.
  • Binary Nodes → Basis for General Binary Trees, Binary Search Trees (BSTs), and AVL Trees.
  • B-Nodes → Special nodes designed for B-Trees, combining the flexibility of N-Ary Nodes with the ordering rules of Binary Trees.

Trees:

Fully implemented tree structures with methods for traversals, insertion, deletion, searching, and more:

N-Ary Trees → Nodes can branch into as many children as needed.

Binary Trees → Fundamental structure for representing hierarchical data.

Binary Search Trees (BST) → Maintain order, enabling efficient searches and insertions.

AVL Trees (Self-Balancing BST) → Automatically balance themselves to guarantee optimal search, insertion, and deletion times.

B-Trees → Used in databases and file systems to manage large amounts of sorted data efficiently.

🚀 Features

  • Implemented in pure Java, with no external dependencies.
  • Clean and extensible OOP design.
  • Educational: great for students or developers who want to understand how trees work under the hood.
  • Includes classic algorithms such as preorder, inorder, postorder, and level-order traversals.

💻 Code Examples

  • Binary Search Tree
BinarySearchTree BST = new BinarySearchTree(); 
// Insert values
BST.addNode(10);
BST.addNode(5);
BST.addNode(15);
BST.addNode(3);
BST.addNode(7);

// Traversals
System.out.println("Inorder Traversal (sorted):");
BST.inOrderTraversal();

System.out.println("\nPreorder Traversal:");
BST.preOrderTraversal();

System.out.println("\nPostorder Traversal:");
BST.postOrderTraversal();

Output:

Is imposible to add 10 to an empty tree, creating tree...
Inorder Traversal (sorted):
3 5 7 10 15 

Preorder Traversal:
10 5 3 7 15 

Postorder Traversal:
3 7 5 15 10 
  • Self-Balacing Tree
 SelfBalancingBST  AVL = new SelfBalancingBST();

// Insert values (tree balances itself automatically)
AVL.addNode(30);
AVL.addNode(20);
AVL.addNode(40);
AVL.addNode(10);
AVL.addNode(5) // triggers rotation

System.out.println("Inorder Traversal (balanced):");
AVL.inOrderTraversal();

Output:

Is imposible to add 30 to an empty tree, creating tree...
Inorder Traversal (balanced):
5 10 20 30 40
  • B-Trees
BTree BTree = new BTree(4); // Order 4 B-Tree

// Insert values
BTree.insertKey(10);
BTree.insertKey(20);
BTree.insertKey(5);
BTree.insertKey(6);
BTree.insertKey(12);

System.out.println("Traversal of B-Tree:");
BTree.preOrderTraversal();

Output:

Traversal of B-Tree:
[ 10 ] [ 5 6 ] [ 12 20 ]

Roadmap

  • ✅ Implement core data structures (N-Ary Trees, Binary Trees, BST, AVL, B-Trees).
  • 🔍 Review the code to make optimizations regarding efficiency.
  • 🧪 Debug the program with several extreme cases.
  • 🛠️ Add more tools if needed.

Lessons Learned

Working on this project was a great challenge, especially when it came to debugging and ensuring that all subprograms were as robust and efficient as possible.
Throughout the process, I gained a deeper understanding of tree data structures and the logic behind their algorithms.

I also learned how to make design decisions when creating algorithms and defining rules, which helped me improve both my problem-solving skills and my ability to write clean, maintainable code.

Feedback

I’d love to hear your thoughts, suggestions, or ideas to improve this project.
Feel free to reach out to me at [email protected] or open an issue in the repository.

Author