自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

結(jié)合React源碼,五分鐘帶你掌握優(yōu)先隊(duì)列

開發(fā) 前端
最近寫一個(gè)需求用到了優(yōu)先隊(duì)列和二叉堆的相關(guān)知識(shí),借此機(jī)會(huì)梳理了一些二叉堆的相關(guān)知識(shí)分享給大家。

最近寫一個(gè)需求用到了優(yōu)先隊(duì)列和二叉堆的相關(guān)知識(shí),借此機(jī)會(huì)梳理了一些二叉堆的相關(guān)知識(shí)分享給大家。

什么是優(yōu)先隊(duì)列

  • 優(yōu)先隊(duì)列是數(shù)據(jù)結(jié)構(gòu)中的基礎(chǔ)概念,與隊(duì)列先進(jìn)先出(FIFO)的出隊(duì)順序不同的是 ,它的出隊(duì)順序與元素的優(yōu)先級(jí)相關(guān)。
  • 例如 React 的時(shí)間分片(React Fiber),它將渲染任務(wù)分了優(yōu)先級(jí),出隊(duì)的順序與任務(wù)的“重要程度”存在關(guān)系,那么滿足這種情況的數(shù)據(jù)結(jié)構(gòu)就是 優(yōu)先隊(duì)列 。

優(yōu)先隊(duì)列的操作

  • 插入:在優(yōu)先隊(duì)列中插入元素,并使隊(duì)列“有序”
  • 刪除最大/最小值:刪除并返回最大/最小的元素,并使隊(duì)列“有序”
  • 查找最大/最小關(guān)鍵字:查找最大/最小的值

優(yōu)先隊(duì)列的實(shí)現(xiàn)比較

 

優(yōu)先隊(duì)列可以由以上多種方式實(shí)現(xiàn),而優(yōu)先隊(duì)列的主要操作是插入和刪除,其中二叉搜索樹和二叉堆這兩項(xiàng)操作的時(shí)間復(fù)雜度均為 logn ,但二叉樹在多次刪除之后容易導(dǎo)致樹的傾斜,同時(shí)查找成本也高于二叉堆,所以最終二叉堆是比較符合實(shí)現(xiàn)優(yōu)先隊(duì)列的數(shù)據(jù)結(jié)構(gòu)。

二叉堆

在二叉堆中數(shù)組中,要保證每個(gè)元素都小于(大于)或等于另外兩個(gè)特定位置的元素。例如下圖的樹中,父節(jié)點(diǎn)總是小于或等于子節(jié)點(diǎn)。

對(duì)于二叉堆有如下性質(zhì):

  • 節(jié)點(diǎn) k 的父節(jié)點(diǎn)下標(biāo)為 k / 2(向下取整)
  • 已某節(jié)點(diǎn)為根節(jié)點(diǎn)的子樹,該節(jié)點(diǎn)是這顆樹的極值

二叉堆的操作

插入

二叉堆的插入非常簡(jiǎn)單,只需要在二叉堆的最后添加要插入的內(nèi)容,并將其“上浮”到正確位置。

嘗試在上面的二叉堆中插入新元素 9,過程如下:

在尾部插入元素 9,與父節(jié)點(diǎn)進(jìn)行對(duì)比,有序性被破壞,與父元素替換位置。

替換成功后,繼續(xù)上一輪操作,與父節(jié)點(diǎn)進(jìn)行對(duì)比,仍然無法滿足有序性,繼續(xù)調(diào)換位置。

再次替換后符合。

程序框架

 

  1. function push { 
  2.   * 在堆尾部添加元素 
  3.   * 執(zhí)行上浮循環(huán) 
  4.     * 與父元素對(duì)比大小,將較大的放在父節(jié)點(diǎn)位置 
  5.  
  6.   return minItem 

實(shí)現(xiàn)

 

  1. function push(heap: Heap, node: Node): void { 
  2.   const index = heap.length; 
  3.   heap.push(node); // 在堆尾部添加元素 
  4.   siftUp(heap, node, index); // 進(jìn)行上浮操作 
  5.  
  6. function siftUp(heap, node, i) { 
  7.   let index = i; 
  8.   while (true) { 
  9.     const parentIndex = (index - 1) >>> 1; // 父節(jié)點(diǎn)位置: parentIndex = childIndex / 2 
  10.     const parent = heap[parentIndex]; 
  11.     if (parent !== undefined && compare(parent, node) > 0) { 
  12.       // The parent is larger. Swap positions. 
  13.       heap[parentIndex] = node; 
  14.       heap[index] = parent; 
  15.       index = parentIndex; 
  16.     } else { 
  17.       // The parent is smaller. Exit. 
  18.       return
  19.     } 
  20.   } 

刪除

取出根節(jié)點(diǎn)的值對(duì)比插入稍微復(fù)雜一點(diǎn),歸納起來可以分為三步:

  • 取出根節(jié)點(diǎn)的值
  • 將最后一個(gè)元素與根節(jié)點(diǎn)進(jìn)行替換,并刪除最后一個(gè)元素
  • 下沉

取出根節(jié)點(diǎn)。

將最后一個(gè)元素與根節(jié)點(diǎn)調(diào)換,并刪除。對(duì)比發(fā)現(xiàn)有序性被破壞,進(jìn)行對(duì)調(diào)。

完成刪除。

程序框架

 

  1. function pop { 
  2.   * 設(shè)定 minItem 保存根節(jié)點(diǎn) 
  3.   * 取出最后一個(gè)節(jié)點(diǎn)與根節(jié)點(diǎn)替換,并刪除最后一個(gè)節(jié)點(diǎn) 
  4.   * 執(zhí)行下沉循環(huán) 
  5.     * 將根元素與左右子節(jié)點(diǎn)對(duì)比,挑選較小的與父節(jié)點(diǎn)替換位置 
  6.  
  7.   return minItem 

實(shí)現(xiàn)

 

  1. export function pop(heap: Heap): Node | null { 
  2.   const first = heap[0]; // 取出根節(jié)點(diǎn) 
  3.   if (first !== undefined) { 
  4.     const last = heap.pop(); // 取出最后一位元素,并刪除 
  5.     if (last !== first) { 
  6.       heap[0] = last; // 與根節(jié)點(diǎn)對(duì)調(diào) 
  7.       siftDown(heap, last, 0); // 下沉 
  8.     } 
  9.     return first
  10.   } else { 
  11.     return null
  12.   } 
  13.  
  14. function siftDown(heap, node, i) { 
  15.   let index = i; 
  16.   const length = heap.length; 
  17.   while (index < length) { 
  18.     const leftIndex = (index + 1) * 2 - 1; 
  19.     const left = heap[leftIndex]; 
  20.     const rightIndex = leftIndex + 1; 
  21.     const right = heap[rightIndex]; 
  22.  
  23.     // If the left or right node is smaller, swap with the smaller of those. 
  24.     // 尋找左右兒子較小的那一個(gè)替換 
  25.     if (left !== undefined && compare(left, node) < 0) { //左子節(jié)點(diǎn)小于根節(jié)點(diǎn) 
  26.       if (right !== undefined && compare(rightleft) < 0) { 
  27.         heap[index] = right
  28.         heap[rightIndex] = node; 
  29.         index = rightIndex; 
  30.       } else { 
  31.         heap[index] = left
  32.         heap[leftIndex] = node; 
  33.         index = leftIndex; 
  34.       } 
  35.     } else if (right !== undefined && compare(right, node) < 0) { // 左子節(jié)點(diǎn)大于根節(jié)點(diǎn),右子節(jié)點(diǎn)小于根節(jié)點(diǎn) 
  36.       heap[index] = right
  37.       heap[rightIndex] = node; 
  38.       index = rightIndex; 
  39.     } else { 
  40.       // Neither child is smaller. Exit. 
  41.       return
  42.     } 
  43.   } 

以下是 react 源碼中 scheduler/src/SchedulerMinHeap.js 關(guān)于最小堆的完整實(shí)現(xiàn):

 

  1. /** 
  2.  * Copyright (c) Facebook, Inc. and its affiliates. 
  3.  * 
  4.  * This source code is licensed under the MIT license found in the 
  5.  * LICENSE file in the root directory of this source tree. 
  6.  * 
  7.  * @flow strict 
  8.  */ 
  9.  
  10. // 定義最小堆極其元素,其中 sortIndex 為最小堆對(duì)比的 key,若 sortIndex 相同,則對(duì)比 id 
  11. type Heap = Array<Node>; 
  12. type Node = {| 
  13.   id: number, 
  14.   sortIndex: number, 
  15. |}; 
  16.  
  17. // 入隊(duì)操作,在入隊(duì)完成之后進(jìn)行“上浮” 
  18. export function push(heap: Heap, node: Node): void { 
  19.   const index = heap.length; 
  20.   heap.push(node); 
  21.   siftUp(heap, node, index); 
  22.  
  23. // 查找最大值 
  24. export function peek(heap: Heap): Node | null { 
  25.   const first = heap[0]; 
  26.   return first === undefined ? null : first
  27.  
  28. // 刪除并返回最大值 
  29. export function pop(heap: Heap): Node | null { 
  30.   const first = heap[0]; // 取出根節(jié)點(diǎn)(哨兵) 
  31.   if (first !== undefined) { 
  32.     const last = heap.pop(); // 取出最后一位元素,并刪除 
  33.     if (last !== first) { // 頭尾并沒有對(duì)撞 
  34.       heap[0] = last; // 與根節(jié)點(diǎn)對(duì)調(diào) 
  35.       siftDown(heap, last, 0); // 下沉 
  36.     } 
  37.     return first
  38.   } else { 
  39.     return null
  40.   } 
  41.  
  42. // 上浮,調(diào)整樹結(jié)構(gòu) 
  43. function siftUp(heap, node, i) { 
  44.   let index = i; 
  45.   while (true) { 
  46.     const parentIndex = (index - 1) >>> 1; // 父節(jié)點(diǎn)位置: parentIndex = childIndex / 2,此處使用位操作,右移一位 
  47.     const parent = heap[parentIndex]; 
  48.     if (parent !== undefined && compare(parent, node) > 0) { // 對(duì)比父節(jié)點(diǎn)和子元素的大小 
  49.       // The parent is larger. Swap positions. 
  50.       heap[parentIndex] = node; // 若父節(jié)點(diǎn)較大,則更換位置 
  51.       heap[index] = parent; 
  52.       index = parentIndex; 
  53.     } else { 
  54.       // The parent is smaller. Exit. 
  55.       return
  56.     } 
  57.   } 
  58.  
  59. // 下沉,調(diào)整樹結(jié)構(gòu) 
  60. function siftDown(heap, node, i) { 
  61.   let index = i; 
  62.   const length = heap.length; 
  63.   while (index < length) { 
  64.     const leftIndex = (index + 1) * 2 - 1; 
  65.     const left = heap[leftIndex]; 
  66.     const rightIndex = leftIndex + 1; 
  67.     const right = heap[rightIndex]; 
  68.  
  69.     // If the left or right node is smaller, swap with the smaller of those. 
  70.     // 尋找左右兒子較小的那一個(gè)替換 
  71.     if (left !== undefined && compare(left, node) < 0) { 
  72.       if (right !== undefined && compare(rightleft) < 0) { // 左子節(jié)點(diǎn)小于根節(jié)點(diǎn) 
  73.         heap[index] = right
  74.         heap[rightIndex] = node; 
  75.         index = rightIndex; 
  76.       } else { 
  77.         heap[index] = left
  78.         heap[leftIndex] = node; 
  79.         index = leftIndex; 
  80.       } 
  81.     } else if (right !== undefined && compare(right, node) < 0) { // 左子節(jié)點(diǎn)大于根節(jié)點(diǎn),右子節(jié)點(diǎn)小于根節(jié)點(diǎn) 
  82.       heap[index] = right
  83.       heap[rightIndex] = node; 
  84.       index = rightIndex; 
  85.     } else { 
  86.       // Neither child is smaller. Exit. 
  87.       return
  88.     } 
  89.   } 
  90.  
  91. function compare(a, b) { 
  92.   // Compare sort index firstthen task id. 
  93.   const diff = a.sortIndex - b.sortIndex; 
  94.   return diff !== 0 ? diff : a.id - b.id; 

堆排序

利用最大/最小堆的特性,我們很容易就能實(shí)現(xiàn)對(duì)數(shù)組的排序,重復(fù)執(zhí)行 pop 就能進(jìn)行升序排列,如果要降序,使用最大堆即可,該操作時(shí)間復(fù)雜度為 nlogn 。

多叉堆

為了追求更優(yōu)的時(shí)間復(fù)雜度,我們可以將二叉堆改為多叉堆實(shí)現(xiàn),下圖為一個(gè)三叉堆:

與二叉堆不同的是對(duì)于含有 N 個(gè)元素的 d 叉堆(通常情況下 d >= 2),隨著 d 的增加,樹高 K = logdN 的斜率會(huì)下降,然而 d 越大,刪除操作的成本會(huì)更高。所以子元素不是越多越好,通常情況下三叉堆和四叉堆的應(yīng)用會(huì)比較常見。

在libev中有這么一段注釋 https://github.com/enki/libev/blob/master/ev.c#L2227,他提及了四叉樹相比二叉堆來說緩存更加友好。 根據(jù)benchmark,在 50000+ 個(gè) watchers 的場(chǎng)景下,四叉樹會(huì)有 5% 的性能優(yōu)勢(shì)。

 

  1. /* 
  2.  * at the moment we allow libev the luxury of two heaps, 
  3.  * a small-code-size 2-heap one and a ~1.5kb larger 4-heap 
  4.  * which is more cache-efficient. 
  5.  * the difference is about 5% with 50000+ watchers. 
  6.  */ 

同樣 Go 語言中的定時(shí)器的 timersBucket 的數(shù)據(jù)結(jié)構(gòu)也采用了最小四叉堆。

 

 


 

結(jié)語

多叉堆,例如四叉堆更加適合數(shù)據(jù)量大,對(duì)緩存要求友好對(duì)場(chǎng)景。二叉堆適用數(shù)據(jù)量比較小且頻繁插入和刪除的場(chǎng)景。通常情況下二叉堆可以滿足大部分情況下的需求,如果編寫底層代碼,并且對(duì)性能有更高的要求,那么可以考慮多叉堆實(shí)現(xiàn)優(yōu)先隊(duì)列。

責(zé)任編輯:未麗燕 來源: ZooTeam Blog
相關(guān)推薦

2025-01-24 08:38:47

2025-03-13 06:22:59

2021-06-07 09:51:22

原型模式序列化

2009-11-17 14:50:50

Oracle調(diào)優(yōu)

2021-10-19 07:27:08

HTTP代理網(wǎng)絡(luò)

2019-12-23 16:42:44

JavaScript前端開發(fā)

2022-05-30 07:51:13

數(shù)據(jù)庫MySQLQPS

2009-11-05 10:55:22

Visual Stud

2021-01-11 09:33:37

Maven數(shù)目項(xiàng)目

2020-02-19 19:26:27

K8S開源平臺(tái)容器技術(shù)

2017-01-10 09:07:53

tcpdumpGET請(qǐng)求

2024-09-13 08:49:45

2024-10-25 15:56:20

2018-01-08 16:19:04

微信程序輪播圖

2020-04-01 16:30:32

TCP互聯(lián)網(wǎng)Linux

2024-08-13 11:13:18

2018-06-26 09:37:07

時(shí)序數(shù)據(jù)庫FacebookNoSQL

2022-06-17 08:05:28

Grafana監(jiān)控儀表盤系統(tǒng)

2024-12-06 11:22:27

2021-06-18 07:34:12

Kafka中間件微服務(wù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)