-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
239 lines (224 loc) · 7.42 KB
/
BinaryTree.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree {
static int index = -1;
public static void main(String[] args) {
int nodes[] = {5,1,3,4,2,7};
Node root = null;
for(int i =0 ; i < nodes.length ; i++)
root = insert(root, nodes[i]);
printRoot2Leaf(root, new ArrayList<>());
}
static void printRoot2Leaf(Node root , ArrayList<Object> path) {
if (root == null)
return;
path.add(root.data);
if (root.left == null && root.right == null)
printPath(path);
else {
printRoot2Leaf(root.left, path);
printRoot2Leaf(root.right, path);
}
path.remove(path.size() - 1);
}
static void printPath(ArrayList<Object> path){
for(int i =0 ; i < path.size() ; i++)
System.out.print(path.get(i)+"-->");
System.out.println();
}
static Node buildTree(Object nodes[]) {
index++;
if ((int) nodes[index] == -1)
return null;
Node newNode = new Node(nodes[index]);
newNode.left = buildTree(nodes);
newNode.right = buildTree(nodes);
return newNode;
}
static Node insert(Node root , int value){
if(root == null) {
root = new Node(value);
return root;
}
if((int)root.data > value)
root.left = insert(root.left , value);
else
root.right = insert(root.right , value);
return root;
}
static Node delete(Node root, int value){
if((int)root.data > value)
root.left = delete(root.left , value);
else if((int)root.data < value)
root.right = delete(root.right , value);
else{
if(root.left == null && root.right == null)
return null;
if(root.left == null)
return root.right;
else if(root.right == null)
return root.left;
Node IS = inorderSuccessor(root.right);
root.data = IS.data;
root.right = delete(root.right ,(int) IS.data);
}
return root;
}
static void preorder(Node root) {
if (root == null)
return;
System.out.println(root.data + " ");
preorder(root.left);
preorder(root.right);
}
static void inorder(Node root) {
if (root == null)
return;
inorder(root.left);
System.out.println(root.data + " ");
inorder(root.right);
}
static void postorder(Node root) {
if (root == null)
return;
postorder(root.left);
postorder(root.right);
System.out.println(root.data + " ");
}
static void levelorder(Node root){
if(root == null)
return;;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
queue.add(null);
while(!queue.isEmpty()) {
Node currentNode = queue.remove();
if (currentNode == null) {
System.out.println();
if (queue.isEmpty())
break;
else
queue.add(null);
} else {
System.out.print(currentNode.data + " ");
if (currentNode.left != null)
queue.add(currentNode.left);
if (currentNode.right != null)
queue.add(currentNode.right);
}
}
}
static boolean search(Node root, int key){
if(root == null)
return false;
if((int)root.data == key)
return true;
if((int)root.data > key)
return search( root.left, key);
else
return search(root.right , key);
}
static int countOfNodes(Node root){
if(root == null)
return 0;
int leftNodes = countOfNodes(root.left);
int rightNodes = countOfNodes(root.right);
return leftNodes + rightNodes +1;
}
static int sumOfNodes(Node root){
if(root == null)
return 0;
int leftsum = sumOfNodes(root.left);
int rightsum = sumOfNodes(root.right);
return leftsum + rightsum + (int)root.data;
}
static int height(Node root){
if(root == null)
return 0;
int leftHeight = height(root.left);
int rightHeight = height(root.right);
int height = Math.max(leftHeight , rightHeight ) + 1;
return height;
}
public boolean isLeaf(Node root){
if(root.left == null && root.right == null)
return true;
return false;
}
static TreeInfo diameter2(Node root){
if(root == null)
return new TreeInfo( 0 ,0);
TreeInfo left = diameter2(root.left);
TreeInfo right = diameter2(root.right);
int height = Math.max(left.height , right.height) + 1;
int diameter1 = left.diameter;
int diameter2 = right.diameter;
int diameter3 = left.height + right.height + 1;
int diameter = Math.max(Math.max(diameter1 , diameter2) , diameter3);
return new TreeInfo(height, diameter);
}
static int diameter(Node root){
if(root == null)
return 0;
int diameter1 = diameter(root.left);
int diameter2 = diameter(root.right);
int diameter3 = height(root.right) + height(root.left) +1;
return Math.max(diameter3 , Math.max(diameter1 , diameter2));
}
static boolean isSubTree(Node root , Node subRoot){
if(subRoot == null)
return true;
if(root == null)
return false;
if(root.data == subRoot.data)
if(isIdentical(root , subRoot))
return true;
return isSubTree(root.left , subRoot) || isSubTree(root.right , subRoot);
}
static boolean isIdentical(Node root , Node subRoot){
if(root == null && subRoot == null)
return true;
if(root == null || subRoot == null)
return false;
if(root.data == subRoot.data)
return isIdentical(root.left , subRoot.left) && isIdentical(root.right , subRoot.right);
return false;
}
static Node inorderSuccessor(Node root){
while (root.left != null)
root = root.left;
return root;
}
static void printRange(Node root, int x , int y){
if(root == null)
return;
if((int)root.data >= x && (int)root.data <= y){
printRange(root.left , x, y);
System.out.println(root.data + " ");
printRange(root.right , x , y);
}
else if ((int)root.data >= y)
printRange(root.left, x, y);
else
printRange(root.right , x, y);
}
static class Node {
Object data;
Node left;
Node right;
Node(Object data) {
this.data = data;
left = null;
right = null;
}
}
static class TreeInfo{
int height;
int diameter;
TreeInfo(int height , int diameter){
this.height = height;
this.diameter = diameter;
}
}
}