【问题标题】:How i can convert this input String to a BST(Binary-search-tree)-JAVA?我如何将此输入字符串转换为 BST(二进制搜索树)-JAVA?
【发布时间】:2019-10-01 18:22:48
【问题描述】:

如果输入字符串是有效或无效的 BST(二叉搜索树),我必须返回 true 或 false。

问题是我输入了一个带有这种“模式”的非典型字符串:(ROOT(LEFT,RIGHT)); 每个元素之间有一个空格,ROOT 是中心节点,LEFT 是左边的节点,RIGHT 是右边的节点。如果一个节点为空,则编码为“-”,一个叶子用数值编码,如果一个节点有一些孩子,则以这种方式编码:例如B有两个孩子(D在左边,E在右边)所以( A ( B ( D , E ) , C ) )

我试图将字符串拆分为一个字符串数组,但我不知道如何填充树并同时检查它是否是 BST。我如何用这个字符串填充树?

我会尝试这个算法:

//我如何拆分字符串

 String[] albero = b.readLine().split("\\,|\\(|\\)|\\-");

//我将如何检查是否是 BST

public class IsBST {

    public boolean isBST(Node root){
        return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    private boolean isBST(Node root, int min, int max){
        if(root == null){
            return true;
        }
        if(root.data <= min || root.data > max){
            return false;
        }
        return isBST(root.left, min, root.data) && isBST(root.right, root.data, max);
    }

例子:

输入 1:(100(19(17(2、7)、3)、36(25、1)))

输出 1:假

输入 2: ( 8 ( 3 ( 1 , 6 ( 4 , 7 ) ) , 10 ( - , 14 ( 13 , - ) ) ) )

输出 2:真

【问题讨论】:

    标签: java string binary-search-tree


    【解决方案1】:

    要填充 BST,您将从拆分的字符串中获取一个元素(哪个无关紧要,将其作为第一个元素可能是最简单的)并将其添加到一个空的(新创建的)BST 中,然后将左侧和右子树等于'-'。然后,您可以创建一个 add 函数,该函数在根为“-”时具有基本情况,并将需要插入的元素放入根中,并将树的左右子节点设置为带有“-”的节点。递归步骤将是调用左子树(如果小于则),或者右子树(如果大于则)。例如:

    public void add (Node root, Node nodeToAdd){
    if (root=='-'){
    root=nodeToAdd;
    root.left= new Node('-');
    root.right=new Node('-');
    }
    if else (nodeToAdd<root) add(root.left, nodeToAdd);
    else add(root.right, nodeToAdd);
    }
    

    所以这将向 BST 添加一个元素并保留二进制属性,因为每个节点都有 2 个子节点(所有叶子都是“-”)。如果你只是通过你的整个字符串运行这个算法,它应该填充一个 BST。注意语法会有所不同,但上面函数的思想应该是一样的。

    【讨论】:

    • 非常感谢,现在我必须从主字符串中填充 BST,检查我的答案。
    【解决方案2】:

    这是我的方法:

    首先,我们将字符串拆分成[int, (, int (or -), int (or -), ...]格式的数组:

    String[] arr = str.substring(1, str.length()-1)
                    .replaceAll("[^0-9-,()]","")
                    .split(",|(?<=\\()|(?=\\()|(?<=\\))|(?=\\))");
    

    我们使用:

    • .substring(1, str.length()-1) 删除括号 字符串的开头和结尾(因为它没用)。
    • .replaceAll("[^0-9-,()]","") 删除每个字符除了 integers-,()
    • .split(",|(?&lt;=\\()|(?=\\()|(?&lt;=\\))|(?=\\))"),)( 分割字符串我们保留括号

    使用输入( 8 ( 3 ( 1 , 6 ( 4 , 7 ) ) , 10 ( - , 14 ( 13 , - ) ) ) ) 运行上述拆分过程将产生:

    String[] array = [8, (, 3, (, 1, 6, (, 4, 7, ), ), 10, (, -, 14, (, 13, -, ), ), )]

    根据需要。


    现在,剩下的就很简单了。对于每个右括号,可以从以下位置构造一个树节点:root, (, left, right, )。 实现此目的的一种方法是使用Stack:对于array 中的每个字符串,如果该字符串是数字或字符-,我们构造一个新节点并将其压入堆栈。每当有右括号时,堆栈上的前 3 个节点是:

    right
    left
    root
    

    完整的实现:

    public Node parseTree(String str){
        Stack<Node> nodes = new Stack<>();
    
        String[] arr = str.substring(1, str.length()-1).replaceAll("[^0-9-,()]","")
            .split(",|(?<=\\()|(?=\\()|(?<=\\))|(?=\\))");
    
        for (int i = 0; i < arr.length; i++){
            String c = arr[i];
            if (c.equals(")")){
                Node right = nodes.pop();
                Node left = nodes.pop();
                Node root = nodes.peek();
                root.left = left;
                root.right = right;
            }
            else if (c.equals("("))
                continue;
            else 
                nodes.push(c.equals("-") ? null : new Node(Integer.parseInt(c)));
        }
    
        return nodes.pop();
    }
    

    Node定义为:

    class Node{
        Node left;
        Node right;
        int data;
        public Node(int data){
            this.data = data;
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 非常感谢。现在我必须自动插入节点,检查我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2018-05-18
    • 2017-03-03
    • 2016-01-14
    • 2015-06-21
    • 2014-10-24
    • 2013-01-30
    相关资源
    最近更新 更多