Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 142 additions & 103 deletions solutions/java/BinarySearchTreeCheck.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
25 changes: 23 additions & 2 deletions solutions/java/RemoveDuplicatesFromString.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -17,4 +25,17 @@ public String getUniqueString(String input) {
}
return sb.toString();
}
}

public String removeDuplicates(String input) {
HashMap<Character, Integer> 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();
}
}