Skip to content

Commit 72c9c3b

Browse files
author
Ram swaroop
committed
added comments
1 parent ca08f59 commit 72c9c3b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/me/ramswaroop/trees/CheckForBST.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class CheckForBST {
2121
* Traverse the tree in in-order fashion and insert all nodes
2222
* in a list and check for sort order of list.
2323
* <p/>
24-
* Concept: Perform in-order traversal of the tree and if
25-
* the result isn't in ascending order then returns false.
24+
* Concept: In-order traversal of a BST is always sorted in ascending
25+
* manner.
2626
*
2727
* @param node
2828
* @param list
@@ -45,10 +45,12 @@ public static <E extends Comparable<E>> boolean isBST(BinaryNode<E> node, List<B
4545
}
4646

4747
/**
48-
* Traverse the tree in in-order fashion and keep track of prev node.
48+
* Traverse the tree in in-order fashion and keep track of its in-order
49+
* predecessor value. If at any point current node's value is found greater
50+
* than its predecessor value then return {@code false}.
4951
* <p/>
50-
* Concept: Perform in-order traversal of the tree and if
51-
* the result isn't in ascending order then returns false.
52+
* Concept: In-order traversal of a BST is always sorted in ascending
53+
* manner.
5254
*
5355
* @param node
5456
* @param prev
@@ -71,6 +73,12 @@ public static <E extends Comparable<E>> boolean isBST(BinaryNode<E> node, Binary
7173
}
7274

7375
/**
76+
* Simplest way to test whether a binary tree is a BST or not.
77+
* <p/>
78+
* CONCEPT: A node's left sub-tree cannot have a value more than
79+
* the node's value and similarly the node's right sub-tree cannot
80+
* have a value less than the node's value.
81+
*
7482
* @param node
7583
* @param minValue
7684
* @param maxValue
@@ -88,6 +96,7 @@ public static <E extends Comparable<E>> boolean isBST(BinaryNode<E> node, E minV
8896
}
8997

9098
public static void main(String a[]) {
99+
// in-order approach
91100
BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
92101
binarySearchTree.put(6);
93102
binarySearchTree.put(3);
@@ -106,6 +115,7 @@ public static void main(String a[]) {
106115
binaryTree.put(9);
107116
out.println("Is BST: ");
108117
out.println(isBST(binaryTree.root, new BinaryNode<Integer>(null)));
118+
// min max approach
109119
out.println("Is BST: ");
110120
out.println(isBST(binarySearchTree.root, Integer.MIN_VALUE, Integer.MAX_VALUE));
111121
out.println("Is BST: ");

0 commit comments

Comments
 (0)