首页 > 编程语言 >Java实现一个轻量的跳表demo

Java实现一个轻量的跳表demo

时间:2023-03-10 09:33:32浏览次数:39  
标签:Node Java cur forwards level int demo key 轻量

import java.util.Random;

public class SkipList {

    private final static int MAX_LEVEL = 16;

    private Node head = new Node(MAX_LEVEL, 0);
    private Random random = new Random();

    public Node search(int key) {
        Node cur = head;
        for(int level=MAX_LEVEL-1;level>=0;level--) {
            while(cur.forwards[level] != null && cur.forwards[level].key < key) {
                cur = cur.forwards[level];
            }
            if(cur.forwards[level] != null && cur.forwards[level].key == key) {
                return cur.forwards[level];
            }
        }
        return null;
    }

    public void insert(int key) {
        Node cur = head;
        int level = randomLevel();
        Node newNode = new Node(level, key);

        Node[] update = new Node[level];
        for(int i=0;i<level;i++) {
            update[i] = head;
        }

        for(int i=level-1;i>=0;i--) {
            while(cur.forwards[i] != null && cur.forwards[i].key < key) {
                cur = cur.forwards[i];
            }
            update[i] = cur;
        }

        for(int i=0;i<level;i++) {
            newNode.forwards[i] = update[i].forwards[i];
            update[i].forwards[i] = newNode;
        }
    }

    public void delete(int key) {
        Node cur = head;
        Node[] update = new Node[MAX_LEVEL];
        for(int i=MAX_LEVEL-1;i>=0;i--) {
            while(cur.forwards[i] != null && cur.forwards[i].key < key) {
                cur = cur.forwards[i];
            }
            update[i] = cur;
        }

        if(cur.forwards[0] != null && cur.forwards[0].key == key) {
            for(int i=MAX_LEVEL-1;i>=0;i--) {
                if(update[i].forwards[i] != null && update[i].forwards[i].key == key) {
                    update[i].forwards[i] = update[i].forwards[i].forwards[i];
                }
            }
        }
    }

    private int randomLevel() {
        int level = 1;
        for(int i=1;i<MAX_LEVEL;i++) {
            if(random.nextInt() % 2 == 1) {
                level++;
            }
        }
        return level;
    }

    public void print() {
        for(int level=MAX_LEVEL-1;level>=0;level--) {
            Node cur = head.forwards[level];
            System.out.print("Level " + (level+1) + ": ");
            while(cur != null) {
                System.out.print(cur.key+" ");
                cur = cur.forwards[level];
            }
            System.out.println();
        }
    }

    class Node {
        public int key;
        public Node[] forwards;

        public Node(int level, int key) {
            this.key = key;
            forwards = new Node[level];
        }
    }

    public static void main(String[] args) {
        SkipList skipList = new SkipList();
        skipList.insert(1);
        skipList.insert(4);
        skipList.insert(2);
        skipList.insert(3);
        skipList.print();
        skipList.delete(4);
        skipList.print();
        Node node = skipList.search(2);
        if(node != null) {
            System.out.println(node.key);
        }
    }
}

标签:Node,Java,cur,forwards,level,int,demo,key,轻量
From: https://www.cnblogs.com/Roni-i/p/17202289.html

相关文章

  • java的byte和C#的byte的不同之处
    Javabytejavabyte是做为最小的数字来处理的,因此它的值域被定义为-128~127,byte,即字节,由8位的二进制组成。在Java中,byte类型的数据是8位带符号的二进制数。在计算机中,8位......
  • java中的特殊文件、日志技术、多线程入门
    一,属性文件(.properties)1,特殊文件概述(必会)我们知道IO流是用来读数据,目的是为了获取其中的信息供我们使用,但是普通的txt文件是杂乱无章的,除非我们规定,自己写。虽然可以但......
  • 生产环境Java应用服务内存泄漏分析与解决
    有个生产环境CRM业务应用服务,情况有些奇怪,监控数据显示内存异常。内存使用率99.%多。通过生产监控看板发现,CRM内存超配或内存泄漏的现象,下面分析一下这个问题过程记录。服......
  • 关于JAVA泛型数组类型擦除引发的问题及解决方案
    先看如下一个DEMO示例代码:(其中doBatchGet被子类重写了1次)publicabstractclassBaseDemoService<T>{publicStringbatchGet(T...ints){Tone=ints[......
  • java中的泛型
    @目录1泛型概述2泛型格式3泛型增强3.1泛型方法单一方法增强整体方法增强3.2泛型类3.3泛型接口约束模式1泛型概述参数化类型。在不创建新的类型的情况下,通过泛型指定的不......
  • Java基础——HashMap 的长度为什么是 2 的幂次方
    HashMap的长度为什么是2的幂次方为了能让HashMap存取高效,尽量较少碰撞,也就是要尽量把数据分配均匀。我们上面也讲到了过了,Hash值的范围值-2147483648到2147483647......
  • java8新特性/函数式编程/lamda/stream流
    新特性简介   java8内置的四大核心函数式接口          其他接口  方法引用               构造使用......
  • Java实现对象空属性(空字符串)转null
    @Slf4jpublicclassConvertUtils{/***@Description主要解决查询时前端传参为空值("")*BeanUtils.copyProperties会把空值带入目标对象中*......
  • Java数据类型转换
    类型转换由于Java是强类型语言,所以要进行有些运算的时候需要用到类型转换。低 ---------------------------------> 高byte,short,char->int->long->float->doub......
  • Java Set Summary
    JavaSetSummary一、概要Set6个类名since线程安全elementnull特点Set1.2HashSet1.2NoYes基于HashMap实现TreeSet1.2NoNo基于TreeMa......