forked from iRupam/NewtonSchoolInfinityJune21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion.java
More file actions
62 lines (50 loc) · 1.33 KB
/
Insertion.java
File metadata and controls
62 lines (50 loc) · 1.33 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
package InfinityJune21.BinarySearchTrees;
public class Insertion {
Node root;
class Node {
int key;
Node left, right;
Node(int key) {
this.key = key;
left = right = null;
}
}
void insert(int key) {
root = insertWrapper(root, key);
}
Node insertWrapper(Node root, int key) {
Node node = new Node(key);
if(root == null) {
root = node;
return root;
}
if(key < root.key) {
root.left = insertWrapper(root.left, key);
}
else {
root.right = insertWrapper(root.right, key);
}
return root;
}
void inorder() {
inorderWrapper(root);
}
void inorderWrapper(Node root) {
if(root != null) {
inorderWrapper(root.left);
System.out.print(root.key + " ");
inorderWrapper(root.right);
}
}
public static void main(String[] args) {
Insertion insertion = new Insertion();
insertion.insert(100);
insertion.insert(110);
insertion.insert(90);
insertion.insert(105);
insertion.insert(95);
insertion.insert(120);
insertion.insert(90);
insertion.inorder();
}
}