본문 바로가기

[자료구조론] - Data Structure/[Concept]

Priority Queue

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define MAX_HEAP 100
 
typedef enum {false,truebool;
typedef char Data;
 
typedef struct{
    Data data;
    int priority;
}HNode;
 
typedef struct{
    HNode items[MAX_HEAP + 1];
    int num;
}Heap;
 
void InitHeap(Heap *pheap){
    pheah->num =0;
}
 
bool IsEmpty(Heap *pheap){
    return pheap->num == 0;
}
 
bool IsFull(Heap *pheap){
    return pheap->num == MAX_HEAP;
}
 
int GetParent(int idx){
    return idx/2;
}
 
int GetLChild(int idx){
    return idx*2;
}
 
int GetRChild(int idx){
    return idx*2+1;
}
 
void Insert(Heap *pheap, Data data, int priority){
    HNode newNode;
    int idx = pheap->num +1;
    if(IsFull(pheap)) exit(1);
 
    while(idx>1){
        int parent = GetParent(idx);
        if(priority > pheap->items[parent].priority){
            pheap->items[idx] = pheap->items[parent];
            idx = parent;
        }else break;
    }
 
    newNode.data= data;
    newNode.priority = priority;
 
    pheap->items[idx] = newNode;
    pheap->num++;
}
 
int GetHighPrioityChild(Heap * pheap, int idx){
    if(GetLChild(idx) > pheap->num) return 0// no child
    else if(GetLChild(idx) == pheap->num) return GetLChild(idx); // left chile only
    else{
        int left = GetLChild(idx);
        int right = GetRChild(idx);
        if(pheap->items[left].priority > pheap->items[right].priority){
            return left;
        }else return right;
    }
}
 
Data Delete(Heap* pheap){
    Data max = pheap->items[1].data;
    HNode last = pheap->items[pheap->num];
    int parent = 1// root
    int child;
 
    while(child = GetHighPrioityChild(pheap, parent)){
        if(last.priority < pheap->items[child].priority){ // get highest child
            pheap->items[parent] = pheap->items[child]; // if condition holds, exchange
            parent = child; // go down
        }else break;
    }
 
    pheap->items[parent] = last;
    pheap->num--;
    return max;
}
 
typedef Heap PQueue;
 
void InitPQueue(PQueue* ppqueue) InitHeap(ppqueue);
bool IsPQEmpty(PQueue* ppqueue) return IsEmpty(ppqueue);
bool IsPQFull(PQueue* ppqueue) return IsFull(ppqueue);
 
void EnQueue(PQueue* ppqueue, Data data, int priority) Insert(ppqueue, data, priority);
Data DeQueue(PQueue* ppqueue) return Delete(ppqueue);
 
void HeapSort(Data a[], int n){
    Heap heap;
    InitHeap(&heap);
 
    for(int i=0; i < n;i++){
        Insert(&heap, a[i],a[i]);
    }
 
    for(int i=n-1;i>=0;i--){
        a[i] = Delete(&heap);
    }
}
cs

'[자료구조론] - Data Structure > [Concept]' 카테고리의 다른 글

10. Graph  (0) 2020.11.17
Infix to Postfix & make calculate tree  (0) 2020.11.03
InOrder - LCR  (0) 2020.11.03
preOrder - CLR  (0) 2020.11.03
postOrder - LRC  (0) 2020.11.03