比较器不适用于二叉树

Comparator doesn't work with Binary Tree

我有这个东西

Comparator.java

public interface Comparator<T> {
    public int compareTo(int num);
}

值Comparator.java

public class valueComparator implements Comparator<Tree.Node> {
    @Override
    public int compareTo(Tree.Node obj, int number) {
        if (obj.getDataNumber() == number) {
            return 0;
        }
        else if (obj.getDataNumber() < number) {
            return -1;
        }
        else return 1;
    }
}

Tree.java

public class Tree {
    public Node root;

    Tree() {
    }

    public static class Node {

        Node(int number, String str, boolean flag) {
            dataNumber = number;
            dataText = str;
            dataBool = flag;
        }

        public int getDataNumber() {
            return this.dataNumber;
        }

        public String getDataText() {
            return this.dataText;
        }

        public boolean getDataBool() {
            return this.dataBool;
        }

        public void setDataText(String text) {
            this.dataText = text;
        }

        public void isDataBool(boolean flag) {
            this.dataBool = flag;
        }

        Node left;
        Node right;
        private int dataNumber;
        private String dataText;
        private boolean dataBool;

    }

    public void binaryTree() {
        root = null;
    }

    public boolean search(int number) {
        return search(root, number);
    }

    valueComparator comp = new valueComparator();

    private boolean search(Node node, int number) {
        if (node == null) {
            return false;
        }
        if (comp.compareTo(node, number) == 0) {
            return true;
        }
        if (comp.compareTo(node, number) == -1) {
            return search(node.left, number);
        }
        else {
            return search(node.right, number);
        }
    }  

    public void insertLeaf(int number, String str, boolean flag) {
    root = insertLeaf(root, number, str, flag);
    }
    private Node insertLeaf(Node node, int number, String str, boolean flag) {
        if (node == null) {
            node = new Node(number, str, flag);
        } else {
            if (number < node.dataNumber) {
                node.left = insertLeaf(node.left, number, str, flag);
            }
            else if (number > node.dataNumber) {
                node.right = insertLeaf(node.right, number, str, flag);
            }
            else {
                System.out.println("The element is already in the tree.");
            }
        }
        return node;
    }
}

Test.java

public class Test {
    public static void main(String args[]) {

        Tree binTree = new Tree();
        binTree.binaryTree();
        binTree.insertLeaf(5, "text2", true);
        binTree.insertLeaf(4, "text4", false);
        binTree.insertLeaf(1, "text1", true);
        binTree.insertLeaf(3, "text3", true);
        binTree.insertLeaf(2, "text5", false);
        System.out.println("Element 3 found: " + binTree.search(3));
    // Element 3 found: false
    }
}

我应该用比较器进行搜索,但我不明白其中的逻辑。 compareTo 方法本身有效,但它停留在搜索的递归调用中。第一遍后,如果compareTo的return不=0,则进入null,跳出递归,return为false。意思是,如果我将树的第一个元素设置为“3”,search(3) 将为 return true,但如果它不同于 3 - false,甚至不会在树中查找它。

当您 插入 一个数字时,您直接将它与节点的值进行比较,如果该数字小于存储在当前节点中的值,您将遵循 left 指针。

然而,当您搜索一个数字时,您使用了一个比较器,它将节点的值与给定的数字进行比较(注意相反的顺序!)如果数字小于比当前节点中的值,你遵循 right link.

根据需要使用直接比较或比较器——但在任何地方都使用相同的方法。