java实现二叉树的(查找),(树的深度),(叶的个数),(结点个数)。

该博客主要围绕二叉树展开,使用Java语言实现相关操作。先介绍了二叉树结点和树根的数据存储结构,接着通过树根创建二叉树,进行先序、中序、后序遍历,还实现了查找元素、求树深度、结点个数和叶的个数等操作,最后进行了测试。

1.二叉树的数据存储结构

(1)二叉树的结点的数据存储结构

class Node {
    //数据元素
    public Object date;
    //左树
    public Node leftchild;
    //右树
    public Node righchild;
    public Node(){
        this.righchild=this.leftchild=null;
    }
}

(2)二叉树的树根的数据存储结构

接下来:

1.我会通过树根创建一颗二叉树(树的模样在下文有图)

2.然后先序,中序,后序遍历二叉树

3.在数根类中写一些对此二叉树进行操作的实例方法

(1)查找某个元素是否存在
(2)求该树的深度
(3)求该树的结点个数
(4)求该树的叶的个数
class binaryTree{
    // 这是一个根节点
    public Node root;
    private boolean search=false;
    private int nodeNumber=0;
    private int LeafNumber=0;
    public binaryTree(){
        this.root=null;
    }
}    

2.树的递归创建以及递归遍历

树如图所示

在这里插入图片描述

public  class TreeApp{
    private int nodeNumber;
    private  int leafnumber;
    public static Node create(Node node){
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        if(!s.equals("#")){
            node=new Node();
            node.date=s;
            node.righchild=create(node.righchild);
            node.leftchild=create(node.leftchild);
        }else {
            //不添加元素 滞空
            node=null;
        }
        return node;
    }
    public static void main(String[] args) {
        TreeApp app=new TreeApp();
        binaryTree tree=new binaryTree();

         tree.root=create(tree.root);
        System.out.println("=====前序遍历====");
         qianxubianli(tree.root);

        System.out.println("=====后序遍历===");
        houbianli(tree.root);

        System.out.println("======中序遍历===");
        zhongxubianli(tree.root);
    }
    public static   void  qianxubianli(Node root){
        if(root==null)return;
        System.out.println(root.date);
        qianxubianli(root.leftchild);
        qianxubianli(root.righchild);
    }
        public static void  houbianli(Node root){
            if(root==null)return;
            houbianli(root.leftchild);
            houbianli(root.righchild);
            System.out.println(root.date);
    }
    public static void zhongxubianli(Node root){
        if(root==null)return;
        zhongxubianli(root.leftchild);
        System.out.println(root.date);
        zhongxubianli(root.righchild);
    }
}

以下是创建方式(键盘输入内容):
在这里插入图片描述

3.重点:上述1.2所有操作的实现

class binaryTree{
    // 这是一个根节点
    public Node root;
    //用于查找
    private boolean search=false;
    //用于结点个数的计算
    private int nodeNumber=0;
    //用于叶的个数计算
    private int LeafNumber=0;
    //无参构造初始化 (养成好习惯  令root=null)
    public binaryTree(){
        this.root=null;
    }
    /*	
    							binaryTree_Operation_realization
    */
//======================查找某个元素(假设树中无重复元素)====================================
  
    public boolean search(Object obj){
        search(this.root,obj);
        return this.search;
    }
    // 这里我选择后序遍历查找(你怎么遍历无所谓随意)
    private   void search(Node node,Object obj){
        if(node==null){
            return;
        }
        search(node.leftchild,obj);
        search(node.righchild,obj);
        // 如果查询到存在obj那么  实例变量search=true;
        if(obj.equals(node.date)){
            this.search=true;
        }
    }
//===========================求树的深度==============================
  
    public int calculationdepth(Node node){
        if(node==null){
            return 0;
        }
        //这里的L和r无先后顺序都可以 只是先算左孩子的深度还是右孩子的深度
        int L_length=calculationdepth(node.leftchild);
        int r_length=calculationdepth(node.righchild);
        // 这样的判断  就可以实现先算左树的深度  然后再递归完回来的时候计算的就是右树深度
        return  L_length>=r_length?L_length+1:r_length+1;
    }
//============================节点个数====================================
  
    public int  Nodenumber(){
        docalculationNodenumber(this.root);
        return this.nodeNumber;
    }
    private void docalculationNodenumber(Node node){
        if (node==null){return;}
        nodeNumber++;
        docalculationNodenumber(node.righchild);
        docalculationNodenumber(node.leftchild);
    }
//======================求叶子个数==========================================
    
    public int LeafNumber(){
        doLeafnumber(this.root);
        return this.LeafNumber;
    }
    private void doLeafnumber(Node node){
        if (node==null){
            return;
        }else{
            if(node.righchild==null && node.leftchild==null){
                LeafNumber++;
            }
        }
        doLeafnumber(node.righchild);
        doLeafnumber(node.leftchild);
    }
}

4.我们进行测试

结果如图:
在这里插入图片描述

class TreeApp{
    private int nodeNumber;
    private  int leafnumber;
    public static Node create(Node node){
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        if(!s.equals("#")){
            node=new Node();
            node.date=s;
            node.righchild=create(node.righchild);
            node.leftchild=create(node.leftchild);
        }else {
            //不添加元素 滞空
            node=null;
        }
        return node;
    }
    public static void main(String[] args) {
    		//树的创建
        binaryTree tree=new binaryTree();
        tree.root=create(tree.root);
        
        System.out.println("============查找==============");
        System.out.println(tree.search("F"));

        System.out.println("==========树的深度============");
        System.out.println(tree.calculationdepth(tree.root));

        System.out.println("==========结点个数============");
        System.out.println(tree.Nodenumber());

        System.out.println("==========叶的个数============");
        System.out.println(tree.LeafNumber());

    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值