Skip to content

Commit 42c1fd6

Browse files
committed
Some minor improvements
1 parent 0dec287 commit 42c1fd6

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Diff for: src/main/java/com/ctci/treesandgraphs/CheckBalanced.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static int checkHeightAndBalance(TreeNode node) {
5757
return Math.max(leftHeight, rightHeight) + 1;
5858
}
5959

60-
private static boolean isBalancedOptimized(TreeNode node) {
60+
public static boolean isBalancedOptimized(TreeNode node) {
6161
return checkHeightAndBalance(node) != Integer.MIN_VALUE;
6262
}
6363

Diff for: src/main/java/com/rampatra/arrays/SortedSubSequence.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*
66
* @author rampatra
77
* @since 10/12/15
8-
* @time: 8:32 PM
98
*/
109
public class SortedSubSequence {
1110

@@ -19,36 +18,36 @@ public class SortedSubSequence {
1918
* 2) Create another auxiliary array greater[0..n-1]. greater[i] should store the index of a number which is greater than arr[i] and is on right side of arr[i]. greater[i] should contain -1 if there is no such element.
2019
* 3) Finally traverse both smaller[] and greater[] and find the index i for which both smaller[i] and greater[i] are not -1.
2120
*
22-
* @param a
21+
* @param arr
2322
*/
24-
public static void printSortedSubSequenceOfSize3(int[] a) {
25-
int len = a.length, min = a[0], max = a[len - 1];
23+
public static void printSortedSubSequenceOfSize3(int[] arr) {
24+
int len = arr.length, min = arr[0], max = arr[len - 1];
2625

2726
int[] smaller = new int[len], larger = new int[len];
2827

2928
smaller[0] = -1;
3029
for (int i = 1; i < len; i++) {
31-
if (a[i] < min) {
30+
if (arr[i] < min) {
3231
smaller[i] = -1;
33-
min = a[i];
32+
min = arr[i];
3433
} else {
3534
smaller[i] = min;
3635
}
3736
}
3837

3938
larger[len - 1] = -1;
4039
for (int i = len - 2; i >= 0; i--) {
41-
if (a[i] > max) {
40+
if (arr[i] > max) {
4241
larger[i] = -1;
43-
max = a[i];
42+
max = arr[i];
4443
} else {
4544
larger[i] = max;
4645
}
4746
}
4847

4948
for (int i = 0; i < len; i++) {
5049
if (smaller[i] != -1 && larger[i] != -1) {
51-
System.out.println(smaller[i] + "," + a[i] + "," + larger[i]);
50+
System.out.println(smaller[i] + "," + arr[i] + "," + larger[i]);
5251
break;
5352
}
5453
}

Diff for: src/main/java/com/rampatra/trees/HeightBalanced.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.rampatra.trees;
22

3+
import com.ctci.treesandgraphs.TreeNode;
34
import com.rampatra.base.BinaryNode;
45
import com.rampatra.base.BinarySearchTree;
56

@@ -19,7 +20,11 @@ public class HeightBalanced<E extends Comparable<E>> extends BinarySearchTree<E>
1920
* 1) Left subtree of T is balanced
2021
* 2) Right subtree of T is balanced
2122
* 3) The difference between heights of left subtree and right subtree is not more than 1.
22-
*
23+
*
24+
* This approach is simple but we are traversing each node multiple times while calculating the height. For a more
25+
* optimized approach see {@link com.ctci.treesandgraphs.CheckBalanced#isBalancedOptimized(TreeNode)} where while
26+
* calculating the height of tree we check whether it is balanced or not simultaneously.
27+
*
2328
* @return True if tree is height balanced otherwise false.
2429
*/
2530
public boolean isHeightBalanced() {

0 commit comments

Comments
 (0)