diff --git a/Tree/BST.java b/Tree/BST.java new file mode 100644 index 0000000..5962b25 --- /dev/null +++ b/Tree/BST.java @@ -0,0 +1,172 @@ +package Java_DSA.Tree; + +import java.util.Scanner; + +public class BST { + private static Node root; + + private static class Node{ + int data; + Node left; + Node right; + + public Node(int data){ + this.data = data; + } + } + + public static Node insert(Node root,int val){ + Node temp = new Node(val); + if(root == null){ + root = temp; + return root; + } + if(val < root.data){ + root.left = insert(root.left,val); + } + else{ + root.right = insert(root.right,val); + } + + return root; + } + + public static void insert(int val){ + Node p = root; + Node ptr = null; + while(p != null){ + ptr = p; + if(val < p.data){ + p = p.left; + } + else if(val > p.data){ + p = p.right; + } + else{ + System.out.println(val +" is already present in tree."); + return; + } + } + Node temp = new Node(val); + + if(ptr == null) + root = temp; + else if(val < ptr.data) + ptr.left = temp; + else + ptr.right = temp; + } + + public static void inorder(Node root){ + if(root == null){ + return; + } + inorder(root.left); + System.out.print(root.data + " "); + inorder(root.right); + } + + public static boolean search(Node root,int key){ + if(root == null){ + return false; + } + if(key < root.data){ + return search(root.left,key); + } + else if(key == root.data){ + return true; + } + else{ + return search(root.right,key); + } + } + + public static Node delete(Node root,int val){ + if(search(root,val)) { + if (val < root.data) { + root.left = delete(root.left, val); + } else if (val > root.data) { + root.right = delete(root.right, val); + } else { //root.data == val + if (root.left == null && root.right == null) { + return null; + } + if (root.left == null) + return root.right; + else if (root.right == null) + return root.left; + else { + Node IS = inorderSuccesser(root.right); + root.data = IS.data; + root.right = delete(root.right, IS.data); + } + } + } + else + System.out.println("Data is not present to delete."); + + return root; + } + + public static Node inorderSuccesser(Node root){ + while(root.left != null){ + root = root.left; + } + return root; + } + + public static int countNode(Node root){ + if(root == null){ + return 0; + } + int leftNode = countNode(root.left); + int rightNode = countNode(root.right); + return leftNode + rightNode + 1; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + //Node root = null; + boolean flag = true; + System.out.println("1)Insert data.\n2)Search.\n3)Delete.\n4)Display inorder.\n5)Count the nodes.\n6)Exit."); + while(flag){ + System.out.println("Enter your choice : "); + int choice = sc.nextInt(); + switch (choice){ + case 1->{ + System.out.println("Enter data to insert : "); + int data = sc.nextInt(); + //root = insert(root,data); + insert(data); + } + case 2->{ + System.out.println("Enter data to search : "); + int searchValue = sc.nextInt(); + if(search(root,searchValue)) + System.out.println("Found"); + else + System.out.println("Not found"); + } + case 3->{ + System.out.println("Enter data to delete : "); + int dataToDelete = sc.nextInt(); + root = delete(root,dataToDelete); + } + case 4->{ + inorder(root); + System.out.println(); + } + case 5->{ + System.out.println("Number of nodes : "+countNode(root)); + System.out.println(); + } + case 6->{ + flag = false; + } + default -> { + System.out.println("Invalid choice..!"); + } + } + } + } +} diff --git a/Tree/BinaryTree.java b/Tree/BinaryTree.java new file mode 100644 index 0000000..987a52f --- /dev/null +++ b/Tree/BinaryTree.java @@ -0,0 +1,73 @@ +package Java_DSA.Tree; + +import java.util.Scanner; + +public class BinaryTree { + private static Node left; + private static Node right; + + private static class Node{ + int data; + Node left; + Node right; + + public Node(int data){ + this.data = data; + } + } + + static int index = -1; + public static Node createTree(int[] nodes){ + index++; + if(nodes[index] == -1){ + return null; + } + Node temp = new Node(nodes[index]); + temp.left = createTree(nodes); + temp.right = createTree(nodes); + + return temp; + } + + public static void preorder(Node root){ + if(root == null){ + return; + } + System.out.print(root.data + " "); + preorder(root.left); + preorder(root.right); + } + + public static void inorder(Node root){ + if(root == null){ + return; + } + inorder(root.left); + System.out.print(root.data + " "); + inorder(root.right); + } + + public static void postorder(Node root){ + if(root == null){ + return; + } + postorder(root.left); + postorder(root.right); + System.out.print(root.data + " "); + + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int[] nodes = {4,7,2,-1,-1,3,9,-1,-1,10,-1,-1,5,-1,15,11,-1,-1,14,-1,-1}; + Node root = createTree(nodes); + System.out.println("Preorder traversal "); + preorder(root); + System.out.println(); + System.out.println("Inorder traversal "); + inorder(root); + System.out.println(); + System.out.println("postorder traversal "); + postorder(root); + } +}