*我已在下面回答了我自己的问题*
此线程中还有其他很好的解决方案。
*原始问题:*
我正在尝试打印一棵我这样构建的树:
树(根(20),树(根(15),叶(10),叶(18)),树(根(25),叶(22),叶(27))我有以下代码:
static int childlev = 0;//This is global var used to keep track of parentheses
public static void printStruct(MyTree tree, int level) {//This prints out the data structure of the tree
assert tree != null;
if (tree instanceof MyLeaf) {//reached the bottom leaf
MyLeaf leaf = (MyLeaf)tree;
System.out.print(",leaf('" + leaf.value+ "':" + leaf.frequency + ")");
childlev=level;
} else if (tree instanceof MyNode) {//reached a parent
if(childlev>level){//print parentheses when right child leaf is reached and level goes back to parent
for(int i=0;i<childlev-level;i++)//child level minus parent level that we are backing up to
System.out.print(")");
childlev=0;
}
if(level!=0){
System.out.print(",");
}
MyNode node = (MyNode)tree;
System.out.print("tree(root("+node.frequency+")");
// traverse left
printStruct(node.left, level+1);
// traverse right
printStruct(node.right, level+1);
}
}
//at the end of my main after calling above function i have this:
for(int i=0; i<p; i++)//This puts in the final parentheses
System.out.print(")");
输出:
tree(root(170),tree(root(70),tree(root(32),tree(root(16),tree(root(8),leaf('x':4),tree(root(4),leaf('l':2),leaf('u':2),leaf('h':8),leaf('n':16)),tree(root(38),tree(root(17),leaf('w':8),leaf('o':9)),tree(root(21),tree(root(10),leaf('v':5),leaf('f':5)),tree(root(11),leaf('y':5),tree(root(6),leaf('g':3),leaf('a':3)))))),tree(root(100),tree(root(47),leaf('t':22),tree(root(25),tree(root(12),tree(root(6),leaf('c':3),tree(root(3),leaf('z':1),leaf('d':2),leaf('r':6),leaf('i':13))),tree(root(53),leaf('e':26),leaf('s':27))))
但正如你所看到的,在叶子('u':2)之后应该有2个括号但是没有。所以,现在如果我们有一个正确的叶子后跟右叶子,我们就不打印任何右括号。我该如何解决? 提前谢谢!
答案 0 :(得分:0)
它并不像你制作它那么难。
首先,您应该删除MyNode
和MyLeaf
类。你不需要它们,它会使树的更新变得不必要。节点是叶子是树。您只需要MyTree
,如下所示:
public class MyTree
{
private MyTree left;
private MyTree right;
private String value;
private int frequency;
// ... constructors etc
public void print()
{
System.out.print("tree(");
if (left != null || right != null)
{
System.out.print("root("+frequency);
if (left != null)
{
System.out.print(",");
left.print();
}
if (right != null)
{
System.out.print(",");
right.print();
}
System.out.print(")"); // ends root()
}
else
{
System.out.print("leaf('"+value+"':"+frequency+")");
}
System.out.print(")"); // ends tree()
}
如果你真的必须保留额外的课程:
public abstract class MyTree
{
// ...
public abstract void print();
}
public class MyNode extends MyTree
{
// ...
public void print()
{
System.out.print("tree(");
System.out.print("root("+this.frequency);
if (left != null)
{
System.out.print(",");
left.print();
}
if (right != null)
{
System.out.print(",");
right.print();
}
System.out.print(")"); // ends root()
System.out.print(")"); // ends tree()
}
}
public class MyLeaf extends MyTree
{
// ...
public void print()
{
System.out.print("tree(");
System.out.print("leaf('"+value+"':"+frequency+")");
System.out.print(")"); // ends tree()
}
}
请注意,您不需要支架计数器,并且该方法不是静态的。
E&安培; OE
答案 1 :(得分:-1)
*我正在回答我自己的问题*
至少可以说我的解决方案有点混乱。
在这个帖子中还有其他很好的解决方案,但由于我的案例有点特殊,我必须找到自己的方法来做到这一点。基本上固定边缘情况。我会为这个项目提供一个链接到我的GitHub,一旦它启动,所以其他人可能会清楚地看到它。
以下是工作版本:
//These are global vars declared up top
static int chl = 0;//keeps track of child level
static int retp = 0;//keeps track of return parent level
static int rl = 0;//keeps track of right leaves
//Here is the function to print out tree:
public static void printStruct(MyTree tree, int level, int right) {//This prints out the data structure of the tree
assert tree != null;
if (tree instanceof MyLeaf) {//reached the bottom leaf
if(right==1 && rl>1){//print parentheses when right leaf is followed by right leaf
for(int i=0;i<chl-retp;i++)
System.out.print(")");
retp--;
}
MyLeaf leaf = (MyLeaf)tree;
System.out.print(",leaf('" + leaf.value+ "':" + leaf.frequency + ")");
chl=level;
} else if (tree instanceof MyNode) {//reached a parent
rl=0;//next node is not leaf set back to zero
if(chl>level){//print parentheses when right child leaf is reached and level goes back to ancestor parent
for(int i=0;i<chl-level;i++)
System.out.print(")");
chl=0;
}
if(level!=0){//if not the first root then add comma
System.out.print(",");
}
MyNode node = (MyNode)tree;
System.out.print("tree(root("+node.frequency+")");
// traverse left
if(node.left instanceof MyNode){
retp=level;
}
printStruct(node.left, level+1, 0);
// traverse right
if(node.right instanceof MyNode){
retp=level;
}else rl++;//next node is right leaf
printStruct(node.right, level+1, 1);
}
}
//at the end of my main when calling above function I have this:
printStruct(tree, 0, 0);
for(int i=0; i<chl; i++)//This puts in the final parentheses
System.out.print(")");
这是正确的输出:
tree(root(170),tree(root(70),tree(root(32),tree(root(16),tree(root(8),leaf('x':4),tree(root(4),leaf('l':2),leaf('u':2))),leaf('h':8)),leaf('n':16)),tree(root(38),tree(root(17),leaf('w':8),leaf('o':9)),tree(root(21),tree(root(10),leaf('v':5),leaf('f':5)),tree(root(11),leaf('y':5),tree(root(6),leaf('g':3),leaf('a':3)))))),tree(root(100),tree(root(47),leaf('t':22),tree(root(25),tree(root(12),tree(root(6),leaf('c':3),tree(root(3),leaf('z':1),leaf('d':2))),leaf('r':6)),leaf('i':13))),tree(root(53),leaf('e':26),leaf('s':27))))