-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathURI1195BinarySearchTree.java
More file actions
113 lines (97 loc) · 2.77 KB
/
URI1195BinarySearchTree.java
File metadata and controls
113 lines (97 loc) · 2.77 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* See
* <a href="https://www.urionlinejudge.com.br/judge/en/problems/view/1195">Binary
* Search Tree</a>
*
* @author Brian Yeicol Restrepo Tangarife
*/
public class URI1195BinarySearchTree {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int C = readInt();
int[] numbers;
for (int numberCase = 1; numberCase <= C; numberCase++) {
read();
numbers = readArray();
Node root = null;
for (int number : numbers) {
root = insert(root, number);
}
out.println("Case " + numberCase + ":");
out.print("Pre.:");
preOrder(root);
out.println();
out.print("In..:");
inOrder(root);
out.println();
out.print("Post:");
postOrder(root);
out.println("\n");
}
out.close();
}
private static Node insert(Node root, int data) {
if (root == null) {
return new Node(data);
} else {
Node cur;
if (data <= root.data) {
cur = insert(root.left, data);
root.left = cur;
} else {
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
private static void preOrder(Node root) {
if (root != null) {
out.print(" " + root.data);
preOrder(root.left);
preOrder(root.right);
}
}
private static void postOrder(Node root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
out.print(" " + root.data);
}
}
private static void inOrder(Node root) {
if (root != null) {
inOrder(root.left);
out.print(" " + root.data);
inOrder(root.right);
}
}
private static String read() throws IOException {
return in.readLine();
}
private static int readInt() throws IOException {
return Integer.parseInt(read());
}
private static int[] readArray() throws IOException {
String[] line = in.readLine().split("\\s");
int l = line.length;
int[] a = new int[l];
for (int i = 0; i < l; i++) {
a[i] = Integer.parseInt(line[i]);
}
return a;
}
}
class Node {
Node left, right;
int data;
Node(int data) {
this.data = data;
left = right = null;
}
}