繁体   English   中英

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

inorder((节点* 2)); //节点= 0,节点* 2 = 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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM