在 JavaScript 中使用最小和最大堆管理流資料:數字運動員健康技術視角

aow054發表於2024-09-22
資料管理在健康技術中至關重要。無論是跟蹤運動員的表現指標還是監控運動員的恢復時間,有效地組織資料都可以對洞察的獲取方式產生重大影響。在這種情況下管理資料的一種強大工具是堆,特別是最小堆和最大堆。在這篇文章中,我們將使用與運動員資料管理相關的實際示例,探索如何在 javascript 中實現和使用最小堆和最大堆。 什麼是堆?堆是一種特殊的基於二叉樹的資料結構,滿足堆屬性。在最小堆中,父節點始終小於或等於其子節點。相反,在最大堆中,父節點始終大於或等於其子節點。這使得堆對於從資料集中高效檢索最小值或最大值特別有用。 最小堆用例:跟蹤恢復時間想象一下您是一名臨床醫生,正在跟蹤運動員鍛鍊後的恢復時間。您希望有效地記錄最短恢復時間,以便快速識別哪位運動員恢復最快。 建立最小堆在 javascript 中,您可以使用陣列建立最小堆,並使用簡單的函式對其進行管理以維護堆屬性:class minheap { constructor() { this.heap = []; } getmin() { return this.heap[0]; } insert(value) { this.heap.push(value); this.bubbleup(); } bubbleup() { let index = this.heap.length - 1; while (index &gt; 0) { let parentindex = math.floor((index - 1) / 2); if (this.heap[parentindex] <h4> 使用最小堆計算運動員恢復時間</h4><p>現在,讓我們將其應用到我們的場景中:<br></p><pre class="brush:php;toolbar:false">const recoverytimes = new minheap();recoverytimes.insert(10); // athlete arecoverytimes.insert(7); // athlete brecoverytimes.insert(12); // athlete cconsole.log("fastest recovery time:", recoverytimes.getmin()); // outputs: 7登入後複製在這裡,最小堆可以讓臨床醫生快速識別恢復時間最快的運動員,這對於在訓練期間做出實時決策至關重要。立即學習“Java免費學習筆記(深入)”; 最大堆用例:監控峰值效能指標另一方面,最大堆非常適合需要跟蹤最高值的場景,例如監控峰值效能指標,例如劇烈鍛鍊期間達到的最大心率。 建立最大堆最大堆的實現方式與最小堆類似,但需要進行一些調整:class maxheap { constructor() { this.heap = []; } getmax() { return this.heap[0]; } insert(value) { this.heap.push(value); this.bubbleup(); } bubbleup() { let index = this.heap.length - 1; while (index &gt; 0) { let parentindex = math.floor((index - 1) / 2); if (this.heap[parentindex] &gt;= this.heap[index]) break; [this.heap[parentindex], this.heap[index]] = [this.heap[index], this.heap[parentindex]]; index = parentindex; } } extractmax() { if (this.heap.length === 1) return this.heap.pop(); const max = this.heap[0]; this.heap[0] = this.heap.pop(); this.bubbledown(); return max; } bubbledown() { let index = 0; const length = this.heap.length; const element = this.heap[0]; while (true) { let leftchildindex = 2 * index + 1; let rightchildindex = 2 * index + 2; let leftchild, rightchild; let swap = null; if (leftchildindex element) swap = leftchildindex; } if (rightchildindex element) || (swap !== null &amp;&amp; rightchild &gt; leftchild) ) { swap = rightchildindex; } } if (swap === null) break; [this.heap[index], this.heap[swap]] = [this.heap[swap], this.heap[index]]; index = swap; } }}登入後複製 使用最大堆實現峰值心率讓我們考慮如何使用最大堆來跟蹤運動員在鍛鍊期間的峰值心率:const heartrates = new maxheap();heartrates.insert(150); // athlete aheartrates.insert(165); // athlete bheartrates.insert(160); // athlete cconsole.log("peak heart rate:", heartrates.getmax()); // outputs: 165登入後複製在這裡,最大堆確保臨床醫生可以快速識別達到最高心率的運動員,這可能表明需要進一步關注或冷卻。 其他基本堆操作除了插入元素和檢索最小值或最大值之外,堆還支援其他基本操作,例如:提取最小/最大:這會刪除堆的根(最小堆中的最小元素或最大堆中的最大元素)並重新平衡堆。heapify:將任意陣列轉換為堆,確保堆屬性得到維護。peek:檢視最小值或最大值,而不將其從堆中刪除。這些操作對於高效管理和實時處理資料至關重要,使堆成為健康技術應用中的寶貴工具。 簡化 python 和 javascript 中的堆操作在python中,heapq模組提供了一種使用列表來管理最小堆的簡單有效的方法。這是一個例子:import heapq# create an empty list to represent the heaprecovery_times = []# add elements to the heapheapq.heappush(recovery_times, 10) # athlete aheapq.heappush(recovery_times, 7) # athlete bheapq.heappush(recovery_times, 12) # athlete c# retrieve the smallest element (fastest recovery time)fastest_recovery_time = heapq.heappop(recovery_times)print(f"fastest recovery time: {fastest_recovery_time}") # outputs: 7登入後複製對於 javascript,雖然沒有內建的堆模組,但您可以使用 @datastructs-js/priority-queue 等第三方庫來實現類似的功能:// First, you would need to install the @datastructures-js/priority-queue library using npm:// npm install @datastructures-js/priority-queueconst { MinPriorityQueue } = require('@datastructures-js/priority-queue');// Create a new min heapconst minHeap = new MinPriorityQueue();// Add elements to the heapminHeap.enqueue(10); // Athlete AminHeap.enqueue(7); // Athlete BminHeap.enqueue(12); // Athlete C// Retrieve the smallest elementconst fastestRecoveryTime = minHeap.dequeue().element;console.log("Fastest recovery time:", fastestRecoveryTime); // Outputs: 7登入後複製透過利用這些工具,您可以專注於應用程式的關鍵方面,例如分析運動員資料,而不必陷入堆實現的細節中。 在 javascript 中高效檢索資料堆,特別是最小堆和最大堆,是在 javascript 中有效管理和檢索關鍵資料的強大工具。無論您是跟蹤恢復時間還是監控峰值效能指標,這些結構都可以幫助臨床醫生和健康技術專業人員快速做出明智的決策。透過理解和實施堆,您可以確保運動員資料井井有條、可訪問,並可在最重要的時候進行分析。透過在健康技術應用程式中使用堆,您將能夠以支援運動員獲得更好結果的方式處理資料,提供最佳化表現和恢復所需的見解。 以上就是在 JavaScript 中使用最小和最大堆管理流資料:數字運動員健康技術視角的詳細內容,更多請關注我的其它相關文章!

相關文章