Java中一个二叉树的例子

目录

定义一个二叉树

前序遍历

中序遍历

后序遍历

求二叉树的节点数

求二叉树的叶子节点数

查找二叉树中是否有值为val的节点

求二叉树的高度

求二叉树第k层的节点

全部代码


定义一个二叉树

public class MyBinTree {
    private static class TreeNode{
            char val;
            // 左子树根节点
            TreeNode left;
            // 右子树的根节点
            TreeNode right;
            public TreeNode(char val) {
                this.val = val;
            }
        }
/**
         * 创建一个二叉树
         * @return
         */
        public static TreeNode build(){
            TreeNode nodeA = new TreeNode('A');
            TreeNode nodeB = new TreeNode('B');
            TreeNode nodeC = new TreeNode('C');
            TreeNode nodeD = new TreeNode('D');
            TreeNode nodeE = new TreeNode('E');
            TreeNode nodeF = new TreeNode('F');
            TreeNode nodeG = new TreeNode('G');
            TreeNode nodeH = new TreeNode('H');
            nodeA.left  = nodeB;
            nodeA.right = nodeC;
            nodeB.left  = nodeD;
            nodeB.right = nodeE;
            nodeE.right = nodeH;
            nodeC.left  = nodeF;
            nodeC.right = nodeG;
            return nodeA;
        }
}

前序遍历

/**
         * 前序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
         * @param root
         */
        public static void preOrder(TreeNode root){
            if (root == null){
                return;
            }
            System.out.print(root.val+"");
            preOrder(root.left);
            preOrder(root.right);
        }

中序遍历

/**
         * 中序遍历,传入一个二叉树的根节点,可以按照中序遍历的方式来输出节点值
         * @param root
         */
        public static void inOrder(TreeNode root){
            if (root == null){
                return;
            }
            inOrder(root.left);
            System.out.print(root.val+"");
            inOrder(root.right);
        }

后序遍历

 /**
         * 后序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
         * @param root
         */
        public static void postOreder(TreeNode root){
            if (root == null){
                return;
            }
            preOrder(root.left);
            preOrder(root.right);
            System.out.print(root.val+"");
        }

求二叉树的节点数

/**
         * 传入一个二叉树的根节点,统计二叉树有多少个节点,返回节点数
         * @param root
         * @return
         */
        public static int getNodes(TreeNode root){
            if (root == null){
                return 0;
            }
            return 1+getNodes(root.left)+getNodes(root.right);
        }

求二叉树的叶子节点数

/**
         * 传入一个二叉树的根节点,统计二叉树有多少个叶子节点
         * @param root
         * @return
         */
        public static int getLeafNodes(TreeNode root){
            if (root == null){
                return 0;
            }
            if (root.left == null || root.right == null){
                return 1;
            }
            return 1+getLeafNodes(root.left)+getLeafNodes(root.right);
        }

查找二叉树中是否有值为val的节点

/**
         * 查找二叉树中有没有值是val的节点
         * @param root
         * @param val
         * @return
         */
        public static boolean contains(TreeNode root,char val){
            if (root == null){
                return false;
            }
            if (root.val == val){
                return true;
            }
            return contains(root.left,val)||contains(root.right,val);
        }

求二叉树的高度

/**
         * 传入一个二叉树的根节点,返回这个二叉树的高度
         * @param root
         * @return
         */
        public static int height(TreeNode root){
            if (root == null){
                return 0;
            }
            return 1+Math.max(height(root.left),height(root.right));
        }

求二叉树第k层的节点

/**
         * 求以为root节点的第k层的节点个数
         * @param root
         * @param k
         * @return
         */
        public static int getKLevelNodes(TreeNode root,int k){
            if (root == null || k <= 0){
                return 0;
            }
            if (k == 1){
                return 1;
            }
            return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1);
        }

全部代码

/**
 * @author happy
 * 二叉树
 */
public class MyBinTree {
    public static void main(String[] args) {
        TreeNode root = build();
        // 前序遍历:ABDEHCFG
        System.out.print("前序遍历:");
        preOrder(root);
        System.out.println();
        // 中序遍历:DBEHAFCG
        System.out.print("中序遍历:");
        inOrder(root);
        System.out.println();
        // 后序遍历:BDEHCFGA
        System.out.print("后序遍历:");
        postOreder(root);
        System.out.println();
        // 这个二叉树共有8个节点
        System.out.println("这个二叉树共有"+getNodes(root)+"个节点");
        // 这个二叉树共有7个叶子节点
        System.out.println("这个二叉树共有"+getLeafNodes(root)+"个叶子节点");
        // true
        System.out.println(contains(root,'A'));
        // false
        System.out.println(contains(root,'I'));
        // 这个二叉树的高度是:4
        System.out.println("这个二叉树的高度是:"+height(root));
        // 这个二叉树第3层的节点个数是:4
        System.out.println("这个二叉树第3层的节点个数是:"+getKLevelNodes(root,3));
    }

    private static class TreeNode{
            char val;
            // 左子树根节点
            TreeNode left;
            // 右子树的根节点
            TreeNode right;
            public TreeNode(char val) {
                this.val = val;
            }
        }

        /**
         * 创建一个二叉树
         * @return
         */
        public static TreeNode build(){
            TreeNode nodeA = new TreeNode('A');
            TreeNode nodeB = new TreeNode('B');
            TreeNode nodeC = new TreeNode('C');
            TreeNode nodeD = new TreeNode('D');
            TreeNode nodeE = new TreeNode('E');
            TreeNode nodeF = new TreeNode('F');
            TreeNode nodeG = new TreeNode('G');
            TreeNode nodeH = new TreeNode('H');
            nodeA.left  = nodeB;
            nodeA.right = nodeC;
            nodeB.left  = nodeD;
            nodeB.right = nodeE;
            nodeE.right = nodeH;
            nodeC.left  = nodeF;
            nodeC.right = nodeG;
            return nodeA;
        }

        /**
         * 前序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
         * @param root
         */
        public static void preOrder(TreeNode root){
            if (root == null){
                return;
            }
            System.out.print(root.val+"");
            preOrder(root.left);
            preOrder(root.right);
        }

        /**
         * 中序遍历,传入一个二叉树的根节点,可以按照中序遍历的方式来输出节点值
         * @param root
         */
        public static void inOrder(TreeNode root){
            if (root == null){
                return;
            }
            inOrder(root.left);
            System.out.print(root.val+"");
            inOrder(root.right);
        }

        /**
         * 后序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
         * @param root
         */
        public static void postOreder(TreeNode root){
            if (root == null){
                return;
            }
            preOrder(root.left);
            preOrder(root.right);
            System.out.print(root.val+"");
        }

        /**
         * 传入一个二叉树的根节点,统计二叉树有多少个节点,返回节点数
         * @param root
         * @return
         */
        public static int getNodes(TreeNode root){
            if (root == null){
                return 0;
            }
            return 1+getNodes(root.left)+getNodes(root.right);
        }

        /**
         * 传入一个二叉树的根节点,统计二叉树有多少个叶子节点
         * @param root
         * @return
         */
        public static int getLeafNodes(TreeNode root){
            if (root == null){
                return 0;
            }
            if (root.left == null || root.right == null){
                return 1;
            }
            return 1+getLeafNodes(root.left)+getLeafNodes(root.right);
        }

        /**
         * 查找二叉树中有没有值是val的节点
         * @param root
         * @param val
         * @return
         */
        public static boolean contains(TreeNode root,char val){
            if (root == null){
                return false;
            }
            if (root.val == val){
                return true;
            }
            return contains(root.left,val)||contains(root.right,val);
        }

        /**
         * 传入一个二叉树的根节点,返回这个二叉树的高度
         * @param root
         * @return
         */
        public static int height(TreeNode root){
            if (root == null){
                return 0;
            }
            return 1+Math.max(height(root.left),height(root.right));
        }

        /**
         * 求以为root节点的第k层的节点个数
         * @param root
         * @param k
         * @return
         */
        public static int getKLevelNodes(TreeNode root,int k){
            if (root == null || k <= 0){
                return 0;
            }
            if (k == 1){
                return 1;
            }
            return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1);
        }

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的二叉树Java 代码例子,注释在代码: ```java // 定义二叉树节点类 class TreeNode { int val; // 节点的值 TreeNode left; // 左子树节点 TreeNode right; // 右子树节点 // 构造函数 TreeNode(int x) { val = x; } } // 定义二叉树类 class BinaryTree { private TreeNode root; // 根节点 // 插入节点 public void insert(int val) { root = insert(root, val); } // 递归插入节点 private TreeNode insert(TreeNode node, int val) { if (node == null) { // 如果节点为空,则新建一个节点 return new TreeNode(val); } // 如果插入的值比当前节点小,则插入左子树 if (val < node.val) { node.left = insert(node.left, val); } // 如果插入的值比当前节点大,则插入右子树 else if (val > node.val) { node.right = insert(node.right, val); } return node; } // 序遍历 public void inorder() { inorder(root); } // 递归序遍历 private void inorder(TreeNode node) { if (node == null) { return; } inorder(node.left); System.out.print(node.val + " "); inorder(node.right); } } // 示例代码 public class BinaryTreeExample { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.insert(50); tree.insert(30); tree.insert(20); tree.insert(40); tree.insert(70); tree.insert(60); tree.insert(80); System.out.print("序遍历结果:"); tree.inorder(); // 序遍历结果:20 30 40 50 60 70 80 } } ``` 以上代码演示了一个简单的二叉树的定义、插入节点和序遍历的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习java的张三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值