PTA Title Set data structures and algorithms (Chinese) 7-5

PTA Title Set data structures and algorithms (Chinese) 7-5 stack path

7-5 stack path (25 minutes)
 

The insertion of a series of a given number of minor vertex initially empty stack H[]. Then for any given index i, from the print H[i]path to the root node.

Input formats:

Each test line 1 contains two positive integers N and M ( the number of pieces ≤ path), respectively, the number of insert elements, and the need to print. The next line is given interval [-10000, 10000] in the N to be inserted into an initially empty stack top small integer. The last line gives the M subscript.

Output formats:

Each index of the given input i, the output from the row in H[i]the data path to the root node. Between numbers separated by a space, end of the line may not have the extra space.

Sample input:

5 3
46 23 26 24 10
5 4 3

Sample output:

24 23 10
46 23 10
26 10

题目分析:考查最小堆(优先队列)的实现 需要注意的是 利用插入操作建立最小(大)堆 和 先把数据读入完全二叉树 再调整的方法 可能建成的堆不同 并且他们的时间复杂度也不同(插入操作所需要的时间复杂度为 o(nlogn) 第二种方法的时间复杂度为 线性时间)
 1 #define _CRT_SECURE_NO_WARNINGS   
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<malloc.h>
 5 
 6 typedef struct HeapStruct* MinHeap;
 7 struct HeapStruct
 8 {
 9     int Size;
10     int Capacity;
11     int* Elements;
12 };
13 
14 MinHeap CreatMinHeap(int Capacity)
15 {
16     MinHeap H;
17     H = (MinHeap)malloc(sizeof(struct HeapStruct));
18     H->Capacity = Capacity;
19     H->Elements = (int*)malloc(H->Capacity * sizeof(int));
20     H->Size = -1;
21     return H;
22 }
23 
24 void Insert(MinHeap H, int Element)
25 {
26     int i = ++H->Size;
27     for (; i > 0 && Element < H->Elements[(i - 1) / 2]; i = (i - 1) / 2)
28         H->Elements[i] = H->Elements[(i - 1) / 2];
29     H->Elements[i] = Element;
30 }
31 
32 void Delete(MinHeap H)
33 {
34     int Tmp = H->Elements[H->Size--];
35     int Parent;
36     int Child;
37     for (Parent=0;(Parent*2+1)<=H->Size;Parent=Child)
38     {
39         Child = Parent * 2 + 1;
40         if (Child != H->Size && H->Elements[Child] > H->Elements[Child + 1])
41             Child++;
42         if (Tmp <= H->Elements[Child])break;
43         else
44             H->Elements[Parent] = H->Elements[Child];
45     }
46     H->Elements[Parent] = Tmp;
47 }
48 
49 void PrecDown(MinHeap H,int i)
50 {
51     int Tmp = H->Elements[i];
52     int  Parent;
53     int Child;
54     for (Parent = i; (Parent * 2 + 1) < H->Size; Parent = Child)
55     {
56         Child = Parent * 2 + 1;
57         if (Child != H->Size && H->Elements[Child] > H->Elements[Child + 1])
58             Child++;
59         if (Tmp <= H->Elements[Child])break;
60         else
61             H->Elements[Parent] = H->Elements[Child];
62     }
63     H->Elements[Parent] = Tmp;
64 }
65 
66 void BuildHeap(MinHeap H)
67 {
68     for (int i = (H->Size - 1) / 2; i>=0; i--)
69         PrecDown(H, i);
70 }
71 
72 void Print(MinHeap H,int i)
73 {
74     while (i>0)
75     {
76         printf("%d ", H->Elements[i]);
77         i = (i - 1) / 2;
78     }
79     printf("%d", H->Elements[0]);
80 }
81 int main()
82 {
83     int N, M;
84     scanf("%d %d", &N, &M);
85     MinHeap H = CreatMinHeap(N);
86     for (int i = 0; i < N; i++)
87     {
88         int num;
89         scanf("%d", &num);
90         Insert(H, num);
91     }
92     for (int j = 0; j < M; j++)
93     {
94         int i;
95         scanf("%d", &i);
96         Print(H, i-1);
97         printf("\n");
98     }
99 }
View Code

 

 

Guess you like

Origin www.cnblogs.com/57one/p/11580443.html
Recommended