#Btree implementation

1 messages · Page 1 of 1 (latest)

craggy reef
#

Hello I am struggling with the implementing part of my assignment that requires me to Create Functions for the BtreeNode class and Btree class and I am unsure of how my implementation of the insert is supposed to work.

public class BTreeNode<T extends Comparable<T>> {
    public Comparable<T>[] nodeData;
    public BTreeNode<T>[] nodeChildren;
    public BTreeNode<T> parent;
    public int size;

    @SuppressWarnings("unchecked")
    public BTreeNode(int size) {
        this.size = size;
        nodeData = new Comparable[size];
        nodeChildren = new BTreeNode[size + 1];
        //nodeData and NodeChildren are set to 1 more than the size of the node to allow for overflow
        this.parent = null;
    }

    public Comparable<T> getIndex(int i) {

        if (i < 0 || i >= size) {
            return null;
        }
        return nodeData[i];
    }

    public BTreeNode<T> ascend() {
        return parent;
    }

    public BTreeNode<T> descend(int i) {
        //return nodeChildren[i];
        if (i < 0 || i >= size + 1) {
            return null;
        }
        return nodeChildren[i];

    }

    /* -------------------------------------------------------------------------- */
    /* Helpers */
    /* -------------------------------------------------------------------------- */

    public String toString() {
        String out = "|";
        for (int i = 0; i < nodeData.length; i++) {
            if (nodeData[i] == null) {
                out += "null|";
            } else {
                out += nodeData[i].toString() + "|";
            }

        }
        return out;
    }

}

above is my current implementation of the BTreeNode class

crisp summitBOT
# craggy reef Hello I am struggling with the implementing part of my assignment that requires ...

Detected code, here are some useful tools:

Formatted code
public class BTreeNode<T extends Comparable<T>> {
  public Comparable<T> [] nodeData;
  public BTreeNode<T> [] nodeChildren;
  public BTreeNode<T> parent;
  public int size;
  @SuppressWarnings("unchecked") public BTreeNode(int size) {
    this .size = size;
    nodeData = new Comparable[size] ;
    nodeChildren = new BTreeNode[size + 1] ;
    //nodeData and NodeChildren are set to 1 more than the size of the node to allow for overflow
    this .parent = null ;
  }
  public Comparable<T> getIndex(int i) {
    if (i < 0 || i >= size) {
      return null ;
    }
    return nodeData[i] ;
  }
  public BTreeNode<T> ascend() {
    return parent;
  }
  public BTreeNode<T> descend(int i) {
    //return nodeChildren[i];
    if (i < 0 || i >= size + 1) {
      return null ;
    }
    return nodeChildren[i] ;
  }
  /* -------------------------------------------------------------------------- */
   /* Helpers */
   /* -------------------------------------------------------------------------- */
  public String toString() {
    String out = "|";
    for (int i = 0; i < nodeData.length; i++) {
      if (nodeData[i]  == null ) {
        out += "null|";
      }
      else {
        out += nodeData[i] .toString() + "|";
      }
    }
    return out;
  }
}
#

<@&987246399047479336> please have a look, thanks.

crisp summitBOT
craggy reef
crisp summitBOT
valid yacht
#

we can discuss in detail but I need more details on why you think it's incorrect/vague. And what you have tried which went wrong.

void belfry
#

So the instructor created that toString method as a testing aid but yet there are no unit tests at all, including the toString method?

crisp summitBOT
#

@craggy reef

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

craggy reef
craggy reef
void belfry
#

Your instructor has given a specification which you should create the stubs for even if you don't have the guts of the methods yet. Personally, when implementing any kind of ADT, I think its important to have unit tests. That's best done with a build system like gradle. Has your instructor

#

said anything about tests?

#

or using maven or gradle?