6-13 Percolate Up and Down(20 point(s))

C语言堆的建立Percolate Up and Down 6-8 Percolate Up and Down (20 point(s)) Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue H ); void... 阅读详情
6-13 Percolate Up and Down(20 point(s))

Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap.

Format of functions:

void PercolateUp( int p, PriorityQueue H );
void PercolateDown( int p, PriorityQueue H );

where int p is the position of the element, and PriorityQueue is defined as the following:

typedef struct HeapStruct *PriorityQueue;
struct HeapStruct {
    ElementType  *Elements;
    int Capacity;
    int Size;
};

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
#define MinData -1

typedef struct HeapStruct *PriorityQueue;
struct HeapStruct {
    ElementType  *Elements;
    int Capacity;
    int Size;
};

PriorityQueue Initialize( int MaxElements ); /* details omitted */

void PercolateUp( int p, PriorityQueue H );
void PercolateDown( int p, PriorityQueue H );

void Insert( ElementType X, PriorityQueue H ) 
{
    int p = ++H->Size;
    H->Elements[p] = X;
    PercolateUp( p, H );
}

ElementType DeleteMin( PriorityQueue H ) 
{ 
    ElementType MinElement; 
    MinElement = H->Elements[1];
    H->Elements[1] = H->Elements[H->Size--];
    PercolateDown( 1, H );
    return MinElement; 
}

int main()
{
    int n, i, op, X;
    PriorityQueue H;

    scanf("%d", &n);
    H = Initialize(n);
    for ( i=0; i<n; i++ ) {
        scanf("%d", &op);
        switch( op ) {
        case 1:
            scanf("%d", &X);
            Insert(X, H);
            break;
        case 0:
            printf("%d ", DeleteMin(H));
            break;
        }
    }
    printf("\nInside H:");
    for ( i=1; i<=H->Size; i++ )
        printf(" %d", H->Elements[i]);
    return 0;
}

/* Your function will be put here */

Sample Input:

9
1 10
1 5
1 2
0
1 9
1 1
1 4
0
0

Sample Output:

2 1 4 
Inside H: 5 10 9

code:
void PercolateUp( int p, PriorityQueue H ){
	int i = p;
	int flag = 0;
	if(i==1)return;
	while(i!=1 && flag==0){
		if(H->Elements[i]<H->Elements[i/2]){//比父节点小,交换 
			int temp = H->Elements[i];
			H->Elements[i] = H->Elements[i/2];
			H->Elements[i/2] = temp;
		}
		else flag = 1;//否则不需要调整flag变1 
		i = i/2;//每次向上找父节点 
	}
	return; 
}
void PercolateDown( int p, PriorityQueue H ){
	int t;//记录较小元素下标,如果发现t和原来的没变则不用再继续调整了,如果t变成了原来的儿子下标,说明继续向下调整 
	int flag = 0;//标记是否需要继续调整
	int i = p;
	while(i*2<=H->Size&&flag==0){//注意!!i*2<=n才能进行下面的比较,如果单纯i<=n如果i=n,乘二肯定没有了 
		if(H->Elements[i]>H->Elements[i*2]){//比较当前点和左儿子的大小,如果现在元素大于左二子,则调整 
			t = i*2;
		}
		else//如果和左儿子比当前元素小,让t等于它本身 
		    t = i;
		if(i*2+1<=H->Size){//如果含有右儿子,则需要比较右儿子 
			if(H->Elements[t]>H->Elements[i*2+1]){//如果之前得到的最小的还比右儿子大
				t = i*2+1;//修改t的值 
			}
		}
		if(t==i){
			flag = 1;//如果t的值还是i本身则说明不需要继续调整了 
		}
		else{
			//交换
			int temp = H->Elements[i];
			H->Elements[i] = H->Elements[t];
			H->Elements[t] = temp; 
			i = t;//继续调整i值变成当前t 
		} 
	}
}

 
二叉堆实现优先队列中的上滤和下滤 优先队列是使用二叉堆来实现 其中很重要的两个操作,一个是插入,另一个是删除最优先值(这里认为是最小值) 其中插入操作可以这样实现: 把X放入二叉堆的最后端,然后再通过“上滤”操作把X放到相应位置 删除操作可以这样实现: 把最顶端的元素(即)最小值删除,然后把堆中最后一个元素先放到最顶端,然后通过“下滤”操作来使其到达正确位置 代码如下: #include #include 阅读详情

相关推荐

【信息科学与工程学】计算机科学与自动化——第一百九十篇 操作系统用户态开发 01

① 切换开销 Tsw​=nreg​×tmem​+tstack_switch​,其中 nreg​ 为保存寄存器数,tmem​ 为内存访问延迟。② 调度策略:加权公平队列,每个协程权重 wi​,时间片 ti​=∑wj​wi​​⋅Ttotal​。② 延迟 L=tsend_proc​+tlink​+trecv_proc​,其中 tlink​=distance/c。① 队列深度 QD 与延迟:假设命令处理时间 tcmd​,队列深度 Q,最大并发 Q,吞吐量 T=Q/tcmd​(理想流水线)。

weixin_49199313的博客 16

二叉堆中上滤和下滤的问题

在学习到堆排序时,对于上滤和下滤的知识掌握的不清楚,回来复习了一下。有了些感悟,记下来。 上滤(percolate up) 上滤一般应用于在一个已经排序好的二叉堆中插入一个新节点。通过上滤,使堆在容纳了新节点后仍能保持原来的堆序。 上滤的主要思想: 首先在堆末新建一个空间,称为空穴(hole),然后比较穴的值和其父节点的值。如果空穴的当前位置满足整个堆的堆序,那么就把要插入的值赋给空穴

DragonBark的博客 4436

线性模型:AR、MA、ARMA、ARMAX、ARX、ARARMAX、OE、BJ等

线性模型:AR、MA、ARMA、ARMAX、ARX、ARARMAX、OE、BJ等

zephyr_wang的博客 1万+

数据结构 C语言最小堆标准模板程序

6-8 Percolate Up and Down (20 point(s)) Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue H ); void...

wwxy1995的博客 668

Linux-RGMII PHY 88E1512 驱动调试记录

1. 简介 1)开发板平台:创龙 AM5728 (tl5728-easy-evm) 2)PHY芯片:Marvell 88E1512 88E1512和88E1510都是一个系列的phy,采用marvell的通用phy驱动,驱动源码路径如下:drivers/net/phy/marvell.c 2. 应用场景 1)系统框图 AM5728连接两个PHY,PHY0的工作模式是 RGMII to Copper (RGMII到电口),PHY1的工作模式是 RGMII to SGMII。 两个PHY与处理器的连接方式都是

weixin_40407893的博客 2万+

Rk3128投影仪救砖操作和固件分享思路

大家搞一条比较硬的东西,插入耳机口,就可以进入刷机模式了的,我的机器是这样的,大家也可以试试。(看不懂可以往下看)

MMX7088的博客 1万+

6-8 Percolate Up and Down (20 分)【堆结点的插入与删除】

堆节点的插入: void PercolateUp( int p, PriorityQueue H ){ //插入时,小的数字向上调整 int i; int temp=H->Elements[p]; for(i=H->Size;temp<H->Elements[i/2];i/=2){ //当插入的数字小于父节点则向上调整 H->Elements[i]=H->Elements[i/2]; } H->Element

Jay98的博客 1600

反光柱定位算法实战03:VEnus代码解析和纯反光柱定位中的可改进方向

本文深入解析了开源框架VEnus的反光柱定位与建图实现流程,包括强度提取、聚类、数据关联、位姿优化等核心模块。同时指出其三大局限:匹配质量脆弱性、单一传感器依赖和地图扩展逻辑缺失,并提出改进方向:融合里程计/IMU数据、引入图优化后端、建立动态地图维护机制。该分析为基于反光柱的定位系统优化提供了实用参考,有助于提升工业场景下的定位鲁棒性和精确性。

Studying…… 520

PTA:Percolate Up and Down (20分)

#include <stdio.h> #include <stdlib.h> typedef int ElementType; #define MinData -1 typedef struct HeapStruct *PriorityQueue; struct HeapStruct { ElementType *Elements; int Capacity; int Size; }; PriorityQueue Initialize( int Ma.

睡觉不流口水 's blog 357

从传统业务到 AI Skill:产品经理也能看懂的能力改造指南(含完整案例)

从传统业务到 AI Skill:产品经理也能看懂的能力改造指南(含完整案例)

专注于编程、AI、人工智能等多个领域内容研究和分析 638

RK3566 Android11默认客户Launcher修改

【代码】RK3566 Android11默认客户Launcher修改。

Android系统开发笔记 1722

4-1 Percolate Up and Down

4-1Percolate Up and Down(7分) Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue H ); void Percol...

kaggle expert,全球排名前1000,清华计算机研究生,兴趣算法工程 2001

Python之afdx包语法、参数和实际应用案例

Python 的 afdx 包是针对 AFDX 协议的轻量级开发工具,专注于 AFDX 数据帧的解析、构造、发送/接收、协议合规性校验等核心能力,广泛应用于航空电子设备测试、仿真、数据监控等场景。

王国平的博客 982

pta Percolate Up and Down(最小堆的插入维护和删除维护)

Write the routines to do a “percolate up” and a “percolate down” in a binary min-heap. Format of functions:void PercolateUp( int p, PriorityQueue H ); void PercolateDown( int p, PriorityQueue H );wher

vocaloid01的博客 4420

PTP(确时间协议,Precision Time Protocol)时间同步

主时钟定期发送带预估发送时间 t1 的报文(若支持硬件时间戳,实际 t1​ 由后续的Follow_Up报文携带)。所有设备启动后通过协商选举出唯一主时钟(Grandmaster),其余设备自动成为从时钟(Slave)。:从时钟记录接收时间 t2,随后向主时钟发送Delay_Req并记录发送时间 t3​。若使用支持PTP的交换机,可修正报文在交换机内的驻留时间,进一步提升精度。从时钟根据offset调整本地时钟,实现与主时钟同步。动态调整本地时钟,抵消传输延迟和时钟漂移。(或通过BMCA自动当选)。

努力努力努力 1303

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

优先队列(二叉堆)原理及基础操作的python实现

此时我们需要将最后一个节点放入根节点,然后进行下滤操作,就完成了我们的删除最小值的操作,并且不会破坏堆序。

yangwen663200的博客 1116

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

ZJU PTA ds 6-1 Percolate Up and Down

Percolate Up and Down Write the routines to do a “percolate up” and a “percolate down” in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue H ); void PercolateDown( int p, PriorityQueue H ); where int p is the position of the

s_w_s_的博客 1226

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

(堆6-3)Percolate Up and Down--C语言

int Size;

2302_80061132的博客 486

最大堆、最小堆数据结构详细解读

堆广泛用于优先队列、排序算法(如堆排序)、调度系统等场景。下面是对最大堆和最小堆的详细介绍。

欢迎来到我的博客!这里是一个专注于计算机技术的分享平台,涵盖编程开发、算法研究、系统架构、软件工程等多个领域。无论你是初学者还是资深开发者,都能在这里找到有价值的内容。 3671

6-8 Percolate Up and Down(20 分)

6-8 Percolate Up and Down(20 分)Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap.Format of functions:void PercolateUp( int p, PriorityQueue H ); void PercolateDown(...

友人A 1282

6-8 Percolate Up and Down (20 分)

Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue H ); void PercolateDown( int p, PriorityQueue H );...

CodeMonkey 1683

堆(优先队列)的基本操作

堆(优先队列)的基本操作 堆是一棵完全二叉树,所以可以利用数组来实现。 以1号作为root,则对于数组中的任意一个i,其左儿子在(2*i),右儿子在(2*i+1),父亲在(i/2)。 下面以最小堆为例,实现一些基本操作: 1.堆的声明 2.PercolateUp, PercolateDown 3.Insert 4.DeleteMin

ParadiseHeaven的博客 571

Java学数据结构(4)——PriorityQueue(优先队列)& 二叉堆(binary heap)

本篇博客介绍二叉堆(binary heap),它的使用对于PriorityQueue(优先队列)的实现相当普遍,以至于当堆(heap)这个词不加修饰地用在优先队列的上下文中时,一般都是指数据结构的这种实现。

Arya的博客,专注后端领域 578

Java Heap - Percolate Up, Percolate Down, and Heapify

一直都背不熟这三个heap最基本的概念,用中英写了一次加强记忆 What is Percolate Up? the element need to be moved up to maintain the heap’s property. e.g., when offering a new element into the heap. 当一个元素被offer进去了heap之后,heap需要维持当前的...

weixin_43143127的博客 1313

4-1 Percolate Up and Down (20分)

4-1 Percolate Up and Down   (20分) Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue H ); void Pe

csucbMIT的博客 1429
上一篇: 6-12 二叉搜索树的操作集(30 point(s))
下一篇: 6-14 Add Two Polynomials(20 point(s))
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值