From acff8424e09a3531cae111a93fb0f6fce1d4404e Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 22 Mar 2019 18:02:48 -0500 Subject: In-order and pre-order binary tree traversal done I worked this out myself, using iterative approach instead of recursive to avoid stack overflow issues. Post-order traversal not done yet. I'll likely follow pseudo code from this wiki page to get that done: https://en.wikipedia.org/wiki/Tree_traversal --- src/main/TreeTraversal.java | 173 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/main/TreeTraversal.java (limited to 'src/main') diff --git a/src/main/TreeTraversal.java b/src/main/TreeTraversal.java new file mode 100644 index 0000000..fee494c --- /dev/null +++ b/src/main/TreeTraversal.java @@ -0,0 +1,173 @@ +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 inOrder() { + Stack keyIndexStack = new Stack<>(); + ArrayList result = new ArrayList(); + // 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 preOrder() { + Stack keyIndexStack = new Stack<>(); + Queue keyIndexQueue = new LinkedList<>(); + ArrayList result = new ArrayList(); + // 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 postOrder() { + Stack keyIndexStack = new Stack<>(); + ArrayList result = new ArrayList(); + // 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; + } + } + + 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 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()); + } +} -- cgit v1.2.3