【问题标题】:Check if a binary tree is a binary search tree using a static method使用静态方法检查二叉树是否为二叉搜索树
【发布时间】:2016-04-26 03:24:38
【问题描述】:

我必须创建一个静态方法来检查给定的树是否是二叉搜索树。它以BinaryTree<String> 作为参数,每个节点只能接触一次。

我以前用数字填充树,但数据类型为字符串,现在我将它们切换为字母,因为有些人认为我想使用整数。

我遇到的问题是,在 tree4 上执行我的 isBST() 方法时,布尔 bst 没有被触发。

到目前为止,我所拥有的是:

public class BinarySearchTree < T extends Comparable < ? super T >> {
    public static boolean isBST(BinaryTree<String> tree) {
        boolean bst = true;

        if (tree.getRootData() != null) {
            Iterator<String> iterator = tree.getInorderIterator();
            String prev = iterator.next();

            while (bst && iterator.hasNext()) {
                String next = iterator.next();
                System.out.println(next); // Debug purposes only
                if (prev.compareTo(next) > 0) {
                    bst = false;
                }
            }
        }
        return bst;
    }
}

对于我的测试用例,我有这个:

public class Test {
    public static void main(String[] args) {
        BinarySearchTree<String> tree1 = new BinarySearchTree<String>();
        BinarySearchTree<String> tree2 = new BinarySearchTree<String>();

        BinaryTree<String> h           = new BinaryTree<String>("h");
        BinaryTree<String> g           = new BinaryTree<String>("g");
        BinaryTree<String> f           = new BinaryTree<String>("f");
        BinaryTree<String> e           = new BinaryTree<String>("e", f, g);
        BinaryTree<String> d           = new BinaryTree<String>("d");

        BinaryTree<String> a           = new BinaryTree<String>("a");
        BinaryTree<String> b           = new BinaryTree<String>("b");

        BinaryTree<String> tree3       = new BinaryTree<String>("c", a, e);
        BinaryTree<String> tree4       = new BinaryTree<String>("c", a, b);

        System.out.println("BinarySearchTree.isBST(tree3): " + BinarySearchTree.isBST(tree3));
        System.out.println("BinarySearchTree.isBST(tree4): " + BinarySearchTree.isBST(tree4));
    }
}

这将返回以下输出:

c
f
e
g
BinarySearchTree.isBST(tree3): true
c
b
BinarySearchTree.isBST(tree4): true

当我从我的静态方法中打印出 compareTo 语句时,我看到第二棵树 (tree4) 在遇到 b 时返回 -1。这就是为什么对于tree4,它返回true,因为布尔值没有被触发。

对此有何建议?

【问题讨论】:

  • 您可以在发现失败后立即通过return false 终止迭代。无需继续。
  • 事实上,只要 bst 标志设置为 false(while 循环中的条件),他就会终止循环,但是是的,然后返回,并且有更多的优化。
  • 伙计们,我要编辑我的问题以显示我已经完成的一些调试
  • 你还没有纠正你原来的错误,(我之前提到过),你永远不会更新“prev”,它总是设置为中序遍历中的第一个节点,所有其他节点在然后将遍历与第一个节点进行比较。一旦你完成使用 (compareTo) 设置 prev 到下一个和 iterator.next() 的检查条件

标签: java data-structures tree binary-tree inorder


【解决方案1】:

String 的 compareTo() 函数根据 String 的字母顺序(自然顺序)而不是实际数值(您要比较的)。
比较整数的字符串值没有多大意义。

无论如何,在您的示例中 - 您需要在执行时更新“prev”,在您的示例树中,每个中序后继都与 4 进行比较(因为它从未更新)并且所有值都大于 4,所以 true 是回来。

在另一种情况下,(当您将 9 更改为 10 时)按字典顺序,10

改用BinaryTree&lt;Integer&gt;tree,您的解决方案应该可以工作

【讨论】:

  • 按字母顺序排列 '5' '7'。在 ASCII 中,0x35
  • Yups 对其进行了编辑以指定正确的示例。谢谢!
  • 一个“正确的例子”并没有解释他在值 4,5,6,7,9 上实际遇到的问题,也没有解释它有效的事实有 10 个。
  • 对不起各位!我应该更清楚地说明我需要使用字符串。我正在比较整数的字符串值。
  • 比较整数的字符串值,没有多大意义,无论如何,你需要更新'prev',在你的示例树中执行时,每个中序后继都与4进行比较(因为它从来没有更新)并且所有值都大于4,因此返回true。从字典上看,10
【解决方案2】:

添加到 Parthas 答案

    public static void main (String[] args) {
    String ten = "10";
    String nine = "9";
    System.out.println(nine.compareTo(ten));
    System.out.println(ten.compareTo(nine));
}

生产:

8

-8

与你想要的相反。

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 1970-01-01
    • 2012-06-05
    • 2017-09-28
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多