【问题标题】:Implementing a JUnit test onto a Binary Search Tree that doesn't have an iterator?在没有迭代器的二叉搜索树上实现 JUnit 测试?
【发布时间】:2018-04-25 03:29:51
【问题描述】:

我一直致力于在没有明确具有迭代器的二叉搜索树上实现 JUnit 测试。代替迭代器,这棵树有一个名为 getNext() 的方法,我假设与 while 循环一起,我可以测试树而不会在测试中出错。问题是我不确定如何在我的测试中实现 while 循环以使我的测试正常工作。

public class BinarySearchTree<T extends Comparable<T>> implements 
BSTInterface<T> {
protected BSTNode<T> root;

boolean found;

protected LinkedUnbndQueue<T> inOrderQueue; // queue of info
protected LinkedUnbndQueue<T> preOrderQueue; // queue of info
protected LinkedUnbndQueue<T> postOrderQueue; // queue of info

public BinarySearchTree()
{
    root = null;
}

private T recGet(T element, BSTNode<T> tree)
{
    if (tree == null)
        return null; // element is not found
    else if (element.compareTo(tree.getInfo()) < 0)
        return recGet(element, tree.getLeft()); // get from left subtree
    else if (element.compareTo(tree.getInfo()) > 0)
        return recGet(element, tree.getRight()); // get from right subtree
    else
        return tree.getInfo(); // element is found
}

public T get(T element)
// Returns an element e from this BST such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
    return recGet(element, root);
}

private T getPredecessor(BSTNode<T> tree)
// Returns the information held in the rightmost node in tree
{
    while (tree.getRight() != null)
        tree = tree.getRight();
    return tree.getInfo();
}

private void inOrder(BSTNode<T> tree)
// Initializes inOrderQueue with tree elements in inOrder order.
{
    if (tree != null) {
        inOrder(tree.getLeft());
        inOrderQueue.enqueue(tree.getInfo());
        inOrder(tree.getRight());
    }
}

private void preOrder(BSTNode<T> tree)
// Initializes preOrderQueue with tree elements in preOrder order.
{
    if (tree != null) {
        preOrderQueue.enqueue(tree.getInfo());
        preOrder(tree.getLeft());
        preOrder(tree.getRight());
    }
}

private void postOrder(BSTNode<T> tree)
// Initializes postOrderQueue with tree elements in postOrder order.
{
    if (tree != null) {
        postOrder(tree.getLeft());
        postOrder(tree.getRight());
        postOrderQueue.enqueue(tree.getInfo());
    }
}

public T getNext(int orderType)
{
    if (orderType == INORDER)
        return inOrderQueue.dequeue();
    else if (orderType == PREORDER)
        return preOrderQueue.dequeue();
    else if (orderType == POSTORDER)
        return postOrderQueue.dequeue();
    else
        return null;
}

我已经从树中删除了常用的 add、remove 和 contains 方法以节省空间。我正在运行的测试使用 TestCase 和 TestRunner。到目前为止,我所拥有的测试只是简单的添加、删除、调整大小和包含测试。任何意见将不胜感激。

【问题讨论】:

  • 您是在问如何测试getNext?调用它会返回一个LinkedUnbndQueue。什么是LinkedUnbndQueue。为什么不通过调用LinkedUnbndQueue 上的方法来断言内容?
  • @user7 不,我正在尝试对树进行任何测试。我的问题与如何让测试运行有关。因为这棵树没有明确地有一个迭代器,所以我很难弄清楚它,但我相信我需要在测试类中使用 getNext 方法和一个 while 循环来让它充当一个迭代器,但我不确定如何做到这一点。
  • 在所有元素耗尽后调用LinkedUnbndQueue.dequeue 会发生什么。它会一直返回 null 吗?
  • @user7 会一直返回null,但是用户在程序完成后结束。使用此搜索树的程序是围绕用户输入构建的,这使得测试更加困难,尤其是当我还在学习如何有效地使用 JUnit 时。

标签: java junit iterator binary-tree binary-search-tree


【解决方案1】:

构建树后,您的测试中将有一个 BinarySearchTree&lt;SomeType&gt; 类型的对象。

然后你可以调用 getNext 直到它返回 null(正如你在 cmets 中所说的那样)

BinarySearchTree<SomeType> tree;
...
SomeType data;
while ((data = tree.getNext()) != null) {
    //do whatever assertion you want to do with the returned data.
}

【讨论】:

  • 我仍然不确定我是否正确理解了您的需求。我很高兴根据您的输入/cmets 进行改进
猜你喜欢
  • 2011-06-02
  • 2017-05-09
  • 2013-11-06
  • 1970-01-01
  • 2020-10-06
  • 2012-07-11
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
相关资源
最近更新 更多