-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
222 lines (192 loc) · 5.49 KB
/
BinaryTree.java
File metadata and controls
222 lines (192 loc) · 5.49 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package ds;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/**
* Standard generic representation of a Binary Tree.
*
* @author shivam.maharshi
*/
public class BinaryTree<V> {
public BinaryTree<V> right;
public BinaryTree<V> left;
public V value;
public BinaryTree(V value) {
this.value = value;
}
public BinaryTree<V> insertLeft(V value) {
this.left = new BinaryTree<V>(value);
return this.left;
}
public BinaryTree<V> insertRight(V value) {
this.right = new BinaryTree<V>(value);
return this.right;
}
public static boolean isComplete(BinaryTree<Integer> root) {
if (root == null) {
return true;
}
return isComplete(root, 0) < 0 ? false : true;
}
/**
* This is my algorithm. A little complex but more efficient.
*/
private static int isComplete(BinaryTree<Integer> root, int level) {
if (level < 0) {
return level;
}
if (root == null) {
return level - 1;
}
int l = isComplete(root.left, level + 1);
int r = isComplete(root.right, level + 1);
if (l < r || l - r >= 2) {
return -1;
} else {
return l;
}
}
/**
* Do level order traversal of a tree and insert -1 for nulls. If there are
* -1s in the middle of the array / queue then it is not a complete tree
* otherwise if there are no -1s or all are at the end then it is a complete
* tree.
*/
public static boolean isComplete1(BinaryTree<Integer> root) {
boolean res = true;
Queue<BinaryTree<Integer>> queue = new LinkedList<BinaryTree<Integer>>();
int[] a = new int[(int) Math.pow(2, root.getHeight(root) + 1)];
Arrays.fill(a, Integer.MIN_VALUE);
queue.add(root);
int i = 0;
while (!queue.isEmpty()) {
BinaryTree<Integer> node = queue.remove();
if (node != null) {
a[i] = node.value;
queue.add(node.left);
queue.add(node.right);
} else {
a[i] = Integer.MIN_VALUE;
}
i++;
}
boolean minOcc = false;
for (i = 0; i < a.length; i++) {
if (minOcc && a[i] != Integer.MIN_VALUE) {
return false;
} else if (a[i] == Integer.MIN_VALUE) {
minOcc = true;
}
}
return res;
}
/*
* Check if the given binary tree is balanced or not. Best approach is to
* find the difference between maxHieght and minHieght.
*/
public boolean isBalanced1() {
int minHieght = Math.min(minHeight(this.left), minHeight(this.right)) + 1;
int maxHieght = Math.max(maxHeight(this.left), maxHeight(this.right)) + 1;
return maxHieght - minHieght < 2;
}
private int maxHeight(BinaryTree<V> node) {
if (node == null) {
return 0;
} else {
return 1 + Math.max(maxHeight(node.right), maxHeight(node.left));
}
}
private int minHeight(BinaryTree<V> node) {
if (node == null) {
return 0;
} else {
return 1 + Math.min(minHeight(node.left), minHeight(node.right));
}
}
/*
* Another approach is to find height and return -1 if subtree is not
* balanced.
*/
public boolean isBalanced2(BinaryTree<V> node) {
return getHeight(node) == -1 ? false : true;
}
public int getHeight(BinaryTree<V> node) {
if (node == null) {
return 0;
}
int leftHeight = getHeight(node.left);
int rightHeight = getHeight(node.right);
if (leftHeight == -1 || rightHeight == -1) {
return -1;
}
if (Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
return Math.max(leftHeight, rightHeight) + 1;
}
public static int[] array = new int[6];
public static int index = 0;
public static void inOrderTraversal(BinaryTree<Integer> node) {
if (node == null)
return;
inOrderTraversal(node.left);
array[index] = node.value;
System.out.println("index : " + index + " :: value : " + node.value);
index++;
inOrderTraversal(node.right);
}
/**
* This will not work. This is incorrect approach.
*/
public static boolean isValidBinarySearchTree(BinaryTree<Integer> root) {
inOrderTraversal(root);
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
return false;
}
}
return true;
}
public static boolean isValidBinarySearchTree(BinaryTree<Integer> node, int min, int max) {
if (node == null) {
return true;
}
return node.value >= min && node.value < max && isValidBinarySearchTree(node.left, min, node.value)
&& isValidBinarySearchTree(node.right, node.value + 1, max);
}
public static void main(String[] args) {
BinaryTree<Integer> root = new BinaryTree<Integer>(100);
BinaryTree<Integer> right = root.insertRight(150);
BinaryTree<Integer> left = root.insertLeft(25);
left.insertRight(12);
left.insertLeft(12);
right.insertLeft(12);
right.insertRight(13);
// System.out.println("Max Height : " + root.maxHeight(root));
// System.out.println("Min Height : " + root.minHeight(root));
// System.out.println("Is tree balanced : " + root.isBalanced1());
// System.out.println(root.isValidBinarySearchTree(root));
// System.out.println(isValidBinarySearchTree(root, Integer.MIN_VALUE,
// Integer.MAX_VALUE));
System.out.println(isComplete(root));
System.out.println(isComplete1(root));
}
public BinaryTree<V> getRight() {
return right;
}
public void setRight(BinaryTree<V> right) {
this.right = right;
}
public BinaryTree<V> getLeft() {
return left;
}
public void setLeft(BinaryTree<V> left) {
this.left = left;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}