Thanks to visit codestin.com
Credit goes to github.com

Skip to content

kamya-creator/BinaryTree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌳 Binary Tree Playground in Java

Java Build License: MIT

A curated set of Java implementations for fundamental and advanced binary tree operations — ideal for interviews, data structures revision, or algorithm practice.


✨ Features

  • ✅ Build binary trees from arrays (level-order input)
  • ✅ Pre-order, in-order, and post-order traversal
  • ✅ Root-to-leaf path sum and path printing
  • ✅ Transformations:
    • ➕ Convert to Left-Cloned Tree
    • 🔄 Revert from Left-Cloned Tree
  • ✅ Remove all leaf nodes
  • 🧪 Modular structure for each concept

📂 Folder Structure

src/
└── org.example.binarytree.pathtoleaves/
    ├── TreeNode.java                     # Core TreeNode definition
    ├── IterativeConstructBinaryTree.java# Builds binary tree from Integer[]
    ├── PathSumI.java                     # Path-to-leaf sum logic + traversal
    ├── TransformToLeftCloned.java       # Converts tree into left-cloned form
    ├── TransformBackLeftCloned.java     # Restores original from cloned tree
    ├── RemoveLeaves.java                # Deletes all leaf nodes from tree

🚀 Getting Started

📦 Prerequisites

  • Java 8+ installed
  • Any IDE (like IntelliJ or Eclipse) or terminal with javac/java

🛠️ How to Run

git clone https://github.com/kamya-creator/BinaryTree.git
cd BinaryTree

# Compile (from src/)
javac -d . src/org/example/binarytree/pathtoleaves/*.java

# Run example: TransformBackLeftCloned
java org.example.binarytree.pathtoleaves.TransformBackLeftCloned

📘 Core Concepts

🔨 1. Build Tree from Array

Integer[] arr = {50, 25, 12, null, null, 37, 30, null, null, null, 75, 62, null, 70, null, null, 87, null, null};
TreeNode root = IterativeConstructBinaryTree.construct(arr);

🔄 2. Left-Cloned Tree & Reversal

TreeNode cloned = TransformToLeftCloned.transform(root);
TreeNode restored = TransformBackLeftCloned.transformBack(cloned);

🍂 3. Remove Leaf Nodes

TreeNode withoutLeaves = RemoveLeaves.removeLeaves(root);

🧠 Example Output

Given:

Integer[] arr = {50, 25, 12, null, null, 37, 30, null, null, null, 75, 62, null, 70, null, null, 87, null, null};

🔍 Before removing leaves:

Pre-order: 50 25 12 37 30 75 62 70 87

🪓 During removal:

Removing leaf node: 12
Removing leaf node: 30
Removing leaf node: 70
Removing leaf node: 87

🌱 Final tree:

Pre-order: 50 25 37 75 62

🧩 Extend This Repo

  • 🔁 Add more tree operations (invert, mirror, height, diameter)
  • 📉 Add BFS / level-order iterative traversal
  • 🧪 Write JUnit test cases for each module
  • 🌐 Convert it into a web visualizer using JavaScript or JavaFX

👤 Author

Kamya
📍 Passionate about Java, DSA, and building developer-focused tools.


📜 License

This project is licensed under the MIT License.


⭐️ Star the Repo

If you found this project useful or inspiring, please consider giving it a ⭐️ on GitHub!

“Code is like humor. When you have to explain it, it’s bad.” – Cory House

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages