**
Write the beginnings of a class for a Table implemented with a Binary Search Tree. (done)
Then create a Table class with only one data member for the root. (done)
Provide the following methods:
a constructor that produces an empty table (done)
a method to insert a given item, (done)
Create a separate class for a Node, with public data members (data, left, right) to support the Table class. (done)
Use the following interface to declare the type of the data.
interface Sortable{ public int compareTo( Sortable b );} (done)
This does not have to be compiled and tested
** Yes, this is a hw assignment (I'm not asking for it to be done for me, it should already be done if I understood it correctly), I'm asking for a code review and any help with commenting on my code (if anything is wrong would be lovely to know (like if my logic is off in a section so I can go to it and address it). I don't have access to tutors as easily now so using this to hopefully check my work and research what I am lacking in understanding is my goal. Otherwise, my train of thought I hope is on the right path to understanding this. Thanks
package com.stackexchange.codereview;
interface Sortable {
public int compareTo(Sortable other);
// a.compareTo(b) returns 0 if a == b,
// -1 if a < b, and +1 if a > b
}
class TreeNode { // This is the separate class for node
public Sortable _data; // this is the data variable
public TreeNode left; // this stores left field
public TreeNode right; // this stores right field
}
class Table {
private TreeNode _root = null; // This is the table class with only data member head
public void insert(Sortable item) { // This insert a new item to the table
int compare = 0;
TreeNode current = _root;
TreeNode parent = null;
while (current != null) {
parent = current;
compare = item.compareTo(current._data);
if (compare == 0)
return;
if (compare < 0)
current = current.left;
else
current = current.right;
}
TreeNode add = new TreeNode();
add._data = item;
add.left = add.right = null;
if (parent == null)
_root = add;
else if (compare <= 0)
parent.left = add;
else
parent.right = add;
}
}
If
shouldn't be capitalized.Sortabe
is missing an L, and so on. Copy the code from Eclipse and paste it in the question body; don't try to transcribe it in by hand. \$\endgroup\$