二叉树Java代码

时间:2014-07-26 13:35:34

标签: java data-structures

我编写以下代码来添加节点并在二叉树中找到它们但是在执行它时没有返回结果,甚至没有返回错误消息所以请告诉我我的代码与我的代码有什么关系如果有任何帮助,请让我失望

这是我的代码:

public class BinaryTree {
    Node root;

    public void addnode(int key, String name) {
        Node newNode = new Node(key, name);
        if (root == null) {
            root = newNode;
        } else {
            Node focusNode = root;
            Node parent;

            while (true) {
                parent = focusNode;
                if (key < focusNode.key) {
                    focusNode = focusNode.leftChild;
                    if (focusNode == null) {
                        parent.leftChild = newNode;
                        return;
                    } else {
                        focusNode = focusNode.rightChild;
                        if (focusNode == null) {
                            parent.rightChild = newNode;
                            return;
                        }
                    }

                }
            }
        }
    }

    public Node findNode(int key) {
        Node focusNode = root;
        while (focusNode.key != key) {

            if (key < focusNode.key) {

                focusNode = focusNode.leftChild;
            } else {

                focusNode = focusNode.rightChild;
            }
            if (focusNode == null)
                return null;

        }
        return focusNode;

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BinaryTree theTree = new BinaryTree();

        theTree.addnode(1, "Ahmad");
        theTree.addnode(2, "Ali");
        theTree.addnode(3, "Sara");
        theTree.addnode(4, "Abed");
        theTree.addnode(5, "Mohammad");

        System.out.println("Search for 4");
        System.out.println(theTree.findNode(4));

    }
}

class Node {
    int key;
    String name;

    Node leftChild;
    Node rightChild;

    Node(int key, String name) {
        this.key = key;
        this.name = name;
    }

    public String toString() {
        return name + "  has a key " + key;
    }
}

2 个答案:

答案 0 :(得分:0)

问题在于将大括号放在错误的位置。只需用这个替换addnode方法

public void addnode(int key, String name) {
    Node newNode = new Node(key, name);
    if (root == null) {
        root = newNode;
        System.out.println("Inserted : "+newNode);
    } else {
        Node focusNode = root;
        Node parent;

        while (true) {
            parent = focusNode;
            if (key < focusNode.key) {
                focusNode = focusNode.leftChild;
                if (focusNode == null) {
                    parent.leftChild = newNode;
                    System.out.println("Inserted : "+newNode);
                    return;
                }
            }
            else {
                    focusNode = focusNode.rightChild;
                    if (focusNode == null) {
                        parent.rightChild = newNode;
                        System.out.println("Inserted : "+newNode);
                        return;
                    }
            }
        }
    }
}

答案 1 :(得分:0)

问题是下面的else应附加到if (key < focusNode.key)部分。

else {
    focusNode = focusNode.rightChild;
    if (focusNode == null) {
        parent.rightChild = newNode;
        return;
    }   
}

您所做的(可能是错误的)是您在if (focusNode == null) {之后写的,这意味着您的代码根本不会处理key > focusNode.key。修复后,您的代码将正常运行。

现在,重要的部分是如何找到自己这个错误。您可以在while循环的开头添加几个System.out.println语句,并在每次迭代时检查几个变量的值。显然,如果使用IDE,则可以设置断点并以此方式进行调试。