Skip to content

Commit 223fe0d

Browse files
committed
Merge pull request blakeembrey#147 from Widea/widea-local
updated BinarySearchTree.java
2 parents 5696d89 + d37dd99 commit 223fe0d

File tree

3 files changed

+209
-105
lines changed

3 files changed

+209
-105
lines changed
Lines changed: 142 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,144 @@
1-
/* Uses Inorder traversal. Only works on unique element BT */
2-
public class BinarySearchTreeCheck {
3-
Node root;
4-
5-
public BinarySearchTreeCheck() {
6-
root = null;
7-
}
8-
9-
public class Node {
10-
int value;
11-
Node left;
12-
Node right;
13-
14-
public Node(int value) {
15-
this.value = value;
16-
this.left = null;
17-
this.right = null;
18-
}
19-
20-
public void setLeft(Node left) {
21-
this.left = left;
22-
}
23-
24-
public Node getLeft() {
25-
return left;
26-
}
27-
28-
public void setRight(Node right) {
29-
this.right = right;
30-
}
31-
32-
public Node getRight() {
33-
return right;
34-
}
35-
36-
public int getValue() {
37-
return value;
38-
}
39-
}
40-
41-
public void createBinaryTree(int option) {
42-
root = new Node(10);
43-
Node one = new Node(8);
44-
Node two = new Node(5);
45-
Node three = new Node(9);
46-
Node four = new Node(15);
47-
switch (option) {
48-
case 1: /* Is BST (Only unique elements) */
49-
root.setLeft(one);
50-
root.setRight(four);
51-
one.setLeft(two);
52-
one.setRight(three);
53-
break;
54-
case 2: /* Not BST (Only unique elements) */
55-
root.setRight(two);
56-
root.setLeft(one);
57-
one.setLeft(four);
58-
one.setRight(three);
59-
break;
60-
default:
61-
break;
62-
}
63-
}
1+
/* checks if a given tree is a BST
2+
1. isBSTBetter checks for a BST with unique elements
3+
2. isBST checks for a BST with unique or non Unique elements
4+
*/
645

65-
public boolean isBSTBetter() {
66-
if (root == null)
67-
return true;
68-
return isBSTBetter(root);
69-
}
70-
71-
private Integer prev = null;
72-
73-
public boolean isBSTBetter(Node cur) {
74-
if (cur == null)
75-
return true;
76-
77-
// Check for the left subtree
78-
if (!isBSTBetter(cur.getLeft())) {
79-
return false;
80-
}
81-
82-
// Check the cur value and update prev
83-
if (prev != null && cur.getValue() <= prev)
84-
return false;
85-
prev = cur.getValue();
86-
87-
// Check for the right subtree
88-
if (!isBSTBetter(cur.getRight())) {
89-
return false;
90-
}
91-
92-
return true;
93-
}
94-
95-
public static void main(String[] args) {
96-
BinarySearchTreeCheck btOne = new BinarySearchTreeCheck();
97-
btOne.createBinaryTree(1);
98-
BinarySearchTreeCheck btTwo = new BinarySearchTreeCheck();
99-
btTwo.createBinaryTree(2);
100-
101-
// Only works if all the elements in the Binary Tree are unique.
102-
System.out.println(btOne.isBSTBetter());
103-
System.out.println(btTwo.isBSTBetter());
104-
}
6+
public class BinarySearchTreeCheck {
7+
Node root;
8+
9+
public BinarySearchTreeCheck() {
10+
root = null;
11+
}
12+
13+
public class Node {
14+
int value;
15+
Node left;
16+
Node right;
17+
18+
public Node(int value) {
19+
this.value = value;
20+
this.left = null;
21+
this.right = null;
22+
}
23+
24+
public void setLeft(Node left) {
25+
this.left = left;
26+
}
27+
28+
public Node getLeft() {
29+
return left;
30+
}
31+
32+
public void setRight(Node right) {
33+
this.right = right;
34+
}
35+
36+
public Node getRight() {
37+
return right;
38+
}
39+
40+
public int getValue() {
41+
return value;
42+
}
43+
}
44+
45+
public void createBinaryTree(int option) {
46+
root = new Node(10);
47+
Node one = new Node(8);
48+
Node two = new Node(5);
49+
Node three = new Node(9);
50+
Node four = new Node(15);
51+
Node five = new Node(8);
52+
switch (option) {
53+
case 1: /* Is BST (Only unique elements) */
54+
root.setLeft(one);
55+
root.setRight(four);
56+
one.setLeft(two);
57+
one.setRight(three);
58+
break;
59+
case 2: /* Not BST (Only unique elements) */
60+
root.setRight(two);
61+
root.setLeft(one);
62+
one.setLeft(four);
63+
one.setRight(three);
64+
break;
65+
case 3: /* Is BST (Non-unique elements) */
66+
root.setLeft(one);
67+
root.setRight(four);
68+
one.setLeft(five);
69+
one.setRight(three);
70+
break;
71+
default:
72+
break;
73+
}
74+
}
75+
76+
public boolean isBSTBetter() {
77+
if (root == null)
78+
return true;
79+
return isBSTBetter(root);
80+
}
81+
82+
private Integer prev = null;
83+
84+
public boolean isBSTBetter(Node cur) {
85+
if (cur == null)
86+
return true;
87+
88+
// Check for the left subtree
89+
if (!isBSTBetter(cur.getLeft())) {
90+
return false;
91+
}
92+
93+
// Check the cur value and update prev
94+
if (prev != null && cur.getValue() <= prev)
95+
return false;
96+
prev = cur.getValue();
97+
98+
// Check for the right subtree
99+
if (!isBSTBetter(cur.getRight())) {
100+
return false;
101+
}
102+
103+
return true;
104+
}
105+
106+
// Checks for tree with non unique elements by comparing the
107+
// minimum and maximum values.
108+
// Author: Viveka Aggarwal
109+
public boolean isBST() {
110+
return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
111+
}
112+
113+
public static boolean isBST(Node n, int min, int max) {
114+
if(n == null)
115+
return true;
116+
117+
if(n.getValue() <= min || n.getValue() > max)
118+
return false;
119+
120+
if(!isBST(n.left, min, n.getValue()) || !isBST(n.right, n.getValue(), max))
121+
return false;
122+
123+
return true;
124+
}
125+
126+
127+
public static void main(String[] args) {
128+
BinarySearchTreeCheck btOne = new BinarySearchTreeCheck();
129+
btOne.createBinaryTree(1);
130+
BinarySearchTreeCheck btTwo = new BinarySearchTreeCheck();
131+
btTwo.createBinaryTree(2);
132+
BinarySearchTreeCheck btThree = new BinarySearchTreeCheck();
133+
btThree.createBinaryTree(3);
134+
135+
// Only works if all the elements in the Binary Tree are unique.
136+
System.out.println(btOne.isBSTBetter());
137+
System.out.println(btTwo.isBSTBetter());
138+
139+
// Works with both unique and non unique elements in a tree.
140+
System.out.println(btOne.isBST());
141+
System.out.println(btTwo.isBST());
142+
System.out.println(btThree.isBST());
143+
}
105144
}

solutions/java/BubbleSort.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted,
3+
* compares each pair of adjacent items and swaps them if they are in the wrong order.
4+
* The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
5+
* Author : Viveka Aggarwal
6+
*/
7+
8+
public class bubbleSort {
9+
public static void sort(int[] arr) {
10+
int n = arr.length-2;
11+
while(n > 0) {
12+
for(int j = 0 ; j <= n ; j++) {
13+
if(arr[j] >= arr[j+1])
14+
swap(j, j+1, arr);
15+
}
16+
n--;
17+
}
18+
}
19+
20+
public static void swap(int a, int b, int[] arr) {
21+
int temp = arr[a];
22+
arr[a] = arr[b];
23+
arr[b] = temp;
24+
}
25+
26+
public static void main(String[] a) {
27+
int[] arr = {23, 3, 12, 54, 34, 77, 78, 87, 92, 12};
28+
sort(arr);
29+
for(int i : arr) {
30+
System.out.print(i + " ");
31+
}
32+
}
33+
}

solutions/java/CombineTwoStrings.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import java.util.HashMap;
12
import java.util.Stack;
23

34
/**
@@ -9,14 +10,21 @@
910
*
1011
* Another solution is to compromise of the space complexity a little, and make
1112
* use of a stack as below.
13+
*
14+
* Addition: We could also use a HashMap to record the rank of each character
15+
* in the combined string and check the two input strings as below.
1216
*/
1317
public class CombineTwoStrings {
1418
public static void main(String[] args) {
1519
CombineTwoStrings cts = new CombineTwoStrings();
1620
String one = "rohit";
1721
String two = "deepthi";
22+
String three = "viveka";
1823
String combined = "rodehepitht";
19-
System.out.print(cts.isValid(one, two, combined));
24+
String combined2 = "rviovehkiat";
25+
System.out.println(cts.isValid(one, two, combined));
26+
System.out.println(cts.isValidTwo(one, three, combined2));
27+
2028
}
2129

2230
public boolean isValid(String one, String two, String combined) {
@@ -40,4 +48,28 @@ else if (comparer == two.charAt(twoIndex))
4048
}
4149
return true;
4250
}
43-
}
51+
52+
// Used a hashMap to save the rank of each character in the combined string.
53+
// Incrementing the pointer of respective string as per the rank in the
54+
// combined string.
55+
public boolean isValidTwo(String one, String two, String combined) {
56+
if(one.length() + two.length() != combined.length())
57+
return false;
58+
HashMap<Integer, Character> map = new HashMap<>();
59+
int key = 0, p1 = 0, p2 = 0, pT = 0;
60+
for(Character i : combined.toCharArray()) {
61+
map.put(key++, i);
62+
}
63+
64+
while(p1 <= one.length() - 1 && p2 <= two.length() - 1 && pT <= combined.length() - 1) {
65+
char temp = map.get(pT++);
66+
if(one.charAt(p1) == temp)
67+
p1++;
68+
else if (two.charAt(p2) == temp)
69+
p2++;
70+
else
71+
return false;
72+
}
73+
return true;
74+
}
75+
}

0 commit comments

Comments
 (0)