Go Language implementation Jump Table (Skiplist)

Source: Internet
Author: User

Jump Table (skiplist) in the REDIS/LEVELDB is the core data structure, I simply rough with Golang implementation of the next.

As far as my simple understanding is concerned, on a regular list, when the insert, through the Random_level (), the layer becomes a lot of layers,

The more data is smaller, the greater the span. Look up from the top down, use space for time.

Write down the test code:

Package Mainimport ("FMT"//"Github.com/xclpkg/algorithm" "Math/rand") func main () {slt: = Newskiplist () for I: = n; i > 0; i--{slt. Insert (i)}slt. Printskiplist () slt. Search (SLT). Search (SLT). Search (SLT). Delete (SLT). Printskiplist ()}const skiplist_maxlevel = 8//32const skiplist_p = 4type skiplist struct {Header []listlevel int}func New Skiplist () *skiplist {return &skiplist{level:1, Header:make ([]list, Skiplist_maxlevel)}}func (SkipList *SkipList) Insert (key int) {update: = make (Map[int]*node) for I: = Len (Skiplist.header)-1; I >= 0; i--{if skiplist.header[i]. Len () > 0 {for E: = Skiplist.header[i]. Front (); E! = nil; E = E.next () {if e.value. int) >= Key {Update[i] = Ebreak}}//HEAER[LV]. List}//header Levellevel: = Skiplist.random_level () if level > skiplist.level {skiplist.level = level}for I: = 0; I < level; i++ {if V, OK: = Update[i]; OK {skiplist.header[i]. InsertBefore (Key, V)} else {Skiplist.header[i]. Pushback (key)}}}func (Skiplist *skiplist) Search (kEY int) *node {for I: = Len (Skiplist.header)-1; I >= 0; i--{if skiplist.header[i]. Len () > 0 {for E: = Skiplist.header[i]. Front (); E! = nil; E = E.next () {switch {case e.value. int) = = Key:fmt. Println ("Found level=", I, "key=", key) return Ecase E.value. (int) > Key:break}}//end for}//end if}//end forreturn Nil}func (skiplist *skiplist) Delete (key int) {for i: = Len (sk Iplist.header)-1; I >= 0; i--{if skiplist.header[i]. Len () > 0 {for E: = Skiplist.header[i]. Front (); E! = nil; E = E.next () {switch {case e.value. int) = = Key:fmt. Println ("Delete level=", I, "key=", Key) Skiplist.header[i].remove (e) Case E.value. (int) > Key:break}}//end for}//end if}//end for}func (skiplist *skiplist) printskiplist () {fmt. Println ("\nskiplist-------------------------------------------") for I: = SKIPLIST_MAXLEVEL-1; I >= 0; i--{fmt. Println ("Level:", I) if skiplist.header[i]. Len () > 0 {for E: = Skiplist.header[i]. Front (); E! = nil; E = E.next () {fmt. Printf ("%d", E.value)}//end for}//end iffmt. Println ("\ n--------------------------------------------------------")}//end For}func (skiplist *skiplist) random_ Level () int {level: = 1for (rand. Int31 () &0xffff)%skiplist_p = = 0 {level + = 1}if Level < Skiplist_maxlevel {return level} else {return skiplist_maxle Vel}}//////////////////////////////////////////////////////type Node struct {Next, prev *nodelist *listvalue Interfa Ce{}}type List struct {root Nodelen int}func (node *node) Next () *node {if p: = Node.next; Node.list! = Nil && P  ! = &node.list.root {return P}return nil}func (node *node) Prev () *node {if p: = Node.prev; Node.list! = Nil && P! = &node.list.root {return p}return nil}func (list *list) Init () *list {list.root.next = &AMP;LIST.ROOTLIST.ROOT.PR EV = &list.rootlist.len = 0return list}func new () *list {return new (list). Init ()}func (list *list) Lazyinit () {if List.root.next = = nil {list. Init ()}}func (list *list) Len () int {return List.len}func (list *list) RemoVE (e *node) *node {e.prev.next = E.nexte.next.prev = E.preve.next = Nile.prev = Nile.list = Nillist.len--return E}func (li St *list) Remove (e *node) interface{} {if e.list = = List {List.remove (e)}return e.value}func (List *list) Front () *node {i F List.len = = 0 {return nil}return list.root.next}func (list *list) back () *node {if List.len = = 0 {return Nil}return list . Root.prev}func (list *list) Insert (E, at *node) *node {n: = At.nextat.next = Ee.prev = Ate.next = Nn.prev = Ee.list = Lis  Tlist.len++return E}func (list *list) Insertvalue (v interface{}, at *node) *node {return List.insert (&node{value:v}, at)}func (list *list) insertbefore (v interface{}, Mark *node) *node {if mark.list! = List {return Nil}return LIST.INSERTV Alue (V, Mark.prev)}func (list *list) pushback (v interface{}) *node {list.lazyinit () return List.insertvalue (V, List.root.prev)}

Effect:  

Skiplist-------------------------------------------Level:7---------------------------------------------------- ----Level:6--------------------------------------------------------level:5------------------------------------ --------------------level:4--------------------------------------------------------level:393------------------ --------------------------------------level:236--------------------------------------------------------Level  : 2, the ten-in-a-si-si  3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45  46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8384 85 86 All in all, 94, 98, Found level= 0 key= 20 Found level= 2 key= 36Found level= 3 key= 93Delete level= 1 key= 55Delete level= 0 key= 55SkipList-------------------------------------------level:7----------------------------- ---------------------------level:6--------------------------------------------------------Level:5------------- -------------------------------------------Level:4-------------------------------------------------------- level:393--------------------------------------------------------level:236---------------------------------- ----------------------Level:16-----------------------  -------------level:01 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 7 9 8485 of the Bay of the Bay of Bayi, the 94 of the 98
Get.


MAIL: [Email protected]
blog:http://blog.csdn.net./xcl168



Go Language implementation Jump Table (Skiplist)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.