-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBstNode.java
More file actions
227 lines (201 loc) · 5.74 KB
/
BstNode.java
File metadata and controls
227 lines (201 loc) · 5.74 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
223
224
225
226
227
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class BstNode{
int data;
BstNode left;
BstNode right;
BstNode(int data){
this.data = data;
left = null;
right = null;
}
public static BstNode insert(int data, BstNode root){
if(root == null){
root = new BstNode(data);
return root;
}
if(data > root.data){
root.right = insert(data, root.right);
}
if(data < root.data){
root.left = insert(data, root.left);
}
return root;
}
public static void indorder(BstNode root){
if(root == null){
return;
}
indorder(root.left);
System.out.print(root.data + " ");
indorder(root.right);
}
public static void predorder(BstNode root){
if(root == null){
return;
}
System.out.print(root.data + " ");
indorder(root.left);
indorder(root.right);
}
public static void postdorder(BstNode root){
if(root == null){
return;
}
indorder(root.left);
indorder(root.right);
System.out.print(root.data + " ");
}
public static void level_order(BstNode root){
if(root == null){
return;
}
Queue<BstNode> q = new LinkedList<>();
q.add(root);
q.add(null);
while (!q.isEmpty()){
BstNode curr = q.remove();
if(curr == null){
System.out.println();
if(q.isEmpty()){
break;
}else{
q.add(null);
}
}else{
System.out.println(curr.data + " ");
if(curr.left != null){
q.add(curr.left);
}
if(curr.right != null){
q.add(curr.right);
}
}
}
}
public static BstNode findsuccessor(BstNode root){
while(root.right != null){
root = root.right;
}
return root;
}
public static BstNode delete_node(BstNode root,int val){
if(root.data < val){
root.right = delete_node(root.right,val);
}
else if(root.data > val){
root.left = delete_node(root.left,val);
}else{
if(root.right == null && root.left == null){
return null;
}else if(root.left == null){
return root.right;
}else if(root.right == null){
return root.left;
}else{
BstNode succ = findsuccessor(root.right);
root.data = succ.data;
root.right = delete_node(root.right,succ.data);
}
}
return root;
}
public static BstNode mirror(BstNode root){
if(root == null){
return null;
}
BstNode le = mirror(root.left);
BstNode ri = mirror(root.right);
root.right = le;
root.left = ri;
return root;
}
public static boolean validate(BstNode root,Integer min, Integer max){
if(root == null){
return true;
}
if(min != null && min >= root.data || max != null && max <= root.data){
return false;
}
return validate(root.left,min,root.data) && validate(root.right,root.data,max);
}
public static void printlis(ArrayList<Integer> lis){
for(int i =0; i < lis.size(); i++ ){
System.out.println(lis.get(i) + " ");
}
System.out.println();
}
public static void all_paths(BstNode root,ArrayList<Integer> lis){
if(root == null){
return;
}
lis.add(root.data);
if(root.left == null && root.right == null){
printlis(lis);
} else {
// Recursively traverse the left and right subtrees
all_paths(root.left, lis);
all_paths(root.right, lis);
}
lis.remove(lis.size()-1);
}
public static int min_bst(BstNode root){
if(root == null){
return -1;
}
while (root.left != null) {
root = root.left;
}
return root.data;
}
public static int max_bst(BstNode root){
if(root == null){
return -1;
}
while (root.right != null) {
root = root.right;
}
return root.data;
}
public static boolean search(BstNode root , int k){
if(root == null){
return false;
}
if(k == root.data){
return true;
}
boolean left_2 = search(root.left,k);
boolean right_2 = search(root.right,k);
return left_2 || right_2;
}
public static void main(String args[]){
int arr[] = {4, 1, 2, 3, 6, 7, 8 };
BstNode root = null;
for(int it : arr){
root = insert(it, root);
}
indorder(root);
System.out.println();
predorder(root);
System.out.println();
postdorder(root);
System.out.println();
level_order(root);
int ma = max_bst(root);
int mi = min_bst(root);
System.out.println(ma);
System.out.println(mi);
int k = 7;
boolean flag = search(root,k);
System.out.println(flag);
BstNode root_2 = delete_node(root,1);
indorder(root_2);
ArrayList<Integer> lis = new ArrayList<>();
all_paths(root_2,lis);
boolean flag_2 = validate(root,null,null);
System.out.println(flag_2);
BstNode node_3 = mirror(root);
indorder(node_3);
}
}