二叉排序树的应用(java)

package com.tree.find;

public class TestSearchBST {
    
    
    private static class BiNode{
        int data;
        BiNode lchild;
        BiNode rchild;
        
    }
    
    public static boolean hasBuild = false;
    public static void createBiTree(BiNode head, int array[]){
        head.data = array[0];
        BiNode next;
        for(int i=1;i<array.length;i++){
            hasBuild = false;
            next = new BiNode();
            next.data = array[i];
            
            build(head,next);
        }
    }
    
    
    public static void build(BiNode t, BiNode next){
        if(t != null){
            
            if(t.data < next.data){
                build(t.rchild, next);
                if(!hasBuild){ // 设置一个标量,如果结点已经构建进树,之后就直接弹出,避免已经构建好的结点出现重复覆盖的现象
                    t.rchild = next;
                    hasBuild = true;
                }
            }else{
                build(t.lchild,next);
                if(!hasBuild){
                    t.lchild = next;
                    hasBuild = true;
                }
            }
        }
        
    }
    public static void forEach(BiNode t){
        if(t != null){
            //中序遍历,可以从小到大排好序
            forEach(t.lchild);
            System.out.println(t.data);
            forEach(t.rchild);
        }
        
    }
    
    //查找
    static boolean isFind = false;
    static BiNode findedNode = null;
    public static void searchBSF(BiNode t, int key){
        if(t == null){
            return;
        }else if(key == t.data){
            findedNode = t; // 把找到的结点赋值给findedNode
            isFind = true;
        }else if(key < t.data){
            searchBSF(t.lchild,key);
        }else{
            searchBSF(t.rchild,key);
        }
    }
    
    //插入
    public static void insertBSF(BiNode t,int key){
        searchBSF(t,key);
        if(!isFind){//如果树中不存在这个数据,就插入
            BiNode n = new BiNode();
            n.data = key;
            hasBuild = false; // 刚开始是没有构建入树的
            build(t,n);
            
        }
        
    }
    
   
    public static void alert(BiNode t, int oldVal, int newVal){
        searchBSF(t,oldVal);
        if(isFind){
            findedNode.data = newVal;        
        }
        
    }
    public static void main(String[] args) {
        BiNode t = new BiNode();
        int arr[] = new int[]{23,63,76,45,12,78,89,44};
        createBiTree(t,arr);
        
        //查找数据是否存在
//        searchBSF(t,23);
//        System.out.println(isFind);
        
        //插入新数据
//        forEach(t);
//        insertBSF(t,50);
//        forEach(t);
        
//        forEach(t);
//        delete(t, 12);
//        System.out.println();
//        forEach(t);
        //searchBSF(t,23);
        //System.out.println(findedNode.data);
        //System.out.println(isFind);
        
        //修改操作
//        forEach(t);
//        alert(t, 23, 111);
//        forEach(t);
    }
}

 

转载于:https://www.cnblogs.com/wwzyy/p/6231002.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值