• 二叉树的Java实现


    import java.util.LinkedList;
    import java.util.List;
    
    
    public class BinaryTree{
    
        private TreeNode root;
    
        public BinaryTree(){}
        public BinaryTree(TreeNode root){
            this.root = root;
        }
        public TreeNode getRoot(){
            return root;
        }
        public void setRoot(TreeNode root){
            this.root = root;
        }
    
        /**
         * create tree
         */
        public void creatTree(int[] datas,List<TreeNode> nodeList){
            for(int nodeIndex=0;nodeIndex<datas.length;nodeIndex++){
                TreeNode node = new TreeNode(datas[nodeIndex]);
                nodeList.add(node);
            }
    
            for(int index=0;index<(datas.length)/2-1;index++){
                nodeList.get(index).setLeft(nodeList.get(index*2+1));
                nodeList.get(index).setRight(nodeList.get(index*2+2));
            }
            int index = datas.length/2-1;
            nodeList.get(index).setLeft(nodeList.get(index*2+1));
            if(datas.length%2==1){
                nodeList.get(index).setRight(nodeList.get(index*2+2));
            }
        }
    
        /**
         * node count
         */
        public int getSize(){
            return getSize(root);
        }
        private int getSize(TreeNode node){
            if(node==null){
                return 0;
            }else{
                int i=getSize(node.getLeft());
                int j=getSize(node.getRight());
                return i+j+1;
            }
        }
    
        /**
         * leaf cuont
         */
        public int getLeaf(TreeNode root){
            int leafCount = 0;
            if(root==null){
                return 0;
            }else{
                if((!root.hasLeft())&&(!root.hasRight())){
                    leafCount++;
                }
            }
            int i = getLeaf(root.getLeft());
            int j = getLeaf(root.getRight());
            return i+j+leafCount;
           
        }
    
        /**
         * tree height
         */
        public int getHeight(){
            return getHeight(root);
        }
        private int getHeight(TreeNode node){
            if(node==null){
                return 0;
            }else{
                int i=getHeight(node.getLeft());
                int j=getHeight(node.getRight());
                return (i<j)?(j+1):(i+1);
            }
        }
    
        /**
         * preOrder
         */
        public void preOrder(TreeNode node){
            if(node!=null){
                System.out.print(node.getData()+" ");
                preOrder(node.getLeft());
                preOrder(node.getRight());
            }
        }
    
        public void nonRecurPreOrder(TreeNode root){
            LinkedList<TreeNode> stack =new LinkedList<>();
            TreeNode currentNode = root;
            TreeNode temp;
            while(currentNode!=null||!stack.isEmpty()){
                while(currentNode!=null){
                    System.out.print(currentNode.getData()+" ");
                    stack.push(currentNode);
                    currentNode=currentNode.getLeft();
                }
                if(!stack.isEmpty()){
                    temp=stack.pop();
                    currentNode=temp.getRight();
                }
                
            }
            System.out.println();
        }
        /**
         * inOrder
         */
        public void inOrder(TreeNode node){
            if(node!=null){
                inOrder(node.getLeft());
                System.out.print(node.getData()+" ");
                inOrder(node.getRight());
            }
        }
    
        public void nonRecurInOrder(TreeNode root){
            LinkedList<TreeNode> stack = new LinkedList<>();
            TreeNode currentNode = root;
            TreeNode temp;
            while(currentNode!=null||!stack.isEmpty()){
                while(currentNode!=null){
                    stack.push(currentNode);
                    currentNode=currentNode.getLeft();
                }
                if(!stack.isEmpty()){
                    temp=stack.pop();
                    System.out.print(temp.getData()+" ");
                    currentNode=temp.getRight();
                }
            }
            System.out.println();
        }
        /**
         * postOrder
         */
        public void postOrder(TreeNode node){
            if(node!=null){
                postOrder(node.getLeft());
                postOrder(node.getRight());
                System.out.print(node.getData()+" ");
            }
        }
    
        public void nonRecurPostOrder(TreeNode root){
            LinkedList<TreeNode> stack = new LinkedList<>();
            TreeNode currentNode = root;
            TreeNode prevNode = root;
            while(currentNode!=null||!stack.isEmpty()){
                while(currentNode!=null){
                    stack.push(currentNode);
                    currentNode=currentNode.getLeft();
                }
                if(!stack.isEmpty()){
                    TreeNode temp = stack.peek().getRight();
                    if(temp==null||temp==prevNode){
                        currentNode=stack.pop();
                        System.out.print(currentNode.getData()+" ");
                        prevNode=currentNode;
                        currentNode=null;
                    }else{
                        currentNode=temp;
                    }
                }
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
    
            BinaryTree tree = new BinaryTree();
            int[] datas=new int[]{1,2,3,4,5,6,7,8,9};
            List<TreeNode> nodeList = new LinkedList<>();
            tree.creatTree(datas, nodeList);
            TreeNode root = nodeList.get(0);
    
            System.out.println("
    =====preOrder======");
            tree.preOrder(root);
            System.out.println("
    =====non pre order======");
            tree.nonRecurPreOrder(root);
            System.out.println("
    =====inOrder=======");
            tree.inOrder(root);
            System.out.println("
    =====non in order======");
            tree.nonRecurInOrder(root);   
            System.out.println("
    =====postOrder=====");
            tree.postOrder(root);
            System.out.println("
    =====non post order======");
            tree.nonRecurPostOrder(root);
            System.out.println("
    =====height=======");
            System.out.println(tree.getHeight(root));
            System.out.println("
    =====size=========");
            System.out.println(tree.getSize(root));
            System.out.println("
    =====leaf=========");
            System.out.println(tree.getLeaf(root));
    
    
        }
    
    
    
    }
    class TreeNode{
        private int data;
        private TreeNode left;
        private TreeNode right;
        public TreeNode(){}
        public TreeNode(int data){
            this.data=data;
            this.left=null;
            this.right=null;
        }
        public TreeNode(int data,TreeNode left,TreeNode right){
            this.data=data;
            this.left=left;
            this.right=right;
        }
    
        public int getData(){
            return data;
        }
        public void setData(int data){
            this.data = data;
        }
        public TreeNode getLeft(){
            return left;
        }
        public void setLeft(TreeNode left){
            this.left = left;
        }
        public TreeNode getRight(){
            return right;
        }
        public void setRight(TreeNode right){
            this.right = right;
        }
    
        public boolean hasLeft(){
            if(this.getLeft()==null)
                return false;
            return true;
        }
        public boolean hasRight(){
            if(this.getRight()==null)
                return false;
            return true;
        }
    }

    按层遍历

     public void laywerOrder(TreeNode root){
            if(root==null)
                return ;
            LinkedList<TreeNode> list = new LinkedList<>();
            list.add(root);
            TreeNode currentNode = root;
            while(!list.isEmpty()){
                currentNode=list.poll();
                System.out.print(currentNode.getData()+" ");
                if(currentNode.hasLeft())
                    list.add(currentNode.getLeft());
                if(currentNode.hasRight())
                    list.add(currentNode.getRight());
            }
        }

    输出:

    =====layer order======
    1 2 3 4 5 6 7 8 9

    结果:

    =====preOrder======
    1 2 4 8 9 5 3 6 7
    =====non pre order======
    1 2 4 8 9 5 3 6 7
    
    =====inOrder=======
    8 4 9 2 5 1 6 3 7
    =====non in order======
    8 4 9 2 5 1 6 3 7
    
    =====postOrder=====
    8 9 4 5 2 6 7 3 1
    =====non post order======
    8 9 4 5 2 6 7 3 1
    
    =====height=======
    4
    
    =====size=========
    9
    
    =====leaf=========
    5
  • 相关阅读:
    自定义 sql Split函数 / 自定义mp_helptext查看存储
    easyUI 节点树选择
    EasyUI手风琴 Tab卡使用
    水晶报表分组,统计,求和,sum()函数使用
    华硕GL502VSK处理器使用XTU降压。
    .
    bootstrap4元素居左居右
    关于npm run dev报错npm ERR! missing script: dev
    使用Vue写一个登陆页面并在管理页面查看和修改
    localStorage注册页面A注册数据在本地储存并在B页面打开
  • 原文地址:https://www.cnblogs.com/liesun/p/9358601.html
Copyright © 2020-2023  润新知