summaryrefslogtreecommitdiff
path: root/src/main/TreeTraversal.java
blob: d69164e215a9f8e9a881588ae1c9108145949be3 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class TreeTraversal {
    static 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 static 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() {
            Stack<Integer> keyIndexStack = new Stack<>();
            ArrayList<Integer> result = new ArrayList<Integer>();
            // Finish the implementation
            // You may need to add a new recursive method to do that
            boolean walkLeft = true;
            int currentIndex = 0;
            keyIndexStack.push(currentIndex);
            while (!keyIndexStack.empty() || right[currentIndex] != -1) {
                if (walkLeft) {
                    if (left[currentIndex] != -1) {
                        currentIndex = left[currentIndex];
                        keyIndexStack.push(currentIndex);
                    } else {
                        result.add(key[keyIndexStack.pop()]);
                        walkLeft = false;
                    }
                } else {
                    if (right[currentIndex] != -1) {
                        currentIndex = right[currentIndex];
                        keyIndexStack.push(currentIndex);
                        walkLeft = true;
                    } else {
                        if (!keyIndexStack.empty()) {
                            currentIndex = keyIndexStack.pop();
                            result.add(key[currentIndex]);
                        }
                    }
                }
            }
            return result;
        }

        List<Integer> preOrder() {
            Stack<Integer> keyIndexStack = new Stack<>();
            Queue<Integer> keyIndexQueue = new LinkedList<>();
            ArrayList<Integer> result = new ArrayList<Integer>();
            // Finish the implementation
            // You may need to add a new recursive method to do that
            boolean walkLeft = true;
            int currentIndex = 0;
            keyIndexStack.push(currentIndex);
            keyIndexQueue.add(currentIndex);
            while (!keyIndexStack.empty() || right[currentIndex] != -1) {
                if (walkLeft) {
                    if (left[currentIndex] != -1) {
                        currentIndex = left[currentIndex];
                        keyIndexQueue.add(currentIndex);
                        keyIndexStack.push(currentIndex);
                    } else {
                        keyIndexStack.pop();
                        walkLeft = false;
                        result.add(key[keyIndexQueue.remove()]);
                    }
                } else {
                    if (right[currentIndex] != -1) {
                        currentIndex = right[currentIndex];
                        keyIndexQueue.add(currentIndex);
                        keyIndexStack.push(currentIndex);
                        walkLeft = true;
                    } else {
                        if (!keyIndexStack.empty()) {
                            currentIndex = keyIndexStack.pop();
                            result.add(key[keyIndexQueue.remove()]);
                        }
                    }
                }
            }
            return result;
        }

        List<Integer> postOrder() {
            Stack<Integer> keyIndexStack = new Stack<>();
            ArrayList<Integer> result = new ArrayList<Integer>();
            // Finish the implementation
            // You may need to add a new recursive method to do that
            boolean walkLeft = true;
            int currentIndex = 0;
            keyIndexStack.push(currentIndex);
            while (!keyIndexStack.empty() || right[currentIndex] != -1) {
                if (walkLeft) {
                    if (left[currentIndex] != -1) {
                        currentIndex = left[currentIndex];
                        keyIndexStack.push(currentIndex);
                    } else {
                        if (right[currentIndex] == -1) {
                            result.add(key[currentIndex]);
                            walkLeft = false;
                            keyIndexStack.pop();
                            currentIndex = keyIndexStack.peek();
                        } else {
                            currentIndex = right[currentIndex];
                            keyIndexStack.push(currentIndex);
                        }
                    }
                } else {
                    if (right[currentIndex] != -1) {
                        currentIndex = right[currentIndex];
                        keyIndexStack.push(currentIndex);
                        walkLeft = true;
                    } else {
                        result.add(key[currentIndex]);
                        keyIndexStack.pop();
                        currentIndex = keyIndexStack.peek();
                    }
                }
            }
            return result;
        }
    }

    static public void main(String[] args) throws IOException {
        new Thread(null, new Runnable() {
            public void run() {
                try {
                    new TreeTraversal().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());
    }
}