如何在Java中进行预顺序遍历?
我正在尝试使用递归方法在二叉树上编写一个预排序遍历算法。以下是我所拥有的:如何在Java中进行预顺序遍历?,java,algorithm,compiler-errors,binary-tree,traversal,Java,Algorithm,Compiler Errors,Binary Tree,Traversal,我正在尝试使用递归方法在二叉树上编写一个预排序遍历算法。以下是我所拥有的: void traverse(BT t) { if (t == null){ return; } System.out.print(t); traverse(t.left); traverse(t.right); } 由于某种原因,它无法编译。我认为问题在于我的代码的其余部分。以下是完整的代码:
void traverse(BT t) {
if (t == null){
return;
}
System.out.print(t);
traverse(t.left);
traverse(t.right);
}
由于某种原因,它无法编译。我认为问题在于我的代码的其余部分。以下是完整的代码:
class ZOrep extends TreeAndRepresentation {
private int k;
ZOrep left;
ZOrep right;
ZOrep( int m, int[] b ) { // given sequence build tree
super( m, b );
N = (M-1)/2;
k = -1;
t = build();
}
ZOrep( int n, BT t ) { // given tree build sequence
super(n, t);
t = build();
traverse( t );
}
BT build() {
return(a[++k] == 0 ? null : new BT( build(), build() ));
}
void traverse(BT t) {
if (t == null){
return;
}
System.out.print(t);
traverse(t.left);
traverse(t.right);
}
}
当我(用我的ZOrep方法)构建树时,我觉得我遗漏了一些东西。这里还有BT课程:
class BT {
BT L; BT R;
BT( BT l, BT r ) { L = l; R = r; }
}
目前我的编译器说它找不到t.left和t.right的符号
目前我的编译器说它找不到t.left和t.right的符号
这是因为t
是BT
,它没有左
和右
我建议您决定将树节点类称为什么。是ZOrep
还是BT
并且只使用其中一个,否则会造成混淆
System.out.print(t);
如果要打印BT,需要在其中添加toString()
方法,因为默认方法不会告诉您任何有用的信息
目前我的编译器说它找不到t.left和t.right的符号
这是因为t
是BT
,它没有左
和右
我建议您决定将树节点类称为什么。是ZOrep
还是BT
并且只使用其中一个,否则会造成混淆
System.out.print(t);
如果要打印BT,需要向其添加
toString()
方法,因为默认方法不会告诉您任何有用的信息。当编译器说找不到符号时,表示您尝试引用的字段不存在
看看你的班级,这是正确的BT
没有left
或right
,它有L
和R
。因此
traverse(t.left);
traverse(t.right);
与
将修复此问题。当编译器说找不到符号时,表示您试图引用的字段不存在 看看你的班级,这是正确的
BT
没有left
或right
,它有L
和R
。因此
traverse(t.left);
traverse(t.right);
与
将解决此问题。您在横向函数中传递了什么?如果是BT对象,则不能使用left和right,必须使用L和R。left和right是对象的一部分,从BT扩展而来,但看起来像是在传递BT。在横向函数中传递什么?如果是BT对象,则不能使用left和right,必须使用L和R。left和right是从BT扩展而来的对象的一部分,但看起来像是在传递BT。/Java 静态字符串树=” 私有静态无效预排序(HuffTree currentObject){ } //这里是班级代码 导入java.util.Objects /**
- 哈夫曼树类 */
// element filling
Byte filling;
// element repeats
int repeats;
// zero child
HuffTree child0;
// child 1
HuffTree child1;
/**
* constructor for tree fathers and leaves
*/
public HuffTree(Byte filling, int repeats, HuffTree child0, HuffTree child1) {
// father filling
this.filling = filling;
// father repeats
this.repeats = repeats;
// zero child
this.child0 = child0;
// child 1
this.child1 = child1;
}
/**
* finding difference between our tree's items
*/
@Override
public int compareTo(HuffTree currentByte) {
return currentByte.repeats - repeats;
}
/**
* take byte code as a string by recursive three search in depth
*/
public String getCodeForByte(Byte currentByte, String wayToFather) {
// there is 4 cases:
if (!Objects.equals(filling, currentByte)) {
// case 1 - zero child found
if (child0 != null) {
// recursive code add for zero child
String currentWay = child0.getCodeForByte(currentByte, wayToFather + "0");
// return temporary string
if (currentWay != null) return currentWay;
}
// case 2 - child 1 found. recursive code add for child 1. return temporary string
if (child1 != null) return child1.getCodeForByte(currentByte, wayToFather + "1");
}
// case 3 - correct leaf found. return correct code
if (Objects.equals(filling, currentByte)) return wayToFather;
// case 4 - wrong leaf found. return null
return null;
}
}//Java
静态字符串树=”
私有静态无效预排序(HuffTree currentObject){
}
//这里是班级代码
导入java.util.Objects
/**
- 哈夫曼树类 */
// element filling
Byte filling;
// element repeats
int repeats;
// zero child
HuffTree child0;
// child 1
HuffTree child1;
/**
* constructor for tree fathers and leaves
*/
public HuffTree(Byte filling, int repeats, HuffTree child0, HuffTree child1) {
// father filling
this.filling = filling;
// father repeats
this.repeats = repeats;
// zero child
this.child0 = child0;
// child 1
this.child1 = child1;
}
/**
* finding difference between our tree's items
*/
@Override
public int compareTo(HuffTree currentByte) {
return currentByte.repeats - repeats;
}
/**
* take byte code as a string by recursive three search in depth
*/
public String getCodeForByte(Byte currentByte, String wayToFather) {
// there is 4 cases:
if (!Objects.equals(filling, currentByte)) {
// case 1 - zero child found
if (child0 != null) {
// recursive code add for zero child
String currentWay = child0.getCodeForByte(currentByte, wayToFather + "0");
// return temporary string
if (currentWay != null) return currentWay;
}
// case 2 - child 1 found. recursive code add for child 1. return temporary string
if (child1 != null) return child1.getCodeForByte(currentByte, wayToFather + "1");
}
// case 3 - correct leaf found. return correct code
if (Objects.equals(filling, currentByte)) return wayToFather;
// case 4 - wrong leaf found. return null
return null;
}
}请将答案中的代码格式化,并添加一些解释?谢谢,请你把答案中的代码格式化并加上一些解释好吗?非常感谢。