JAVA六种遍历二叉树代码

先根遍历,中根遍历,后根遍历 二叉树并且 循环+递归两种方式

//二叉树节点class
public class BinaryTreeNode {
    private int value;
    public BinaryTreeNode leftNode;
    public BinaryTreeNode rightNode;

    public void setValue(int v){
        this.value = v;
    }
    public int getValue(){
        return this.value;
    }

}

//创建二叉树
public class BinaryTree {
    public static BinaryTreeNode treeRoot;
    static{
        treeRoot = new BinaryTreeNode();
        treeRoot.setValue(1);

        treeRoot.leftNode = new BinaryTreeNode();
        treeRoot.leftNode.setValue(2);

        treeRoot.rightNode = new BinaryTreeNode();
        treeRoot.rightNode.setValue(3);

        treeRoot.leftNode.leftNode = new BinaryTreeNode();
        treeRoot.leftNode.leftNode.setValue(4);
        treeRoot.leftNode.rightNode = new BinaryTreeNode();
        treeRoot.leftNode.rightNode.setValue(5);

        treeRoot.rightNode.leftNode = new BinaryTreeNode();
        treeRoot.rightNode.leftNode.setValue(6);
        treeRoot.rightNode.rightNode = new BinaryTreeNode();
        treeRoot.rightNode.rightNode.setValue(7);
    }
}

//创建带访问次数控制code的节点包装类
//此类为 后根遍历 循环方法提供帮助, 与别的遍历方法无关 
package Chapter2;

public class BinaryTreeNodeCode{
    private BinaryTreeNode binaryTreeNode;
    private int code;

    public BinaryTreeNodeCode( BinaryTreeNode binaryTreeNode,int code){
        this.binaryTreeNode = binaryTreeNode;
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public BinaryTreeNode getBinaryTreeNode() {
        return binaryTreeNode;
    }


}


//开始进行遍历操作
import java.util.ArrayList;
import java.util.LinkedList;

public class BinaryTreeOprate {
    public static void printTreeFirstRoot(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        System.out.println(treeRoot.getValue());
        printTreeFirstRoot(treeRoot.leftNode);
        printTreeFirstRoot(treeRoot.rightNode);
    }
    public static void printTreeSecoundRoot(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        printTreeSecoundRoot(treeRoot.leftNode);
        System.out.println(treeRoot.getValue());
        printTreeSecoundRoot(treeRoot.rightNode);
    }
    public static void printTreeThiredRoot(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        printTreeThiredRoot(treeRoot.leftNode);
        printTreeThiredRoot(treeRoot.rightNode);
        System.out.println(treeRoot.getValue());
    }
    public static void printTreeFirstRootFor(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        LinkedList<BinaryTreeNode> treeNodeStack = new LinkedList<>();
        treeNodeStack.push(treeRoot);
        while(!treeNodeStack.isEmpty()){
            BinaryTreeNode treeNode = treeNodeStack.getLast();
            treeNodeStack.removeLast();
            System.out.println(treeNode.getValue());
            if(treeNode.rightNode != null){
                treeNodeStack.addLast(treeNode.rightNode);
            }
            if(treeNode.leftNode != null){
                treeNodeStack.addLast(treeNode.leftNode);
            }


        }
    }
    public static void printTreeSecoundRootFor(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        LinkedList<BinaryTreeNode> treeNodeStack = new LinkedList<>();
        treeNodeStack.addLast(treeRoot);
        while(!treeNodeStack.isEmpty()){
            if(treeNodeStack.getLast().leftNode != null){
                treeNodeStack.addLast(treeNodeStack.getLast().leftNode);
            }else{
                BinaryTreeNode treeNode = treeNodeStack.getLast();
                System.out.println(treeNode.getValue());
                if(treeNode.rightNode != null){
                    treeNodeStack.addLast(treeNode.rightNode);
                }
            }

        }
    }
    /*
     * 规定访问一个节点 所做事情代表的 号码
     * 1 入栈(入栈之后,此节点代码为 1
     * 2 访问左右子节点(如果代码是1, 则下一步进行 访问左右子节点,刷代码为2)
     * 3 弹出并且输出 (如果代码是2 则下一步该进行弹出栈,并且输出值)
     */
    public static void printTreeThiredRootFor(BinaryTreeNode treeRoot) throws Exception{
        if(treeRoot == null){
            return;
        }
        LinkedList<BinaryTreeNodeCode> treeNodeStack = new LinkedList<>();
        treeNodeStack.addLast(new BinaryTreeNodeCode(treeRoot, 1));
        while(!treeNodeStack.isEmpty()){
            //如果栈顶元素的code == 1
            //把栈顶元素的code set成2
            //把栈顶元素的right节点入栈(!=null的话)并设置code=1
            //把栈顶元素的left节点入栈 (!=null的话)并设计code=1

            if(treeNodeStack.getLast().getCode() == 1){
                //获取栈顶的元素
                BinaryTreeNodeCode treeNodeCode = treeNodeStack.getLast();
                //因为这是第二次访问,所以置为2
                treeNodeCode.setCode(2);
                //将栈顶的元素左右子树入栈,并且设置code=1
                if(treeNodeCode.getBinaryTreeNode().rightNode != null){
                    treeNodeStack.addLast(new BinaryTreeNodeCode(treeNodeCode.getBinaryTreeNode().rightNode, 1));
                }
                if(treeNodeCode.getBinaryTreeNode().leftNode != null){
                    treeNodeStack.addLast(new BinaryTreeNodeCode(treeNodeCode.getBinaryTreeNode().leftNode, 1));
                }

            //如果code == 2
            //则将栈顶元素的值输出并且弹出
            }else if(treeNodeStack.getLast().getCode() == 2){
                System.out.println(treeNodeStack.getLast().getBinaryTreeNode().getValue());
                treeNodeStack.removeLast();
            }else{
                throw new Exception("出现code != 1  && != 2");
            }

        }
    }
    public static void main(String[] args) throws Exception {
        BinaryTreeNode treeRoot = BinaryTree.treeRoot;
        System.out.println("======先根遍历===递归=====");
        printTreeFirstRoot(treeRoot);
        System.out.println("======先根遍历===循环=====");
        printTreeFirstRootFor(treeRoot);
        System.out.println("======中根遍历===递归=====");
        printTreeSecoundRoot(treeRoot);
        System.out.println("======中根遍历===循环=====");
        printTreeSecoundRoot(treeRoot);
        System.out.println("======后根遍历===递归=====");
        printTreeThiredRoot(treeRoot);
        System.out.println("======后根遍历===循环=====");
        printTreeThiredRootFor(treeRoot);
    }
}

/////////运行结果////////////////
/*
======先根遍历===递归=====
1
2
4
5
3
6
7
======先根遍历===循环=====
1
2
4
5
3
6
7
======中根遍历===递归=====
4
2
5
1
6
3
7
======中根遍历===循环=====
4
2
5
1
6
3
7
======后根遍历===递归=====
4
5
2
6
7
3
1
======后根遍历===循环=====
4
5
2
6
7
3
1

*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章