20170513_二叉树的生成

二叉树的生成

一层一层进行输入,如果孩子为空则输入-1

private static TreeNode createTree() {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        MyQueen queen=new MyQueen();
        String data=in.nextLine();
        TreeNode father=new TreeNode(data);
        TreeNode root;
        TreeNode child;
        queen.offer(father);
        while(true)
        {
            if(queen.isEmpty())
            {
                break;
            }
            root=(TreeNode) queen.peek();
            data=in.nextLine();
            if(!data.equals("-1"))
            {
                child=new TreeNode(data);
                queen.offer(child);
                root.lChild=child;
            }
            data=in.nextLine();
            if(!data.equals("-1"))
            {
                child=new TreeNode(data);
                queen.offer(child);
                root.rChild=child;
            }
            queen.poll();
        }
        return father;
    }
}

树的主要遍历方式

  • 前序遍历:根结点 ---> 左子树 ---> 右子树

  • 中序遍历:左子树---> 根结点 ---> 右子树

  • 后序遍历:左子树 ---> 右子树 ---> 根结点

  • 层次遍历(广度优先遍历):一层一层地遍历

image

上面二叉树的遍历结果

  • 前序遍历:1 2 4 5 7 8 3 6

  • 中序遍历:4 2 7 5 8 1 3 6

  • 后序遍历:4 7 8 5 2 6 3 1

  • 层次遍历:1 2 3 4 5 6 7 8

    四种遍历方式的代码

/**
     * 二叉树的先序遍历
     * @param node 根节点
     */
    public static void preRootTreaverse(TreeNode node)
    {
        if(node==null)
            return;
        System.out.print(node.data+" ");
        preRootTreaverse(node.lChild);
        preRootTreaverse(node.rChild);
    }
    /**
     * 二叉树的中序遍历
     * @param node 根节点
     */
    public static void inRootTreaverse(TreeNode node)
    {
        if(node==null)
            return;
        inRootTreaverse(node.lChild);
        System.out.print(node.data+" ");
        inRootTreaverse(node.rChild);
    }
    /**
     * 二叉树的后序遍历
     * @param node
     */
    public static void postRootTreaverse(TreeNode node)
    {
        if(node==null)
            return;
        postRootTreaverse(node.lChild);
        postRootTreaverse(node.rChild);
        System.out.print(node.data+" ");
    }
    /**
     * 二叉树广度遍历
     * @param root
     */
    public static void bfs(TreeNode root)
    {
        MyQueen queen=new MyQueen();
        queen.offer(root);
        TreeNode lChirld,rChirld;
        while(!queen.isEmpty())
        {
            root=(TreeNode)queen.peek();
            System.out.print(root.data+" ");
            lChirld=root.lChild;
            if(lChirld!=null)
            {
                queen.offer(lChirld);
            }
            rChirld=root.rChild;
            if(rChirld!=null)
            {
                queen.offer(rChirld);
            }
            queen.poll();
        }
    }

二叉树叶节点的个数

/**
     * 广度遍历获得叶节点个数
     * @param root
     * @return
     */
public static int getLeftNum(TreeNode root)
    {
        int count=0;
        int flag=0;
        MyQueen queen=new MyQueen();
        queen.offer(root);
        TreeNode lChirld,rChirld;
        while(!queen.isEmpty())
        {
            root=(TreeNode)queen.peek();
            lChirld=root.lChild;
            if(lChirld!=null)
            {
                queen.offer(lChirld);
            }
            else
            {
                flag+=1;
            }
            rChirld=root.rChild;
            if(rChirld!=null)
            {
                queen.offer(rChirld);
            }
            else
            {
                flag+=1;
            }
            //如果同时没有左孩子和有孩子 那么叶节点个数加一
            if(flag==2)
            {
                count++;
            }
            //标志还原
            flag=0;
            queen.poll();
        }
        return count;
    }
    /**
     * 通过深度遍历获得叶节点个数
     * @param node
     */
    public static void getLeftNum2(TreeNode node)
    {
        if(node.lChild==null&&node.rChild==null)
        {
            nums++;
            return;
        }
        if(node.lChild!=null)
            getLeftNum2(node.lChild);
        if(node.rChild!=null)
            getLeftNum2(node.rChild);
    }

二叉树根节点的个数

/**
     * 广度遍历获得根节点个数
     * @param root
     * @return
     */
    public static int getRootNum(TreeNode root)
    {
        int count=0;
        //是否为叶子
        boolean isLeft=true;
        MyQueen queen=new MyQueen();
        queen.offer(root);
        TreeNode lChirld,rChirld;
        while(!queen.isEmpty())
        {
            root=(TreeNode)queen.peek();
            lChirld=root.lChild;
            //左右孩子任意一个不为空 说明为根节点
            if(lChirld!=null)
            {
                queen.offer(lChirld);
                isLeft=false;
            }
            rChirld=root.rChild;
            if(rChirld!=null)
            {
                queen.offer(rChirld);
                isLeft=false;
            }
            if(!isLeft)
            {
                count++;
            }
            isLeft=true;
            queen.poll();
        }
        return count;
    }
    /**
     * 通过深度遍历获得根节点个数
     * @param node
     */
    public static void getRootNum2(TreeNode node)
    {
        if(node.lChild==null&&node.rChild==null)
        {
            return;
        }
        else
            nums++;
        if(node.lChild!=null)
            getRootNum2(node.lChild);
        if(node.rChild!=null)
            getRootNum2(node.rChild);
    }

测试代码

public static void main(String[] args) {
        
        TreeNode father=createTree();
        preRootTreaverse(father);
        System.out.println("先序遍历");
        inRootTreaverse(father);
        System.out.println("中序遍历");
        postRootTreaverse(father);
        System.out.println("后序遍历");
        bfs(father);
        System.out.println("广度遍历");
        System.out.println("通过广度遍历获得叶节点个数:"+getLeftNum(father));
        System.out.println("通过广度遍历获得根节点个数:"+getRootNum(father));
        getLeftNum2(father);
        System.out.println("通过深度遍历获得叶节点个数:"+nums);
        nums=0;
        getRootNum2(father);
        System.out.println("通过深度遍历获得根节点个数:"+nums);
        
    }
输入数据(构建上面图的二叉树)
1
2
3
4
5
-1
6
-1
-1
7
8
-1
-1
-1
-1
-1
-1
输出结果
4 2 7 5 8 1 3 6 中序遍历
4 7 8 5 2 6 3 1 后序遍历
1 2 3 4 5 6 7 8 广度遍历
通过广度遍历获得叶节点个数:4
通过广度遍历获得根节点个数:4
通过深度遍历获得叶节点个数:4
通过深度遍历获得根节点个数:4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值