From c3e31bbd596877d399f137f3152e5e7d3254125b Mon Sep 17 00:00:00 2001 From: Omkar Dattatraya Babar <116364228+omkar2508@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:57:49 +0530 Subject: [PATCH 1/2] Implement Binary Tree Operations: Insertion, Deletion, and Search Added a BinaryTree class that implements basic operations for a binary tree. Implemented insert(Node root, int value) for inserting a node in the binary tree using level-order traversal. Implemented delete(Node root, int value) for deleting a node by replacing it with the deepest node in the tree. Implemented search(Node root, int value) for searching a node in the binary tree using level-order traversal (BFS). --- Programs/binary_tree.java | 179 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 Programs/binary_tree.java diff --git a/Programs/binary_tree.java b/Programs/binary_tree.java new file mode 100644 index 0000000..1d83c21 --- /dev/null +++ b/Programs/binary_tree.java @@ -0,0 +1,179 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +class BinaryTree { + static class Node { + int data; + Node left, right; + + public Node(int data) { + this.data = data; + this.left = this.right = null; + } + } + + // Insertion in Binary Tree + public static Node insert(Node root, int value) { + Node newNode = new Node(value); + if (root == null) { + return newNode; + } + + // Level order insertion + Queue queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + Node current = queue.poll(); + if (current.left == null) { + current.left = newNode; + return root; + } else if (current.right == null) { + current.right = newNode; + return root; + } else { + queue.add(current.left); + queue.add(current.right); + } + } + return root; + } + + // Deletion in Binary Tree + public static Node delete(Node root, int value) { + if (root == null) return null; + + if (root.data == value) { + return null; // Root node itself is to be deleted + } + + Queue queue = new LinkedList<>(); + queue.add(root); + Node targetNode = null; + Node deepestNode = null; + + // Find the node to delete and the deepest node + while (!queue.isEmpty()) { + deepestNode = queue.poll(); + + if (deepestNode.data == value) { + targetNode = deepestNode; + } + + if (deepestNode.left != null) { + queue.add(deepestNode.left); + } + + if (deepestNode.right != null) { + queue.add(deepestNode.right); + } + } + + if (targetNode != null) { + targetNode.data = deepestNode.data; + root = removeDeepestNode(root, deepestNode); + } + return root; + } + + // Remove deepest node (used after deletion) + private static Node removeDeepestNode(Node root, Node deepestNode) { + Queue queue = new LinkedList<>(); + queue.add(root); + Node temp = null; + + while (!queue.isEmpty()) { + temp = queue.poll(); + + if (temp == deepestNode) { + deepestNode = null; + break; + } + + if (temp.right != null) { + if (temp.right == deepestNode) { + temp.right = null; + break; + } else { + queue.add(temp.right); + } + } + + if (temp.left != null) { + if (temp.left == deepestNode) { + temp.left = null; + break; + } else { + queue.add(temp.left); + } + } + } + return root; + } + + // Search in Binary Tree + public static boolean search(Node root, int value) { + if (root == null) return false; + + Queue queue = new LinkedList<>(); + queue.add(root); + + while (!queue.isEmpty()) { + Node current = queue.poll(); + if (current.data == value) { + return true; // Node found + } + if (current.left != null) queue.add(current.left); + if (current.right != null) queue.add(current.right); + } + return false; // Node not found + } + + // Main method to execute operations + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Node root = null; + + int choice; + do { + System.out.println("\n--- Binary Tree Operations ---"); + System.out.println("1. Insert Node"); + System.out.println("2. Delete Node"); + System.out.println("3. Search Node"); + System.out.println("4. Exit"); + System.out.print("Enter your choice: "); + choice = scanner.nextInt(); + + switch (choice) { + case 1: + System.out.print("Enter node value to insert: "); + int insertValue = scanner.nextInt(); + root = insert(root, insertValue); + System.out.println("Node inserted successfully."); + break; + case 2: + System.out.print("Enter node value to delete: "); + int deleteValue = scanner.nextInt(); + root = delete(root, deleteValue); + System.out.println("Node deleted successfully."); + break; + case 3: + System.out.print("Enter node value to search: "); + int searchValue = scanner.nextInt(); + if (search(root, searchValue)) { + System.out.println("Node found."); + } else { + System.out.println("Node not found."); + } + break; + case 4: + System.out.println("Exiting..."); + break; + default: + System.out.println("Invalid choice, try again."); + } + } while (choice != 4); + + scanner.close(); + } +} From b565ef5ac5773e15816e063b0cbcadbb9e283bbe Mon Sep 17 00:00:00 2001 From: Omkar Dattatraya Babar <116364228+omkar2508@users.noreply.github.com> Date: Wed, 8 Jan 2025 00:06:21 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cb3d676..a0a67f6 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ It is very easy to contribute, you may follow these steps - 99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list 100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring. 101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java - +102. [Binary Tree](https://github.com/PrajaktaSathe/Java/blob/main/Programs/binary_tree.java)- Program to implement Binary Tree Operations: Insertion, Deletion, and Search. # Contributors - ## A big thank you to all our contributors!!!