From f2650a2d4f229453f9c0d6e087d57412fe9357f6 Mon Sep 17 00:00:00 2001 From: widea Date: Sun, 9 Aug 2015 19:44:14 -0400 Subject: [PATCH 1/2] updated BinarySearchTree.java --- solutions/java/BinarySearchTreeCheck.java | 245 +++++++++++++--------- 1 file changed, 142 insertions(+), 103 deletions(-) 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 From 0a12e156e50c14a77f756d1eafdba6de6b6a7229 Mon Sep 17 00:00:00 2001 From: Viveka Date: Wed, 12 Aug 2015 10:44:05 -0400 Subject: [PATCH 2/2] Update RemoveDuplicatesFromString.java --- .../java/RemoveDuplicatesFromString.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/solutions/java/RemoveDuplicatesFromString.java b/solutions/java/RemoveDuplicatesFromString.java index 1dfe597..77ec01f 100644 --- a/solutions/java/RemoveDuplicatesFromString.java +++ b/solutions/java/RemoveDuplicatesFromString.java @@ -1,8 +1,16 @@ +// Program to remove duplicates from a given string +// 1. getUniqueString(String) method uses a boolean array. +// 2. removeDuplicates(String) method uses a hash map. +// Both the methods have O(n) space and time complexity. (n being the string length) + +import java.util.HashMap; + public class RemoveDuplicatesFromString { public static void main(String[] args) { RemoveDuplicatesFromString rsd = new RemoveDuplicatesFromString(); String input = "Tree Traversal"; - System.out.println(rsd.getUniqueString(input)); + System.out.println("Method 1: " +rsd.getUniqueString(input)); + System.out.println("Method 2: " +rsd.removeDuplicates(input)); } public String getUniqueString(String input) { @@ -17,4 +25,17 @@ public String getUniqueString(String input) { } return sb.toString(); } -} \ No newline at end of file + + public String removeDuplicates(String input) { + HashMap map = new HashMap<>(); + StringBuffer sb = new StringBuffer(""); + for (int i = 0; i < input.length(); i++) { + char c = input.charAt(i); + if (!map.containsKey(c)) { + sb.append(c); + map.put(c, 1); + } + } + return sb.toString(); + } +}