二叉树的操作这里都有~~~~~~

定义二叉树的结点类型

public class BinTree {
    public int root;//结点值
    public BinTree left;//左孩子结点
    public BinTree right;//右孩子结点
    //定义有参构造方法为树结点赋值
    public BinTree(int root,BinTree left,BinTree right){
        this.root = root;
        this.left = left;
        this.right = right;
    }
}

二叉树的随机创建

   //二叉树的随机创建(这种创建的树偏左。。。(*^_^*))
    public static void createRandomBinTree(BinTree binTree){
        //任意给一组数据模拟树的结点
        int[] arr = new int[]{1,2,3,4,5};
        BinTree node = binTree;//定义一个结点node指向当前结点
        for(int i = 0;i < arr.length;i++){
            //采用随机法生成当前的结点的左右孩子结点
            if(Math.random()>0.5){//生成左孩子结点
                node.left = new BinTree(arr[i],null,null);
                node.right = null;
                node = node.left;
            }else{//生成右孩子结点
                node.left = null;
                node.right = new BinTree(arr[i],null,null);
                node = node.right;
            }
        }
    }

完全二叉树的创建

    //完全二叉树的创建(使用队列实现)
    public static void createBinTree(BinTree binTree){
        //任意给一组数据模拟树的结点
        int[] arr = new int[]{1,2,3,4,5};
        //定义一个队列,将树的结点存进去
        Queue<BinTree> queue = new LinkedList<>();
        queue.add(binTree);//根节点放置到队列当中
        for(int i=0;i<arr.length;i++){
            if(!queue.isEmpty()){
                BinTree node = queue.poll();//poll()操作是取当前队列的队头元素并删除
                node.left = new BinTree(arr[i],null,null);
                if(i<arr.length-1){
                    node.right = new BinTree(arr[++i],null,null);
                }
                queue.add(node.left);
                queue.add(node.right);
            }
        }
    }

二叉树的先、中、后序(递归)遍历

 //二叉树的先序遍历(递归)
    public static void preOrder(BinTree  node){
        System.out.print(node.root+" ");
        if(node.left!=null){
            preOrder(node.left);
        }
        if(node.right!=null){
            preOrder(node.right);
        }
    }
    //二叉树的中序遍历(递归)
    public static void midOrder(BinTree  node){
        if(node.left!=null){
            midOrder(node.left);
        }
        System.out.print(node.root+" ");
        if(node.right!=null){
            midOrder(node.right);
        }
    }
    //二叉树的后序遍历(递归)
    public static void lastOrder(BinTree  node){
        if(node.left!=null){
            lastOrder(node.left);
        }
        if(node.right!=null){
            lastOrder(node.right);
        }
        System.out.print(node.root+" ");
    }

二叉树的层次遍历+非递归求深度

//二叉树的层次遍历(先上再下,先左再右)
    // 同时利用层次遍历求树的深度(非递归求深度)
    public static void levelOrder(BinTree root){
        Queue<BinTree> queue = new LinkedList<>();
        queue.add(root);
        System.out.print(root.root+" ");//首先输出根节点的值
        int deep = 0;
        while(!queue.isEmpty()){
            int size = queue.size();//size用来标记当前层次的结点数量
            while(size>0){
                BinTree node = queue.poll();
                if(node.left!=null){
                    System.out.print(node.left.root+" ");
                    queue.add(node.left);
                }
                if(node.right!=null){
                    System.out.print(node.right.root+" ");
                    queue.add(node.right);
                }
                --size;
            }
            ++deep;
        }
        System.out.println("\n二叉树的深度为:"+deep);
    }

二叉树的深度

 //二叉树的深度(递归)
    public static int deep(BinTree root){
        if(root == null)
            return 0;
        int l = deep(root.left);
        int r = deep(root.right);
        return l>r?1+1:r+1;
    }

二叉树的翻转/镜像

public static TreeNode mirrorTree(TreeNode root) {
        if (root == null || (root.left == null && root.right == null))
            return root;
        //定义一个临时结点用来交换左右子树的结点
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        if(root.left!=null){
            mirrorTree(root.left);
        }
        if(root.right!=null){
            mirrorTree(root.right);
        }
        return root;
    }

操作持续更新中…

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值