LUA資料結構 跳表

2021-10-13 05:42:32 字數 3862 閱讀 7904

本著開源,共享的精神,我將一些常用的基於lua的資料結構共享出來,大家可以直接使用

本**是乙個字乙個字敲出來的,如果你覺得有用給個贊或加個關注

local skiplist = {};

local skipnode = {};

function skipnode:new()

self = ,back = {},maxlevel = 1};

setmetatable(self,);

return self;

endskiplist.head = skipnode:new();

local toplevel = 1;

function skiplist:gettoplevel()

return toplevel;

endfunction skiplist:insert(address,value)

local node = skipnode:new();

node.address = address;

node.value = value;

node.maxlevel = self:random();

local update = {};

local nextupdate = {};

for i = 1,node.maxlevel do

update[i] = skiplist.head;

endlocal p = skiplist.head;

--每層搜尋最小節點

for k = 1,node.maxlevel do

local i = node.maxlevel - k+1;--從稀疏的頂層開始

while p.forward[i] ~= nil do

if p.forward[i].value>value then

break;

endp = p.forward[i];

endupdate[i] = p;

nextupdate[i] = update[i].forward[i];

end--按照每個索引依次的插入索引

for i= 1,node.maxlevel do

local cache = update[i].forward[i];

update[i].forward[i] = node;

node.forward[i] = cache;

if nextupdate[i] == nil then

node.back[i] = update[i];

else

node.back[i] = update[i];

nextupdate[i].back[i] = node;

endend

--獲取最大索引

if toplevel< node.maxlevel then

toplevel = node.maxlevel;

endreturn node;

endfunction skiplist:delete(value)

local update = {};

local nextupdate = {};

local p = skiplist.head;

for k = 1,toplevel do

local i = toplevel - k+1;

while p.forward[i] ~= nil do

if p.forward[i].value > value then

break;

endp = p.forward[i];

endupdate[i] = p;

nextupdate[i] = update[i].forward[i];

endif p.forward[1] ~= nil then

for k = 1,toplevel do

local i = toplevel - k + 1 ;

if update[i].back[i] ~= nil then

update[i].back[i].forward[i] = update[i].forward[i];

endif nextupdate[i] ~= nil then

nextupdate[i].back[i] = update[i].back[i];

endend

endendfunction skiplist:printall()

local p = skiplist.head;

local val = "";

while (p.forward[1] ~= nil) do

val = val..(p.forward[1].value .. " ");

p = p.forward[1];

endval = val.." ";

print(val);

end-- print(math.randomseed(tostring(os.time()):reverse():sub(1, 7)))

function skiplist:random()

local level = 1;

while math.random(100)<50 and level<16 do

level = level + 1;

endreturn level;

endfunction skiplist:find(value)

local p = skiplist.head;

for k = 1,toplevel do

local i = toplevel - k + 1 ;

while p .forward[i] ~= nil do

if p.forward[i].value > value then

break;

endp = p.forward[i];

endend

return p;

endlocal command = {};

function command.insert(address,value)

skiplist:insert(address,value);

return value;

endfunction command.delete(val)

skiplist:delete(val)

endfunction command.find(val)

if skiplist:find(val) == nil then

return nil;

endreturn skiplist:find(val).address;

endfunction command.getrange(val,range)

local temphead = skiplist:find(val);

local p = temphead;

if p == nil then

return nil;

endlocal arr = {};

while p ~= nil do

if (p.value - val)> range then

break;

endarr[p.address] = true;

p = p.forward[1];

endp = temphead;

while p ~= nil do

if (val - p.value)>= range then

break;

endarr[p.address] = true;

p = p.back[1];

endreturn arr;

end

以下是飛書鏈結,飛書鏈結裡面的版本會比這個版本更新一些,有什麼更新都會在這個版本裡面更新

資料結構之跳表

字典有一些形如 字典的節點 鍊錶節點定義 template struct pairnode pairnode const pair element element element pairnode const pair element,pairnode next element element 字典...

redis 資料結構 跳表

要先有跳表的資料結構基礎 跳表是鍊錶的乙個變種,通過增加多餘的指標,將單向鍊錶變成多向鍊錶,進而使跳表的查詢效率和平衡二叉樹看齊 平均o logn 最壞o n 而且較之二叉樹實現方便。而跳表本身,有一些比較迷的實現策略 比如,新增節點的層次是通過隨機數 拋硬幣 指定的,存在乙個隨機概率,這在redi...

資料結構 跳表改造

儲存鍵值對id value值 儲存100萬資料對 查詢value區間,比如10到100 個數?查詢id區間,比如10到100 個數?查詢某id或某value,排名第幾?在跳表實體加個索引號 skiplist skiplist new skiplist skiplist.put 5,5 skiplis...