SHOW:
|
|
- or go back to the newest paste.
1 | import java.io.*; | |
2 | import java.util.*; | |
3 | ||
4 | public class BinaryTreeTraversal { | |
5 | ||
6 | /** | |
7 | * @param args | |
8 | */ | |
9 | public static void main(String[] args) { | |
10 | Scanner input = new Scanner("input.txt"); | |
11 | int n = input.nextInt(); | |
12 | int m = (int) Math.pow(2,n)-1; | |
13 | List<BinaryNode<Character>> list = new ArrayList<BinaryNode<Character>>(); | |
14 | ||
15 | for (Character c : input.nextLine().toCharArray()) { | |
16 | list.add(new BinaryNode<Character>(Character.isLetter(c) ? c : null)); | |
17 | } | |
18 | BinaryNode<Character> root = list.get(0); | |
19 | ||
20 | - | // HERE: turn the flat, unrelated array of BinaryNode's into a real tree starting with root. |
20 | + | for (int i = 1; i <= list.size(); i++) { |
21 | BinaryNode<Character> node = list.get(i-1); | |
22 | if (node == null) | |
23 | continue; | |
24 | node.setLeft(list.get(2*i); | |
25 | node.setRight(list.get(2*i+1); | |
26 | } | |
27 | ||
28 | System.out.println(root.toStringPreOrder()); | |
29 | System.out.println(root.toStringInOrder()); | |
30 | System.out.println(root.toStringPostOrder()); | |
31 | // done! | |
32 | } | |
33 | } |