04-树9. Path in a Heap (25)

本文介绍如何构建一个最小堆并实现从指定节点到根节点的路径查找功能。输入包括序列大小、待检查的索引数量及数值序列,输出则是从指定索引开始到根节点的路径。

04-树9. Path in a Heap (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given.

Output Specification:

For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line.

Sample Input:
5 3
46 23 26 24 10
5 4 3
Sample Output:
24 23 10
46 23 10
26 10
 
 
#include <stdio.h>
void insert(int *heap, int val) {
	int child = ++heap[0];
	int parent = child / 2;
	while (parent > 0 && heap[parent] > val) {			//上滤
		heap[child] = heap[parent];
		child = parent;
		parent = child / 2;
	}
	heap[child] = val;
}
int main() {
//	freopen("test.txt", "r", stdin);
	int n, m;
	scanf("%d%d", &n, &m);
	int heap[1000] = {};				//0位置放置堆中元素大小,1位置为堆顶
	for (int i = 0; i < n; ++i) {
		int num;
		scanf("%d", &num);
		insert(heap, num);
	}
	while (m--) {
		int index;
		scanf("%d", &index);
		while (index > 0) {
			printf("%d", heap[index]);
			if (index != 1)
				printf(" ");
			index /= 2;
		}
		printf("\n");
	}
	return 0;
}


题目链接:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%919

### 最小的数据结构及其数组实现 #### 定义与特性 最小是一种特殊的形数据结构,满足性质:如果 \(P\) 是父节点而 \(C\) 是其子节点,则父节点的关键字小于等于子节点的关键字[^3]。这种结构通常用于高效地获取集合中的最小元素。 #### 数组存储方式 由于最小可以被看作是一棵完全二叉,因此可以通过数组来有效地表示它[^1]。对于任意索引为 \(i\) 的节点: - 父节点的索引为 \(\text{Parent}(i) = \lfloor(i-1)/2\rfloor\)[^5] - 左孩子的索引为 \(\text{LeftChild}(i) = 2*i + 1\) - 右孩子的索引为 \(\text{RightChild}(i) = 2*i + 2\) 通过这种方式,无需显式定义指针即可操作结构,从而节省内存并提高效率。 #### 构建最小 构建一个完整的最小涉及将一组随机数插入到数组中,并调整它们的位置以保持属性。以下是具体方法: ```python def build_min_heap(arr): n = len(arr) # 自底向上调整 for i in range(n//2 - 1, -1, -1): heapify_down(arr, i, n) def heapify_down(arr, idx, size): smallest = idx left = 2 * idx + 1 right = 2 * idx + 2 if left < size and arr[left] < arr[smallest]: smallest = left if right < size and arr[right] < arr[smallest]: smallest = right if smallest != idx: arr[idx], arr[smallest] = arr[smallest], arr[idx] heapify_down(arr, smallest, size) ``` 上述函数 `build_min_heap` 将输入列表转换成有效的最小。 #### 插入(PUSH) 和 删除(POP) 操作 为了支持动态更新,需提供两种基本操作——插入新元素和移除最小元素。 ##### PUSH 操作 当向中添加一个新的数值时,应将其放置于数组末尾,随后执行上浮过程直至恢复序关系。 ```python def push(heap, val): heap.append(val) current_idx = len(heap)-1 while current_idx > 0: parent_idx = (current_idx - 1)//2 if heap[parent_idx] <= heap[current_idx]: break heap[parent_idx], heap[current_idx] = heap[current_idx], heap[parent_idx] current_idx = parent_idx ``` ##### POP 操作 删除最小值意味着取出根节点上的项,然后重新排列剩余部分形成新的合法形态。 ```python def pop(heap): if not heap: raise IndexError("pop from empty heap") root_value = heap[0] last_val = heap.pop() if heap: heap[0] = last_val heapify_down(heap, 0, len(heap)) return root_value ``` 以上代码片段展示了如何在 Python 中实现这些功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值