-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.h
More file actions
87 lines (57 loc) · 1.89 KB
/
heap.h
File metadata and controls
87 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#ifndef HEAP_H
#define HEAP_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#define M 50
typedef struct Heap {
float heap[M];
int twin[M];
int heapSize;
} Heap;
typedef struct database {
Heap* minHeap, * maxHeap;
int dataBaseSize;
} database;
//index of parent
int Parent(int i);
//index of left child
int Left(int i);
//index of right child
int Right(int i);
//swap two elements
void fexchange(float* x, float* y);
void exchange(int* x, int* y);
//fix error on heap
void Heapify_down_max(Heap* A, Heap* B, int i); //A is max-heap, B is min-heap
void Heapify_down_min(Heap* A, Heap* B, int i); //A is min-heap, B is max-heap
////fix error on heap
void Heapify_up_max(Heap* A, Heap* B, int i); //A is max-heap, B is min-heap
void Heapify_up_min(Heap* A, Heap* B, int i); //A is min-heap, B is max-heap
//build max_heap from given array
void Build_Heap_max(Heap* A, Heap* B, int n);
//build min_heap from fiven array
void Build_Heap_min(Heap* A, Heap* B, int n);
//insert new node with key x to both data heaps
void Insert(database* data, float* x);
//insert key into max-heap.
void Heap_Insert_max(Heap* A, Heap* B, float* key);//A is max-heap, B is min-heap
//insert key into min-heap
void Heap_Insert_min(Heap* A, Heap* B, float* key);//A is min-heap, B is max-heap
//initialiaze struct by given array
database* Init(float* S, int n);
//adding x to struct, passing database struct in order to get to heaps
void Insert(database* data, float x);
//return max val in data in O(1) time-complexity
float Find_max(database* data);
//return min val in data in O(1) time-complexity
float Find_min(database* data);
void Del(Heap* A, Heap* B, int i);
//Delete root of min_heap, uses 'twin' index to delete value from max-heap
//then correct both heaps
void Del_Min(database* data);
//same as Del_min just for max value.
void Del_Max(database* data);
int message();
#endif