Java中的遍历树

Java中的遍历树,java,Java,我在遍历一棵树时遇到了一个问题,我的输出如下所示 如果这棵树是这样的话,图像就是这样的。[http://www.java-forums.org/attachments/advanced-java/3355d1332821031t-traversing-binary-tree-root-each-branch-binarytree.png][1] 输出: A、 A1、A2、B1、B2 A、 A1、B1、A2、B2 A、 A1、B1、B2、A2 A、 B1、A1、A2、B2 A、 B1、A1、B2、

我在遍历一棵树时遇到了一个问题,我的输出如下所示

如果这棵树是这样的话,图像就是这样的。[http://www.java-forums.org/attachments/advanced-java/3355d1332821031t-traversing-binary-tree-root-each-branch-binarytree.png][1]

输出:

  • A、 A1、A2、B1、B2
  • A、 A1、B1、A2、B2
  • A、 A1、B1、B2、A2
  • A、 B1、A1、A2、B2
  • A、 B1、A1、B2、A2
  • A、 B1、B2、A1、A2
  • 我知道这与preorder遍历非常相似,但preorder不会在另一次节点拆分为左节点和右节点时输出父节点。有什么建议吗

    这是我的代码,但我被困在打印出来

    public class BinaryTreeTest {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    
        int countA = 0;
        int countB = 0;
        ArrayList listA = new ArrayList();
        ArrayList listB = new ArrayList();
        listA.add("A1");
        listA.add("A2");
        listA.add("A3");
        listB.add("B1");
        listB.add("B2");
        listB.add("B3");
        //listB.add("B1");
        Node root = new Node("START");
        constructTree(root, countA, countB, listA, listB);
    
        //printInOrder(root);
        //printFromRoot(root);
    
    
    
    }
    
    
    public static class Node{
        private Node left;
        private Node right;
        private String value;
        public Node(String value){
            this.value = value;
        }
    }
    
    public static void constructTree(Node node, int countA, int countB, ArrayList listA, ArrayList listB){
        if(countA < listA.size()){
            if(node.left == null){
                System.out.println("There is no left node. CountA is " + countA);
                System.out.println("Created new node with value: " + listA.get(countA).toString() + " with parent, "
                        + node.value);
                System.out.println();
                node.left = new Node(listA.get(countA).toString());  
                constructTree(node.left, countA+1, countB, listA, listB);    
            }else{
                System.out.println("There is a left node. CountA + 1 is " + countA+1);
                constructTree(node.left, countA+1, countB, listA, listB);    
            }
        }
        if(countB < listB.size()){
            if(node.right == null){
                System.out.println("There is no right node. CountB is " + countB);
                System.out.println("Created new node with value: " + listB.get(countB).toString() + " with parent, "
                        + node.value);
                System.out.println();
                node.right = new Node(listB.get(countB).toString());
                constructTree(node.right, countA, countB+1, listA, listB); 
            }else{
                System.out.println("There is a right node. CountB + 1 is " + countB+1);
                constructTree(node.right, countA, countB+1, listA, listB);
            }
        }
    }
    
    公共类二进制树测试{
    /**
    *@param指定命令行参数
    */
    公共静态void main(字符串[]args){
    //此处的TODO代码应用程序逻辑
    int countA=0;
    int countB=0;
    ArrayList listA=新的ArrayList();
    ArrayList listB=新的ArrayList();
    列表A.添加(“A1”);
    列表A.添加(“A2”);
    列表A.添加(“A3”);
    清单B.添加(“B1”);
    清单B.添加(“B2”);
    清单B.添加(“B3”);
    //清单B.添加(“B1”);
    节点根=新节点(“开始”);
    构造树(根、countA、countB、listA、listB);
    //打印顺序(根);
    //printFromRoot(root);
    }
    公共静态类节点{
    私有节点左;
    私有节点权;
    私有字符串值;
    公共节点(字符串值){
    这个值=值;
    }
    }
    公共静态void构造函数树(节点节点、int countA、int countB、ArrayList listA、ArrayList listB){
    if(countA
    您要做的是使用深度优先算法遍历树

    你会在互联网上找到很多例子。这取决于你如何制作你的树。你可以制作一个从左到右传递每个子对象的递归算法,或者如果你已经加载了一个对象树,那么可以使用访问者模式


    首先,看一看

    ,最重要的代码在哪里?问题到底是什么?从您展示的内容来看,您需要做的是在每次移动中获得最左边的节点。您还需要将节点标记为已完成。