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
+ */
64
5
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
+ }
105
144
}
0 commit comments