-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBSTJAVA.java
More file actions
261 lines (244 loc) · 6.74 KB
/
BSTJAVA.java
File metadata and controls
261 lines (244 loc) · 6.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.JavaClassAssignment;
import java.util.Scanner;
class BSTNode
{
BSTNode left, right;
int data;
public BSTNode(int n)
{
left = null;
right = null;
data = n;
}
// I have used setLeft for Setting Left Node
public void setLeft(BSTNode n)
{
left = n;
}
// I have used setRight for Setting right Node
public void setRight(BSTNode n)
{
right = n;
}
// I have used getters here using my OOP Knowledge
public BSTNode getLeft()
{
return left;
}
public BSTNode getRight()
{
return right;
}
public int getData()
{
return data;
}
}
class BST
{
public static BSTNode root;
public BST()
{
root = null;
}
// I have used this Function to Check whether the Tree is Empty or not
public boolean isEmpty()
{
return root == null;
}
// I have used below Functions for CASE:1 ----Creation
public void create(int data)
{
root = insert(root, data);
}
private BSTNode create(BSTNode node, int data)
{
if(node != null) {
System.out.println("Tree is Already Created");
}
else {
node = new BSTNode(data);
}
return node;
}
//I have Used this Function for CASE 2: ----Display: preorder traversal
public void preorder()
{
preorder(root);
}
private void preorder(BSTNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
// I have used this Function for CASE 3: ----Insertion
public void insert(int data)
{
root = insert(root, data);
}
private BSTNode insert(BSTNode node, int data)
{
if (node == null)
node = new BSTNode(data);
else
{
if (data <= node.getData())
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
//I have Used this Function for CASE 4: ----Deletion
public void delete(int k)
{
if (isEmpty())
System.out.println("Tree Empty");
else
{
root = delete(root, k);
System.out.println(k + " deleted from the tree");
}
}
private BSTNode delete(BSTNode root, int k)
{
BSTNode p, p2, n;
if (root.getData() == k)
{
BSTNode lt, rt;
lt = root.getLeft();
rt = root.getRight();
if (lt == null && rt == null)
return null;
else if (lt == null)
{
p = rt;
return p;
}
else if (rt == null)
{
p = lt;
return p;
}
else
{
p2 = rt;
p = rt;
while (p.getLeft() != null)
p = p.getLeft();
p.setLeft(lt);
return p2;
}
}
if (k < root.getData())
{
n = delete(root.getLeft(), k);
root.setLeft(n);
}
else
{
n = delete(root.getRight(), k);
root.setRight(n);
}
return root;
}
// I have used below Function for CASE 5: ----Height
public int height(BSTNode r) {
if (r == null)
return 0;
else {
int lDepth = height(r.left);
int rDepth = height(r.right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
// I have Used below Functions for CASE 6: ----Nodes Counting
public int countNodes() {
return countNodes(root);
}
private int countNodes(BSTNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
//I have Used this Function for CASE 7: ----Inorder traversal
public void inorder()
{
inorder(root);
}
private void inorder(BSTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BST bst = new BST();
System.out.println("Binary Search Tree Test\n");
char ch;
do {
System.out.println("\nBinary Search Tree Operations\n");
System.out.println("1. Creation ");
System.out.println("2. Display: Preorder Traversal");
System.out.println("3. Insertion");
System.out.println("4. Deletion");
System.out.println("5. Height");
System.out.println("6. Node count");
System.out.println("7. Inorder Traversal");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter First Element of the Tree");
bst.create(scan.nextInt());
break;
case 2:
System.out.print("\nPre order : ");
bst.preorder();
break;
case 3:
System.out.println("Enter element to insert");
bst.insert(scan.nextInt());
break;
case 4:
System.out.println("Enter element to delete");
bst.delete(scan.nextInt());
break;
case 5:
System.out.println("Height of Tree is: " + bst.height(BST.root));
break;
case 6:
System.out.println("Nodes = " + bst.countNodes());
break;
case 7:
System.out.print("\nIn order : ");
bst.inorder();
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
}
}