Java二叉树的基本操作

import java.util.*;

class TreeNode{
    char data;
    TreeNode left;
    TreeNode right;
    public TreeNode(char data){
        this.data=data;
        this.right=null;
        this.left=null;
    }
}

class BiTree{
    static int index=0;
    //构建二叉树
    public TreeNode CreateTree(String str) {
        if (str.charAt(index) == '#') {
            index++;
            return null;
        } else {
            TreeNode root = new TreeNode(str.charAt(index));
            index++;
            root.left = CreateTree(str);
            root.right = CreateTree(str);
            return root;
        }
    }
    public void setIndex(){index=0;}
    //先序遍历
    public void PreOrder(TreeNode root){
        if(root==null)
            return;
        else
        {
            System.out.print(root.data+" ");
            PreOrder(root.left);
            PreOrder(root.right);
        }
    }
    //中序遍历
    public void Inorder(TreeNode root){
        if(root==null)
            return;
        else
        {
            Inorder(root.left);
            System.out.print(root.data+" ");
            Inorder(root.right);
        }
    }
    //后序遍历
    public void PostOrder(TreeNode root){
        if(root==null)
            return;
        else
        {
            PostOrder(root.left);
            PostOrder(root.right);
            System.out.print(root.data+" ");
        }
    }
    //获取二叉树的最大深度
    public int MaxDepth(TreeNode root){
        return root==null ? 0:Math.max(MaxDepth(root.left),MaxDepth(root.right))+1;
    }
    //第k层节点的个数
    public int LevelSize(TreeNode root,int k){
        if(k<1||root==null)
            return 0;
        if(k==1)
            return 1;
        else
            return LevelSize(root.left,k-1)+LevelSize(root.right,k-1);
    }
    //判断二叉树是否是平衡二叉树
    public boolean isBalanced(TreeNode root){
        if(root==null)
            return true;
        int leftDepth=MaxDepth(root.left);
        int rightDepth=MaxDepth(root.right);
        return Math.abs(leftDepth-rightDepth)<2 && isBalanced(root.left) && isBalanced(root.right);
    }
    //获取节点个数
    //若为空,则节点个数为0,
    //若不为空,则节点个数=左子树节点个数+右子树节点个数+1(自己)
    public int TreeSize(TreeNode root)
    {
        return root==null ? 0:TreeSize(root.left)+TreeSize(root.right)+1;
    }
    //获取叶子节点个数
    //若为空,则叶子节点个数为0
    //若节点的左子树和右子树均为空,则叶子节点个数为1
    //除上述两种情况外,说明该树存在字数,其叶子节点个数=左子树的叶子节点个数+右子树的叶子节点个数
    public int TreeLeafSize(TreeNode root)
    {
        if(root==null)
            return 0;
        if(root.left==null && root.right==null)
            return 1;
        return TreeLeafSize(root.left)+TreeLeafSize(root.right);
    }
    //
}

public class Main{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        String s=in.next();
        BiTree tree=new BiTree();
        TreeNode root=tree.CreateTree(s);
        tree.Inorder(root);
        System.out.println();
        System.out.println(tree.MaxDepth(root));
        System.out.println(tree.LevelSize(root,3));
        if(tree.isBalanced(root))
            System.out.println("The BinaryTree is a balanced tree");
        else
            System.out.println("The BinaryTree is not a balanced tree");
        System.out.println(tree.TreeSize(root));
        System.out.println(tree.TreeLeafSize(root));
        tree.setIndex();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树的基本算法包括二叉树的创建、遍历和查找等操作。下面是Java语言实现二叉树基本算法的示例代码: ```java // 定义二叉树节点类 class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; this.left = null; this.right = null; } } // 二叉树类 class BinaryTree { private TreeNode root; public BinaryTree() { this.root = null; } // 创建二叉树 public void createBinaryTree(int[] arr) { if (arr == null || arr.length == 0) { return; } this.root = createBinaryTree(arr, 0, arr.length - 1); } private TreeNode createBinaryTree(int[] arr, int start, int end) { if (start > end) { return null; } int mid = (start + end) / 2; TreeNode root = new TreeNode(arr[mid]); root.left = createBinaryTree(arr, start, mid - 1); root.right = createBinaryTree(arr, mid + 1, end); return root; } // 前序遍历 public void preOrder() { preOrder(this.root); } private void preOrder(TreeNode root) { if (root == null) { return; } System.out.print(root.val + " "); preOrder(root.left); preOrder(root.right); } // 中序遍历 public void inOrder() { inOrder(this.root); } private void inOrder(TreeNode root) { if (root == null) { return; } inOrder(root.left); System.out.print(root.val + " "); inOrder(root.right); } // 后序遍历 public void postOrder() { postOrder(this.root); } private void postOrder(TreeNode root) { if (root == null) { return; } postOrder(root.left); postOrder(root.right); System.out.print(root.val + " "); } // 查找节点 public TreeNode search(int val) { return search(this.root, val); } private TreeNode search(TreeNode root, int val) { if (root == null || root.val == val) { return root; } TreeNode left = search(root.left, val); if (left != null) { return left; } return search(root.right, val); } } // 测试代码 public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7}; BinaryTree binaryTree = new BinaryTree(); binaryTree.createBinaryTree(arr); System.out.print("前序遍历:"); binaryTree.preOrder(); System.out.println(); System.out.print("中序遍历:"); binaryTree.inOrder(); System.out.println(); System.out.print("后序遍历:"); binaryTree.postOrder(); System.out.println(); System.out.println("查找节点 4:" + binaryTree.search(4).val); } } ``` 输出结果为: ``` 前序遍历:4 2 1 3 6 5 7 中序遍历:1 2 3 4 5 6 7 后序遍历:1 3 2 5 7 6 4 查找节点 4:4 ``` 以上代码实现了二叉树的创建、前序遍历、中序遍历、后序遍历和查找节点等基本算法。其中,创建二叉树使用了二分法,遍历使用了递归方式实现,查找节点使用了递归方式和回溯思想实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值