Java 中 BinaryTree 的 inorder 方法(数组实现)
inorder method at BinaryTree in Java (array implementation)
如何为我的二叉树实现编写正确的排序方法?
这是我的测试:
class Main {
public static void main(String[] args) {
BinaryTree myTree = new BinaryTree();
myTree.inorder(0);
}
}
public class BinaryTree {
char[] tree = {'k', 'q', 'r', 'g', 'e', 'i', 'y', 'p', 'l', 'b', 'x', 'm', 'g', 't', 'u', 'v', 'z'};
public void inorder(int node) {
if(node < tree.length) {
inorder((node * 2));
System.out.print(tree[node] + " ");
inorder(((node * 2) + 1));
}
}
}
myTree.inorder(0); // 参数:0
顺序((节点* 2)); // 节点 = 0,节点 * 2 = 0,
所以参数会一直为0是死循环
public class BinaryTree {
char[] tree = {'k', 'q', 'r', 'g', 'e', 'i', 'y', 'p', 'l', 'b', 'x', 'm', 'g', 't', 'u', 'v', 'z'};
public void inorder(int node) {
if(node < tree.length) {
inorder((node * 2) + 1);
System.out.print(tree[node] + " ");
inorder(((node * 2) + 2));
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.inorder(0);
}
}
如何为我的二叉树实现编写正确的排序方法?
这是我的测试:
class Main {
public static void main(String[] args) {
BinaryTree myTree = new BinaryTree();
myTree.inorder(0);
}
}
public class BinaryTree {
char[] tree = {'k', 'q', 'r', 'g', 'e', 'i', 'y', 'p', 'l', 'b', 'x', 'm', 'g', 't', 'u', 'v', 'z'};
public void inorder(int node) {
if(node < tree.length) {
inorder((node * 2));
System.out.print(tree[node] + " ");
inorder(((node * 2) + 1));
}
}
}
myTree.inorder(0); // 参数:0
顺序((节点* 2)); // 节点 = 0,节点 * 2 = 0,
所以参数会一直为0是死循环
public class BinaryTree {
char[] tree = {'k', 'q', 'r', 'g', 'e', 'i', 'y', 'p', 'l', 'b', 'x', 'm', 'g', 't', 'u', 'v', 'z'};
public void inorder(int node) {
if(node < tree.length) {
inorder((node * 2) + 1);
System.out.print(tree[node] + " ");
inorder(((node * 2) + 2));
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.inorder(0);
}
}