JavaScript での最小ヒープと最大ヒープによるストリーミング データの管理: デジタル アスリートのヘルステクノロジーの視点

PHPz
リリース: 2024-08-31 11:02:32
オリジナル
802 人が閲覧しました

Managing Streaming Data with Min and Max Heaps in JavaScript: A Digital Athlete Health Tech Perspective

データ管理はヘルステクノロジーにおいて非常に重要です。パフォーマンス指標を追跡する場合でも、アスリートの回復時間を監視する場合でも、データを効率的に整理することで、洞察を得る方法に大きな違いが生まれます。このようなシナリオでデータを管理するための強力なツールの 1 つはヒープ、特に最小ヒープと最大ヒープです。この投稿では、アスリートのデータ管理に関連する実際の例を使用して、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 > 0) {
            let parentIndex = Math.floor((index - 1) / 2);
            if (this.heap[parentIndex] <= this.heap[index]) break;
            [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
            index = parentIndex;
        }
    }

    extractMin() {
        if (this.heap.length === 1) return this.heap.pop();
        const min = this.heap[0];
        this.heap[0] = this.heap.pop();
        this.bubbleDown();
        return min;
    }

    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 < length) {
                leftChild = this.heap[leftChildIndex];
                if (leftChild < element) swap = leftChildIndex;
            }

            if (rightChildIndex < length) {
                rightChild = this.heap[rightChildIndex];
                if (
                    (swap === null && rightChild < element) ||
                    (swap !== null && rightChild < leftChild)
                ) {
                    swap = rightChildIndex;
                }
            }

            if (swap === null) break;
            [this.heap[index], this.heap[swap]] = [this.heap[swap], this.heap[index]];
            index = swap;
        }
    }
}
ログイン後にコピー

アスリートの回復時間に最小ヒープを使用する

それでは、これをシナリオに適用してみましょう:

const recoveryTimes = new MinHeap();
recoveryTimes.insert(10); // Athlete A
recoveryTimes.insert(7);  // Athlete B
recoveryTimes.insert(12); // Athlete C

console.log("Fastest recovery time:", recoveryTimes.getMin()); // Outputs: 7
ログイン後にコピー

ここで、最小ヒープにより、臨床医は回復時間が最も早いアスリートを素早く特定できます。これは、トレーニング セッション中にリアルタイムで意思決定を行うために重要です。

最大ヒープの使用例: ピーク パフォーマンス メトリクスの監視

一方、最大ヒープは、激しいワークアウト中に到達した最大心拍数などのピークパフォーマンス指標を監視するなど、最高値を追跡する必要があるシナリオに最適です。

最大ヒープの作成

最大ヒープは、いくつかの調整を加えて最小ヒープと同様に実装できます。

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 > 0) {
            let parentIndex = Math.floor((index - 1) / 2);
            if (this.heap[parentIndex] >= 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 < length) {
                leftChild = this.heap[leftChildIndex];
                if (leftChild > element) swap = leftChildIndex;
            }

            if (rightChildIndex < length) {
                rightChild = this.heap[rightChildIndex];
                if (
                    (swap === null && rightChild > element) ||
                    (swap !== null && rightChild > 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 A
heartRates.insert(165); // Athlete B
heartRates.insert(160); // Athlete C

console.log("Peak heart rate:", heartRates.getMax()); // Outputs: 165
ログイン後にコピー

ここで、最大ヒープにより、臨床医は最高心拍数に達したアスリートを迅速に特定できます。これは、さらなる注意やクールダウンが必要であることを示している可能性があります。

その他の基本的なヒープ操作

要素の挿入と最小値または最大値の取得に加えて、ヒープは次のような他の基本的な操作をサポートします。

  • 最小値/最大値の抽出: これにより、ヒープのルート (最小ヒープ内の最小要素、または最大ヒープ内の最大要素) が削除され、ヒープのバランスが再調整されます。
  • Heapify: 任意の配列をヒープに変換し、ヒープのプロパティが維持されるようにします。
  • ピーク: ヒープから削除せずに最小値または最大値を表示します。

これらの操作は、データをリアルタイムで効率的に管理および処理するために不可欠であり、ヒープを医療技術アプリケーションの貴重なツールにします。

Python と JavaScript でのヒープ操作の簡素化

Python では、heapq モジュールはリストを使用して最小ヒープを管理するシンプルかつ効率的な方法を提供します。以下に例を示します:

import heapq

# Create an empty list to represent the heap
recovery_times = []

# Add elements to the heap
heapq.heappush(recovery_times, 10)  # Athlete A
heapq.heappush(recovery_times, 7)   # Athlete B
heapq.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 の場合、組み込みのヒープ モジュールはありませんが、@data Structures-js/priority-queue などのサードパーティ ライブラリを使用して同様の機能を実現できます。

// First, you would need to install the @datastructures-js/priority-queue library using npm:
// npm install @datastructures-js/priority-queue

const { MinPriorityQueue } = require('@datastructures-js/priority-queue');

// Create a new min heap
const minHeap = new MinPriorityQueue();

// Add elements to the heap
minHeap.enqueue(10); // Athlete A
minHeap.enqueue(7);  // Athlete B
minHeap.enqueue(12); // Athlete C

// Retrieve the smallest element
const fastestRecoveryTime = minHeap.dequeue().element;
console.log("Fastest recovery time:", fastestRecoveryTime); // Outputs: 7
ログイン後にコピー

これらのツールを活用することで、ヒープ実装の詳細に囚われることなく、アスリート データの分析など、アプリケーションの重要な側面に集中できます。

JavaScript でデータを効率的に取得する

ヒープ、特に最小ヒープと最大ヒープは、JavaScript で重要なデータを効率的に管理および取得するための強力なツールです。回復時間を追跡する場合でも、ピークパフォーマンス指標を監視する場合でも、これらの構造は、臨床医や医療技術専門家が情報に基づいた意思決定を迅速に行うのに役立ちます。ヒープを理解して実装することで、アスリートのデータが整理され、アクセス可能になり、最も重要なときにすぐに分析できるようになります。

ヘルステック アプリケーションでヒープを使用すると、アスリートのより良い結果をサポートする方法でデータを処理できるようになり、パフォーマンスと回復を最適化するために必要な洞察が得られます。

以上がJavaScript での最小ヒープと最大ヒープによるストリーミング データの管理: デジタル アスリートのヘルステクノロジーの視点の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!