1

我编写了一个代码,用于向二叉树插入按名称排序的元素泛型类型。但不要认为它是正确的。

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;
}

你能建议我更正吗?

4

3 回答 3

2

插入需要创建一个新节点。我现在不知道如何创建它们,因为我没有看到构造函数,但我建议以下内容:

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; 
} 
于 2012-10-07T10:05:52.890 回答
1
public Boolean add(int data){
    Node node = new Node(data);
    if(isEmpty()){
        root = node;
    }else{
        Node temp = root;
        while(true){
            if(data < temp.getData()){
                if(temp.getLeft() != null)
                    temp = temp.getLeft();
                else
                    break;
            }else{
                if(temp.getRight() != null)
                    temp = temp.getRight();
                else
                    break;
            }
        }
        if(data < temp.getData())
            temp.setLeft(node);
        else
            temp.setRight(node);
    }
    return true;
}
于 2020-11-17T22:26:20.453 回答
-1

正如 amadeus 所提到的,while 循环的末尾不应有分号:

BTNode temp = root;
    if (root == null) {
        root.setElement(e);
        return;
    }
    while (temp != null)
     {
       if (temp.element().getClass().getName().compareTo(e.getClass().getName()) < 0) {
           if(temp.getRight() != null)
             temp = temp.getRight();
           else
             {
               temp.createRight(e);
               temp = null; //or break
             }
       } else {
           if(temp.getLeft() != null)
             temp = temp.getLeft();
           else
             {
               temp.createLeft(e);
               temp = null; //or break
             }
       }
     }

    return true;
于 2012-10-07T09:54:09.893 回答