zoukankan      html  css  js  c++  java
  • 二叉排序树的应用(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);
        }
    }
  • 相关阅读:
    以太坊公开拍卖智能合约案例
    部署智能合约
    ERC20 Token
    查看crontab运行状态
    php把excel数值格式转成日期格式
    PHP识别中文编码并自动转换为UTF-8
    python中文件内容替换
    mysqldump备份指定的数据
    linux设置服务器时间
    git常用命令
  • 原文地址:https://www.cnblogs.com/wwzyy/p/6231002.html
Copyright © 2011-2022 走看看