Exercise 4.3-Path in the Heap-Programming Questions

tags: PTA: Zhejiang University Edition_Data Structure_Problem Set

Exercise 4.3-Path in the Heap-Programming Questions

Problem-solving code

#include<stdio.h>
#include<stdlib.h>
#define MINH -10001
typedef struct heap* pheap;
struct heap {
	int* data;
	int size;
};
pheap CreatHeap(int N);
void InsertMin(pheap H, int D);
void OutputPath(pheap H, int I);
int main()
{
	int N;//n of ele
	int M;//n of path
	scanf("%d %d", &N, &M);
	pheap H = CreatHeap(N);
	int i, temp_data;
	for (i = 0; i < N; i++) {
		scanf("%d", &temp_data);
		InsertMin(H,temp_data);
	}
	int temp_index;
	int flag = 1;
	for (i = 0; i < M; i++) {
		scanf("%d", &temp_index);
		if (flag) flag = 0;
		else printf("\n");
		OutputPath(H, temp_index);
	}
	return 0;
}
pheap CreatHeap(int N) {
	pheap H = (pheap)malloc(sizeof(struct heap));
	H->data = malloc((N + 1) * sizeof(int));
	H->size = 0;
	H->data[0] = MINH;
	return H;
}
void InsertMin(pheap H, int D) {
	if (H->size == 1000) return;
	int i;
	for (i = ++H->size ; H->data[i / 2] > D; i /= 2) H->data[i] = H->data[i / 2];
	H->data[i] = D;
}
void OutputPath(pheap H, int I) {
	int flag = 1;
	int i;
	for (i = I; i >= 1; i /= 2) {
		if (flag) flag = 0;
		else printf(" ");
		printf("%d", H->data[i]);
	}
}

Test Results

Problem sorting

1. I refer to the video for the first time, and I will meet it next time. It is actually quite simple, mainly to understand the structure of the large top pile and the small top pile.

Intelligent Recommendation

[SICP Exercise] 147 Exercise 4.3

Exercise 4-3 original Exercise 4.3. Rewrite eval so that the dispatch is done in data-directed style. Compare this with the datadirected differentiation procedure of exercise 2.73. (You may use the ca...

Programming questions - path in matrix

Programming questions - path in matrix topic Analyze C # code topic Please design a function to determine if there is a path that contains all characters containing a string in a matrix. The path can ...

4.3 Paths in the heap (25 points)

Path in the heap Insert a series of given numbers into a small top heap H[] that is initially empty. Then for any given index i, print the path from H[i] to the root node. Input format: The first line...

Exercise questions 5 (IO programming)

IO programming 1. Document reading and writing Exercise: Please read a local text file as a STR and print it out: 2. Operation files and directories Write an OS module to write one can be implementedd...

Programming exercise questions about parameters

topic: There are now two variables, respectively, A = {Name: 'xm'}, b = [4], we do not have the third variable to exchange the value of A and B. Idea: (1) Merge two objects into one object; (2) AB tak...

More Recommendation

4.3 Programming to achieve id3

View the complete code and data set Reference URL: https://blog.csdn.net/snoopy_yuan/article/details/68959025...

Programming Questions-the shortest path (Dijkstra)

Question: Design a program, input the start node, end the node, edge. Output from the beginning node to the shortest path of the end node. ] Format description.   Example: Enter: 1 3    ...

Search Path 4.3 prerequisite files

scenes to be used Large systems, there is a dependence between multiple projects, distributed across multiple separate folder, when those paths change, only need to modify the search path, without cum...

Object-oriented and C++ programming exercise 13th (heap and copy constructor)

The answer is made or inquired by the blogger himself, for reference only 2-1 In the definition of the class, the function used to allocate memory space for the object, initialize the data members of ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top