From 926b055030fcd6f8564138819c747b97bf664954 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Mon, 7 Jan 2019 20:06:57 -0600 Subject: Tree height recursive version done! Following example starter code, I had to specify stackSize when creating the thread. Without it, it won't pass tests due to stack overflow error. I'll still try to work out an iterative version using Queue. We'll see. --- src/main/TreeHeight.java | 156 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 src/main/TreeHeight.java (limited to 'src/main/TreeHeight.java') diff --git a/src/main/TreeHeight.java b/src/main/TreeHeight.java new file mode 100644 index 0000000..0e5119a --- /dev/null +++ b/src/main/TreeHeight.java @@ -0,0 +1,156 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.*; + +class TreeHeight { + static class FastScanner { + StringTokenizer tok = new StringTokenizer(""); + BufferedReader in; + + FastScanner(InputStream in) { + this.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()); + } + } + + static class Node { + private T key; + private Node parent = null; + private List> children = new ArrayList<>(); + + Node(T key) { + this.key = key; + } + + void addChild(Node child) { + child.setParent(this); + this.children.add(child); + } + + private void setParent(Node parent) { + this.parent = parent; + } + + public Node getParent(Node parent) { + return this.parent; + } + + List> getChildren() { + return children; + } + + public T getKey() { + return key; + } + + void setKey(T key) { + this.key = key; + } + + } + + static int getHeight(int[] a) { + Node[] nodes = new Node[a.length]; + int rootIndex = 0; + for (int i = 0; i < a.length; i++) { + nodes[i] = new Node<>(-5); + } + for (int i = 0; i < a.length; i++) { + int parentIndex = a[i]; + if (parentIndex == -1) { + rootIndex = i; + nodes[i].setKey(i); + } else { + nodes[i].setKey(i); + nodes[parentIndex].addChild(nodes[i]); + } + } +// return treeLevelCounter(nodes, rootIndex); + return treeLevelCounterRecursive(nodes[rootIndex]); + } + + private static int treeLevelCounterRecursive(Node node) { + if (node.getChildren().size() == 0) + return 1; + int maxCount = Integer.MIN_VALUE; + int tempCount; + for (int i = 0; i < node.getChildren().size(); i++) { + tempCount = treeLevelCounterRecursive(node.getChildren().get(i)); + if (tempCount > maxCount) + maxCount = tempCount; + } + return 1 + maxCount; + } + + private static int treeLevelCounter(Node[] nodes, int rootIndex) { + int levelCounter = 0; + Queue> q = new LinkedList<>(); + q.add(nodes[rootIndex]); + while (!q.isEmpty()) { + Node n = q.remove(); + int i = n.getChildren().size(); + if (i != 0) { + for (int j = 0; j < i; j++) { + q.add(n.getChildren().get(j)); + } + } + levelCounter = levelCounter + 1; + } + return levelCounter; + } + + static int getHeightNaive(int[] a) { + int n = a.length; + int maxHeight = 0; + for (int vertex = 0; vertex < n; vertex++) { + int height = 0; + for (int i = vertex; i != -1; i = a[i]) + height++; + maxHeight = Math.max(maxHeight, height); + } + return maxHeight; + } + +// public static void main(String[] args) throws IOException { +// FastScanner scanner = new FastScanner(System.in); +// int n = scanner.nextInt(); +// int[] a = new int[n]; +// for (int i = 0; i < n; i++) { +// a[i] = scanner.nextInt(); +// } +// System.out.println(getHeight(a)); +// } + public static void main(String[] args) { + new Thread(null, new Runnable() { + public void run() { + try { + runTheThing(); + } catch (IOException ignored) { + } + } + }, "1", 1 << 26).start(); + + } + + private static void runTheThing() throws IOException { + FastScanner scanner = new FastScanner(System.in); + int n = scanner.nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = scanner.nextInt(); + } + System.out.println(getHeight(a)); + } + +} -- cgit v1.2.3