diff --git a/solutions/java/BinarySearchTreeCheck.java b/solutions/java/BinarySearchTreeCheck.java index efdda83..7380076 100644 --- a/solutions/java/BinarySearchTreeCheck.java +++ b/solutions/java/BinarySearchTreeCheck.java @@ -1,105 +1,144 @@ -/* Uses Inorder traversal. Only works on unique element BT */ -public class BinarySearchTreeCheck { - Node root; - - public BinarySearchTreeCheck() { - root = null; - } - - public class Node { - int value; - Node left; - Node right; - - public Node(int value) { - this.value = value; - this.left = null; - this.right = null; - } - - public void setLeft(Node left) { - this.left = left; - } - - public Node getLeft() { - return left; - } - - public void setRight(Node right) { - this.right = right; - } - - public Node getRight() { - return right; - } - - public int getValue() { - return value; - } - } - - public void createBinaryTree(int option) { - root = new Node(10); - Node one = new Node(8); - Node two = new Node(5); - Node three = new Node(9); - Node four = new Node(15); - switch (option) { - case 1: /* Is BST (Only unique elements) */ - root.setLeft(one); - root.setRight(four); - one.setLeft(two); - one.setRight(three); - break; - case 2: /* Not BST (Only unique elements) */ - root.setRight(two); - root.setLeft(one); - one.setLeft(four); - one.setRight(three); - break; - default: - break; - } - } +/* checks if a given tree is a BST + 1. isBSTBetter checks for a BST with unique elements + 2. isBST checks for a BST with unique or non Unique elements + */ - public boolean isBSTBetter() { - if (root == null) - return true; - return isBSTBetter(root); - } - - private Integer prev = null; - - public boolean isBSTBetter(Node cur) { - if (cur == null) - return true; - - // Check for the left subtree - if (!isBSTBetter(cur.getLeft())) { - return false; - } - - // Check the cur value and update prev - if (prev != null && cur.getValue() <= prev) - return false; - prev = cur.getValue(); - - // Check for the right subtree - if (!isBSTBetter(cur.getRight())) { - return false; - } - - return true; - } - - public static void main(String[] args) { - BinarySearchTreeCheck btOne = new BinarySearchTreeCheck(); - btOne.createBinaryTree(1); - BinarySearchTreeCheck btTwo = new BinarySearchTreeCheck(); - btTwo.createBinaryTree(2); - - // Only works if all the elements in the Binary Tree are unique. - System.out.println(btOne.isBSTBetter()); - System.out.println(btTwo.isBSTBetter()); - } +public class BinarySearchTreeCheck { + Node root; + + public BinarySearchTreeCheck() { + root = null; + } + + public class Node { + int value; + Node left; + Node right; + + public Node(int value) { + this.value = value; + this.left = null; + this.right = null; + } + + public void setLeft(Node left) { + this.left = left; + } + + public Node getLeft() { + return left; + } + + public void setRight(Node right) { + this.right = right; + } + + public Node getRight() { + return right; + } + + public int getValue() { + return value; + } + } + + public void createBinaryTree(int option) { + root = new Node(10); + Node one = new Node(8); + Node two = new Node(5); + Node three = new Node(9); + Node four = new Node(15); + Node five = new Node(8); + switch (option) { + case 1: /* Is BST (Only unique elements) */ + root.setLeft(one); + root.setRight(four); + one.setLeft(two); + one.setRight(three); + break; + case 2: /* Not BST (Only unique elements) */ + root.setRight(two); + root.setLeft(one); + one.setLeft(four); + one.setRight(three); + break; + case 3: /* Is BST (Non-unique elements) */ + root.setLeft(one); + root.setRight(four); + one.setLeft(five); + one.setRight(three); + break; + default: + break; + } + } + + public boolean isBSTBetter() { + if (root == null) + return true; + return isBSTBetter(root); + } + + private Integer prev = null; + + public boolean isBSTBetter(Node cur) { + if (cur == null) + return true; + + // Check for the left subtree + if (!isBSTBetter(cur.getLeft())) { + return false; + } + + // Check the cur value and update prev + if (prev != null && cur.getValue() <= prev) + return false; + prev = cur.getValue(); + + // Check for the right subtree + if (!isBSTBetter(cur.getRight())) { + return false; + } + + return true; + } + + // Checks for tree with non unique elements by comparing the + // minimum and maximum values. + // Author: Viveka Aggarwal + public boolean isBST() { + return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE); + } + + public static boolean isBST(Node n, int min, int max) { + if(n == null) + return true; + + if(n.getValue() <= min || n.getValue() > max) + return false; + + if(!isBST(n.left, min, n.getValue()) || !isBST(n.right, n.getValue(), max)) + return false; + + return true; + } + + + public static void main(String[] args) { + BinarySearchTreeCheck btOne = new BinarySearchTreeCheck(); + btOne.createBinaryTree(1); + BinarySearchTreeCheck btTwo = new BinarySearchTreeCheck(); + btTwo.createBinaryTree(2); + BinarySearchTreeCheck btThree = new BinarySearchTreeCheck(); + btThree.createBinaryTree(3); + + // Only works if all the elements in the Binary Tree are unique. + System.out.println(btOne.isBSTBetter()); + System.out.println(btTwo.isBSTBetter()); + + // Works with both unique and non unique elements in a tree. + System.out.println(btOne.isBST()); + System.out.println(btTwo.isBST()); + System.out.println(btThree.isBST()); + } } \ No newline at end of file diff --git a/solutions/java/BubbleSort.java b/solutions/java/BubbleSort.java new file mode 100644 index 0000000..bf9f767 --- /dev/null +++ b/solutions/java/BubbleSort.java @@ -0,0 +1,33 @@ +/* + * Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, + * compares each pair of adjacent items and swaps them if they are in the wrong order. + * The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. + * Author : Viveka Aggarwal + */ + +public class bubbleSort { + public static void sort(int[] arr) { + int n = arr.length-2; + while(n > 0) { + for(int j = 0 ; j <= n ; j++) { + if(arr[j] >= arr[j+1]) + swap(j, j+1, arr); + } + n--; + } + } + + public static void swap(int a, int b, int[] arr) { + int temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; + } + + public static void main(String[] a) { + int[] arr = {23, 3, 12, 54, 34, 77, 78, 87, 92, 12}; + sort(arr); + for(int i : arr) { + System.out.print(i + " "); + } + } +} diff --git a/solutions/java/CombineTwoStrings.java b/solutions/java/CombineTwoStrings.java index ba4834f..5f0c431 100644 --- a/solutions/java/CombineTwoStrings.java +++ b/solutions/java/CombineTwoStrings.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.Stack; /** @@ -9,14 +10,21 @@ * * Another solution is to compromise of the space complexity a little, and make * use of a stack as below. + * + * Addition: We could also use a HashMap to record the rank of each character + * in the combined string and check the two input strings as below. */ public class CombineTwoStrings { public static void main(String[] args) { CombineTwoStrings cts = new CombineTwoStrings(); String one = "rohit"; String two = "deepthi"; + String three = "viveka"; String combined = "rodehepitht"; - System.out.print(cts.isValid(one, two, combined)); + String combined2 = "rviovehkiat"; + System.out.println(cts.isValid(one, two, combined)); + System.out.println(cts.isValidTwo(one, three, combined2)); + } public boolean isValid(String one, String two, String combined) { @@ -40,4 +48,28 @@ else if (comparer == two.charAt(twoIndex)) } return true; } -} \ No newline at end of file + + // Used a hashMap to save the rank of each character in the combined string. + // Incrementing the pointer of respective string as per the rank in the + // combined string. + public boolean isValidTwo(String one, String two, String combined) { + if(one.length() + two.length() != combined.length()) + return false; + HashMap map = new HashMap<>(); + int key = 0, p1 = 0, p2 = 0, pT = 0; + for(Character i : combined.toCharArray()) { + map.put(key++, i); + } + + while(p1 <= one.length() - 1 && p2 <= two.length() - 1 && pT <= combined.length() - 1) { + char temp = map.get(pT++); + if(one.charAt(p1) == temp) + p1++; + else if (two.charAt(p2) == temp) + p2++; + else + return false; + } + return true; + } +}