二叉树以及其常用方法的实现(java)

本文详细介绍了如何使用Java实现二叉树的各种常见操作,包括插入、删除、查找等,通过注释清晰地解释了代码逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注释都在代码里面了

package MyAlgorithms;

import java.util.LinkedList;
import java.util.Queue;

public class TreeNode <T>{

    //每个节点都有左右结点和当前的节点的值
    T value;
    TreeNode<T> leftchild;
    TreeNode<T> rightchild;
    TreeNode(T a){
        this.value = a;
    }

    //构造空结点
    TreeNode(){

    }

    //添加左孩子结点
    public void addleft(T v){
        this.leftchild = new TreeNode<T>(v);
    }

    //添加左孩子结点并且左孩子结点没有值时
    public void addleft(){
        this.leftchild = new TreeNode<>();
    }

    //添加右孩子结点
    public void addright(T v){
        this.rightchild = new TreeNode<T>(v);
    }

    //添加左孩子结点并且左孩子结点没有值时
    public void addright(){
        this.rightchild = new TreeNode<>();
    }

    //得到左结点
    public static <T> TreeNode<T> getLeftchild(TreeNode<T> tree){
        if(tree==null||tree.leftchild==null) return null;
        else return tree.leftchild;
    }

    //得到右结点
    public static <T> TreeNode<T> getRightchild(TreeNode<T> tree){
        if(tree==null||tree.rightchild==null) return null;
        else return tree.rightchild;
    }


    //重写equals方法
    public boolean equals(Object o){
        if(!(o instanceof TreeNode)) return false;
        return ((TreeNode) o).value == this.value;

    }

    //用递归得到结点的数目
    public static <T> int getTreeNum(TreeNode<T> tree){
        if(tree==null) return 0;
        else return 1 + getTreeNum(tree.leftchild) + getTreeNum(tree.rightchild);
    }

    //得到树的最大深度
    public static <T> int getMaxLength(TreeNode<T> tree){
        if(tree == null) return 0;
        else{
            int MaxlengthOfLeft=1,MaxLengthOfRight=1;
            return Math.max(MaxlengthOfLeft+getMaxLength(tree.leftchild),MaxLengthOfRight+getMaxLength(tree.rightchild));
        }
    }

    //显示当前结点
    public static <T> void showCurrentNode(TreeNode<T> tree){
        if(tree == null)return;
        System.out.print(tree.value+" ");
    }
    //先序遍历 :根左右
    public static <T> void PreOrderTravel(TreeNode<T> tree){
        if(tree==null) return;
        else{
            showCurrentNode(tree);
            PreOrderTravel(tree.leftchild);
            PreOrderTravel(tree.rightchild);

        }
    }

    //中序遍历 :左根右
    public static <T> void MidOrderTravel(TreeNode<T> tree){
        if(tree == null) return;
        else{
            MidOrderTravel(tree.leftchild);
            showCurrentNode(tree);
            MidOrderTravel(tree.rightchild);

        }
    }

    //后序遍历 :左右根
    public static <T> void BackOrderTravel(TreeNode<T> tree){
        MidOrderTravel(tree.leftchild);
        MidOrderTravel(tree.rightchild);
        showCurrentNode(tree);

    }

    //按层次遍历(也可以改成广度优先搜索)
    public static <T> void LayelTraverse(TreeNode<T> tree){
        if(tree==null) return;
        Queue<TreeNode<T>> queue = new LinkedList<>();
        queue.offer(tree);
        while(queue.peek()!=null){
            TreeNode<T> temp = queue.poll();
            System.out.print(temp.value+" ");
            if(temp.leftchild!=null)
                queue.offer(temp.leftchild);
            if(temp.rightchild!=null)
                queue.offer(temp.rightchild);
        }
    }

    //叶子结点的个数
    public static <T> int getLeafNum(TreeNode<T> tree){
        if(tree==null) return 0;
        if(tree.leftchild==null&&tree.rightchild==null) return 1;
        return getLeafNum(tree.leftchild)+getLeafNum(tree.rightchild);
    }


    public static void main(String [] args){
        TreeNode<Integer> test = new TreeNode<>(1);
        test.addleft(2);
        test.addright(3);
        TreeNode<Integer> firstLeftChild = getLeftchild(test);
        firstLeftChild.addleft(4);
        firstLeftChild.addright(5);
        TreeNode<Integer> secondLeftChild = getLeftchild(firstLeftChild);
        secondLeftChild.addleft(6);
        System.out.print("the deepth of test is "+getMaxLength(test)+"\n");
        System.out.print("the total num of node test is "+getTreeNum(test));
        System.out.println();
        System.out.println("先序遍历tree ");
        PreOrderTravel(test);
        System.out.println();
        System.out.println("中序遍历tree ");
        MidOrderTravel(test);
        System.out.println();
        System.out.println("后序遍历tree ");
        BackOrderTravel(test);
        System.out.println();
        System.out.println("层次遍历");
        LayelTraverse(test);
        System.out.println();
        System.out.print("the leaf of test is "+getLeafNum(test));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值