-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathCheckBST.java
140 lines (125 loc) · 4.85 KB
/
CheckBST.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*Solution to check if a binary tree is a Binary Search tree.
This solution works for duplicate input values of nodes as well.
Constraints:
0<= NodeValue <= 10^4
Incorrect Approach:
It is not enough to check if the left and right node of current node are smaller and larger than it respectively.
This approach would't work for the following example which is actually not a Binary Search Tree --
15
/ \
12 28
/ \
9 24
Correct Approach:
It is a property of Binary Search Tree that its inorder traversal produces a sorted list in ascending order. By inorder traversal of the tree we store the elements in a list and then check if the list is sorted.
Time Complexity : O(n)
Space Complexity : O(n)
*/
import java.util.*;
//Class which defines the structure of each node in the binary tree
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
}
}
public class CheckBST {
//Create a binary tree by taking input level wise
public static Node createBinaryTree() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter root node value or -1 to exit: ");
int rootData = sc.nextInt();
Node root = null;
if (rootData != -1) {
root = new Node(rootData);
} else {
sc.close();
return root;
}
Queue < Node > tree = new LinkedList < > ();
tree.add(root);
while (!tree.isEmpty()) {
Node temp = tree.remove();
System.out.print("Enter left child of " + temp.data + " or -1 if there is no left child: ");
int leftChild = sc.nextInt();
// if leftChild is not null create and add left child node
if (leftChild != -1) {
Node child = new Node(leftChild);
tree.add(child);
temp.left = child;
}
System.out.print("Enter right child of " + temp.data + " or -1 if there is no right child: ");
int rightChild = sc.nextInt();
//if rightChild is not null create and add right child node
if (rightChild != -1) {
Node child = new Node(rightChild);
tree.add(child);
temp.right = child;
}
}
sc.close();
return root;
}
static ArrayList < Integer > arr = new ArrayList < Integer > ();
//Function to produce a list by inorder traversal of tree
public static void inorder(Node root) {
if (root == null)
return;
inorder(root.left);
arr.add(root.data);
inorder(root.right);
}
//Function to check if a Binary Tree is a Binary Search Tree
public static boolean checkBST(Node root) {
if (root == null)
return true;
boolean check = true;
inorder(root);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i - 1) >= arr.get(i)) {
check = false;
break;
}
}
return check;
}
// The checkBST method Takes Space to Store in order traversal
// we can optimize the checkBST function by doing the check in inOrderTraversal itself and modifying the lower and upper bounds of values in each recursive call
// The below method has time Complexity of O(N) and Space Complexity of O(logN) where N are number of nodes
public static boolean checkBSTOptimized(Node root,int min,int max){
if(root==null)return true;
if(root.data <min || root.data>max)return false;
boolean left=checkBSTOptimized(root.left,min, root.data);
boolean right=checkBSTOptimized(root.right,root.data,max);
return left && right;
}
public static void main(String[] args) {
Node root = createBinaryTree();
boolean check;
// check = checkBST(root);
check= checkBSTOptimized(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
if (check == true)
System.out.println("It is a Binary Search Tree");
else
System.out.println("It is not a Binary Search Tree");
}
}
/* Sample Input :
Enter root node value or -1 to exit: 15
Enter left child of 15 or -1 if there is no left child: 12
Enter right child of 15 or -1 if there is no right child: 28
Enter left child of 12 or -1 if there is no left child: 9
Enter right child of 12 or -1 if there is no right child: 24
Enter left child of 28 or -1 if there is no left child: -1
Enter right child of 28 or -1 if there is no right child: -1
Enter left child of 9 or -1 if there is no left child: -1
Enter right child of 9 or -1 if there is no right child: -1
Enter left child of 24 or -1 if there is no left child: -1
Enter right child of 24 or -1 if there is no right child: -1
Sample Output :
It is not a Binary Search Tree
Time Complexity : O(n)
Space Complexity : O(n)
Space Complexity of Optimized method O(logN)
*/