跳表
package mainimport ( "errors" "math" "math/rand"
) func main ( ) { }
const MAX_LEVEL = 16 type T comparabletype skipListHandle[ T comparable] interface { insert ( data T, score int32 ) ( err error ) delete ( data T, score int32 ) int findNode ( data T, score int32 ) ( error , * skipListNode[ T] )
} type skipListNode[ T comparable] struct { data Tscore int32 level int forwards [ ] * skipListNode[ T]
} type skipList[ T comparable] struct { head, tail * skipListNode[ T] level int length int32
} func createSkipList[ T comparable] ( data T) * skipList[ T] { return & skipList[ T] { level: 1 , length: 0 , head: createNode[ T] ( data, math. MinInt32, MAX_LEVEL) , }
} func createNode[ T comparable] ( data T, score int32 , level int ) * skipListNode[ T] { return & skipListNode[ T] { data: data, score: score, forwards: make ( [ ] * skipListNode[ T] , MAX_LEVEL, MAX_LEVEL) , level: level, }
}
func ( list * skipList[ T] ) Insert ( data T, score int32 ) error { currenNode := list. headpath := [ MAX_LEVEL] * skipListNode[ T] { } for i := MAX_LEVEL - 1 ; i >= 0 ; i-- { for currenNode. forwards[ i] != nil { if currenNode. forwards[ i] . score > score { path[ i] = currenNodebreak } currenNode = currenNode. forwards[ i] } if currenNode. forwards[ i] == nil { path[ i] = currenNode} } level := 1 for i := 1 ; i < MAX_LEVEL; i++ { if rand. Int31 ( ) % 7 == 1 { level++ } } newNode := createNode ( data, score, level) for i := 0 ; i <= level- 1 ; i++ { next := path[ i] . forwards[ i] newNode. forwards[ i] = nextpath[ i] . forwards[ i] = newNode} if level > list. level { list. level = level} list. length++ return errors. New ( "插入失败" )
} func ( list * skipList[ T] ) Delete ( data T, score int32 ) int { currenNode := list. headpath := [ MAX_LEVEL] * skipListNode[ T] { } for i := list. level - 1 ; i >= 0 ; i-- { path[ i] = list. headfor currenNode. forwards[ i] != nil { if currenNode. forwards[ i] . score == score && currenNode. forwards[ i] . data == data { path[ i] = currenNodebreak } currenNode = currenNode. forwards[ i] } } currenNode = path[ 0 ] . forwards[ 0 ] for i := currenNode. level - 1 ; i >= 0 ; i-- { if path[ i] == list. head && currenNode. forwards[ i] == nil { list. level = i} if nil == path[ i] . forwards[ i] { path[ i] . forwards[ i] = nil } else { path[ i] . forwards[ i] = path[ i] . forwards[ i] . forwards[ i] } } list. length-- return 0
} func ( list skipList[ T] ) FindNode ( v T, score int32 ) ( err error , node * skipListNode[ T] ) { cur := list. headfor i := list. level - 1 ; i >= 0 ; i-- { for nil != cur. forwards[ i] { if cur. forwards[ i] . score == score && cur. forwards[ i] . data == v { return nil , cur. forwards[ i] } else if cur. forwards[ i] . score > score { break } cur = cur. forwards[ i] } } return errors. New ( "请传入查找的值" ) , nil
}
测试
package mainimport ( "testing"
) func Test_createNode ( t * testing. T) { sl := createSkipList[ int ] ( 0 ) sl. Insert ( 1 , 95 ) t. Log ( sl. head. forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl) t. Log ( "-----------------------------" ) sl. Insert ( 2 , 88 ) t. Log ( sl. head. forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl) t. Log ( "-----------------------------" ) sl. Insert ( 3 , 100 ) t. Log ( sl. head. forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] . forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl) t. Log ( "-----------------------------" ) t. Log ( sl. FindNode ( 2 , 88 ) ) t. Log ( "-----------------------------" ) sl. Delete ( 1 , 95 ) t. Log ( sl. head. forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl. head. forwards[ 0 ] . forwards[ 0 ] . forwards[ 0 ] ) t. Log ( sl) t. Log ( "-----------------------------" )
}