Q: how to add index for a binary tree in java? In my case, I want to create a binary tree by taking the depth as the input.
class BTreeNode<T> {
    BTreeNode<T> left,right;
    int index;
    T value;
    
    BTreeNode(T value, int index){
        this.index = index;
        this.value = value;
    }
}

class Tree<T> {
    int depth;
    BTreeNode<T> root;
        
    public static <K> eTree<K> create(int depth){
      if(depth > 0) {
          Tree<K> newtree = new SparseTree<>();
          newtree.root = new BTreeNode<>(null,0);
          newtree.addLevel(newtree.root, 0, depth);
          return newtree;
       } 
      else {
          throw new Error();
       }
    }
    
    private void addLevel(BTreeNode<T> node, int depth, int deepest){
        if(depth == deepest - 1) {
            return;
        }
        // ???
        node.left = new BTreeNode<>(node.value,node.index+1);
        node.right = new BTreeNode<>(node.value,node.index+2);
        addLevel(node.left,depth + 1,deepest);
        addLevel(node.right,depth + 1,deepest);
    }
}

Then, I want to add an index for every node in the tree accordingly. For example, for a tree with 7 nodes (depth == 3), the index for every node will be 0 1 2 3 4 5 6.
What I can do in my code to achieve this? I did several attempts but all failed. Any suggestions? Thanks!

A: because
index_left = 2*index_father+1
index_right = 2*index_father+2

you can do something like this
private void addLevel(BTreeNode<T> node, int depth, int deepest, int index){
    if(depth == deepest - 1) {
        return;
    }
    node.left = new BTreeNode<>(node.value,index*2+1);
    node.right = new BTreeNode<>(node.value,index*2+2);
    addLevel(node.left,depth + 1,deepest, index*2+1);
    addLevel(node.right,depth + 1,deepest,index*2+2);
}

