From 22f88789b5dd579d7e6eb6bb362b6d2c26dea990 Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Tue, 4 Dec 2018 17:12:03 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0JavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84SkipList=E7=9A=84=E5=AE=9E=E7=8E=B0=202.?= =?UTF-8?q?=E4=BF=AE=E6=94=B9JavaScript=E7=89=88=E6=9C=AChashmap=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=BA=8F=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + javascript/17_skiplist/SkipList.js | 95 +++++++++++++++++++ .../{17_hashmap => 18_hashmap}/HashTable.html | 0 3 files changed, 98 insertions(+) create mode 100644 javascript/17_skiplist/SkipList.js rename javascript/{17_hashmap => 18_hashmap}/HashTable.html (100%) diff --git a/.gitignore b/.gitignore index c456446b..fb159b51 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ hs_err_pid* # editor files .vscode .*.swp + +# WebStorm +.idea/ diff --git a/javascript/17_skiplist/SkipList.js b/javascript/17_skiplist/SkipList.js new file mode 100644 index 00000000..e123f71f --- /dev/null +++ b/javascript/17_skiplist/SkipList.js @@ -0,0 +1,95 @@ +/** + * author dreamapplehappy + * 关于代码的一些解释可以看一下这里:https://github.com/dreamapplehappy/blog/tree/master/2018/12/02 + */ + +const MAX_LEVEL = 16; + +class Node{ + data = -1; + maxLevel = 0; + refer = new Array(MAX_LEVEL); +} + +class SkipList{ + levelCount = 1; + head = new Node(); + + static randomLevel() { + let level = 1; + for(let i = 1; i < MAX_LEVEL; i++) { + if(Math.random() < 0.5) { + level++; + } + } + return level; + } + + insert(value) { + const level = SkipList.randomLevel(); + const newNode = new Node(); + newNode.data = value; + newNode.maxLevel = level; + const update = new Array(level).fill(new Node()); + let p = this.head; + for(let i = level - 1; i >= 0; i--) { + while(p.refer[i] !== undefined && p.refer[i].data < value) { + p = p.refer[i]; + } + update[i] = p; + } + for(let i = 0; i < level; i++) { + newNode.refer[i] = update[i].refer[i]; + update[i].refer[i] = newNode; + } + if(this.levelCount < level) { + this.levelCount = level; + } + } + + find(value) { + if(!value){return null} + let p = this.head; + for(let i = this.levelCount - 1; i >= 0; i--) { + while(p.refer[i] !== undefined && p.refer[i].data < value) { + p = p.refer[i]; + } + } + + if(p.refer[0] !== undefined && p.refer[0].data === value) { + return p.refer[0]; + } + return null; + } + + remove(value) { + let _node; + let p = this.head; + const update = new Array(new Node()); + for(let i = this.levelCount - 1; i >= 0; i--) { + while(p.refer[i] !== undefined && p.refer[i].data < value){ + p = p.refer[i]; + } + update[i] = p; + } + + if(p.refer[0] !== undefined && p.refer[0].data === value) { + _node = p.refer[0]; + for(let i = 0; i <= this.levelCount - 1; i++) { + if(update[i].refer[i] !== undefined && update[i].refer[i].data === value) { + update[i].refer[i] = update[i].refer[i].refer[i]; + } + } + return _node; + } + return null; + } + + printAll() { + let p = this.head; + while(p.refer[0] !== undefined) { + // console.log(p.refer[0].data) + p = p.refer[0]; + } + } +} diff --git a/javascript/17_hashmap/HashTable.html b/javascript/18_hashmap/HashTable.html similarity index 100% rename from javascript/17_hashmap/HashTable.html rename to javascript/18_hashmap/HashTable.html