Skip to content

Instantly share code, notes, and snippets.

@FernandoGV667
Created September 9, 2016 06:15
Show Gist options
  • Save FernandoGV667/f7a4ce99cc20f588b54b57fc10707f7f to your computer and use it in GitHub Desktop.
Save FernandoGV667/f7a4ce99cc20f588b54b57fc10707f7f to your computer and use it in GitHub Desktop.
Starter File for a program that prints a binary tree using the 3 depth first traversals
import java.util.*;
import java.io.*;
public class tree_orders {
class FastScanner {
StringTokenizer tok = new StringTokenizer("");
BufferedReader in;
FastScanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (!tok.hasMoreElements())
tok = new StringTokenizer(in.readLine());
return tok.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
public class TreeOrders {
int n;
int[] key, left, right;
void read() throws IOException {
FastScanner in = new FastScanner();
n = in.nextInt();
key = new int[n];
left = new int[n];
right = new int[n];
for (int i = 0; i < n; i++) {
key[i] = in.nextInt();
left[i] = in.nextInt();
right[i] = in.nextInt();
}
}
List<Integer> inOrder() {
ArrayList<Integer> result = new ArrayList<Integer>();
// Finish the implementation
// You may need to add a new recursive method to do that
return result;
}
List<Integer> preOrder() {
ArrayList<Integer> result = new ArrayList<Integer>();
// Finish the implementation
// You may need to add a new recursive method to do that
return result;
}
List<Integer> postOrder() {
ArrayList<Integer> result = new ArrayList<Integer>();
// Finish the implementation
// You may need to add a new recursive method to do that
return result;
}
}
static public void main(String[] args) throws IOException {
new Thread(null, new Runnable() {
public void run() {
try {
new tree_orders().run();
} catch (IOException e) {
}
}
}, "1", 1 << 26).start();
}
public void print(List<Integer> x) {
for (Integer a : x) {
System.out.print(a + " ");
}
System.out.println();
}
public void run() throws IOException {
TreeOrders tree = new TreeOrders();
tree.read();
print(tree.inOrder());
print(tree.preOrder());
print(tree.postOrder());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment