删除排序二进制树的(对象键)

时间:2018-05-30 20:56:27

标签: java sorting tree binary

我的任务是为我的Sorted Binary Tree实现remove()方法。我需要通过一系列由我们老师指定的测试,而我正在努力这样做。这是我目前的代码,它是从我们的教科书中大量借用的。

reverse()

这是我努力通过的考验:

@Override
@SuppressWarnings("unchecked")
public V remove(Object key) throws NoSuchElementException {
    if(!containsKey((K) key)){
        throw new NoSuchElementException();
    }

    ReturnObject oldEntry = new ReturnObject(null);
    SortedTreeMap<K, V> newRoot = removeEntry(getRoot(this), (K) key, oldEntry);

    if(newRoot != null){
        setRoot(newRoot.data);
        setLeftChild(newRoot.leftChild);
        setRightChild(newRoot.rightChild);
    } else{
        this.data = null;
    }

    if(oldEntry.get() != null){
        size--;
    }


    return oldEntry.get();
}

/*
*   Removes the entry in a given root node of a subtree.
*   rootNode is the root node of the subtree.
*   Returns the root node of the revised subtree.
*
* */
private SortedTreeMap<K,V> removeFromRoot(SortedTreeMap<K, V> rootNode){
    if(rootNode.hasLeftChild() && rootNode.hasRightChild()){
        SortedTreeMap<K,V> leftSubtreeRoot = rootNode.getLeftChild();
        SortedTreeMap<K,V> largestNode = findLargest(leftSubtreeRoot);

        rootNode.setRoot(largestNode.getRoot());

        rootNode.setLeftChild(removeLargest(leftSubtreeRoot));
    } else if(rootNode.hasRightChild()) {
        rootNode = rootNode.getRightChild();
    } else{
        rootNode = rootNode.getLeftChild();
    }
    return rootNode;
}

/*
*   Finds the node containing the largest entry in a given tree.
*   rootNode is the root node of the tree.
*   Returns the node containing the largest entry in the tree.
*
* */
private SortedTreeMap<K,V> findLargest(SortedTreeMap<K, V> rootNode){
    if(rootNode.hasRightChild()){
        rootNode = findLargest(rootNode.getRightChild());
    }
    return rootNode;
}

/*
*   Removes the node containing the largest entry in a given tree.
*   rootNode is the root node of the tree.
*   Returns the root node of the revised tree.
*
* */
private SortedTreeMap<K,V> removeLargest(SortedTreeMap<K, V> rootNode){
    if(rootNode.hasRightChild()){
        SortedTreeMap<K,V> rightEntry = rootNode.getRightChild();
        rightEntry = removeLargest(rightEntry);
        rootNode.setRightChild(rightEntry);
    } else{
        rootNode = rootNode.getLeftChild();
    }
    return rootNode;
}

/*
*   Removes an entry from the tree rooted at a given node.
*   rootNode is a reference to the root of a tree.
*   entry is the object to be removed.
*   oldEntry is an object whose data field is null
*   Returns the root node of the resulting tree; if entry matches
*        an entry in the tree, oldEntry's data field is the entry
*        that was removed from the tree; otherwise it is null.
*       */
private SortedTreeMap<K,V> removeEntry(SortedTreeMap<K,V> rootNode, K entry, ReturnObject oldEntry){
    if(rootNode != null){
        K rootData = rootNode.data.key;
        int comparison = entry.compareTo(rootData);

        if(comparison == 0){
            oldEntry.set(rootNode.data.value);
            rootNode = removeFromRoot(rootNode);
        } else if(comparison < 0){
            SortedTreeMap<K,V> leftEntry = rootNode.getLeftChild();
            SortedTreeMap<K,V> subtreeRoot = removeEntry(leftEntry, entry, oldEntry);
            rootNode.setLeftChild(subtreeRoot);
        } else{
            SortedTreeMap<K,V> rightEntry = rootNode.getRightChild();
            rootNode.setRightChild(removeEntry(rightEntry, entry, oldEntry));
        }
    }
    return rootNode;
}

我得到的错误信息是:

/**
 * Check that entry is no longer in map after remove
 */
public Property remove_removes_entry() {
    return property(isKVList,
            kvs -> implies(kvs.length() > 0,
                    () -> property(choose(0, kvs.length()-1),
                            i -> {
                                P2<Integer, String> entry = kvs.index(i);

                                SortedTreeMap<Integer, String> tm = new SortedTreeMap<>(intOrd.toComparator());
                                kvs.foreachDoEffect(kv -> tm.add(kv._1(), kv._2()));

                                tm.remove(entry._1());
                                List<Integer> keys = fromIterator(tm.keys().iterator());
                                return prop(!keys.exists(key -> key.equals(entry._1())));
                            })
            )
    );
}

我可以进入0到60次传递,但我没有得到remove方法失败的情况。我试图进行Junit测试,但没有成功。

以下是我班级的更多背景信息:

java.lang.Error: Falsified after 2 passed tests with arguments: List(List((0,gzprxt),(4,lntpqj),(-5,caki),(-6,jzf)),2)

0 个答案:

没有答案