无法搜索二叉搜索树

时间:2017-11-13 19:08:57

标签: java binary-search-tree

尝试搜索二进制搜索树,但似乎我无法弄清楚为什么where语句不起作用,该方法调用String的输入,但它表示需要一个Node。有任何建议如何解决这个问题?

这是节点类:

public class Node
{
    String key;
    Node left, right;

    public Node(String entry)
    {
        key = entry;
        left = right = null;
    }

    public Node getLeft()
    {
        return left;
    }

    public Node getRight()
    {
        return right;
    }

    public String getKey(String entry)
    {
        if(this.key.equals(key))
        {
            return key;
        }

        if(entry.compareTo(this.key) < 0)
        {
            return left == null ? null : left.getKey(entry);
        }
        else
        {
            return right == null ? null : right.getKey(entry);
        }
    }

    public void setLeft(Node left)
    {
        this.left = left;
    }

    public void setRight(Node right)
    {
        this.right = right;
    }
}

这是我的二进制搜索树:

public class BinarySearchTree
{
    Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public void insert(String key)
    {
        root = insertRec(root, key);
    }

    public Node insertRec(Node root, String key)
    {
        if(root == null)
        {
            root = new Node(key);
            return root;
        }

        if(key.compareTo(root.toString()) == -1)
        {
            root.setLeft(insertRec(root.getLeft(), key));
        }
        else if(key.compareTo(root.toString()) == 1)
        {
            root.setRight(insertRec(root.getRight(), key));
        }

        return root;
    }

    public Node search(String key)
    {
        return root == null ? null : root.getKey(key);
    }

    public void printPostOrder(Node node)
    {
        if(node == null)
            return;

        printPostOrder(root.getLeft());

        printPostOrder(node.getRight());

        System.out.print(node.getKey() + ", ");
    }

    public void printInOrder(Node node)
    {
        if(node == null)
            return;

        printInOrder(node.getLeft());

        System.out.print(node.getKey() + ", ");

        printInOrder(node.getRight());
    }

    public void printPreOrder(Node node)
    {
        if(node == null)
            return;

        System.out.print(node.getKey() + ", ");

        printPreOrder(node.getLeft());

        printPreOrder(node.getRight());
    }

    public void printPostOrder()
    {
        printPostOrder(root);
    }

    public void printInOrder()
    {
        printInOrder(root);
    }

    public void printPreOrder()
    {
        printPreOrder(root);
    }
}

如果您需要更多信息,请随时与我们联系。

1 个答案:

答案 0 :(得分:0)

怎么了? 方法getKey中的类节点中的代码错误将您的返回字符串更改为节点,并将if等于并返回此处。

实施例:。

public Node getKey(String entry)
{
    if(this.key.equals(entry))
    {
        return this;
    }
}

如果你改变了你的代码工作。

全班BinarySearchTree测试好了

 public class BinarySearchTree
 {
    Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public void insert(String key)
    {
        root = insertRec(root, key);
    }

    public Node insertRec(Node root, String key)
    {
        if(root == null)
        {
            root = new Node(key);
            return root;
        }

        if(key.compareTo(root.toString()) == -1)
        {
            root.setLeft(insertRec(root.getLeft(), key));
        }
        else if(key.compareTo(root.toString()) == 1)
        {
            root.setRight(insertRec(root.getRight(), key));
        }

        return root;
    }

    public Node search(String key)
    {
        return root == null ? null : root.getKey(key);
    }

    public void printPostOrder(Node node)
    {
        if(node == null)
            return;

        printPostOrder(root.getLeft());

        printPostOrder(node.getRight());

        System.out.print(node.getKey("") + ", ");
    }

    public void printInOrder(Node node)
    {
        if(node == null)
            return;

        printInOrder(node.getLeft());

        System.out.print(node.getKey("") + ", ");

        printInOrder(node.getRight());
    }

    public void printPreOrder(Node node)
    {
        if(node == null)
            return;

        System.out.print(node.getKey("") + ", ");

        printPreOrder(node.getLeft());

        printPreOrder(node.getRight());
    }

    public void printPostOrder()
    {
        printPostOrder(root);
    }

    public void printInOrder()
    {
        printInOrder(root);
    }

    public void printPreOrder()
    {
        printPreOrder(root);
    }

    public static void main (String args[]){
        BinarySearchTree bs = new BinarySearchTree();
        bs.insert("A");
        bs.search("A");
    }
 }

和节点完成

public class Node
{
    String key;
    Node left, right;

    public Node(String entry)
    {
        key = entry;
        left = right = null;
    }

    public Node getLeft()
    {
        return left;
    }

    public Node getRight()
    {
        return right;
    }

    public Node getKey(String entry)
    {
        if(this.key.equals(key))
        {
            return this;
        }

        if(entry.compareTo(this.key) < 0)
        {
            return left == null ? null : left.getKey(entry);
        }
        else
        {
            return right == null ? null : right.getKey(entry);
        }
    }

    public void setLeft(Node left)
    {
        this.left = left;
    }

    public void setRight(Node right)
    {
        this.right = right;
    }
}