-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinTree.java
More file actions
66 lines (56 loc) · 1.4 KB
/
Copy pathBinTree.java
File metadata and controls
66 lines (56 loc) · 1.4 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
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
}
}
class process {
Node root;
public void insert(int data) {
root = insertrec(root, data);
}
public Node insertrec(Node root, int data) {
if (root == null) {
root = new Node(data);
} else if (data < root.data) {
root.left = insertrec(root.left, data);
} else if (data > root.data) {
root.right = insertrec(root.right, data);
}
return root;
}
public void Inorder() {
inorderrec(root);
}
public void inorderrec(Node root) {
if (root != null) {
inorderrec(root.left);
System.out.print(root.data);
inorderrec(root.right);
}
}
public void preorder() {
preorderrec(root);
}
public void preorderrec(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preorderrec(root.left);
preorderrec(root.right);
}
}
}
public class BinTree {
public static void main(String[] args) {
process ps = new process();
ps.insert(1);
ps.insert(2);
ps.insert(3);
ps.insert(4);
ps.insert(5);
ps.Inorder();
ps.preorder();
}
}