How to implement in-order, pre-order and post-order traversals of a binary tree with data from 3 arrays
<p>I understand that the binary tree can be implemented easily this way:</p>

<pre><code>class Node {
    int key;
    Node left, right;
    public Node(int item) {
        key = item;
        left = right = null;
    }
}

class BinaryTree {
    Node root;
    public BinaryTree() {
      root = null;
    }
}
</code></pre>

<p>Also the methods for traversal that I've figured out are:</p>

<pre><code>void printInorder(Node node) {
    if (node == null) return;
    printInorder(node.left);
    System.out.print(node.key + " ");
    printInorder(node.right);
}

void printPreorder(Node node) {
    if (node == null) return;
    System.out.print(node.key + " ");
    printPreorder(node.left);
    printPreorder(node.right);
}

void printPostorder(Node node) {
    if (node == null) return;
    printPostorder(node.left);
    printPostorder(node.right);
    System.out.print(node.key + " ");
}
</code></pre>

<p>However, I'm given <a href="https://gist.github.com/FernandoGV667/f7a4ce99cc20f588b54b57fc10707f7f" rel="nofollow">this starter file</a> where the tree data is in 3 arrays: key[],left[] and right[], so key[] elements are the data of the nodes, left and right elements are the indexes of the left and right child of the ith node, so Node root is keys[0], with left child keys[left[0]] and keys[right[0].</p>

<p>I'm not sure how (or if I need) to convert the 3 arrays into a Binary tree using the Node and BinaryTree classes. <strong>Where should the Node and BinaryTree classes should go?</strong> Outside of tree_orders? Inside of tree_orders but outside of TreeOrders? (sorry about the "creative" naming convention, not mine)</p>

<p><strong>Do I need to iterate over the three arrays to build the tree nodes?</strong></p>

<p>I tried implementing the insert(int data) and insert(Node n, int data) methods below to convert the arrays into nodes but it doesn't seem to fill the tree.</p>

<pre><code>Node insert(int data) {
     root = insert(root, data);
     return node;
}

Node insert(Node node, int data) {
     if (node == null) {
          node = new Node(data)         
     }
     else {
          if (node.left == null) insert(node.left, data);
          else insert(node.right, data);
     }
     return node;
}
</code></pre>

<p>It's just 5 months that I've started learning programming (picked Java) and I've worked with Trees before but this starter is an OOP puzzle for me, I'll need to recheck my OOP knowledge.</p>

<p>This is an example of how the input and output should show (-1 = null node / 5 = number of given nodes):</p>

<pre><code>Input:
5
4 1 2
2 3 4
5 -1 -1
1 -1 -1
3 -1 -1

Output:
1 2 3 4 5
4 2 1 3 5
1 3 2 5 4 
</code></pre>