关于python:SkipList跳表

66次阅读

共计 2498 个字符,预计需要花费 7 分钟才能阅读完成。

Python 实现
class Skiplist:

def __init__(self):
    self.head = Node()  # dummy head
def search(self, target: int) -> bool:
    node = self.head
    while node:
        while node.right and node.right.val < target:
            node = node.right
        if node.right and node.right.val == target:
            return True
        node = node.down  # 这一层没有,往下一层
    return False
def add(self, num: int) -> None:
    nodes = []
    node = self.head
    while node:
        # Move to the right in the current level
        while node.right and node.right.val < num:
            node = node.right
        nodes.append(node)
        # Move to the next level
        node = node.down
    insert = True
    down = None
    while insert and nodes:
        node = nodes.pop()
        node.right = Node(num, node.right, down)
        down = node.right
        insert = (random.getrandbits(1) == 0)
    # Create a new level with a dummy head
    # right = None
    # down = current head
    if insert:
        self.head = Node(-1, None, self.head)
def erase(self, num: int) -> bool:
    node = self.head
    found = False
    while node:
        # Move to the right in the current level
        while node.right and node.right.val < num:
            node = node.right
        # Find the target node
        if node.right and node.right.val == num:
            # Delete by skipping
            node.right = node.right.right
            found = True
        # Move to the next level
        node = node.down
    return found

class Node:

def __init__(self, val=-1, right=None, down=None):
    self.val = val
    self.right = right
    self.down = down

Your Skiplist object will be instantiated and called as such:

obj = Skiplist()

param_1 = obj.search(target)

obj.add(num)

param_3 = obj.erase(num)

Java 实现
class Skiplist {

class Node {
    int val;
    Node right;
    Node down;
    Node(){this(-1, null, null);
    }
    Node(int val, Node right, Node down) {
        this.val = val;
        this.right = right;
        this.down = down;
    }
}
Node head;
public Skiplist() {this.head = new Node();
}
public boolean search(int target) {
    Node node = head;
    while(node != null) {while(node.right != null && node.right.val < target)
            node = node.right;
        if(node.right != null && node.right.val == target)
            return true;
        node = node.down;
    }
    return false;
}
public void add(int num) {
    Node node = head;
    LinkedList<Node> nodes = new LinkedList<>();
    while(node != null) {while(node.right != null && node.right.val < num) {node = node.right;}
        nodes.add(node);
        node = node.down;
    }
    // 向上回溯,[货币代码](https://www.gendan5.com/currencycode.html) 依据概率判断是否新增节点
    boolean isInsert = true;    // 最上面一层 100% 要新增
    double c = 1.0;
    Node down = null;           // 记录一下上面一层的 down 指针
    while (!nodes.isEmpty() && isInsert) {Node cur = nodes.removeLast();
        cur.right = new Node(num, cur.right, down);
        down = cur.right;
        isInsert = count(c++);
    }
    if(isInsert)
        this.head = new Node(-1, null, head);
}
public boolean erase(int num) {
    Node node = head;
    boolean res = false;
    while(node != null) {while(node.right != null && node.right.val < num) {node = node.right;}
        if(node.right != null && node.right.val == num) {
            res = true;
            node.right = node.right.right;
        }
        node = node.down;
    }
    return res;
}
// 计算概率
static boolean count(double i) {double x = new Random().nextDouble();
    double y = 1 / Math.pow(2, i);
    return x <= y;
}

}

正文完
 0