Search code examples
javadata-structuresb-tree

B-Tree Implementation - shall I make the Node class a static member class or not?


I need to implement a B-Tree for University:

I have an "outer" class B-Tree with attributes root and _degree. The class to represent the nodes is implemented as a static-member class:

public class BTree<E> {
    private Node<E> root;
    // the minimal degree
    private int degree;

    public BTree(int degree) {
        if (degree < 2) {
            throw new IllegalArgumentException();
        }

        this.degree = degree;
    }

    // other stuff

    private static class Node<T> {
        T[] elements       = (T[])new Object[degree * 2 - 1];
        Node<T>[] children = (Node<T>[])new Object[degree * 2];
        int size           = 0;
    }
}

So, now my problem is: As I implemented the Node class as a static member class, I can't access the degree attribute of the outer class.

Now I have to choices:

  1. Make the Node class an inner class (non-static member class) OR
  2. Create a constructor for the Node class and pass the degree in every time I need to construct a Node.

What would be the best choice? Making it an inner class would mean the Nodes would all have a reference to the Btree (outer class), but making it a static member class would mean I would have to pass the degree in every time.


Solution

  • I would keep it static and pass degree in. That way you ensure that Node cannot know any details about BTree.