Search code examples
javatreenon-recursive

java binary tree insert function non recursive


I have written a code for inserting to the binary tree an element generic's type which is ordered by their names. Don't think it is correct though.

public boolean insert(E e) {
    BTNode temp = root;
    if (root == null) {
        root.setElement(e);
    }
    while (temp != null)
    if (temp.element().getClass().getName().compareTo(e.getClass().getName()) < 0) {
        temp = temp.getRight();
    } else {
        temp = temp.getLeft();
    }
    temp.setElement(e);
    return true;
}

Can you suggest me corrections ?


Solution

  • An insert would need to create a new node. I don't now how to create them as I haven't see the constuctor, but I suggest something along the lines of:

    public boolean insert(E e) {        
        if (root == null) {
            root = new BTNode();
            root.setElement(e); //how would this work with a null root?
            return true; //that's it, we're done (when is this ever false by the way?)
        }
        BTNode current = root; 
        while (true) { //brackets! indenting is important for readabilty
            BTNode parent=current;
            if (current.element().getClass().getName().compareTo(e.getClass().getName()) < 0) {
                current = current.getRight();
                if(current==null) { //we don't have a right node, need to make one
                  current = new BTNode();
                  parent.setRight(current);
                  break; //we have a new node in "current" that is empty
                }
            } else { 
                current= current.getLeft();
                if(current==null) { //we don't have a left node, need to make one
                  current = new BTNode();
                  parent.setLeft(current);
                  break;  //we have a new node in "current" that is empty
                }
            }
        }
        current.setElement(e); 
        return true; 
    }