跳表的实现代码

package skiplist;

//节点
class Node{
    int value = -1;
    int level;//跨越几层
    Node[] next;//指向下一个节点

    public Node(int value, int level) {
        this.value = value;
        this.level = level;
        this.next = new Node[level];
    }
}
//跳跃表
public class SkipList {
    //允许的最大层数
    int maxLevel = 16;
    //头节点,充当辅助。
    Node head = new Node(-1, 16);
    //当前跳跃表节点的个数
    int size = 0;
    //当前跳跃表的层数,初始化为1层。
    int levelCount = 1;


    public Node find(int value) {
        Node temp = head;
        for (int i = levelCount - 1; i >= 0; i--) {
            while (temp.next[i] != null && temp.next[i].value < value) {
                temp = temp.next[i];
            }
        }
        //判断是否有该元素存在
        if (temp.next[0] != null && temp.next[0].value == value) {
            System.out.println(value + "  查找成功");
            return temp.next[0];
        } else {
            return null;
        }
    }
    // 为了方便,跳跃表在插入的时候,插入的节点在当前跳跃表是不存在的
    //不允许插入重复数值的节点。
    public void insert(int value) {
        int level = getLevel();
        Node newNode = new Node(value, level);
        //update用于记录要插入节点的前驱
        Node[] update = new Node[level];

        Node temp = head;
        for (int i = level - 1; i >= 0; i--) {
            while (temp.next[i] != null && temp.next[i].value < value) {
                temp = temp.next[i];
            }
            update[i] = temp;
        }
        //把插入节点的每一层连接起来
        for (int i = 0; i < level; i++) {
            newNode.next[i] = update[i].next[i];
            update[i].next[i] = newNode;
        }
        //判断是否需要更新跳跃表的层数
        if (level > levelCount) {
            levelCount = level;
        }
        size++;
        System.out.println(value + " 插入成功");
    }

    public void delete(int value) {
        Node[] update = new Node[levelCount];
        Node temp = head;

        for (int i = levelCount - 1; i >= 0; i--) {
            while (temp.next[i] != null && temp.next[i].value < value) {
                temp = temp.next[i];
            }
            update[i] = temp;
        }

        if (temp.next[0] != null && temp.next[0].value == value) {
            size--;
            System.out.println(value + " 删除成功");
            for (int i = levelCount - 1; i >= 0; i--) {
                if (update[i].next[i] != null && update[i].next[i].value == value) {
                    update[i].next[i] = update[i].next[i].next[i];
                }
            }
        }
    }

    //打印所有节点
    public void printAllNode() {
        Node temp = head;
        while (temp.next[0] != null) {
            System.out.println(temp.next[0].value + "  ");
            temp = temp.next[0];
        }
    }

    //模拟抛硬币
    private int getLevel() {
        int level = 1;
        while (true) {
            int t = (int)(Math.random() * 100);
            if (t % 2 == 0) {
                level++;
            } else {
                break;
            }
        }
        System.out.println("当前的level = " + level);
        return level;
    }

    //测试数据
    public static void main(String[] args) {
        SkipList list = new SkipList();
        for (int i = 0; i < 6; i++) {
            list.insert(i);
        }
        list.printAllNode();
        list.delete(4);
        list.printAllNode();
        System.out.println(list.find(3));
        System.out.println(list.size + " " + list.levelCount);
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: package mainimport ( "fmt" )// 节点 type node struct { key int value string left *node right *node }// 跳表 type skipList struct { head *node length int }func main() { sl := &skipList{ head: &node{ key: 0, value: "", left: nil, right: nil, }, length: 0, } // 插入 sl.insert(1, "a") sl.insert(2, "b") sl.insert(3, "c") sl.insert(4, "d") sl.insert(5, "e") fmt.Println(sl) // 查找 fmt.Println(sl.find(4)) // 删除 sl.delete(4) fmt.Println(sl) }// 插入 func (sl *skipList) insert(key int, value string) { // 创建新节点 newNode := &node{ key: key, value: value, left: nil, right: nil, } // 定义当前节点为头结点 cur := sl.head // 寻找位置插入 for cur.right != nil { if cur.right.key > key { break } cur = cur.right } // 插入 newNode.right = cur.right cur.right = newNode newNode.left = cur // 增加长度 sl.length++ }// 查找 func (sl *skipList) find(key int) string { cur := sl.head for cur.right != nil { if cur.right.key > key { break } cur = cur.right } if cur.key == key { return cur.value } return "" }// 删除 func (sl *skipList) delete(key int) { cur := sl.head for cur.right != nil { if cur.right.key > key { break } cur = cur.right } if cur.right != nil && cur.right.key == key { cur.right = cur.right.right if cur.right != nil { cur.right.left = cur } sl.length-- } } ### 回答2: 跳表是一种快速查找的数据结构,类似于有序链表加上多级索引的结构。下面是用Go语言实现跳表代码: ```go package main import ( "fmt" "math" ) type SkipListNode struct { value int next []*SkipListNode } type SkipList struct { head *SkipListNode level int length int } func NewSkipListNode(value, level int) *SkipListNode { return &SkipListNode{ value: value, next: make([]*SkipListNode, level), } } func NewSkipList() *SkipList { // 创建头节点,值为负无穷 head := NewSkipListNode(math.MinInt32, 32) return &SkipList{ head: head, level: 0, length: 0, } } func (list *SkipList) Insert(value int) { update := make([]*SkipListNode, list.level+1) node := list.head // 查找插入位置 for i := list.level; i >= 0; i-- { for node.next[i] != nil && node.next[i].value < value { node = node.next[i] } update[i] = node } // 随机生成索引层数,使其符合跳表的概率分布 level := 0 for (level < 32-1) && (level < list.level+1) && (rand.Int()%2 == 0) { level++ } // 创建新节点 insertNode := NewSkipListNode(value, level+1) // 更新索引 for i := 0; i <= level; i++ { insertNode.next[i] = update[i].next[i] update[i].next[i] = insertNode } // 更新跳表的层数 if level > list.level { list.level = level } list.length++ } func (list *SkipList) Search(target int) bool { node := list.head // 从最高层开始搜索 for i := list.level; i >= 0; i-- { for node.next[i] != nil && node.next[i].value <= target { if node.next[i].value == target { return true } node = node.next[i] } } return false } func main() { list := NewSkipList() list.Insert(3) list.Insert(5) list.Insert(8) list.Insert(10) list.Insert(12) fmt.Println(list.Search(8)) // true fmt.Println(list.Search(6)) // false } ``` 以上是一个简单的跳表实现,包括插入和搜索两个基本操作。通过随机生成索引层数,可以实现快速的查找。 ### 回答3: 下面是使用Go语言实现跳表代码段示例: ```go package main import ( "fmt" "math" "math/rand" ) type Node struct { value int next []*Node } type SkipList struct { head *Node level int } func newNode(value, level int) *Node { return &Node{ value: value, next: make([]*Node, level), } } func newSkipList() *SkipList { return &SkipList{ head: newNode(math.MinInt32, 1), level: 1, } } func (skiplist *SkipList) search(target int) bool { curr := skiplist.head for i := skiplist.level - 1; i >= 0; i-- { for curr.next[i] != nil && curr.next[i].value < target { curr = curr.next[i] } if curr.next[i] != nil && curr.next[i].value == target { return true } } return false } func (skiplist *SkipList) insert(value int) { level := skiplist.randomLevel() newNode := newNode(value, level) update := make([]*Node, level) curr := skiplist.head for i := level - 1; i >= 0; i-- { for curr.next[i] != nil && curr.next[i].value < value { curr = curr.next[i] } update[i] = curr } for i := 0; i < level; i++ { newNode.next[i] = update[i].next[i] update[i].next[i] = newNode } if level > skiplist.level { skiplist.level = level } } func (skiplist *SkipList) erase(value int) { update := make([]*Node, skiplist.level) curr := skiplist.head for i := skiplist.level - 1; i >= 0; i-- { for curr.next[i] != nil && curr.next[i].value < value { curr = curr.next[i] } update[i] = curr } if curr.next[0] != nil && curr.next[0].value == value { for i := skiplist.level - 1; i >= 0; i-- { if update[i].next[i] != nil && update[i].next[i].value == value { update[i].next[i] = update[i].next[i].next[i] } } } } func (skiplist *SkipList) randomLevel() int { level := 1 for rand.Float32() < 0.5 && level < 32 { level++ } return level } func main() { skiplist := newSkipList() skiplist.insert(3) skiplist.insert(6) skiplist.insert(9) skiplist.insert(2) skiplist.insert(5) skiplist.insert(8) fmt.Println(skiplist.search(6)) // 输出: true fmt.Println(skiplist.search(12)) // 输出: false skiplist.erase(6) fmt.Println(skiplist.search(6)) // 输出: false } ``` 此代码实现了一个基本的跳表数据结构,其中SkipList是一个包含了跳表头结点和层数的结构体。可以通过insert方法向跳表中插入元素,通过search方法在跳表中搜索元素,通过erase方法删除跳表中的某个元素。运行main函数可以看到跳表的基本操作结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙崎流河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值