Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在给定空终止符的情况下旋转二叉树递归_Java_Binary Search Tree - Fatal编程技术网

Java 如何在给定空终止符的情况下旋转二叉树递归

Java 如何在给定空终止符的情况下旋转二叉树递归,java,binary-search-tree,Java,Binary Search Tree,我正在从文件中读取字符串,每当节点的子节点为空时,我都会记录“~”。然后我将这些字符串添加到二叉树中。但我的代码只是将所有字符串(包括“~”)添加到左树节点中 如何让算法在到达“~”时停止添加左侧节点并插入右侧节点(当然,除非下一个字符串也是“~”) 这是我的密码: // Reads the elements in the tree in pre-order public void fromFile() { BufferedReader in; String s = null;

我正在从文件中读取字符串,每当节点的子节点为空时,我都会记录“~”。然后我将这些字符串添加到二叉树中。但我的代码只是将所有字符串(包括“~”)添加到左树节点中

如何让算法在到达“~”时停止添加左侧节点并插入右侧节点(当然,除非下一个字符串也是“~”)

这是我的密码:

// Reads the elements in the tree in pre-order
public void fromFile()
{
   BufferedReader in;
   String s = null;
   try {
        in = new BufferedReader(new FileReader("animal_game.txt"));
        StringBuffer stringBuffer = new StringBuffer();
        while( (s=in.readLine()) != null )
        {

            stringBuffer.append(s);
            stringBuffer.append("\n");

        }

        fromFile(stringBuffer.toString());


        in.close();
   } 
   catch (IOException ex) 
   {
           Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
   } 

}

public void fromFile(String s)
{
    if (root == null)
    {
        root = new Node<>((T)s);
        size++;
    } 
    else 
    {
        fromFile(root, s);   
    }
}

// helper function
private void fromFile( Node<T> node, String s) 
{
        // if null tree node reached, 
        if(s==NULL_TREE_NODE)
        {
            fromFile(node, s);
        }

        // insert left node
        if (node.no == null) 
        {
            node.no = new Node<>((T)s);
        } 
        else 
        {
            fromFile(node.no, s);
        }

        // insert right node
        if (node.yes == null) 
        {
            node.yes = new Node<>((T)s);
        } 
        else{
            fromFile(node.yes, s);
        }
}
//按预定顺序读取树中的元素
public void fromFile()
{
缓冲读取器;
字符串s=null;
试一试{
in=新的BufferedReader(新的文件阅读器(“animal_game.txt”);
StringBuffer StringBuffer=新的StringBuffer();
而((s=in.readLine())!=null)
{
stringBuffer.append;
stringBuffer.append(“\n”);
}
fromFile(stringBuffer.toString());
in.close();
} 
捕获(IOEX异常)
{
Logger.getLogger(Tree.class.getName()).log(Level.SEVERE,null,ex);
} 
}
公共void fromFile(字符串s)
{
if(root==null)
{
根=新节点((T)s);
大小++;
} 
其他的
{
fromFile(root,s);
}
}
//辅助函数
私有void fromFile(节点,字符串s)
{
//如果到达空树节点,
if(s==NULL\u树\u节点)
{
fromFile(节点,s);
}
//插入左节点
if(node.no==null)
{
node.no=新节点((T)s);
} 
其他的
{
fromFile(node.no,s);
}
//插入右节点
如果(node.yes==null)
{
node.yes=新节点((T)s);
} 
否则{
fromFile(node.yes,s);
}
}
这是我将树保存到文件的代码:

// Writes the elements in the tree in pre-order
public void toFile()
{
    // Writes in preorder starting with the root node
    if (root != null) 
    {
        BufferedWriter out;
        try {
            out = new BufferedWriter(new FileWriter("animal_game.txt"));
            toFile(out, root);
            out.close();
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
}

// Helper function
private void toFile(BufferedWriter out, Node<T> node) 
{
    try {

        if (node == null) {
        out.write(NULL_TREE_NODE); // null
        out.newLine();
        return;
    }
    //assert !node.data.equals(NULL_TREE_NODE); // Reserver for us..
    out.write((String)node.data); // these nodes hold Strings
    out.newLine();
    toFile(out, node.no);
    toFile(out, node.yes);

    } 
    catch (IOException ex) 
    {
        Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
    }

}
//按预先顺序写入树中的元素
公共文件
{
//以根节点开始的预顺序写入
if(root!=null)
{
缓冲写入输出;
试一试{
out=new BufferedWriter(new FileWriter(“animal_game.txt”);
toFile(out,root);
out.close();
} 
捕获(IOEX异常)
{
Logger.getLogger(Tree.class.getName()).log(Level.SEVERE,null,ex);
} 
}
}
//辅助函数
私有void-toFile(BufferedWriter-out,节点)
{
试一试{
if(node==null){
out.write(NULL_TREE_NODE);//NULL
out.newLine();
返回;
}
//assert!node.data.equals(NULL_TREE_node);//为我们保留服务器。。
out.write((String)node.data);//这些节点包含字符串
out.newLine();
toFile(out,node.no);
toFile(out,node.yes);
} 
捕获(IOEX异常)
{
Logger.getLogger(Tree.class.getName()).log(Level.SEVERE,null,ex);
}
}
这是我的档案


它是哺乳动物吗

它是爬行动物吗

它是鱼吗

鹈鹕

~

~

鲨鱼

~

~

它灭绝了吗

乌龟

~

~

速度捕捉器

~

~

它有毛皮吗

~

~


Cat

您应该将fromFile()更改为与toFile()匹配:与其接受代表整个文件的节点和字符串,不如使用BufferedReader,使其能够轻松地读取单独的行。此外,将helper build函数更改为返回节点,以便在~节点的情况下可以返回null。 然后,可以递归地构建整个树,当到达~个节点时,返回null返回树:

 private Node<T> fromFile(BufferedReader s) throws IOException
    {

            String line = s.readLine();
            if(line == null) throw new IllegalArgumentException("File does not specify complete tree");
            // if null tree node reached, 
            if(line.equals(NULL_TREE_NODE)) return null;
            Node<T> node = new Node<>();
            node.data = line;
            node.no = fromFile(s);
            node.yes = fromFile(s);
            return node;
    }
我还对它进行了修改,使用了try with resources语句,以方便使用,并保证在出现异常时正确释放资源。看


希望这有帮助。

您能给我们完整的文件吗?这个过程的总体目标是什么?目前,fromFile(Node,String)似乎总是在不向节点实际添加任何信息的情况下终止,除非s==NULL\u TREE\u Node,在这种情况下,它只调用自身导致堆栈溢出?另外,您是否打算使用==表示字符串相等?这是我的树类中的一个函数。我主要用treename.fromFile()调用它;但是这会创建一个包含所有左节点(包括“~”字符)的树,如果s==NULL\u tree\u NODE,我该如何调用它的父节点?我的意思是你能给我们展示一下整个树类吗。我认为它正在生成所有左节点,因为它首先检查node.no(left)是否为null。我仍然不知道字符串数据是如何进入树的。首先将所有对象放在一个字符串中是一个好主意吗?用换行符分隔列表是否更有意义?(您已经获得了读者提供的信息)。当到达空树节点时,不应该“调用父节点”,而应该返回,因为如果父节点的结构正确,您最初是从父节点调用的。
public void fromFile()
{
   try(BufferedReader in = new BufferedReader(new FileReader("animal_game.txt"))) 
   {
        root = fromFile(in);
   } 
   catch (IOException ex) 
   {
           Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
   } 

}