From e3725edb9e57147b0ae1931ef0b71fee50813ba6 Mon Sep 17 00:00:00 2001 From: jinit jain Date: Tue, 6 Oct 2020 09:50:16 +0530 Subject: [PATCH] add BST and height of BST --- HeightOfBinaryTree.java | 96 ++++++++++++++++++++++ ImplementatingBST.java | 172 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+) create mode 100644 HeightOfBinaryTree.java create mode 100644 ImplementatingBST.java diff --git a/HeightOfBinaryTree.java b/HeightOfBinaryTree.java new file mode 100644 index 0000000..8754a32 --- /dev/null +++ b/HeightOfBinaryTree.java @@ -0,0 +1,96 @@ +import java.io.*; +import java.util.*; + + +public class HeightOfBinaryTree { + + private static class TreeNode { + int value; + TreeNode left; + TreeNode right; + + public TreeNode(int val) { + this.value = val; + this.left = null; + this.right = null; + } + } + + public static void main(String[] args) { + TreeNode root = null; + root = new TreeNode(10); + root.left = new TreeNode(7); + root.right = new TreeNode(14); + root.right.left = new TreeNode(17); + root.right.left.right = new TreeNode(24); + root.left.left = new TreeNode(20); + /* + 10 + / \ + 7 14 + / / + 20 17 + \ + 24 + */ + System.out.println("-- Tree --"); + inOrder(root); + System.out.println("\n-- Height of the root -- "); + System.out.println(getHeight(root)); + System.out.println("-- Height of the left subtree -- "); + System.out.println(getHeight(root.left)); + System.out.println("-- Height of the right subtree -- "); + System.out.println(getHeight(root.right)); + } + + //InOrder Traversal to print nodes + private static void inOrder(TreeNode root) { + // TODO Auto-generated method stub + if (root == null) { + return; + } + inOrder(root.left); + System.out.print(root.value + " "); + inOrder(root.right); + + } + + //height of the tree + private static int getHeight(TreeNode root) { + if(root == null) { + //height of null tree is zero + return 0; + } + int leftHeight = getHeight(root.left); + int rightHeight = getHeight(root.right); + /* + 10 + / \ + 5 20 + / + 12 + height of left subrtree = 1 + height of right subrtree = 2 + heifht of root = max(1 , 2) + 1 = 3 + */ + return Math.max(leftHeight , rightHeight) + 1; + } +} + +/* +--Tree-- // inorder traversal of the root +20 7 10 17 24 14 +//height of root and its sub trees +-- Height of the root -- +4 +-- Height of the left subtree -- +2 +-- Height of the right subtree -- +3 +[n = number of nodes] +Time complexities +getHeight -> O(n) +inorderTraversal -> O(n) +Space Complextiy +O(n) -> for storing all nodes n nodes + */ \ No newline at end of file diff --git a/ImplementatingBST.java b/ImplementatingBST.java new file mode 100644 index 0000000..f6aec60 --- /dev/null +++ b/ImplementatingBST.java @@ -0,0 +1,172 @@ +//Implement Binary Search Tree and Perform Operations : Insertion, Deletion, Searching +//Contributed by @jinicode : https://github.com/jinicode +import java.util.Scanner; + +class TreeNode { + int value; + TreeNode left; + TreeNode right; + + public TreeNode(int val) { + this.value = val; + this.left = null; + this.right = null; + } +} + +public class ImplementingBST { + static TreeNode root = null; + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner sc = new Scanner(System.in); + System.out.println("---Insert Implementation---"); + System.out.println("How many elements do you want to insert"); + int n = sc.nextInt(); + System.out.println("Enter elements to BST"); + do { + int ele = sc.nextInt(); + root = insertToBST(root, ele); + System.out.println("Successfully inserted " + ele); + System.out.println("BST looks like "); + inOrderBST(root); + System.out.println(); + } while (n-- > 1); + System.out.println(); + System.out.println("---Searching Impelementation--"); + System.out.println("Enter element to search"); + int ele = sc.nextInt(); + boolean found = searchElementInBST(root, ele); + if (found) { + System.out.println("Found " + ele); + } else { + System.out.println("Did not Find " + ele); + } + System.out.println("---Delete Implementation---"); + System.out.println("Enter element to deleted"); + ele = sc.nextInt(); + root = deleteElementFromBST(root, ele); + System.out.println("---After deletion, BST looks like---"); + inOrderBST(root); + + } + //delete node from BST + private static TreeNode deleteElementFromBST(TreeNode root, int ele) { + // TODO Auto-generated method stub + if (root == null) { + return root; + } + if (ele < root.value) { + root.left = deleteElementFromBST(root.left, ele); + } else if (ele > root.value) { + root.right = deleteElementFromBST(root.right, ele); + } else { + if (root.left == null) + return root.right; + else if (root.right == null) + return root.left; + root.value = minValue(root.right); + root.right = deleteElementFromBST(root.right, root.value); + } + + return root; + } + + //finding minimum successor + private static int minValue(TreeNode root) { + // TODO Auto-generated method stub + int minv = root.value; + while (root.left != null) + { + minv = root.left.value; + root = root.left; + } + return minv; + } + + //search element in BST + private static boolean searchElementInBST(TreeNode root, int ele) { + // TODO Auto-generated method stub + if (root == null) { + return false; + } + if (ele == root.value) { + return true; + } + + if (ele < root.value) { + return searchElementInBST(root.left, ele); + } + + return searchElementInBST(root.right, ele); + } + //InOrder Traversal to print nodes + private static void inOrderBST(TreeNode root) { + // TODO Auto-generated method stub + if (root == null) { + return; + } + inOrderBST(root.left); + System.out.print(root.value + " "); + inOrderBST(root.right); + + } + //Insert to BST + private static TreeNode insertToBST(TreeNode root, int ele) { + // TODO Auto-generated method stub + if (root == null) { + root = new TreeNode(ele); + return root; + } + if (ele < root.value) { + root.left = insertToBST(root.left, ele); + } else { + root.right = insertToBST(root.right, ele); + } + return root; + } + +} + +/* + * ---Insert Implementation--- +How many elements do you want to insert +5 +Enter elements to BST +10 +Successfully inserted 10 +BST looks like +10 +9 +Successfully inserted 9 +BST looks like +9 10 +5 +Successfully inserted 5 +BST looks like +5 9 10 +20 +Successfully inserted 20 +BST looks like +5 9 10 20 +12 +Successfully inserted 12 +BST looks like +5 9 10 12 20 +---Searching Impelementation-- +Enter element to search +12 +Found 12 +---Delete Implementation--- +Enter element to deleted +9 +---After deletion, BST looks like--- +5 10 12 20 +------------------------------------ +Time Complexity : +Insertion : O(h) [h = Height of BST] +Deletion : O(h) [h = Height of BST] +Searching : O(h) [h = Height of BST] +Space Complexity : +O(n) [n = Number of Nodes in BST] +*/ \ No newline at end of file