summaryrefslogtreecommitdiff
path: root/src/main/TreeHeight.java
blob: f21d607017f45e78e7865c57c22108698249714d (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
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<T> {
        private T key;
        private Node<T> parent = null;
        private List<Node<T>> children = new ArrayList<>();

        Node(T key) {
            this.key = key;
        }

        void addChild(Node<T> child) {
            child.setParent(this);
            this.children.add(child);
        }

        private void setParent(Node<T> parent) {
            this.parent = parent;
        }

        public Node<T> getParent(Node<T> parent) {
            return this.parent;
        }

        List<Node<T>> 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<Integer> 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<Integer>[] nodes, int rootIndex) {
        int levelCounter = 1;
        Queue<Node<Integer>> q = new LinkedList<>();
        int popCount = 0;
        int childrenCount = nodes[rootIndex].getChildren().size();
        int grandChildrenCount = 0;
        for (int i = 0; i < childrenCount; i++) {
            q.add(nodes[rootIndex].getChildren().get(i));
        }
        while (!q.isEmpty()) {
            if (popCount < childrenCount) {
                Node<Integer> n = q.remove();
                int i = n.getChildren().size();
                if (i != 0) {
                    for (int j = 0; j < i; j++) {
                        q.add(n.getChildren().get(j));
                    }
                }
                grandChildrenCount = grandChildrenCount + i;
                popCount = popCount + 1;
            } else {
                popCount = 0;
                childrenCount = grandChildrenCount;
                grandChildrenCount = 0;
                levelCounter = levelCounter + 1;
            }
        }
        return levelCounter + 1;
    }

    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));
//    }

}