0

0

在 JavaScript 中使用最小和最大堆管理流数据:数字运动员健康技术视角

WBOY

WBOY

发布时间:2024-08-31 10:36:10

|

420人浏览过

|

来源于dev.to

转载

在 javascript 中使用最小和最大堆管理流数据:数字运动员健康技术视角

数据管理在健康技术中至关重要。无论是跟踪运动员的表现指标还是监控运动员的恢复时间,有效地组织数据都可以对洞察的获取方式产生重大影响。在这种情况下管理数据的一种强大工具是堆,特别是最小堆和最大堆。在这篇文章中,我们将使用与运动员数据管理相关的实际示例,探索如何在 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

在这里,最小堆可以让临床医生快速识别恢复时间最快的运动员,这对于在训练期间做出实时决策至关重要。

立即学习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 > 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;
        }
    }
}

使用最大堆实现峰值心率

让我们考虑如何使用最大堆来跟踪运动员在锻炼期间的峰值心率:

Vondy
Vondy

下一代AI应用平台,汇集了一流的工具/应用程序

下载
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:将任意数组转换为堆,确保堆属性得到维护。
  • peek:查看最小值或最大值,而不将其从堆中删除。

这些操作对于高效管理和实时处理数据至关重要,使堆成为健康技术应用中的宝贵工具。

简化 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,虽然没有内置的堆模块,但您可以使用 @datastructs-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 中有效管理和检索关键数据的强大工具。无论您是跟踪恢复时间还是监控峰值性能指标,这些结构都可以帮助临床医生和健康技术专业人员快速做出明智的决策。通过理解和实施堆,您可以确保运动员数据井井有条、可访问,并可在最重要的时候进行分析。

通过在健康技术应用程序中使用堆,您将能够以支持运动员获得更好结果的方式处理数据,提供优化表现和恢复所需的见解。

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

753

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

636

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

758

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

618

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1262

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

577

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

707

2023.08.11

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

63

2026.01.14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Django 教程
Django 教程

共28课时 | 3.1万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号