如何将这两个功能合并在一起?

时间:2013-12-10 02:47:36

标签: java

public boolean insert(K key, V value) {
    if (contains(key)) { //if the binary tree contains it don't insert it.
        return false;
    }
    if (rNode == null) { //if the root node is empty, there is nothing in the tree
        //create a new DictionaryNode.
        rNode = new DictionaryNode<K, V>(key, value);
        curNode = rNode;
    } else {//if the above aren't true then you can insert it.
        placeNode(key, value, rNode, null, false); //use private method placeNode
    }
    changeCounter++;
    currentSize++;
    return true;
}//end insert

这是另一个功能。我希望能够在insert方法中执行placeNode所做的所有操作。我希望能够摆脱我的placeNode方法。

private void placeNode(K key, V value, DictionaryNode<K, V> node, DictionaryNode<K, V> parent, boolean nodeLeft) {
    if (node == null) {
        if (nodeLeft) {
            parent.lChild = new DictionaryNode<K, V>(key, value);
        } else {
            parent.rChild = new DictionaryNode<K, V>(key, value);
        }
    } else if (((Comparable<K>) key).compareTo(node.key) < 0) {
        placeNode(key, value, node.lChild, node, true);
    } else {
        placeNode(key, value, node.rChild, node, false);
    }
}//end placeNode

2 个答案:

答案 0 :(得分:2)

除了将这两个功能合并在一起的头痛之外,还有 nothing 。他们执行不同的操作,因此,应尽可能将其分成较小的代码块。

方法的分割是 精细 。不要试图合并它们。

你可以做的唯一真实的事情是用你想要移除的方法的确切主体替换你的方法调用......但这会使事情变得非常复杂。

另外,您的演员((Comparable<K>) key可以产生ClassCastException。如果绑定到K的类型不具有可比性,那么在运行时,您将遇到一个主要问题。泛型并不意味着以这种方式使用 - 您希望提供编译时类型的安全性。

幸运的是,您可以通过在类中添加类型的上限来解决此问题:

public class DictionaryNode<K extends Comparable<K>, V> {
    // implementation to follow
}

答案 1 :(得分:1)

private DictionaryNode<K, V> curNode = rNode;    
public boolean insert(K key, V value) {
            if (contains(key)) { // if the binary tree contains it don't insert it.
                return false;
            }
            if (rNode == null) { // if the root node is empty, there is nothing in
                                    // the tree
                                    // create a new DictionaryNode.
                rNode = new DictionaryNode<K, V>(key, value);

            } else {// if the above aren't true then you can insert it.

                int c = ((Comparable<K>)key).compareTo(curNode.key);


                if (c < 0) {
                    if (curNode.lChild == null) {
                        curNode.lChild = new DictionaryNode<K, V>(key, value);
                    }
                    else {
                        curNode = curNode.lChild;
                        return insert(key, value);
                    }
                }
                else {
                    if (curNode.rChild == null) {
                        curNode.rChild = new DictionaryNode<K, V>(key, value);
                    }
                    else {
                        curNode = curNode.rChild;
                        return insert(key, value);
                    }
                }

            }
            curNode = rNode;
            changeCounter++;
            currentSize++;
            return true;
        }

编辑 -

假设K和V来自类声明,则应将它们声明为

public SomeClass<K extends Comparable<K>, V extends Comparable<V> { ... }
正如Makoto建议的那样。

相关问题