두 갈래 트리 차원 반복, 깊이 계산(귀속+비귀속)

3721 단어 두 갈래 나무
import java.util.LinkedList;
import java.util.List;

public class BinaryTreeDeep {
    public static void main(String[] args){
        Tree tree1=new Tree("1");
        Tree tree2=new Tree("2");
        Tree tree3=new Tree("3");
        Tree tree4=new Tree("4");
        Tree tree5=new Tree("5");
        Tree tree7=new Tree("7");
        Tree tree8=new Tree("8");
        Tree tree9=new Tree("9");
        Tree tree10=new Tree("10");

        tree1.setLeft(tree2);
        tree1.setRight(tree3);
        tree2.setLeft(tree4);
        tree3.setRight(tree5);
        tree5.setLeft(tree7);
        tree7.setLeft(tree8);
        tree8.setRight(tree9);
        tree4.setRight(tree10);

        /*
               1
             2    3
           4         5
             10         7
                      8
                          9
         */

        int deep=getDeep(tree1);
        System.out.println(" ( ) "+deep);

        int deep1=getDeep1(tree1);
        System.out.println(" ( ) "+deep1);

        System.out.println(" :");
        levelTraversal(tree1);
    }

    private static int getDeep(Tree root){

        if(null==root){
            return 0;
        }

        if(null==root.getLeft()&&null==root.getRight()){
            return 1; // 1
        }

        int left=0;
        int right=0;
        if(null!=root.getLeft()){
            left=getDeep(root.getLeft());
        }
        if(null!=root.getRight()){
            right=getDeep(root.getRight());
        }
        int deep=Math.max(left,right)+1;
        return deep;
    }

    private static int getDeep1(Tree root){
        if(null==root){
            return 0;
        }

        List nodes=new LinkedList<>();
        ((LinkedList) nodes).offer(root);
        int current=0;
        int deep=0;
        int levelNodeSize=0;
        while(nodes.size()>0){
            levelNodeSize=nodes.size();// 
            current=0;
            while(current) nodes).poll();
                if(null!=tmp.getLeft()){
                    ((LinkedList) nodes).offer(tmp.getLeft());
                }

                if(null!=tmp.getRight()){
                    ((LinkedList) nodes).offer(tmp.getRight());
                }

                current++;
            }

            deep++;
        }

        return deep;
    }

    private static void levelTraversal(Tree root){
        List nodes=new LinkedList<>();
        ((LinkedList) nodes).offer(root);
        while(!nodes.isEmpty()){
            Tree tmp=((LinkedList) nodes).poll();
            System.out.print(tmp.getRoot()+" ");
            if(null!=tmp.getLeft()){
                ((LinkedList) nodes).offer(tmp.getLeft());
            }

            if(null!=tmp.getRight()){
                ((LinkedList) nodes).offer(tmp.getRight());
            }
        }
        System.out.println("");
    }
}

class Tree{
    private String root;
    private Tree left;
    private Tree right;

    public Tree(String root) {
        this.root = root;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }

    public Tree getLeft() {
        return left;
    }

    public void setLeft(Tree left) {
        this.left = left;
    }

    public Tree getRight() {
        return right;
    }

    public void setRight(Tree right) {
        this.right = right;
    }


}

좋은 웹페이지 즐겨찾기