我将如何搜索我的二叉树以找到目标?

问题描述 投票:0回答:1

我一直在这段代码上工作了一段时间,没有运气。我知道我需要通过这种方法来传递我的树,但是我不确定如何做到这一点。

我认为我的算法是正确的,但是无论我输入什么内容,它都会告诉我“ null”。任何反馈都可以提供帮助,我认为它一直返回null,因为我不知道如何将树传递给该方法。

下面,我的find2方法应该使用我的add2方法创建的树来找到选定的节点。这也包括我的主要方法。

这里是代码:

public boolean add2(E item) {
    root = add2(root, item);
    return addReturn;
}

private Node<E> add2(Node<E> localRoot, E item){
    if(localRoot == null) {
        //Adding item if tree is empty...
        addReturn = true;
        return new Node<>(item);
    } else if (item.compareTo(localRoot.data) == 0) {
        //Returning item if equal to root...
        addReturn = false;
        return localRoot;
    } else if (item.compareTo(localRoot.data) < 0) {
        //Adding item to right side of tree if less than the root...
        localRoot.right = add(localRoot.right, item);
        return localRoot;
    } else {
        //Adding item to left side of tree if grater than the root...
        localRoot.left = add(localRoot.left, item);
        return localRoot;
    }
}

//wrapper method
public E find2(E target) {
    return find2(root, target);
}

//recursive method
private E find2(Node<E> localRoot, E target) {
    if(localRoot == null) {
        return null;
    }
    int compResult2 = target.compareTo(localRoot.data);

    if(compResult2 == 0) {
        return localRoot.data;
    } else if (compResult2 < 0) {
        return find2(localRoot.right, target);
    } else {
        return find2(localRoot.left, target);
    }
}

public class BinarySearchTreeTest {

public static void main(String[] args) {

    //Object creation
    BinarySearchTree<Integer> tree = new BinarySearchTree<>();

    //Tree building...
    tree.add2(50);
    tree.add2(70);
    tree.add2(80);
    tree.add2(60);
    tree.add2(30);
    tree.add2(40);
    tree.add2(20);

    //Output tree
    System.out.println("\nThe built binary search tree:");
    System.out.println("\n" + tree.toString());

    System.out.println("\nSearch the node with data 60: " + tree.find2(60));
    System.out.println("Search the node with data 65: " + tree.find2(65));
    System.out.println("Search the node with data 20: " + tree.find2(20));
    System.out.println("Search the node with data 25: " + tree.find2(25));

}

}

java data-structures binary-search-tree
1个回答
0
投票

我尝试到本地,将add2方法调用更改为add2而不是add

        static class BinarySearchTree<Item extends Comparable> {

        Node root;
        boolean addReturn;

        class Node {
            Item data;
            Node left;
            Node right;

            public Node(Item e) {
                data = e;
            }
        }

        public boolean add2(Item item) {
            root = add2(root, item);
            return addReturn;
        }

        private Node add2(Node localRoot, Item item) {
            if (localRoot == null) {
                // Adding item if tree is empty...
                addReturn = true;
                return new Node(item);
            } else if (item.compareTo(localRoot.data) == 0) {
                // Returning item if equal to root...
                addReturn = false;
                return localRoot;
            } else if (item.compareTo(localRoot.data) < 0) {
                // Adding item to right side of tree if less than the root...
                localRoot.right = add2(localRoot.right, item);
                return localRoot;
            } else {
                // Adding item to left side of tree if grater than the root...
                localRoot.left = add2(localRoot.left, item);
                return localRoot;
            }
        }

        // wrapper method
        public Item find2(Item target) {
            return find2(root, target);
        }

        // recursive method
        private Item find2(Node localRoot, Item target) {
            if (localRoot == null) {
                return null;
            }
            int compResult2 = target.compareTo(localRoot.data);
            if (compResult2 == 0) {
                return localRoot.data;
            } else if (compResult2 < 0) {
                return find2(localRoot.right, target);
            } else {
                return find2(localRoot.left, target);
            }
        }
    }

    public static void main(String[] args) {

        // Object creation
        BinarySearchTree<Integer> tree = new BinarySearchTree<>();

        // Tree building...
        tree.add2(50);
        tree.add2(70);
        tree.add2(80);
        tree.add2(60);
        tree.add2(30);
        tree.add2(40);
        tree.add2(20);

        // Output tree
        System.out.println("\nThe built binary search tree:");
        System.out.println("\n" + tree.toString());

        System.out.println("\nSearch the node with data 60: " + tree.find2(60));
        System.out.println("Search the node with data 65: " + tree.find2(65));
        System.out.println("Search the node with data 20: " + tree.find2(20));
        System.out.println("Search the node with data 25: " + tree.find2(25));

    }

,并给我输出:

Search the node with data 60: 60
Search the node with data 65: null
Search the node with data 20: 20
Search the node with data 25: null
© www.soinside.com 2019 - 2024. All rights reserved.