亚欧色一区w666天堂,色情一区二区三区免费看,少妇特黄A片一区二区三区,亚洲人成网站999久久久综合,国产av熟女一区二区三区

  • 發布文章
  • 消息中心
點贊
收藏
評論
分享
原創

lua語言實現基于二叉堆的優先級隊列

2023-09-19 05:58:30
33
0

heap_swap

內部接口,交換堆中兩個節點

local function heap_swap(list, i, j)
    local temp = list[i]
    list[i] = list[j]
    list[j] = temp
    list[i].index = i
    list[j].index = j
end

heap_up

內部接口,當修改節點導致不再滿足堆的性質時調用,將不斷與上層節點交換直至滿足堆的性質

local function heap_up(list, index)
    local parent_index
    while index > 1 do
        parent_index = math.floor(index / 2)
        if list[index].priority >= list[parent_index].priority then
            break
        end
        heap_swap(list, index, parent_index)
        index = parent_index
    end
end

heap_down

內部接口,當修改節點導致不再滿足堆的性質時調用,將不斷與下層節點交換直至滿足堆的性質

local function heap_down(list, index)
    local child_index1, child_index2, min_index
    local len = #list
    while true do
        child_index1 = index * 2
        child_index2 = child_index1 + 1
        if child_index1 > len then
            break
        elseif child_index1 == len then
            min_index = len
        else
            min_index = child_index1
            if list[child_index2].priority < list[child_index1].priority then
                min_index = child_index2
            end
        end
        if list[index].priority <= list[min_index].priority then
            break
        end
        heap_swap(list, index, min_index)
        index = min_index
    end
end

heap_pop

提供給外部的api,移除堆中最小節點

function _M.pop(list)
    if #list == 0 then
        return nil, nil
    end
    local res = list[1]
    heap_swap(list, 1, #list)
    list[#list] = nil
    heap_down(list, 1)
    return res.priority, res.data
end

heap_push

提供給外部的api,插入新節點

function _M.push(list, priority, data)
    local index = #list + 1
    local node = {
        priority = priority,
        data = data,
        index = index
    }
    list[index] = node
    heap_up(list, index)
    return node
end

heap_delete

提供給外部的api,移除任意位置的節點

heap_pop也相當于heap_delete(list, 1)

function _M.delete(list, index)
    if index > #list then
        return
    end
    heap_swap(list, index, #list)
    list[#list] = nil
    local parent_index = math.floor(index / 2)
    if parent_index > 0 and list[parent_index].priority > list[index].priority then
        heap_up(list, index)
    else
        heap_down(list, index)
    end
end

 

0條評論
0 / 1000
w****h
3文章數
0粉絲數
w****h
3 文章 | 0 粉絲
原創

lua語言實現基于二叉堆的優先級隊列

2023-09-19 05:58:30
33
0

heap_swap

內部接口,交換堆中兩個節點

local function heap_swap(list, i, j)
    local temp = list[i]
    list[i] = list[j]
    list[j] = temp
    list[i].index = i
    list[j].index = j
end

heap_up

內部接口,當修改節點導致不再滿足堆的性質時調用,將不斷與上層節點交換直至滿足堆的性質

local function heap_up(list, index)
    local parent_index
    while index > 1 do
        parent_index = math.floor(index / 2)
        if list[index].priority >= list[parent_index].priority then
            break
        end
        heap_swap(list, index, parent_index)
        index = parent_index
    end
end

heap_down

內部接口,當修改節點導致不再滿足堆的性質時調用,將不斷與下層節點交換直至滿足堆的性質

local function heap_down(list, index)
    local child_index1, child_index2, min_index
    local len = #list
    while true do
        child_index1 = index * 2
        child_index2 = child_index1 + 1
        if child_index1 > len then
            break
        elseif child_index1 == len then
            min_index = len
        else
            min_index = child_index1
            if list[child_index2].priority < list[child_index1].priority then
                min_index = child_index2
            end
        end
        if list[index].priority <= list[min_index].priority then
            break
        end
        heap_swap(list, index, min_index)
        index = min_index
    end
end

heap_pop

提供給外部的api,移除堆中最小節點

function _M.pop(list)
    if #list == 0 then
        return nil, nil
    end
    local res = list[1]
    heap_swap(list, 1, #list)
    list[#list] = nil
    heap_down(list, 1)
    return res.priority, res.data
end

heap_push

提供給外部的api,插入新節點

function _M.push(list, priority, data)
    local index = #list + 1
    local node = {
        priority = priority,
        data = data,
        index = index
    }
    list[index] = node
    heap_up(list, index)
    return node
end

heap_delete

提供給外部的api,移除任意位置的節點

heap_pop也相當于heap_delete(list, 1)

function _M.delete(list, index)
    if index > #list then
        return
    end
    heap_swap(list, index, #list)
    list[#list] = nil
    local parent_index = math.floor(index / 2)
    if parent_index > 0 and list[parent_index].priority > list[index].priority then
        heap_up(list, index)
    else
        heap_down(list, index)
    end
end

 

文章來自個人專欄
文章 | 訂閱
0條評論
0 / 1000
請輸入你的評論
0
0