在这个网站上基本上存在相同的问题,除了它说不要在这里提出问题是一个链接。 Binary Tree Recursive Function
我需要打印出一个看起来像这样但是任意大小的二叉树:
--------x-------
----x-------x---
--x---x---x---x-
-x-x-x-x-x-x-x-x
xxxxxxxxxxxxxxxx
然而,当我执行代码输出错误以及无尽的打印
:::X:::::X::X:XXXXX
并且在这下面有一条蓝线,我可以点击它,它会弹出一个窗口,上面写着“找不到来源”的无尽X的
at sun.nio.cs.SingleByte.withResult(Unknown Source)
at sun.nio.cs.SingleByte.access$000(Unknown Source)
at sun.nio.cs.SingleByte$Encoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByte$Encoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at BinaryBuilder.display(BinaryBuilder.java:25)
at BinaryBuilder.display(BinaryBuilder.java:31)
at BinaryBuilder.display(BinaryBuilder.java:31)
我到目前为止的代码是不正常工作,我遇到了递归和理解堆栈帧执行顺序的问题。请帮助我以为我是在正确的轨道上使用行从递归返回。我需要一些指导和推动正确的方向:)
import java.util.Scanner;
public class BinaryBuilder {
int levels = 0;
int width = 0;
int leaves = 0;
Scanner sn = new Scanner(System.in);
public BinaryBuilder() {
//prt("how many leaves?");
//leaves = sn.nextInt();
//levels = (int)Math.sqrt((double)leaves);
}
public void setLevelLeaves(int l,int le){
levels = l;
leaves = le;
}
public void display(int left, int right, int row){
int i =left;
int mid = (left+right)/2; //maybe a +1
if(row>levels){
return;
}
while(i <= right){
if(i==mid){
System.out.print("X");
}else{
System.out.print(":");
}
i++;
}
display(left, mid, row++);
display(mid, right, row++);
}
public void prt(String n){
System.out.println(n);
}
}
主要
public class PartBTest {
public PartBTest() {
}
public static void main(String[] args) {
BinaryBuilder bb = new BinaryBuilder();
//bb.prt("width will be reduced to a factor of 2");
bb.setLevelLeaves(3, 8);
bb.display( 0, bb.leaves-1, 0);
}
}
快乐编码:}
答案 0 :(得分:1)
哇!
对于迟到的回复感到抱歉,有点被一些游戏搞砸了,但我终于解决了这个问题。
import java.util.Scanner;
public class Testing {
public static void main(final String[] args) {
final Scanner in = new Scanner(System.in);
System.out.print("How many rows would you like?");
final int rows = in.nextInt();
int mod = (1 << (rows - 1)) - 1;
for(int i = 0; i < rows; i++){
for(int j = 0; j < mod; j++){
System.out.print("-");
}
System.out.print("X");
for (int j = 0; j < (1 << i) - 1; j++){
for(int k = 0; k < 2 * mod + 1; k++){
System.out.print("-");
}
System.out.print("X");
}
for(int j = 0; j < mod; j++){
System.out.print("-");
}
mod >>= 1;
System.out.println();
}
in.close();
}
}
这是一种非常简单的逻辑方法。它使用2和2的幂的基本原理来计算需要完成的工作以及应该如何完成。如您所见,第一行将有0b1
X,第二行将有0b10
X,依此类推。从那里,您还可以看到x之前所需的破折号。如果有4行,则第一行需要0b111
短划线,第二行需要0b11
。从那里,它只是重复做破折号的相反衰减。第一个需要0个重复,第二个需要1个。
如果您需要更多解释,我很乐意这样做。
编辑1:更多解释
对于以下说明,我将使用rows = 4
进行分析。因此,输出应类似于:
-------X-------
---X-------X---
-X---X---X---X-
X-X-X-X-X-X-X-X
让我们分解一行。以第二个为例,我们可以得出逻辑流程为:
的结论---
---x
n = 1
,(-------x
) - &gt; ---x-------x
---x-------x---
这可以针对所有案例进行复制。
第一种情况:缓冲区= 7,n = 0 - >自n = 0以来没有生成重复部分 第二种情况:缓冲区= 3,n = 1,缓冲区中的破折号= 7 第三种情况:缓冲区= 1,n = 3,缓冲区中的破折号= 3 第四种情况:缓冲区= 0,n = 7,缓冲区中的破折号= 1
在所有这些情况下,您可以看到变量都与2减1(Mersenne primes)的幂相关。
使用这种技术,我们得出一些基本公式的结论:
(1 << (rows - 1)) - 1)
使用行数(4)将值0001
转换为1000
,然后减去一个0111
,让我们留下7,即初始缓冲区。
(1 << i) - 1
使用我们所在的当前行(范围为0-3)来产生重复的次数。将0,1,2和3分别插入到我们得到的公式中:0,1,3 ,7(每行重复中间部分的次数)
2 * mod - 1
用于计算上面公式中使用的“中间部分”中的破折号数量
mod >>= 1
改变了mod。这允许第一个公式从初始值7变为3,再变为1,然后变为0。
答案 1 :(得分:0)
尝试直接显示输出会使递归变得麻烦。 此外,您可能想重新考虑您的参数。我相信叶子数量应该足够了。
我会采用不同的方式,通过为每个调用返回一个字符串列表,而不是立即打印输出。这样,您可以通过使用{leaf count} / 2运行它来实现main方法,将列表与自身合并(通过连接相同索引中的行),然后为根添加新的标题行。
以下是我的解决方案。请注意,它只接受一个2的幂的参数:
public static List<String> buildTree(int leafs) {
if (leafs == 1)
return Arrays.asList("x");
// Recursive call
List<String> subTree = buildTree(leafs/2);
ArrayList<String> merged = new ArrayList<String>();
// Add new header
String blanks = String.format("%-" + (leafs/2) + "s", "").replace(' ', '-');
String header = blanks + "x" + blanks.substring(1);
merged.add(header);
// Duplicate subtree
for (String row : subTree)
merged.add(row + row);
return merged;
}