I am trying to create a complete binary tree using a linked list, instead of arraylist, without comparing node values. What I mean is on inserting a new value, I do not wish to compare if the value is less, greater or equal than the root's value, so as to add it either to the left link or to the right link, but still be able to create a complete binary tree.
Do you think it's possible? If yes, do you have any idea or can you point me to something I can use/read to do it?
EDIT:
Here's an example to make my question more clear: I am adding the numbers in the order presented through the insert method: 1 2 3 4 5 6 the insert method takes care of the structure of the tree. 1 becomes the root since it's the first value added. 2 is the left child of 1 3 the right child of 1 4 the left child of 2 5 the right child of 2 6 the left child of 3
SOLUTION:
public void insert(Comparable item) {
total++;
if (root == null) {
root = new TreeNode(item);
} else {
int quotient=total;
Stack path = new Stack();
TreeNode p = root; // helper node to follow path
quotient /= 2; // skip last step
while (quotient>1) { // build path to follow
path.push(quotient%2);
quotient /= 2;
}
while (!path.isEmpty()) {
Object q = path.pop();
if (q.equals(0))
p = p.getLeft();
else
p = p.getRight();
}
if (total%2==0) // check last step
p.setLeft(new TreeNode(item));
else
p.setRight(new TreeNode(item));
}
}